From a1b03ec1b4e88bab97627160bbbce29e7256467a Mon Sep 17 00:00:00 2001 From: Yihau Chen Date: Thu, 20 Oct 2022 17:34:08 +0800 Subject: [PATCH] add test_save for install config (#28397) --- Cargo.lock | 1 + install/Cargo.toml | 1 + install/src/config.rs | 76 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 0fc81a8793..34a7b912e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5396,6 +5396,7 @@ dependencies = [ "lazy_static", "nix", "reqwest", + "scopeguard", "semver 1.0.14", "serde", "serde_yaml 0.9.13", diff --git a/install/Cargo.toml b/install/Cargo.toml index 0fb5c9d516..9cda2da455 100644 --- a/install/Cargo.toml +++ b/install/Cargo.toml @@ -23,6 +23,7 @@ indicatif = "0.17.1" lazy_static = "1.4.0" nix = "0.25.0" reqwest = { version = "0.11.12", default-features = false, features = ["blocking", "brotli", "deflate", "gzip", "rustls-tls", "json"] } +scopeguard = "1.1.0" semver = "1.0.14" serde = { version = "1.0.144", features = ["derive"] } serde_yaml = "0.9.13" diff --git a/install/src/config.rs b/install/src/config.rs index 31cc1560da..62ba76bd6c 100644 --- a/install/src/config.rs +++ b/install/src/config.rs @@ -86,3 +86,79 @@ impl Config { self.releases_dir.join(release_id) } } + +#[cfg(test)] +mod test { + use { + super::*, + scopeguard::defer, + std::{ + env, + fs::{read_to_string, remove_file}, + }, + }; + + #[test] + fn test_save() { + let root_dir = env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR"); + let json_rpc_url = "https://api.mainnet-beta.solana.com"; + let pubkey = Pubkey::default(); + let config_name = "config.yaml"; + let config_path = format!("{}/{}", root_dir, config_name); + + let config = Config::new(&root_dir, json_rpc_url, &pubkey, None); + + assert_eq!(config.save(config_name), Ok(())); + defer! { + remove_file(&config_path).unwrap(); + } + + assert_eq!( + read_to_string(&config_path).unwrap(), + format!( + "--- +json_rpc_url: https://api.mainnet-beta.solana.com +update_manifest_pubkey: +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +- 0 +current_update_manifest: null +update_poll_secs: 3600 +explicit_release: null +releases_dir: {}/releases +active_release_dir: {}/active_release +", + root_dir, root_dir + ), + ); + } +}