Skip to content

Commit 99af3ef

Browse files
test: avoid changing os.environ (#562)
1 parent 33fc49d commit 99af3ef

4 files changed

Lines changed: 40 additions & 47 deletions

File tree

src/extensions/score_source_code_linker/tests/test_repo_source_link_integration.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@
4545
"""
4646

4747

48-
@pytest.fixture(scope="session")
48+
@pytest.fixture(scope="module")
4949
def sphinx_base_dir(tmp_path_factory: TempPathFactory) -> Path:
5050
return tmp_path_factory.mktemp("test_module_links_repo")
5151

5252

53-
@pytest.fixture(scope="session")
53+
@pytest.fixture(scope="module")
5454
def git_repo_setup(sphinx_base_dir: Path) -> Path:
5555
"""Creating git repo, to make testing possible"""
5656
repo_path = sphinx_base_dir
@@ -66,11 +66,10 @@ def git_repo_setup(sphinx_base_dir: Path) -> Path:
6666
cwd=repo_path,
6767
check=True,
6868
)
69-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(repo_path)
7069
return repo_path
7170

7271

73-
@pytest.fixture(scope="session")
72+
@pytest.fixture(scope="module")
7473
def create_demo_files(sphinx_base_dir: Path, git_repo_setup: Path):
7574
repo_path = sphinx_base_dir
7675

@@ -249,11 +248,12 @@ def test_repo_grouped_cache_generated(
249248
sphinx_base_dir: Path,
250249
git_repo_setup: Path,
251250
create_demo_files: None,
251+
monkeypatch: pytest.MonkeyPatch,
252252
):
253253
"""Happy path: Repo grouped cache file is generated after Sphinx build"""
254+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
254255
app = sphinx_app_setup()
255256
try:
256-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
257257
app.build()
258258

259259
repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
@@ -279,11 +279,12 @@ def test_repo_grouping_preserves_metadata(
279279
sphinx_base_dir: Path,
280280
git_repo_setup: Path,
281281
create_demo_files: None,
282+
monkeypatch: pytest.MonkeyPatch,
282283
):
283284
"""Happy path: Repo metadata (name, hash, url) is preserved in cache"""
285+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
284286
app = sphinx_app_setup()
285287
try:
286-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
287288
app.build()
288289

289290
repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
@@ -306,11 +307,12 @@ def test_repo_grouping_multiple_needs_per_repo(
306307
sphinx_base_dir: Path,
307308
git_repo_setup: Path,
308309
create_demo_files: None,
310+
monkeypatch: pytest.MonkeyPatch,
309311
):
310312
"""Happy path: Multiple needs from same repo are grouped together"""
313+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
311314
app = sphinx_app_setup()
312315
try:
313-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
314316
app.build()
315317

316318
repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
@@ -338,14 +340,15 @@ def test_repo_cache_json_format(
338340
sphinx_base_dir: Path,
339341
git_repo_setup: Path,
340342
create_demo_files: None,
343+
monkeypatch: pytest.MonkeyPatch,
341344
):
342345
"""
343346
Repo cache JSON has correct
344347
structure and excludes metadata from links
345348
"""
349+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
346350
app = sphinx_app_setup()
347351
try:
348-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
349352
app.build()
350353

351354
repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
@@ -389,11 +392,12 @@ def test_repo_cache_rebuilds_when_missing(
389392
sphinx_base_dir: Path,
390393
git_repo_setup: Path,
391394
create_demo_files: None,
395+
monkeypatch: pytest.MonkeyPatch,
392396
):
393397
"""Edge case: Repo cache is regenerated if deleted"""
398+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
394399
app = sphinx_app_setup()
395400
try:
396-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
397401
app.build()
398402

399403
repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"
@@ -423,11 +427,12 @@ def test_repo_grouping_with_golden_file(
423427
sphinx_base_dir: Path,
424428
git_repo_setup: Path,
425429
create_demo_files: None,
430+
monkeypatch: pytest.MonkeyPatch,
426431
):
427432
"""Happy path: Generated repo cache matches expected golden file"""
433+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
428434
app = sphinx_app_setup()
429435
try:
430-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
431436
app.build()
432437

433438
repo_cache = app.outdir / "score_repo_grouped_scl_cache.json"

src/extensions/score_source_code_linker/tests/test_source_code_link_integration.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
from src.helper_lib import find_ws_root
4242

4343

44-
@pytest.fixture(scope="session")
44+
@pytest.fixture(scope="module")
4545
def sphinx_base_dir(tmp_path_factory: TempPathFactory) -> Path:
4646
return tmp_path_factory.mktemp("test_git_repo")
4747

4848

49-
@pytest.fixture(scope="session")
49+
@pytest.fixture(scope="module")
5050
def git_repo_setup(sphinx_base_dir: Path) -> Path:
5151
"""Creating git repo, to make testing possible"""
5252

@@ -64,11 +64,10 @@ def git_repo_setup(sphinx_base_dir: Path) -> Path:
6464
cwd=repo_path,
6565
check=True,
6666
)
67-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(repo_path)
6867
return repo_path
6968

7069

71-
@pytest.fixture(scope="session")
70+
@pytest.fixture(scope="module")
7271
def create_demo_files(sphinx_base_dir: Path, git_repo_setup: Path):
7372
repo_path = sphinx_base_dir
7473

@@ -511,11 +510,12 @@ def test_source_link_integration_ok(
511510
sphinx_base_dir: Path,
512511
git_repo_setup: Path,
513512
create_demo_files: None,
513+
monkeypatch: pytest.MonkeyPatch,
514514
):
515515
"""This is a test description"""
516+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
516517
app = sphinx_app_setup()
517518
try:
518-
os.environ["BUILD_WORKSPACE_DIRECTORY"] = str(sphinx_base_dir)
519519
app.build()
520520
ws_root = find_ws_root()
521521
assert ws_root is not None
@@ -574,8 +574,10 @@ def test_source_link_integration_non_existent_id(
574574
sphinx_base_dir: Path,
575575
git_repo_setup: Path,
576576
create_demo_files: None,
577+
monkeypatch: pytest.MonkeyPatch,
577578
):
578579
"""Asserting warning if need not found"""
580+
monkeypatch.setenv("BUILD_WORKSPACE_DIRECTORY", str(sphinx_base_dir))
579581
app = sphinx_app_setup()
580582
try:
581583
app.build()

src/extensions/score_source_code_linker/tests/test_xml_parser.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"""
2323

2424
import json
25-
import os
2625
import xml.etree.ElementTree as ET
2726
from collections.abc import Callable
2827
from pathlib import Path
@@ -467,41 +466,27 @@ def test_get_metadata_from_test_path_local():
467466
assert md["url"] == ""
468467

469468

470-
def test_get_metadata_from_test_path_combo_with_hash(tmp_path: Path):
469+
def test_get_metadata_from_test_path_combo_with_hash(
470+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
471+
):
471472
"""Combo builds with 'hash' in known_good.json populate metadata correctly."""
472473
json_file = tmp_path / "known_good.json"
473474
json_file.write_text(json.dumps(_KNOWN_GOOD_WITH_HASH))
474-
475-
old = os.environ.get("KNOWN_GOOD_JSON")
476-
try:
477-
os.environ["KNOWN_GOOD_JSON"] = str(json_file)
478-
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
479-
finally:
480-
if old is None:
481-
os.environ.pop("KNOWN_GOOD_JSON", None)
482-
else:
483-
os.environ["KNOWN_GOOD_JSON"] = old
484-
475+
monkeypatch.setenv("KNOWN_GOOD_JSON", str(json_file))
476+
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
485477
assert md["repo_name"] == "score_docs_as_code"
486478
assert md["hash"] == "abc123hashvalue"
487479
assert md["url"] == "https://github.com/eclipse-score/docs-as-code"
488480

489481

490-
def test_get_metadata_from_test_path_combo_with_version(tmp_path: Path):
482+
def test_get_metadata_from_test_path_combo_with_version(
483+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
484+
):
491485
"""Combo builds with 'version' in known_good.json populate metadata correctly."""
492486
json_file = tmp_path / "known_good.json"
493487
json_file.write_text(json.dumps(_KNOWN_GOOD_WITH_VERSION))
494-
495-
old = os.environ.get("KNOWN_GOOD_JSON")
496-
try:
497-
os.environ["KNOWN_GOOD_JSON"] = str(json_file)
498-
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
499-
finally:
500-
if old is None:
501-
os.environ.pop("KNOWN_GOOD_JSON", None)
502-
else:
503-
os.environ["KNOWN_GOOD_JSON"] = old
504-
488+
monkeypatch.setenv("KNOWN_GOOD_JSON", str(json_file))
489+
md = xml_parser.get_metadata_from_test_path(_COMBO_TEST_PATH)
505490
assert md["repo_name"] == "score_docs_as_code"
506491
assert md["hash"] == "v2.1.0"
507492
assert md["url"] == "https://github.com/eclipse-score/docs-as-code"

src/helper_lib/test_helper_lib.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,26 @@ def test_get_current_git_hash_invalid_repo(temp_dir: Path):
263263
get_current_git_hash(temp_dir)
264264

265265

266-
def test_runfiles_dir_found(temp_dir: Path):
266+
def test_runfiles_dir_found(temp_dir: Path, monkeypatch: pytest.MonkeyPatch):
267267
"""Test Runfiles dir found when provided and it's actually there"""
268268
runfiles_dir = temp_dir / "runfiles_here"
269269
runfiles_dir.mkdir(parents=True)
270-
os.environ["RUNFILES_DIR"] = str(runfiles_dir)
271-
os.chdir(runfiles_dir)
270+
monkeypatch.setenv("RUNFILES_DIR", str(runfiles_dir))
271+
# No chdir needed: get_runfiles_dir() reads the env var directly and never consults CWD.
272272
result = get_runfiles_dir()
273273
assert Path(result) == runfiles_dir
274-
os.environ.pop("RUNFILES_DIR", None)
275274

276275

277-
def test_runfiles_dir_missing_triggers_exit(temp_dir: Path):
276+
def test_runfiles_dir_missing_triggers_exit(
277+
temp_dir: Path, monkeypatch: pytest.MonkeyPatch
278+
):
278279
"""Testing if the runfiles exit via sys.exit if runfiles are set but don't exist"""
279280
runfiles_dir = temp_dir / "does_not_exist"
280-
os.environ["RUNFILES_DIR"] = str(runfiles_dir)
281+
monkeypatch.setenv("RUNFILES_DIR", str(runfiles_dir))
281282
with pytest.raises(SystemExit) as e:
283+
# No chdir needed: get_runfiles_dir() reads the env var directly and never consults CWD.
282284
get_runfiles_dir()
283285
assert "Could not find runfiles_dir" in str(e.value)
284-
os.environ.pop("RUNFILES_DIR", None)
285286

286287

287288
def test_git_root_search_success(git_repo: Path, monkeypatch: pytest.MonkeyPatch):

0 commit comments

Comments
 (0)