From bce51adca0ae5ddecff8ebfe5c3c00ce36fd242f Mon Sep 17 00:00:00 2001 From: Joseph Micheli Date: Sun, 28 Apr 2024 23:38:06 -0500 Subject: [PATCH] Fix broken stump_config tests by adding env var management crate --- Cargo.lock | 10 ++ core/Cargo.toml | 1 + core/src/config/stump_config.rs | 151 ++++++++++-------- .../src/notifier/discord_client.rs | 1 + .../src/notifier/telegram_client.rs | 2 + 5 files changed, 97 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 381aade81..b694d7675 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6423,6 +6423,7 @@ dependencies = [ "serde-xml-rs", "serde_json", "specta", + "temp-env", "tempfile", "thiserror", "tokio", @@ -6835,6 +6836,15 @@ dependencies = [ "windows 0.39.0", ] +[[package]] +name = "temp-env" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96374855068f47402c3121c6eed88d29cb1de8f3ab27090e273e420bdabcf050" +dependencies = [ + "parking_lot 0.12.1", +] + [[package]] name = "tempdir" version = "0.3.7" diff --git a/core/Cargo.toml b/core/Cargo.toml index 53570015e..6e5532327 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -52,6 +52,7 @@ regex = "1.10.4" alphanumeric-sort = "1.5.3" [dev-dependencies] +temp-env = "0.3.6" tempfile = { workspace = true } criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] } diff --git a/core/src/config/stump_config.rs b/core/src/config/stump_config.rs index 26f084319..ce9607f89 100644 --- a/core/src/config/stump_config.rs +++ b/core/src/config/stump_config.rs @@ -525,42 +525,49 @@ mod tests { #[test] fn test_getting_config_from_environment() { - // Set environment variables - env::set_var(PROFILE_KEY, "release"); - env::set_var(PORT_KEY, "1337"); - env::set_var(VERBOSITY_KEY, "3"); - env::set_var(DB_PATH_KEY, "not_a_real_path"); - env::set_var(CLIENT_KEY, "not_a_real_dir"); - env::set_var(CONFIG_DIR_KEY, "also_not_a_real_dir"); - env::set_var(DISABLE_SWAGGER_KEY, "true"); - env::set_var(HASH_COST_KEY, "24"); - env::set_var(SESSION_TTL_KEY, (3600 * 24).to_string()); - env::set_var(SESSION_EXPIRY_INTERVAL_KEY, (60 * 60 * 8).to_string()); - - // Create a new StumpConfig and load values from the environment. - let config = StumpConfig::new("not_a_dir".to_string()) - .with_environment() - .unwrap(); - - // Confirm values are as expected - assert_eq!( - config, - StumpConfig { - profile: "release".to_string(), - port: 1337, - verbosity: 3, - pretty_logs: true, - db_path: Some("not_a_real_path".to_string()), - client_dir: "not_a_real_dir".to_string(), - config_dir: "also_not_a_real_dir".to_string(), - allowed_origins: vec![], - pdfium_path: None, - disable_swagger: true, - password_hash_cost: 24, - session_ttl: 3600 * 24, - expired_session_cleanup_interval: 60 * 60 * 8, - scanner_chunk_size: DEFAULT_SCANNER_CHUNK_SIZE, - } + temp_env::with_vars( + [ + (PROFILE_KEY, Some("release")), + (PORT_KEY, Some("1337")), + (VERBOSITY_KEY, Some("2")), + (DB_PATH_KEY, Some("not_a_real_path")), + (CLIENT_KEY, Some("not_a_real_dir")), + (CONFIG_DIR_KEY, Some("also_not_a_real_dir")), + (DISABLE_SWAGGER_KEY, Some("true")), + (HASH_COST_KEY, Some("24")), + (SESSION_TTL_KEY, Some(&(3600 * 24).to_string())), + ( + SESSION_EXPIRY_INTERVAL_KEY, + Some(&(60 * 60 * 8).to_string()), + ), + ], + || { + // Create a new StumpConfig and load values from the environment. + let config = StumpConfig::new("not_a_dir".to_string()) + .with_environment() + .unwrap(); + + // Confirm values are as expected + assert_eq!( + config, + StumpConfig { + profile: "release".to_string(), + port: 1337, + verbosity: 2, + pretty_logs: true, + db_path: Some("not_a_real_path".to_string()), + client_dir: "not_a_real_dir".to_string(), + config_dir: "also_not_a_real_dir".to_string(), + allowed_origins: vec![], + pdfium_path: None, + disable_swagger: true, + password_hash_cost: 24, + session_ttl: 3600 * 24, + expired_session_cleanup_interval: 60 * 60 * 8, + scanner_chunk_size: DEFAULT_SCANNER_CHUNK_SIZE, + } + ); + }, ); } @@ -670,38 +677,46 @@ mod tests { #[test] fn test_simulate_first_boot() { - env::set_var(PORT_KEY, "1337"); - env::set_var(VERBOSITY_KEY, "2"); - env::set_var(DISABLE_SWAGGER_KEY, "true"); - env::set_var(HASH_COST_KEY, "1"); - - let tempdir = tempfile::tempdir().expect("Failed to create temporary directory"); - // Now we can create a StumpConfig rooted at the temporary directory - let config_dir = tempdir.path().to_string_lossy().to_string(); - let generated = StumpConfig::new(config_dir.clone()) - .with_config_file() - .expect("Failed to generate StumpConfig from Stump.toml") - .with_environment() - .expect("Failed to generate StumpConfig from environment"); - - let expected = StumpConfig { - profile: "debug".to_string(), - port: 1337, - verbosity: 2, - pretty_logs: true, - db_path: None, - client_dir: "./dist".to_string(), - config_dir, - allowed_origins: vec![], - pdfium_path: None, - disable_swagger: true, - password_hash_cost: 1, - session_ttl: DEFAULT_SESSION_TTL, - expired_session_cleanup_interval: DEFAULT_SESSION_EXPIRY_CLEANUP_INTERVAL, - scanner_chunk_size: DEFAULT_SCANNER_CHUNK_SIZE, - }; - - assert_eq!(generated, expected); + temp_env::with_vars( + [ + (PORT_KEY, Some("1337")), + (VERBOSITY_KEY, Some("2")), + (DISABLE_SWAGGER_KEY, Some("true")), + (HASH_COST_KEY, Some("1")), + ], + || { + let tempdir = + tempfile::tempdir().expect("Failed to create temporary directory"); + // Now we can create a StumpConfig rooted at the temporary directory + let config_dir = tempdir.path().to_string_lossy().to_string(); + let generated = StumpConfig::new(config_dir.clone()) + .with_config_file() + .expect("Failed to generate StumpConfig from Stump.toml") + .with_environment() + .expect("Failed to generate StumpConfig from environment"); + + assert_eq!( + generated, + StumpConfig { + profile: "debug".to_string(), + port: 1337, + verbosity: 2, + pretty_logs: true, + db_path: None, + client_dir: "./dist".to_string(), + config_dir, + allowed_origins: vec![], + pdfium_path: None, + disable_swagger: true, + password_hash_cost: 1, + session_ttl: DEFAULT_SESSION_TTL, + expired_session_cleanup_interval: + DEFAULT_SESSION_EXPIRY_CLEANUP_INTERVAL, + scanner_chunk_size: DEFAULT_SCANNER_CHUNK_SIZE, + } + ); + }, + ); } fn get_mock_config_file() -> String { diff --git a/crates/integrations/src/notifier/discord_client.rs b/crates/integrations/src/notifier/discord_client.rs index 4bb5013e2..a98b9675a 100644 --- a/crates/integrations/src/notifier/discord_client.rs +++ b/crates/integrations/src/notifier/discord_client.rs @@ -76,6 +76,7 @@ mod tests { DiscordClient::new(webhook_url) } + #[ignore = "No token"] #[tokio::test] async fn test_send_message() { let client = get_debug_client(); diff --git a/crates/integrations/src/notifier/telegram_client.rs b/crates/integrations/src/notifier/telegram_client.rs index 363a6f205..8f67cc65d 100644 --- a/crates/integrations/src/notifier/telegram_client.rs +++ b/crates/integrations/src/notifier/telegram_client.rs @@ -61,6 +61,8 @@ mod tests { let chat_id = std::env::var("DUMMY_TG_CHAT_ID").expect("Failed to load chat ID"); TelegramClient::new(token, chat_id) } + + #[ignore = "No token"] #[tokio::test] async fn test_send_message() { let client = get_debug_client();