From 2a1b60d45cbaa1603df599094087362c76e48c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20Ra=C3=9Fmann?= Date: Fri, 15 Mar 2024 19:58:13 +0100 Subject: [PATCH] Fix failing tests dfgdfgfd dsf --- leptos_config/Cargo.toml | 1 + leptos_config/src/lib.rs | 22 ++++--- leptos_config/src/tests.rs | 63 +++++++++++--------- leptos_config/tests/config.rs | 107 +++++++++++++++++++++++----------- 4 files changed, 123 insertions(+), 70 deletions(-) diff --git a/leptos_config/Cargo.toml b/leptos_config/Cargo.toml index 3cf77f301e..65eed37377 100644 --- a/leptos_config/Cargo.toml +++ b/leptos_config/Cargo.toml @@ -19,3 +19,4 @@ typed-builder = "0.18" [dev-dependencies] tokio = { version = "1", features = ["rt", "macros"] } tempfile = "3" +temp-env = { version = "0.3.6", features = ["async_closure"] } diff --git a/leptos_config/src/lib.rs b/leptos_config/src/lib.rs index 89c33035be..77cfffeb83 100644 --- a/leptos_config/src/lib.rs +++ b/leptos_config/src/lib.rs @@ -303,30 +303,34 @@ impl TryFrom for ReloadWSProtocol { pub fn get_config_from_str( text: &str, ) -> Result { - let re: Regex = - Regex::new(r"(?m)^\[package.metadata.leptos\]\s*\n").unwrap(); + let re: Regex = Regex::new(r"(?m)^\[package.metadata.leptos\]").unwrap(); let re_workspace: Regex = - Regex::new(r"(?m)^\[\[workspace.metadata.leptos\]\]\s*\n").unwrap(); + Regex::new(r"(?m)^\[\[workspace.metadata.leptos\]\]").unwrap(); - let options_start; + let metadata_name; + let start; match re.find(text) { Some(found) => { - options_start = found.end(); + metadata_name = "[package.metadata.leptos]"; + start = found.start(); } None => match re_workspace.find(text) { Some(found) => { - options_start = found.end(); + metadata_name = "[[workspace.metadata.leptos]]"; + start = found.start(); } None => return Err(LeptosConfigError::ConfigSectionNotFound), }, }; // so that serde error messages have right line number - let newlines = text[..options_start].matches('\n').count(); - let input = "\n".repeat(newlines) + &text[options_start..]; + let newlines = text[..start].matches('\n').count(); + let input = "\n".repeat(newlines) + &text[start..]; + // so the settings will be interpreted as root level settings + let toml = input.replace(metadata_name, ""); let settings = Config::builder() // Read the "default" configuration file - .add_source(File::from_str(&input, FileFormat::Toml)) + .add_source(File::from_str(&toml, FileFormat::Toml)) // Layer on the environment-specific values. // Add in settings from environment variables (with a prefix of LEPTOS) // E.g. `LEPTOS_RELOAD_PORT=5001 would set `LeptosOptions.reload_port` diff --git a/leptos_config/src/tests.rs b/leptos_config/src/tests.rs index 1520892732..e40a854c56 100644 --- a/leptos_config/src/tests.rs +++ b/leptos_config/src/tests.rs @@ -30,44 +30,53 @@ fn ws_from_str_test() { #[test] fn env_w_default_test() { - std::env::set_var("LEPTOS_CONFIG_ENV_TEST", "custom"); - assert_eq!( - env_w_default("LEPTOS_CONFIG_ENV_TEST", "default").unwrap(), - String::from("custom") - ); - std::env::remove_var("LEPTOS_CONFIG_ENV_TEST"); - assert_eq!( - env_w_default("LEPTOS_CONFIG_ENV_TEST", "default").unwrap(), - String::from("default") - ); + _ = temp_env::with_var("LEPTOS_CONFIG_ENV_TEST", Some("custom"), || { + assert_eq!( + env_w_default("LEPTOS_CONFIG_ENV_TEST", "default").unwrap(), + String::from("custom") + ); + }); + + _ = temp_env::with_var_unset("LEPTOS_CONFIG_ENV_TEST", || { + assert_eq!( + env_w_default("LEPTOS_CONFIG_ENV_TEST", "default").unwrap(), + String::from("default") + ); + }); } #[test] fn env_wo_default_test() { - std::env::set_var("LEPTOS_CONFIG_ENV_TEST", "custom"); - assert_eq!( - env_wo_default("LEPTOS_CONFIG_ENV_TEST").unwrap(), - Some(String::from("custom")) - ); - std::env::remove_var("LEPTOS_CONFIG_ENV_TEST"); - assert_eq!(env_wo_default("LEPTOS_CONFIG_ENV_TEST").unwrap(), None); + _ = temp_env::with_var("LEPTOS_CONFIG_ENV_TEST", Some("custom"), || { + assert_eq!( + env_wo_default("LEPTOS_CONFIG_ENV_TEST").unwrap(), + Some(String::from("custom")) + ); + }); + + _ = temp_env::with_var_unset("LEPTOS_CONFIG_ENV_TEST", || { + assert_eq!(env_wo_default("LEPTOS_CONFIG_ENV_TEST").unwrap(), None); + }); } #[test] fn try_from_env_test() { // Test config values from environment variables - std::env::set_var("LEPTOS_OUTPUT_NAME", "app_test"); - std::env::set_var("LEPTOS_SITE_ROOT", "my_target/site"); - std::env::set_var("LEPTOS_SITE_PKG_DIR", "my_pkg"); - std::env::set_var("LEPTOS_SITE_ADDR", "0.0.0.0:80"); - std::env::set_var("LEPTOS_RELOAD_PORT", "8080"); - std::env::set_var("LEPTOS_RELOAD_EXTERNAL_PORT", "8080"); - std::env::set_var("LEPTOS_ENV", "PROD"); - std::env::set_var("LEPTOS_RELOAD_WS_PROTOCOL", "WSS"); + let config = temp_env::with_vars( + [ + ("LEPTOS_OUTPUT_NAME", Some("app_test")), + ("LEPTOS_SITE_ROOT", Some("my_target/site")), + ("LEPTOS_SITE_PKG_DIR", Some("my_pkg")), + ("LEPTOS_SITE_ADDR", Some("0.0.0.0:80")), + ("LEPTOS_RELOAD_PORT", Some("8080")), + ("LEPTOS_RELOAD_EXTERNAL_PORT", Some("8080")), + ("LEPTOS_ENV", Some("PROD")), + ("LEPTOS_RELOAD_WS_PROTOCOL", Some("WSS")), + ], + || LeptosOptions::try_from_env().unwrap(), + ); - let config = LeptosOptions::try_from_env().unwrap(); assert_eq!(config.output_name, "app_test"); - assert_eq!(config.site_root, "my_target/site"); assert_eq!(config.site_pkg_dir, "my_pkg"); assert_eq!( diff --git a/leptos_config/tests/config.rs b/leptos_config/tests/config.rs index 3bfd0c2e18..70c5acc312 100644 --- a/leptos_config/tests/config.rs +++ b/leptos_config/tests/config.rs @@ -23,13 +23,7 @@ env = "PROD" const CARGO_TOML_CONTENT_ERR: &str = r#"\ [package.metadata.leptos] -_output-name = "app-test" -_site-root = "my_target/site" -_site-pkg-dir = "my_pkg" -_site-addr = "0.0.0.0:80" -_reload-port = "8080" -_reload-external-port = "8080" -_env = "PROD" +- invalid toml - "#; #[tokio::test] @@ -43,10 +37,23 @@ async fn get_configuration_from_file_ok() { let path: &Path = cargo_tmp.as_ref(); let path_s = path.to_string_lossy().to_string(); - let config = get_configuration(Some(&path_s)) - .await - .unwrap() - .leptos_options; + let config = temp_env::async_with_vars( + [ + ("LEPTOS_OUTPUT_NAME", None::<&str>), + ("LEPTOS_SITE_ROOT", None::<&str>), + ("LEPTOS_SITE_PKG_DIR", None::<&str>), + ("LEPTOS_SITE_ADDR", None::<&str>), + ("LEPTOS_RELOAD_PORT", None::<&str>), + ("LEPTOS_RELOAD_EXTERNAL_PORT", None::<&str>), + ], + async { + get_configuration(Some(&path_s)) + .await + .unwrap() + .leptos_options + }, + ) + .await; assert_eq!(config.output_name, "app-test"); assert_eq!(config.site_root, "my_target/site"); @@ -91,10 +98,23 @@ async fn get_config_from_file_ok() { write!(output, "{CARGO_TOML_CONTENT_OK}").unwrap(); } - let config = get_config_from_file(&cargo_tmp) - .await - .unwrap() - .leptos_options; + let config = temp_env::async_with_vars( + [ + ("LEPTOS_OUTPUT_NAME", None::<&str>), + ("LEPTOS_SITE_ROOT", None::<&str>), + ("LEPTOS_SITE_PKG_DIR", None::<&str>), + ("LEPTOS_SITE_ADDR", None::<&str>), + ("LEPTOS_RELOAD_PORT", None::<&str>), + ("LEPTOS_RELOAD_EXTERNAL_PORT", None::<&str>), + ], + async { + get_config_from_file(&cargo_tmp) + .await + .unwrap() + .leptos_options + }, + ) + .await; assert_eq!(config.output_name, "app-test"); assert_eq!(config.site_root, "my_target/site"); @@ -129,9 +149,18 @@ async fn get_config_from_file_empty() { #[test] fn get_config_from_str_content() { - let config = get_config_from_str(CARGO_TOML_CONTENT_OK) - .unwrap() - .leptos_options; + let config = temp_env::with_vars_unset( + [ + "LEPTOS_OUTPUT_NAME", + "LEPTOS_SITE_ROOT", + "LEPTOS_SITE_PKG_DIR", + "LEPTOS_SITE_ADDR", + "LEPTOS_RELOAD_PORT", + "LEPTOS_RELOAD_EXTERNAL_PORT", + ], + || get_config_from_str(CARGO_TOML_CONTENT_OK).unwrap(), + ); + assert_eq!(config.output_name, "app-test"); assert_eq!(config.site_root, "my_target/site"); assert_eq!(config.site_pkg_dir, "my_pkg"); @@ -146,16 +175,20 @@ fn get_config_from_str_content() { #[tokio::test] async fn get_config_from_env() { // Test config values from environment variables - std::env::set_var("LEPTOS_OUTPUT_NAME", "app-test"); - std::env::set_var("LEPTOS_SITE_ROOT", "my_target/site"); - std::env::set_var("LEPTOS_SITE_PKG_DIR", "my_pkg"); - std::env::set_var("LEPTOS_SITE_ADDR", "0.0.0.0:80"); - std::env::set_var("LEPTOS_RELOAD_PORT", "8080"); - std::env::set_var("LEPTOS_RELOAD_EXTERNAL_PORT", "8080"); - - let config = get_configuration(None).await.unwrap().leptos_options; - assert_eq!(config.output_name, "app-test"); + let config = temp_env::async_with_vars( + [ + ("LEPTOS_OUTPUT_NAME", Some("app-test")), + ("LEPTOS_SITE_ROOT", Some("my_target/site")), + ("LEPTOS_SITE_PKG_DIR", Some("my_pkg")), + ("LEPTOS_SITE_ADDR", Some("0.0.0.0:80")), + ("LEPTOS_RELOAD_PORT", Some("8080")), + ("LEPTOS_RELOAD_EXTERNAL_PORT", Some("8080")), + ], + async { get_configuration(None).await.unwrap().leptos_options }, + ) + .await; + assert_eq!(config.output_name, "app-test"); assert_eq!(config.site_root, "my_target/site"); assert_eq!(config.site_pkg_dir, "my_pkg"); assert_eq!( @@ -166,13 +199,19 @@ async fn get_config_from_env() { assert_eq!(config.reload_external_port, Some(8080)); // Test default config values - std::env::remove_var("LEPTOS_SITE_ROOT"); - std::env::remove_var("LEPTOS_SITE_PKG_DIR"); - std::env::remove_var("LEPTOS_SITE_ADDR"); - std::env::remove_var("LEPTOS_RELOAD_PORT"); - std::env::set_var("LEPTOS_RELOAD_EXTERNAL_PORT", "443"); + let config = temp_env::async_with_vars( + [ + ("LEPTOS_OUTPUT_NAME", None::<&str>), + ("LEPTOS_SITE_ROOT", None::<&str>), + ("LEPTOS_SITE_PKG_DIR", None::<&str>), + ("LEPTOS_SITE_ADDR", None::<&str>), + ("LEPTOS_RELOAD_PORT", None::<&str>), + ("LEPTOS_RELOAD_EXTERNAL_PORT", None::<&str>), + ], + async { get_configuration(None).await.unwrap().leptos_options }, + ) + .await; - let config = get_configuration(None).await.unwrap().leptos_options; assert_eq!(config.site_root, "target/site"); assert_eq!(config.site_pkg_dir, "pkg"); assert_eq!( @@ -180,7 +219,7 @@ async fn get_config_from_env() { SocketAddr::from_str("127.0.0.1:3000").unwrap() ); assert_eq!(config.reload_port, 3001); - assert_eq!(config.reload_external_port, Some(443)); + assert_eq!(config.reload_external_port, None); } #[test]