Skip to content

Commit ddad99a

Browse files
committed
fix(golden): make update_goldens.py / check_semver_bump.py work as direct scripts
Both scripts have docstrings and corpus-test failure hints that tell users to invoke them as `python tests/golden/<script>.py ...` from the repo root. But Python does not auto-add the repo root to sys.path for direct script invocation, so the absolute `from tests.golden...` imports fail with ModuleNotFoundError. Prepend the repo root to sys.path when each script is run as __main__, gated on `__package__ is None` so the module form continues to work.
1 parent f5de1ea commit ddad99a

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

tests/golden/check_semver_bump.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
from __future__ import annotations
99

10+
# Allow direct script invocation (`python tests/golden/check_semver_bump.py`)
11+
# in addition to module form (`python -m tests.golden.check_semver_bump`).
12+
# When run as a script, Python does not auto-add the repo root to sys.path,
13+
# so absolute imports rooted at `tests.golden...` would fail. This script
14+
# happens to have no such imports today, but keep the guard symmetrical with
15+
# update_goldens.py so future imports do not silently regress the script form.
16+
if __name__ == "__main__" and __package__ is None:
17+
import sys
18+
from pathlib import Path
19+
20+
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
21+
1022
import argparse
1123
import re
1224
import subprocess

tests/golden/update_goldens.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010

1111
from __future__ import annotations
1212

13+
# Allow direct script invocation (`python tests/golden/update_goldens.py`)
14+
# in addition to module form (`python -m tests.golden.update_goldens`).
15+
# When run as a script, Python does not auto-add the repo root to sys.path,
16+
# so the absolute `from tests.golden...` imports below would fail.
17+
if __name__ == "__main__" and __package__ is None:
18+
import sys
19+
from pathlib import Path
20+
21+
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
22+
1323
import argparse
1424
import difflib
1525
import fnmatch

0 commit comments

Comments
 (0)