Skip to content

test: avoid changing os.environ#562

Merged
AlexanderLanin merged 2 commits into
eclipse-score:mainfrom
etas-contrib:environ
May 28, 2026
Merged

test: avoid changing os.environ#562
AlexanderLanin merged 2 commits into
eclipse-score:mainfrom
etas-contrib:environ

Conversation

@AlexanderLanin
Copy link
Copy Markdown
Member

Why

Tests that directly assign to os.environ and clean up manually are fragile: if a test raises before the cleanup code runs, the environment variable leaks into every subsequent test in the same process. Because several of these fixtures were scoped to session, a leaked BUILD_WORKSPACE_DIRECTORY or KNOWN_GOOD_JSON could silently influence unrelated tests, making failures order-dependent and hard to reproduce. The manual try/finally pattern for saving and restoring the old value is also noisy boilerplate that obscures the test's actual intent.

A subtler problem with session-scoped fixtures is that monkeypatch only supports function and module scope — you can't use it with session. The fix must therefore reduce the fixture scope to module before switching to monkeypatch.

What

Replace all direct os.environ mutations in tests with monkeypatch.setenv. This is pytest's standard mechanism for environment isolation: it automatically reverts every change after the test (or module) finishes, even if the test raises. No manual save/restore or os.environ.pop calls are needed.

Fixtures in test_repo_source_link_integration.py and test_source_code_link_integration.py that were previously session-scoped are downgraded to module-scoped so that monkeypatch can be used safely. The git_repo_setup fixtures no longer set BUILD_WORKSPACE_DIRECTORY themselves; each test that needs it sets it via monkeypatch.setenv instead, keeping environment side-effects visible at the call site.

In test_helper_lib.py, the os.chdir call in test_runfiles_dir_found is also removed — it was unnecessary because get_runfiles_dir() reads the env var directly and never consults the working directory.

Changes

  • test_repo_source_link_integration.py — downgrade sphinx_base_dir, git_repo_setup, create_demo_files from session to module scope; remove os.environ assignment from git_repo_setup; replace six direct os.environ["BUILD_WORKSPACE_DIRECTORY"] = … assignments with monkeypatch.setenv
  • test_source_code_link_integration.py — same scope downgrade; remove os.environ assignment from git_repo_setup; replace two direct assignments with monkeypatch.setenv
  • test_xml_parser.py — replace two manual try/finally save-restore patterns around KNOWN_GOOD_JSON with monkeypatch.setenv
  • test_helper_lib.py — replace manual os.environ mutation and os.environ.pop cleanup with monkeypatch.setenv; remove the unnecessary os.chdir call

@github-actions
Copy link
Copy Markdown

The created documentation from the pull request is available at: docu-html

@github-actions
Copy link
Copy Markdown

License Check Results

🚀 The license check job ran with the Bazel command:

bazel run --lockfile_mode=error //src:license-check

Status: ⚠️ Needs Review

Click to expand output
[License Check Output]
Extracting Bazel installation...
Starting local Bazel server (8.6.0) and connecting to it...
INFO: Invocation ID: 00e45415-885f-4826-b93f-d8e835c268dc
Computing main repo mapping: 
Loading: 
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
    currently loading: src
WARNING: Target pattern parsing failed.
ERROR: Skipping '//src:license-check': no such target '//src:license-check': target 'license-check' not declared in package 'src' defined by /home/runner/work/docs-as-code/docs-as-code/src/BUILD
ERROR: no such target '//src:license-check': target 'license-check' not declared in package 'src' defined by /home/runner/work/docs-as-code/docs-as-code/src/BUILD
INFO: Elapsed time: 5.392s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

Copy link
Copy Markdown
Contributor

@MaximilianSoerenPollak MaximilianSoerenPollak left a comment

Choose a reason for hiding this comment

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

Ye that looks good to me.
Makes sense ot use the monkeypatch for the thing it actually was meant to be used.

@AlexanderLanin AlexanderLanin merged commit 99af3ef into eclipse-score:main May 28, 2026
12 of 13 checks passed
@AlexanderLanin AlexanderLanin deleted the environ branch May 28, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants