Skip to content

Commit 76bee79

Browse files
yoshuawuytsCopilot
andcommitted
Fix flaky config env-var test race
The config tests test_config_file_path_override_with_env_var and test_new_method_without_wassette_config_file_env both mutated the shared WASSETTE_CONFIG_FILE environment variable using a manual SetEnv helper and std::env::remove_var. When run in parallel they raced, intermittently clearing the var set by one test and causing Config::new to fall back to the default component directory. Use the temp_env crate (already used by the other env-dependent tests in this module) to serialize access to the environment variable, and remove the now-unused SetEnv helper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent a261198 commit 76bee79

1 file changed

Lines changed: 19 additions & 39 deletions

File tree

src/config.rs

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ impl Config {
172172

173173
#[cfg(test)]
174174
mod tests {
175-
use std::ffi::OsString;
176175
use std::fs;
177176

178177
use tempfile::TempDir;
@@ -223,32 +222,6 @@ mod tests {
223222
}
224223
}
225224

226-
struct SetEnv<'a> {
227-
old: Option<OsString>,
228-
key: &'a str,
229-
}
230-
231-
impl Drop for SetEnv<'_> {
232-
fn drop(&mut self) {
233-
if let Some(old_value) = &self.old {
234-
std::env::set_var(self.key, old_value);
235-
} else {
236-
std::env::remove_var(self.key);
237-
}
238-
}
239-
}
240-
241-
impl<'a> SetEnv<'a> {
242-
fn new(key: &'a str, value: &'a str) -> Self {
243-
let old_value = std::env::var_os(key);
244-
std::env::set_var(key, value);
245-
SetEnv {
246-
old: old_value,
247-
key,
248-
}
249-
}
250-
}
251-
252225
#[test]
253226
fn test_config_file_not_exists_succeeds_with_defaults() {
254227
let temp_dir = TempDir::new().unwrap();
@@ -332,14 +305,15 @@ component_dir = "/config/component/dir"
332305
// It should try to use the default config location, which likely won't exist
333306
// but should still succeed with defaults
334307

335-
// Ensure WASSETTE_CONFIG_FILE is not set
336-
std::env::remove_var("WASSETTE_CONFIG_FILE");
337-
338-
let serve_config = create_test_cli_config();
339-
let config = Config::new(&serve_config).expect("Failed to create config");
308+
// Ensure WASSETTE_CONFIG_FILE is not set, using temp_env to serialize
309+
// access to the shared environment variable across tests.
310+
temp_env::with_var_unset("WASSETTE_CONFIG_FILE", || {
311+
let serve_config = create_test_cli_config();
312+
let config = Config::new(&serve_config).expect("Failed to create config");
340313

341-
// Should use CLI defaults since no config file exists
342-
assert_eq!(config.component_dir, PathBuf::from("/test/component/dir"));
314+
// Should use CLI defaults since no config file exists
315+
assert_eq!(config.component_dir, PathBuf::from("/test/component/dir"));
316+
});
343317
}
344318

345319
#[test]
@@ -372,12 +346,18 @@ policy_file = "custom_policy.yaml"
372346
"#;
373347
fs::write(&config_file, toml_content).unwrap();
374348

375-
// Use SetEnv helper to manage WASSETTE_CONFIG_FILE environment variable
376-
let _env = SetEnv::new("WASSETTE_CONFIG_FILE", config_file.to_str().unwrap());
377-
378-
let config = Config::new(&empty_test_cli_config()).expect("Failed to create config");
349+
// Use temp_env to serialize access to the shared WASSETTE_CONFIG_FILE
350+
// environment variable, preventing races with other tests.
351+
temp_env::with_var(
352+
"WASSETTE_CONFIG_FILE",
353+
Some(config_file.to_str().unwrap()),
354+
|| {
355+
let config =
356+
Config::new(&empty_test_cli_config()).expect("Failed to create config");
379357

380-
assert_eq!(config.component_dir, PathBuf::from("/custom/component/dir"));
358+
assert_eq!(config.component_dir, PathBuf::from("/custom/component/dir"));
359+
},
360+
);
381361
}
382362

383363
#[test]

0 commit comments

Comments
 (0)