Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,10 @@ impl Args {

pub fn print_deprecation_warning(dir: &Path) {
let print = Print::new(false);
let global_dir = global_config_path().expect("Couldn't retrieve global directory.");
let global_dir = fs::canonicalize(&global_dir).expect("Couldn't expand global directory.");
let Ok(global_dir) = global_config_path() else {
return;
};
let global_dir = fs::canonicalize(&global_dir).unwrap_or(global_dir);

// No warning if local and global dirs are the same (e.g., both set to STELLAR_CONFIG_HOME)
if dir == global_dir {
Expand Down Expand Up @@ -843,6 +845,7 @@ pub fn cli_config_file() -> Result<PathBuf, Error> {
#[cfg(all(test, unix))]
mod tests {
use super::*;
use serial_test::serial;
use std::collections::HashMap;

#[test]
Expand Down Expand Up @@ -884,4 +887,47 @@ mod tests {
perms.mode() & 0o777
);
}

struct EnvGuard(Vec<(String, Option<String>)>);

impl EnvGuard {
fn new(vars: &[&str]) -> Self {
let saved = vars
.iter()
.map(|k| (k.to_string(), std::env::var(k).ok()))
.collect();
Self(saved)
}
}

impl Drop for EnvGuard {
fn drop(&mut self) {
for (k, v) in &self.0 {
match v {
Some(val) => std::env::set_var(k, val),
None => std::env::remove_var(k),
}
}
}
}

#[test]
#[serial]
fn test_print_deprecation_warning_no_panic_when_global_dir_missing() {
let tmp = tempfile::tempdir().unwrap();
let _guard = EnvGuard::new(&["STELLAR_CONFIG_HOME", "XDG_CONFIG_HOME", "HOME"]);

std::env::remove_var("STELLAR_CONFIG_HOME");
std::env::remove_var("XDG_CONFIG_HOME");

let fake_home = tmp.path().join("home");
std::fs::create_dir_all(&fake_home).unwrap();
std::env::set_var("HOME", &fake_home);

Comment thread
fnando marked this conversation as resolved.
let local_dir = tmp.path().join("workdir/.stellar");
std::fs::create_dir_all(&local_dir).unwrap();

// Must not panic even though ~/.config/stellar does not exist
print_deprecation_warning(&local_dir);
}
}
Loading