-
Notifications
You must be signed in to change notification settings - Fork 0
Inject mock env for Ninja tests #101
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd606f3
Refactor test ninja env handling
leynos 2c45397
Refactor test ninja setup with path guard
leynos 1ff8261
refactor path guard linting
leynos a3eec3e
Guard dead code expectations in test helpers
leynos 29284b1
Factor shared test env and ninja helpers
leynos 55e4f0a
Move expect after doc comments
leynos dcf9e1e
Add test EnvLock
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| //! Helpers for validating build file paths via fake Ninja binaries. | ||
|
|
||
| use std::fs::{self, File}; | ||
| use std::io::Write; | ||
| use std::path::PathBuf; | ||
| use tempfile::TempDir; | ||
|
|
||
| /// Create a fake Ninja that validates the build file path provided via `-f`. | ||
| /// | ||
| /// The script exits with status `1` if the file is missing or not a regular | ||
| /// file, otherwise `0`. | ||
| pub fn fake_ninja_check_build_file() -> (TempDir, PathBuf) { | ||
| let dir = TempDir::new().expect("temp dir"); | ||
| let path = dir.path().join("ninja"); | ||
| let mut file = File::create(&path).expect("script"); | ||
| writeln!( | ||
| file, | ||
| concat!( | ||
| "#!/bin/sh\n", | ||
| "if [ \"$1\" = \"-f\" ] && [ ! -f \"$2\" ]; then\n", | ||
| " echo 'missing build file: $2' >&2\n", | ||
| " exit 1\n", | ||
| "fi\n", | ||
| "exit 0" | ||
| ), | ||
| ) | ||
| .expect("write script"); | ||
| #[cfg(unix)] | ||
| { | ||
| use std::os::unix::fs::PermissionsExt; | ||
| let mut perms = fs::metadata(&path).expect("meta").permissions(); | ||
| perms.set_mode(0o755); | ||
| fs::set_permissions(&path, perms).expect("perms"); | ||
| } | ||
| (dir, 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,40 @@ | ||
| //! Helpers for environment manipulation in process tests. | ||
| //! | ||
| //! Provides fixtures and utilities for managing `PATH` and writing minimal | ||
| //! manifests. | ||
|
|
||
| use mockable::MockEnv; | ||
| use rstest::fixture; | ||
| use std::io::{self, Write}; | ||
|
|
||
| /// Fixture: capture the original `PATH` via a mocked environment. | ||
| /// | ||
| /// Returns a `MockEnv` that yields the current `PATH` when queried. Tests can | ||
| /// modify the real environment while the mock continues to expose the initial | ||
| /// value. | ||
| #[fixture] | ||
| pub fn mocked_path_env() -> MockEnv { | ||
| let original = std::env::var("PATH").unwrap_or_default(); | ||
| let mut env = MockEnv::new(); | ||
| env.expect_raw() | ||
| .withf(|k| k == "PATH") | ||
| .returning(move |_| Ok(original.clone())); | ||
| env | ||
| } | ||
|
|
||
| /// Write a minimal manifest to `file`. | ||
| /// | ||
| /// The manifest declares a single `hello` target that prints a greeting. | ||
| pub fn write_manifest(file: &mut impl Write) -> io::Result<()> { | ||
| writeln!( | ||
| file, | ||
| concat!( | ||
| "netsuke_version: \"1.0.0\"\n", | ||
| "targets:\n", | ||
| " - name: hello\n", | ||
| " recipe:\n", | ||
| " kind: command\n", | ||
| " command: \"echo hi\"\n" | ||
| ), | ||
| ) | ||
| } |
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,22 @@ | ||
| //! Serialise environment mutations across tests. | ||
| //! | ||
| //! The `EnvLock` guard ensures that changes to global state like `PATH` are | ||
| //! synchronised, preventing interference between concurrently running tests. | ||
|
|
||
| use std::sync::{Mutex, MutexGuard}; | ||
|
|
||
| #[allow(dead_code, reason = "only some tests mutate PATH")] | ||
| /// Global mutex protecting environment changes. | ||
| static ENV_LOCK: Mutex<()> = Mutex::new(()); | ||
|
|
||
| #[allow(dead_code, reason = "only some tests mutate PATH")] | ||
| /// RAII guard that holds the global environment lock. | ||
| pub struct EnvLock(MutexGuard<'static, ()>); | ||
|
|
||
| impl EnvLock { | ||
| #[allow(dead_code, reason = "only some tests mutate PATH")] | ||
| /// Acquire the global lock serialising environment mutations. | ||
| pub fn acquire() -> Self { | ||
| Self(ENV_LOCK.lock().expect("env lock")) | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Using
#[allow(dead_code)]instead of#[expect(dead_code)]contradicts the custom rule that forbids#[allow]in favor of#[expect].Context Used: Rule -
#[allow]is forbidden. (link)