#[test]
fn the_settings_guard_and_the_env_guard_are_one_lock() {
assert!(
!newt_core::process_env::held_by_current_thread(),
"a fresh test thread holds no environment"
);
let settings = newt_core::test_guard::GlobalSettingsGuard::acquire();
assert!(
newt_core::process_env::held_by_current_thread(),
"GlobalSettingsGuard must hold the process-env lock — otherwise it \
is a second lock over the same variables, which is #1850"
);
drop(settings);
assert!(!newt_core::process_env::held_by_current_thread());
let env = crate::test_env_guard::env_write_guard();
assert!(
newt_core::process_env::held_by_current_thread(),
"test_env_guard must hold the SAME lock — this is the other half \
of the pair that raced"
);
drop(env);
assert!(!newt_core::process_env::held_by_current_thread());
}
#[test]
fn the_env_guard_excludes_every_other_thread() {
let env = crate::test_env_guard::env_write_guard();
let taken = std::thread::spawn(|| newt_core::process_env::try_lock().is_some())
.join()
.expect("probe thread");
assert!(
!taken,
"a second thread entered the process environment while the env \
write guard was held"
);
drop(env);
}
#[test]
fn a_held_guard_does_not_block_the_production_writer_on_its_own_thread() {
let _env = crate::test_env_guard::env_write_guard();
let saved = std::env::var("NEWT_OPENAI_API").ok();
crate::apply_openai_api_env(newt_core::OpenAiApi::Responses);
assert_eq!(
std::env::var("NEWT_OPENAI_API").ok().as_deref(),
Some("responses"),
"the production writer must complete while this thread holds the lock"
);
newt_core::process_env::set_or_remove("NEWT_OPENAI_API", saved.as_deref());
}
#[test]
fn production_env_writes_go_through_the_process_env_lock() {
const SET: &str = concat!("std::env::", "set_var(");
const REMOVE: &str = concat!("std::env::", "remove_var(");
for (name, src, baseline) in [
(
"commands/model.rs",
include_str!("../commands/model.rs"),
0usize,
),
(
"commands/settings.rs",
include_str!("../commands/settings.rs"),
0,
),
("lib.rs", include_str!("../lib.rs"), 15),
(
"lib_tests/process_env_isolation_tests.rs",
include_str!("process_env_isolation_tests.rs"),
0,
),
] {
let found = src.matches(SET).count() + src.matches(REMOVE).count();
assert!(
found <= baseline,
"{name}: {found} direct env mutations, baseline {baseline} — a new \
one must go through newt_core::process_env (#1850). This baseline \
ratchets DOWN only; lower it in the PR that removes a site."
);
}
}