1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Shared global mutex for env-mutating tests.
//!
//! `cargo test` parallelises tests within a single binary, so any test
//! that mutates the process environment (`std::env::set_var` /
//! `remove_var`) races against other tests reading those variables.
//! Hold this mutex across the env-mutation block to serialise without
//! standing up a per-file mutex in every test module.
//!
//! Usage:
//!
//! ```no_run
//! use anodizer_core::test_helpers::env::env_mutex;
//!
//! let _g = env_mutex().lock().unwrap_or_else(|e| e.into_inner());
//! // SAFETY: serialised by the mutex above; pair set / remove.
//! unsafe { std::env::set_var("FOO", "1") };
//! // ... test body ...
//! unsafe { std::env::remove_var("FOO") };
//! ```
//!
//! Recovering from a poisoned lock (`.unwrap_or_else(|e| e.into_inner())`)
//! is intentional: a panicking test that holds the guard pollutes the
//! mutex state, but subsequent tests still want to serialise correctly.
//! Cleaning the env var on test-body unwind is the test author's
//! responsibility — wrap in a scope guard if the test panics
//! unpredictably.
use ;
/// Process-wide mutex shared by every test that mutates the env. Lazily
/// initialised on first call; safe to drop the returned guard with
/// `let _g = env_mutex().lock()....`
/// RAII env-var override that restores the prior value on drop, so a panicking
/// assertion cannot leak a mutated env into a sibling test — the scope guard
/// the module doc above says is otherwise the test author's responsibility.
/// Callers must hold [`env_mutex`] for the guard's lifetime so the
/// process-global env is mutated by one test at a time.
;