The script hard-coded PACKAGE='diet_guard', so other guard repos could not run it (build-background-service R3). PACKAGE is now a required positional (no silent default: that is the fail-open shape the gate guards against); --tests-root defaults to PACKAGE/tests. Targets match PACKAGE or PACKAGE.*, so pkg_a no longer claims pkg_ab. Adoption snippet in docs/DOCS-patch-targets.md. A shared ~/src/utils/patch_targets/ package is a separate follow-up, once a second repo adopts the script. Todo: 6d0ec573 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1.8 KiB
Patch-target gate (scripts/check_patch_targets.py)
mock.patch("pkg.module.NAME", ...) fails open. When a refactor moves NAME, the old path still
imports, the patch redirects nothing, and the test stays green while it writes to real user state.
The script reads every test file, finds each literal patch("<package>....") target, and exits 1
when a target no longer resolves.
Usage
Run it from the repo root:
python3 scripts/check_patch_targets.py PACKAGE [--tests-root DIR]
PACKAGEis required. The script checks only targets inside it (pkgorpkg.*;pkg_otheris notpkg). It has no default, because a silent default would check the wrong package and pass, which is the fail-open shape the gate exists to catch.--tests-rootdefaults toPACKAGE/tests.- The package must be a flat package at the repo root (
./PACKAGE/__init__.py). There is nosrc/layout support yet. - Exit codes:
0clean,1stale targets (each printed asfile:line: patch target does not resolve: ...), a package that will not import or a missing tests root,2usage error.
Adopting it in another guard repo
Copy scripts/check_patch_targets.py into the repo's scripts/, then add this hook to
.pre-commit-config.yaml under a repo: local block:
- id: patch-targets
name: test patch targets resolve
entry: python3 scripts/check_patch_targets.py <your_package>
language: system
pass_filenames: false
files: ^<your_package>/
The script puts the repo root on sys.path itself, so the hook works on a CI runner that never
installed the package.
Follow-up: a shared copy
Each adopter copies the script today. Moving it to a shared ~/src/utils/patch_targets/ package
is a separate follow-up, done once a second repo actually adopts it.