Skip to content

Introduce saved_env context manager for restoring environment in tests#5015

Merged
boegel merged 2 commits intoeasybuilders:developfrom
Flamefire:restore-test-env
Nov 21, 2025
Merged

Introduce saved_env context manager for restoring environment in tests#5015
boegel merged 2 commits intoeasybuilders:developfrom
Flamefire:restore-test-env

Conversation

@Flamefire
Copy link
Copy Markdown
Contributor

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_steps restore the environment? When it exits it will have e.g. toolchain modules still loaded.
We currently save the environment in build_and_install_software and restore it in build_and_install_one before processing/calling run_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.

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.
@Flamefire Flamefire force-pushed the restore-test-env branch 2 times, most recently from 636cc1b to 5476d7f Compare October 6, 2025 14:15
@boegel boegel changed the title Introduce context manager for restoring environment in tests Introduce saved_env context manager for restoring environment in tests Oct 8, 2025
@boegel boegel added this to the next release (5.2.0?) milestone Oct 8, 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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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...

Copy link
Copy Markdown
Contributor Author

@Flamefire Flamefire Nov 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks for clarifying!

Copy link
Copy Markdown
Member

@boegel boegel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@boegel boegel merged commit 825f842 into easybuilders:develop Nov 21, 2025
72 of 73 checks passed
@Flamefire Flamefire deleted the restore-test-env branch November 21, 2025 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants