Skip to content

Commit

Permalink
fix config tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifropc committed Nov 18, 2024
1 parent 7453642 commit 901bd90
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 45 deletions.
96 changes: 52 additions & 44 deletions cmd/crates/soroban-test/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,35 @@ fn ls(sandbox: &TestEnv) -> Vec<String> {
.collect::<Vec<_>>()
}

pub const NETWORKS: &str = "\
local
futurenet
mainnet
testnet
";

#[test]
fn set_and_remove_network() {
TestEnv::with_default(|sandbox| {
add_network(sandbox, "local");
let dir = sandbox.dir().join(".soroban").join("network");
add_network(sandbox, "custom");
let dir = sandbox.dir().join(".stellar").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "local.toml");
assert_eq!(file.file_name().to_str().unwrap(), "custom.toml");
let res = ls(sandbox);
assert_eq!(res[0], "local");
assert_eq!(res[0], "custom");
sandbox
.new_assert_cmd("network")
.arg("rm")
.arg("local")
.arg("custom")
.assert()
.success();

sandbox
.new_assert_cmd("network")
.arg("ls")
.assert()
.stdout("\n");
});
}

#[test]
fn use_default_futurenet() {
TestEnv::with_default(|sandbox| {
sandbox
.new_assert_cmd("keys")
.args(["generate", "alice", "--network", "futurenet"])
.assert()
.success();
let dir = sandbox.dir().join(".soroban").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "futurenet.toml");
});
}

#[test]
fn use_default_testnet() {
TestEnv::with_default(|sandbox| {
sandbox
.new_assert_cmd("keys")
.args(["generate", "alice", "--network", "testnet"])
.assert()
.success();
let dir = sandbox.dir().join(".soroban").join("network");
let mut read_dir = std::fs::read_dir(dir).unwrap();
let file = read_dir.next().unwrap().unwrap();
assert_eq!(file.file_name().to_str().unwrap(), "testnet.toml");
.stdout(NETWORKS);
});
}

Expand Down Expand Up @@ -118,7 +95,7 @@ fn set_and_remove_global_network() {
.arg("ls")
.arg("--global")
.assert()
.stdout("global\n");
.stdout(format!("global\n{}", NETWORKS));

sandbox
.new_assert_cmd("network")
Expand All @@ -134,23 +111,43 @@ fn set_and_remove_global_network() {
.env("XDG_CONFIG_HOME", dir.to_str().unwrap())
.arg("ls")
.assert()
.stdout("\n");
.stdout(NETWORKS);
}

#[test]
fn multiple_networks() {
let sandbox = TestEnv::default();
let ls = || -> Vec<String> { ls(&sandbox) };

add_network(&sandbox, "local");
println!("{:#?}", ls());
add_network(&sandbox, "custom");
println!("{:#?}", ls());
add_network(&sandbox, "local2");

assert_eq!(ls().as_slice(), ["local".to_owned(), "local2".to_owned()]);
assert_eq!(
ls().as_slice(),
[
"custom".to_owned(),
"local2".to_owned(),
"local".to_owned(),
"futurenet".to_owned(),
"mainnet".to_owned(),
"testnet".to_owned()
]
);

sandbox.cmd::<network::rm::Cmd>("local").run().unwrap();
sandbox.cmd::<network::rm::Cmd>("custom").run().unwrap();

assert_eq!(ls().as_slice(), ["local2".to_owned()]);
assert_eq!(
ls().as_slice(),
[
"local2".to_owned(),
"local".to_owned(),
"futurenet".to_owned(),
"mainnet".to_owned(),
"testnet".to_owned()
]
);

let sub_dir = sandbox.dir().join("sub_directory");
fs::create_dir(&sub_dir).unwrap();
Expand All @@ -168,7 +165,17 @@ fn multiple_networks() {
.run()
.unwrap();

assert_eq!(ls().as_slice(), ["local2".to_owned(), "local3".to_owned()]);
assert_eq!(
ls().as_slice(),
[
"local2".to_owned(),
"local3".to_owned(),
"local".to_owned(),
"futurenet".to_owned(),
"mainnet".to_owned(),
"testnet".to_owned()
]
);
}

#[test]
Expand Down Expand Up @@ -205,7 +212,7 @@ fn generate_key() {
.assert()
.stdout(predicates::str::contains("test_2\n"));
let file_contents =
fs::read_to_string(sandbox.dir().join(".soroban/identity/test_2.toml")).unwrap();
fs::read_to_string(sandbox.dir().join(".stellar/identity/test_2.toml")).unwrap();
assert_eq!(
file_contents,
format!("seed_phrase = \"{DEFAULT_SEED_PHRASE}\"\n")
Expand Down Expand Up @@ -368,6 +375,7 @@ fn set_default_identity() {

sandbox
.new_assert_cmd("env")
.env_remove("SOROBAN_ACCOUNT")
.assert()
.stdout(predicate::str::contains("STELLAR_ACCOUNT=alice"))
.success();
Expand Down
8 changes: 7 additions & 1 deletion cmd/soroban-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@ impl Config {

pub fn save(&self) -> Result<(), locator::Error> {
let toml_string = toml::to_string(&self)?;
let mut file = File::create(locator::config_file()?)?;
let path = locator::config_file()?;
let parent = path.parent();
if let Some(parent) = parent {
fs::create_dir_all(parent)?;
}
// Depending on the platform, this function may fail if the full directory path does not exist
let mut file = File::create(path)?;
file.write_all(toml_string.as_bytes())?;

Ok(())
Expand Down

0 comments on commit 901bd90

Please sign in to comment.