Skip to content

Commit 057a2f6

Browse files
committed
Add test for process env variable precedence over config.toml [env]
1 parent 6093315 commit 057a2f6

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

  • crates/project-model/src

crates/project-model/src/env.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,45 @@ env.RA_TEST_NOT_AN_OBJECT = "value"
172172
assert_eq!(env.get("RA_TEST_OVERWRITTEN").as_deref(), Some("newvalue"));
173173
assert_eq!(env.get("RA_TEST_NOT_AN_OBJECT").as_deref(), Some("value"));
174174
}
175+
176+
#[test]
177+
fn cargo_config_env_respects_process_env() {
178+
use itertools::Itertools;
179+
180+
let cwd = paths::AbsPathBuf::try_from(
181+
paths::Utf8PathBuf::try_from(std::env::current_dir().unwrap()).unwrap(),
182+
)
183+
.unwrap();
184+
let config_path = cwd.join(".cargo").join("config.toml");
185+
186+
// SAFETY: this test is not run in parallel with other tests that depend on these env vars.
187+
unsafe {
188+
std::env::set_var("RA_TEST_PROCESS_ENV_STRING", "from_process");
189+
std::env::set_var("RA_TEST_PROCESS_ENV_TABLE", "from_process");
190+
std::env::set_var("RA_TEST_PROCESS_ENV_FORCED", "from_process");
191+
}
192+
193+
let raw = r#"
194+
env.RA_TEST_PROCESS_ENV_STRING = "from_config"
195+
env.RA_TEST_PROCESS_ENV_TABLE.value = "from_config"
196+
env.RA_TEST_PROCESS_ENV_FORCED.value = "from_config"
197+
env.RA_TEST_PROCESS_ENV_FORCED.force = true
198+
"#;
199+
let raw = raw.lines().map(|l| format!("{l} # {config_path}")).join("\n");
200+
let config = CargoConfigFile::from_string_for_test(raw);
201+
let extra_env = FxHashMap::default();
202+
let env = cargo_config_env(&Some(config), &extra_env);
203+
204+
// Plain string form should use process env value, not config value
205+
assert_eq!(env.get("RA_TEST_PROCESS_ENV_STRING").as_deref(), Some("from_process"));
206+
// Table form without force should use process env value, not config value
207+
assert_eq!(env.get("RA_TEST_PROCESS_ENV_TABLE").as_deref(), Some("from_process"));
208+
// Table form with force=true should override process env
209+
assert_eq!(env.get("RA_TEST_PROCESS_ENV_FORCED").as_deref(), Some("from_config"));
210+
211+
unsafe {
212+
std::env::remove_var("RA_TEST_PROCESS_ENV_STRING");
213+
std::env::remove_var("RA_TEST_PROCESS_ENV_TABLE");
214+
std::env::remove_var("RA_TEST_PROCESS_ENV_FORCED");
215+
}
216+
}

0 commit comments

Comments
 (0)