Expand description
Process working-directory guard for tests. Process-current-directory guard for tests.
The process working directory is global mutable state:
a test that calls std::env::set_current_dir mutates a value every other test in the same binary observes.
CurrentDirGuard makes that safe by
- holding a process-wide lock for its lifetime, so concurrent tests in the same binary cannot interleave their directory changes, and
- restoring the previous working directory on drop, so a change never leaks into a later test.
It is the working-directory analogue of the env-var mutex pattern: any test that depends on or mutates the current directory should hold a guard for the duration of that dependence.
use rskit_testutil::CurrentDirGuard;
let _cwd = CurrentDirGuard::change_to(repo_root)?;
// ... code here observes `repo_root` as the working directory ...
// the previous directory is restored when `_cwd` drops.Structsยง
- Current
DirGuard - An RAII guard that pins the process working directory for its lifetime and restores the previous directory when dropped.