-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor PathGuard for injectable env and add mock test #105
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
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.
| 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); | ||
| } | ||
| } | ||
|
Comment on lines
+15
to
+20
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Prefer rstest fixtures for reusable mocks per repository testing guidance Adopt According to the team’s guidance (DeepWiki), tests should use use rstest::fixture;
#[fixture]
fn mock_env() -> MockEnv {
let mut env = MockEnv::new();
// set common expectations here if needed
env
}Would you like me to refactor the tests to use 🤖 Prompt for AI Agents |
||
|
|
||
| #[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")); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |||||
| //! This module provides helpers for creating fake executables along with | ||||||
| //! logging utilities used in behavioural tests. | ||||||
|
|
||||||
| #![allow(unexpected_cfgs, reason = "test utilities use custom cfg")] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: violates custom rule forbidding
Suggested change
Context Used: Rule - |
||||||
|
|
||||||
| pub mod env_lock; | ||||||
| pub mod path_guard; | ||||||
|
|
||||||
|
|
@@ -17,6 +19,7 @@ use tempfile::TempDir; | |||||
| /// Create a fake Ninja executable that exits with `exit_code`. | ||||||
| /// | ||||||
| /// Returns the temporary directory and the path to the executable. | ||||||
| #[cfg(test)] | ||||||
| pub fn fake_ninja(exit_code: i32) -> (TempDir, PathBuf) { | ||||||
| let dir = TempDir::new().expect("temp dir"); | ||||||
| let path = dir.path().join("ninja"); | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.