Introduce saved_env context manager for restoring environment in tests#5015
Merged
boegel merged 2 commits intoeasybuilders:developfrom Nov 21, 2025
Merged
Introduce saved_env context manager for restoring environment in tests#5015boegel merged 2 commits intoeasybuilders:developfrom
saved_env context manager for restoring environment in tests#5015boegel merged 2 commits intoeasybuilders:developfrom
Conversation
When no changes are to be made we can exit early which especially avoids the `module list` command whose output is unused in this case causing unecessary overhead.
Many tests modify the environment in some steps and need to restore it afterwards. To avoid missing this the contextmanager does that automatically.
33fe61a to
5476d7f
Compare
1 task
636cc1b to
5476d7f
Compare
saved_env context manager for restoring environment in tests
boegel
reviewed
Nov 19, 2025
| assert_cuda_report(missing_cc=0, additional_cc=0, missing_ptx=0, log=outtxt, stdout=stdout, num_checked=0) | ||
|
|
||
| # Restore original environment | ||
| modify_env(os.environ, start_env, verbose=False) |
Member
There was a problem hiding this comment.
@Flamefire You're not introducing the use of the saved_env context manager in this test, so won't that result in leaking changes made to their environment from this test to other tests?
See the use of setvar in this test...
Contributor
Author
There was a problem hiding this comment.
The teardown of EnhancedTestcase resets the environment for every test:
# restore original environment
modify_env(os.environ, self.orig_environ, verbose=False)
So we don't need to do this at the end of the test.
saved_env is only required if we need to restore the environment within a test.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Many tests modify the environment in some steps and need to restore it afterwards.
To avoid missing this the contextmanager does that automatically which avoids repeating the logic in various tests
I recently ran into this issue where a test failed with a misleading error.
Broader question: Should
EasyBlock.run_all_stepsrestore the environment? When it exits it will have e.g. toolchain modules still loaded.We currently save the environment in
build_and_install_softwareand restore it inbuild_and_install_onebefore processing/callingrun_all_steps, although that additionally catches changes done by hooks. However the changes done to the environment when loading the hooks are only in effect for the first installation as then the hooks are cached. So I think we can ignore that case.