Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
dfgdfgfd

dsf
  • Loading branch information
Gunnar Raßmann committed Mar 15, 2024
1 parent e3fca3d commit 2a1b60d
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 70 deletions.
1 change: 1 addition & 0 deletions leptos_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
22 changes: 13 additions & 9 deletions leptos_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,30 +303,34 @@ impl TryFrom<String> for ReloadWSProtocol {
pub fn get_config_from_str(
text: &str,
) -> Result<LeptosOptions, LeptosConfigError> {
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`
Expand Down
63 changes: 36 additions & 27 deletions leptos_config/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
107 changes: 73 additions & 34 deletions leptos_config/tests/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand All @@ -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!(
Expand All @@ -166,21 +199,27 @@ 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!(
config.site_addr,
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]
Expand Down

0 comments on commit 2a1b60d

Please sign in to comment.