Repository navigation
refactor(tools): wrap check_workflow_quoting and validate in testable functions - #73
Conversation
… functions Extract offending_lines() and check_provenance() functions alongside main() entry points to allow unit testing with synthetic fixtures without executing at module import time. CLI behavior and exit codes remain unchanged. Closes #64
hanthor
left a comment
There was a problem hiding this comment.
Small, behaviour-preserving, and it unblocks four other open PRs. This looks ready.
Gates are identical to the unmodified-main baseline, which is what a pure refactor should produce. Merged into main (54cc8d1):
PR#73 fc=0 val=0 pytest=0 | validated 193 source RPMs | 72 passed in 0.68s
PR#73 quoting=0 runtime_contract=0 unittest=0 | validated runtime contract: 72 packages
Baseline: 193 SRPMs, 72 passed, 72 contract packages. No delta anywhere — the CLI paths behave exactly as before.
Both scripts are live CI gates, so the seams matter. I confirmed how they are invoked:
tools/validate.py— the entirevalidatejob in.github/workflows/validate.yml(- run: python3 tools/validate.py), plusjust validatevia thecheckrecipe.tools/check_workflow_quoting.py— first line of theValidate factory policy and testsstep inrebuild-rpms.yml'spreparejob, ahead ofruntime_contract.pyand the unittest discovery.
Both currently run as module-level scripts, so any test has to shell out or fake sys.argv and the real repo tree. raise SystemExit(main()) under a __main__ guard with main(root: Path = Path(".")) / main(workflows_dir: Path | None = None) is the minimal change that fixes that, and it keeps exit codes identical — which is the property that matters, since rebuild-rpms.yml reads them.
It composes with the test PRs. #61 and #71 both add tests/test_validate.py, and #59/#61 both add tests/test_check_workflow_quoting.py. Those suites drive the scripts through the CLI, so they neither need nor break this refactor. Confirmed:
$ git merge pr/73 && git merge pr/61 && python3 -m pytest tests -q
97 passed in 1.54s
No conflict. Worth landing this first regardless, so the next round of tests can call offending_lines() and check_provenance() directly instead of round-tripping through a subprocess and a synthetic factory tree. That is the whole point of issue #64 and this PR delivers it.
Two minor notes, neither blocking:
-
check_workflow_quoting.pycurrently reportschecked 10 workflows: no build script contains a quote that would close it— that count is a useful assertion for whichever test PR lands, and it changed recently (commit54cc8d1widened the scan fromrebuild-rpms.ymlto every workflow). Nothing to do here, just worth knowing the behaviour being wrapped is two commits old. -
offending_lines()returninglist[tuple[str, int, str]]is fine, but a small named tuple or dataclass would make the eventual test assertions read better thanresult[0][1] == 42. Entirely optional.
No CI has run on this PR (get_check_runs → total_count: 0), but given the change is two __main__ guards and two function extractions with no behaviour delta, and both gates pass locally on the merged tree, I do not think that is a blocker here the way it is for the larger PRs.
Generated by Claude Code
Summary
Wrap module-level scripts into callable functions with testable seams, keeping existing CLI behavior and exit codes identical:
tools/check_workflow_quoting.py:def offending_lines(workflows: Iterable[Path]) -> list[tuple[str, int, str]]def main(workflows_dir: Path | None = None) -> intif __name__ == "__main__": raise SystemExit(main())tools/validate.py:def check_provenance(path: Path, data: dict) -> Nonedef main(root: Path = Path(".")) -> intif __name__ == "__main__": raise SystemExit(main())This provides the necessary seams so unit tests can pass synthetic workflow files and
.hummingbird-upstream.jsonfixtures without executing the scripts against the real repo at module import time.Closes #64