-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce NinjaEnvGuard and NINJA_ENV-based test isolation #243
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
+156
−54
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9b58b8
refactor(tests): use NINJA_ENV for ninja binary in tests instead of PATH
leynos 962d0e8
test(runner): add test to verify NINJA_ENV overrides PATH
leynos bfd54eb
fix(env): hold EnvLock while overriding NINJA_ENV to prevent races
leynos dadad61
test(runner): add tests for NINJA_ENV environment variable handling
leynos 907453c
refactor(tests): extract setup function for tests using NINJA_ENV
leynos 275c7fd
refactor(tests): pass ninja_path in fixtures to better manage NINJA_E…
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Test isolation with `NINJA_ENV` | ||
|
|
||
| Netsuke resolves the Ninja binary from the `NINJA_ENV` environment variable | ||
| before falling back to `ninja` on `PATH`. Tests should override `NINJA_ENV` | ||
| instead of mutating `PATH` so they can execute in parallel without stepping on | ||
| each other's environment. | ||
|
|
||
| ## Why prefer `NINJA_ENV` | ||
|
|
||
| - Mutating `PATH` is global and risks races when tests run concurrently. | ||
| - `override_ninja_env` scopes changes via an `EnvGuard`, restoring the previous | ||
| value even if the test fails. | ||
| - Keeping `PATH` untouched avoids coupling to the developer's shell setup. | ||
| - `override_ninja_env` also holds a process-wide lock for the guard's lifetime, | ||
| preventing parallel tests from interleaving `NINJA_ENV` mutations. | ||
|
|
||
| ## Fixture pattern | ||
|
|
||
| Use a fixture to create a fake Ninja executable and point `NINJA_ENV` at it. | ||
| The fixture keeps the temporary directory alive for the test duration and | ||
| automatically restores the environment on drop. | ||
|
|
||
| ```rust | ||
| use rstest::fixture; | ||
| use test_support::env::{NinjaEnvGuard, SystemEnv, override_ninja_env}; | ||
| use test_support::fake_ninja; | ||
|
|
||
| #[fixture] | ||
| fn ninja_in_env() -> anyhow::Result<(tempfile::TempDir, NinjaEnvGuard)> { | ||
| let (ninja_dir, ninja_path) = fake_ninja(0)?; | ||
| let env = SystemEnv::new(); | ||
| let guard = override_ninja_env(&env, ninja_path.as_path()); | ||
| Ok((ninja_dir, guard)) | ||
| } | ||
| ``` | ||
|
|
||
| Inject the fixture into tests that need a controlled Ninja binary: | ||
|
|
||
| ```rust | ||
| #[rstest] | ||
| fn run_build_uses_fake_ninja( | ||
| (_, _guard): (tempfile::TempDir, NinjaEnvGuard), | ||
| ) { | ||
| // run the command-line interface (CLI) here; the guard restores NINJA_ENV | ||
| // on drop | ||
| } | ||
| ``` | ||
|
|
||
| ## Dos and don'ts | ||
|
|
||
| - Do keep the guard alive until after the CLI invocation so `NINJA_ENV` stays | ||
| set. | ||
| - Do avoid explicit `drop` calls for `PathBuf` values; they do not own external | ||
| resources. | ||
| - Don't add `#[serial]` purely to protect `PATH` mutations; prefer the fixture | ||
| above to keep tests parallel-friendly. | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| ## Precedence over `PATH` | ||
|
|
||
| `NINJA_ENV` should override any `ninja` found on `PATH`. When asserting this in | ||
| tests, place a failing fake Ninja on `PATH` with `prepend_dir_to_path` and set | ||
| `NINJA_ENV` to a working fake Ninja via `override_ninja_env`. The test should | ||
| pass only if `NINJA_ENV` is respected. | ||
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.
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.