Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Environment variables do not overwrite Config.toml options #2433

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion leptos_config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "../README.md"
rust-version.workspace = true

[dependencies]
config = { version = "0.14", default-features = false, features = ["toml"] }
config = { version = "0.14", default-features = false, features = ["toml", "convert-case"] }
regex = "1.7.0"
serde = { version = "1.0.151", features = ["derive"] }
thiserror = "1.0.38"
Expand All @@ -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"] }
19 changes: 13 additions & 6 deletions leptos_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pub mod errors;

use crate::errors::LeptosConfigError;
use config::{Config, File, FileFormat};
use config::{Case, Config, File, FileFormat};
use regex::Regex;
use std::{
convert::TryFrom, env::VarError, fs, net::SocketAddr, path::Path,
Expand Down Expand Up @@ -300,7 +300,9 @@ impl TryFrom<String> for ReloadWSProtocol {

/// Loads [LeptosOptions] from a Cargo.toml text content with layered overrides.
/// If an env var is specified, like `LEPTOS_ENV`, it will override a setting in the file.
pub fn get_config_from_str(text: &str) -> Result<ConfFile, LeptosConfigError> {
pub fn get_config_from_str(
text: &str,
) -> Result<LeptosOptions, LeptosConfigError> {
let re: Regex = Regex::new(r"(?m)^\[package.metadata.leptos\]").unwrap();
let re_workspace: Regex =
Regex::new(r"(?m)^\[\[workspace.metadata.leptos\]\]").unwrap();
Expand All @@ -324,14 +326,18 @@ pub fn get_config_from_str(text: &str) -> Result<ConfFile, LeptosConfigError> {
// so that serde error messages have right line number
let newlines = text[..start].matches('\n').count();
let input = "\n".repeat(newlines) + &text[start..];
let toml = input.replace(metadata_name, "[leptos-options]");
// 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(&toml, FileFormat::Toml))
// Layer on the environment-specific values.
// Add in settings from environment variables (with a prefix of LEPTOS and '_' as separator)
// Add in settings from environment variables (with a prefix of LEPTOS)
// E.g. `LEPTOS_RELOAD_PORT=5001 would set `LeptosOptions.reload_port`
.add_source(config::Environment::with_prefix("LEPTOS").separator("_"))
.add_source(
config::Environment::with_prefix("LEPTOS")
.convert_case(Case::Kebab),
)
.build()?;

settings
Expand Down Expand Up @@ -361,7 +367,8 @@ pub async fn get_config_from_file<P: AsRef<Path>>(
) -> Result<ConfFile, LeptosConfigError> {
let text = fs::read_to_string(path)
.map_err(|_| LeptosConfigError::ConfigNotFound)?;
get_config_from_str(&text)
let leptos_options = get_config_from_str(&text)?;
Ok(ConfFile { leptos_options })
}

/// Loads [LeptosOptions] from environment variables or rely on the defaults
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
156 changes: 122 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 All @@ -197,3 +236,52 @@ fn leptos_options_builder_default() {
assert_eq!(conf.reload_port, 3001);
assert_eq!(conf.reload_external_port, None);
}

#[test]
fn environment_variable_override() {
// first check without variables set
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");
assert_eq!(
config.site_addr,
SocketAddr::from_str("0.0.0.0:80").unwrap()
);
assert_eq!(config.reload_port, 8080);
assert_eq!(config.reload_external_port, Some(8080));

// check the override
let config = temp_env::with_vars(
[
("LEPTOS_OUTPUT_NAME", Some("app-test2")),
("LEPTOS_SITE_ROOT", Some("my_target/site2")),
("LEPTOS_SITE_PKG_DIR", Some("my_pkg2")),
("LEPTOS_SITE_ADDR", Some("0.0.0.0:82")),
("LEPTOS_RELOAD_PORT", Some("8082")),
("LEPTOS_RELOAD_EXTERNAL_PORT", Some("8082")),
],
|| get_config_from_str(CARGO_TOML_CONTENT_OK).unwrap(),
);

assert_eq!(config.output_name, "app-test2");
assert_eq!(config.site_root, "my_target/site2");
assert_eq!(config.site_pkg_dir, "my_pkg2");
assert_eq!(
config.site_addr,
SocketAddr::from_str("0.0.0.0:82").unwrap()
);
assert_eq!(config.reload_port, 8082);
assert_eq!(config.reload_external_port, Some(8082));
}
Loading