From 03a5cf1e74ca51b8c25844b79c1c6e3196a71708 Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Tue, 17 Sep 2024 11:51:53 +0100 Subject: [PATCH] add more esp-config tests --- esp-config/Cargo.toml | 3 + esp-config/src/generate.rs | 118 ++++++++++++++++++++++++++----------- 2 files changed, 88 insertions(+), 33 deletions(-) diff --git a/esp-config/Cargo.toml b/esp-config/Cargo.toml index b8fc6c3e867..43413ec6894 100644 --- a/esp-config/Cargo.toml +++ b/esp-config/Cargo.toml @@ -7,6 +7,9 @@ rust-version = "1.79.0" [dependencies] document-features = "0.2.10" +[dev-dependencies] +temp-env = "0.3.6" + [features] ## Enable the generation and parsing of a config build = [] \ No newline at end of file diff --git a/esp-config/src/generate.rs b/esp-config/src/generate.rs index 972b92e2cb3..508ed10d532 100644 --- a/esp-config/src/generate.rs +++ b/esp-config/src/generate.rs @@ -243,42 +243,94 @@ mod test { #[test] fn env_override() { - unsafe { - env::set_var("ESP_TEST_NUMBER", "0xaa"); - env::set_var("ESP_TEST_STRING", "Hello world!"); - env::set_var("ESP_TEST_BOOL", "true"); - } - - let configs = generate_config( - "esp-test", - &[ - ("number", Value::Number(999), "NA"), - ("string", Value::String("Demo".to_owned()), "NA"), - ("bool", Value::Bool(false), "NA"), + temp_env::with_vars( + [ + ("ESP_TEST_NUMBER", Some("0xaa")), + ("ESP_TEST_STRING", Some("Hello world!")), + ("ESP_TEST_BOOL", Some("true")), ], - false, - ); - - assert_eq!( - match configs.get("ESP_TEST_NUMBER").unwrap() { - Value::Number(num) => *num, - _ => unreachable!(), + || { + let configs = generate_config( + "esp-test", + &[ + ("number", Value::Number(999), "NA"), + ("string", Value::String("Demo".to_owned()), "NA"), + ("bool", Value::Bool(false), "NA"), + ("number_default", Value::Number(999), "NA"), + ("string_default", Value::String("Demo".to_owned()), "NA"), + ("bool_default", Value::Bool(false), "NA"), + ], + false, + ); + + // some values have changed + assert_eq!( + match configs.get("ESP_TEST_NUMBER").unwrap() { + Value::Number(num) => *num, + _ => unreachable!(), + }, + 0xaa + ); + assert_eq!( + match configs.get("ESP_TEST_STRING").unwrap() { + Value::String(val) => val, + _ => unreachable!(), + }, + "Hello world!" + ); + assert_eq!( + match configs.get("ESP_TEST_BOOL").unwrap() { + Value::Bool(val) => *val, + _ => unreachable!(), + }, + true + ); + + // the rest are the defaults + assert_eq!( + match configs.get("ESP_TEST_NUMBER_DEFAULT").unwrap() { + Value::Number(num) => *num, + _ => unreachable!(), + }, + 999 + ); + assert_eq!( + match configs.get("ESP_TEST_STRING_DEFAULT").unwrap() { + Value::String(val) => val, + _ => unreachable!(), + }, + "Demo" + ); + assert_eq!( + match configs.get("ESP_TEST_BOOL_DEFAULT").unwrap() { + Value::Bool(val) => *val, + _ => unreachable!(), + }, + false + ); }, - 0xaa - ); - assert_eq!( - match configs.get("ESP_TEST_STRING").unwrap() { - Value::String(val) => val, - _ => unreachable!(), - }, - "Hello world!" - ); - assert_eq!( - match configs.get("ESP_TEST_BOOL").unwrap() { - Value::Bool(val) => *val, - _ => unreachable!(), + ) + } + + #[test] + #[should_panic] + fn env_unknown_bails() { + temp_env::with_vars( + [ + ("ESP_TEST_NUMBER", Some("0xaa")), + ("ESP_TEST_RANDOM_VARIABLE", Some("")), + ], + || { + generate_config("esp-test", &[("number", Value::Number(999), "NA")], false); }, - true ); } + + #[test] + #[should_panic] + fn env_invalid_values_bails() { + temp_env::with_vars([("ESP_TEST_NUMBER", Some("Hello world"))], || { + generate_config("esp-test", &[("number", Value::Number(999), "NA")], false); + }); + } }