-
Notifications
You must be signed in to change notification settings - Fork 0
Explain PATH mutability hazards in tests #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leynos
merged 7 commits into
main
from
codex/expand-comments-in-ninja-fixtures-and-refactor
Aug 12, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c2a603f
Test PATH guard helper
leynos b8fbb4d
Clarify env fixtures and test empty PATH
leynos 71ddea4
Handle missing PATH in helper
leynos 768ca46
Refactor PATH helpers to use mutable env trait
leynos 604e0ef
Clarify path guard usage
leynos a7cb79a
Allow PathGuard to use injected env
leynos fde9d5f
Apply formatting
leynos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| //! Tests for scoped manipulation of `PATH` via `prepend_dir_to_path` and | ||
| //! `PathGuard`. | ||
|
|
||
| use mockable::Env; | ||
| use rstest::rstest; | ||
| use serial_test::serial; | ||
|
|
||
| #[path = "support/env.rs"] | ||
| mod env; | ||
| mod support; | ||
| use env::{SystemEnv, mocked_path_env, prepend_dir_to_path}; | ||
| use support::env_lock::EnvLock; | ||
|
|
||
| #[rstest] | ||
| #[serial] | ||
| fn prepend_dir_to_path_sets_and_restores() { | ||
| let env = mocked_path_env(); | ||
| let original = env.raw("PATH").expect("PATH should be set in mock"); | ||
| let dir = tempfile::tempdir().expect("temp dir"); | ||
| let guard = prepend_dir_to_path(&env, dir.path()); | ||
| let after = std::env::var("PATH").expect("path var"); | ||
| let first = std::env::split_paths(&after).next().expect("first path"); | ||
| assert_eq!(first, dir.path()); | ||
| drop(guard); | ||
| let restored = std::env::var("PATH").expect("path var"); | ||
| assert_eq!(restored, original); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[serial] | ||
| fn prepend_dir_to_path_handles_empty_path() { | ||
| let original = std::env::var_os("PATH"); | ||
| { | ||
| let _lock = EnvLock::acquire(); | ||
| unsafe { std::env::set_var("PATH", "") }; | ||
| } | ||
| let env = SystemEnv::new(); | ||
| let dir = tempfile::tempdir().expect("temp dir"); | ||
| let guard = prepend_dir_to_path(&env, dir.path()); | ||
| let after = std::env::var_os("PATH").expect("path var"); | ||
| let paths = std::env::split_paths(&after) | ||
| .filter(|p| !p.as_os_str().is_empty()) | ||
| .collect::<Vec<_>>(); | ||
| assert_eq!(paths, vec![dir.path().to_path_buf()]); | ||
| drop(guard); | ||
| assert_eq!(std::env::var_os("PATH"), Some(std::ffi::OsString::new())); | ||
| { | ||
| let _lock = EnvLock::acquire(); | ||
| if let Some(path) = original { | ||
| unsafe { std::env::set_var("PATH", path) }; | ||
| } else { | ||
| unsafe { std::env::remove_var("PATH") }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[serial] | ||
| fn prepend_dir_to_path_handles_missing_path() { | ||
| let original = std::env::var_os("PATH"); | ||
| { | ||
| let _lock = EnvLock::acquire(); | ||
| unsafe { std::env::remove_var("PATH") }; | ||
| } | ||
| let env = SystemEnv::new(); | ||
| let dir = tempfile::tempdir().expect("temp dir"); | ||
| let guard = prepend_dir_to_path(&env, dir.path()); | ||
| let after = std::env::var_os("PATH").expect("PATH should exist after prepend"); | ||
| let paths: Vec<_> = std::env::split_paths(&after).collect(); | ||
| assert_eq!(paths, vec![dir.path().to_path_buf()]); | ||
| drop(guard); | ||
| assert!(std::env::var_os("PATH").is_none()); | ||
| { | ||
| let _lock = EnvLock::acquire(); | ||
| if let Some(path) = original { | ||
| unsafe { std::env::set_var("PATH", path) }; | ||
| } else { | ||
| unsafe { std::env::remove_var("PATH") }; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //! Tests for PATH restoration behaviour using mock environments. | ||
| //! | ||
| //! Verifies that `PathGuard` restores `PATH` without mutating the real | ||
| //! process environment. | ||
|
|
||
| #[path = "support/env_lock.rs"] | ||
| mod env_lock; | ||
| #[path = "support/path_guard.rs"] | ||
| mod path_guard; | ||
|
|
||
| use mockall::{Sequence, mock}; | ||
| use path_guard::{Env, PathGuard}; | ||
| use std::ffi::OsStr; | ||
|
|
||
| mock! { | ||
| pub Env {} | ||
| impl Env for Env { | ||
| unsafe fn set_var(&mut self, key: &str, val: &OsStr); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn restores_path_without_touching_real_env() { | ||
| let mut env = MockEnv::new(); | ||
| let mut seq = Sequence::new(); | ||
| env.expect_set_var() | ||
| .withf(|k, v| k == "PATH" && v == OsStr::new("/tmp")) | ||
| .times(1) | ||
| .in_sequence(&mut seq) | ||
| .return_const(()); | ||
| env.expect_set_var() | ||
| .withf(|k, v| k == "PATH" && v == OsStr::new("/orig")) | ||
| .times(1) | ||
| .in_sequence(&mut seq) | ||
| .return_const(()); | ||
| { | ||
| let mut guard = PathGuard::with_env("/orig".into(), env); | ||
| unsafe { | ||
| guard.env_mut().set_var("PATH", OsStr::new("/tmp")); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.