mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-28 15:09:17 +01:00
Compare commits
No commits in common. "db58478669f8bf02a84a2a1b00314251ecb8164b" and "5435a7ff9704568885d77b437123eb7c19d520ab" have entirely different histories.
db58478669
...
5435a7ff97
@ -42,7 +42,7 @@ Rules (all machine-checked):
|
|||||||
python3 ~/src/utils/repo_contract/check.py --repo <path> # 0 pass / 1 violations / 2 unreadable
|
python3 ~/src/utils/repo_contract/check.py --repo <path> # 0 pass / 1 violations / 2 unreadable
|
||||||
python3 ~/src/utils/repo_contract/bootstrap.py --repo <path> # print proposal
|
python3 ~/src/utils/repo_contract/bootstrap.py --repo <path> # print proposal
|
||||||
python3 ~/src/utils/repo_contract/bootstrap.py --repo <path> --write
|
python3 ~/src/utils/repo_contract/bootstrap.py --repo <path> --write
|
||||||
python3 ~/src/utils/repo_contract/sweep.py # ~/data/claude-scratch/repo-contract/SWEEP.md; --dry-run prints it instead, --out PATH writes elsewhere
|
python3 ~/src/utils/repo_contract/sweep.py # ~/data/claude-scratch/repo-contract/SWEEP.md
|
||||||
```
|
```
|
||||||
|
|
||||||
Bootstrap never overwrites: it appends the section (or inserts missing lines at
|
Bootstrap never overwrites: it appends the section (or inserts missing lines at
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import argparse
|
|
||||||
import sys
|
import sys
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
@ -83,46 +82,19 @@ def render(results: list[Result]) -> str:
|
|||||||
return "\n".join(out) + "\n"
|
return "\n".join(out) + "\n"
|
||||||
|
|
||||||
|
|
||||||
def parse_args(argv: list[str] | None, default_out: Path) -> argparse.Namespace:
|
|
||||||
"""Parse argv; argparse exits 0 on --help and 2 on bad args before any walk."""
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
prog="sweep.py",
|
|
||||||
description=(
|
|
||||||
"Run the repo contract check over every git repo directly under ~/src "
|
|
||||||
"and ~ and write a markdown report (SWEEP.md)."
|
|
||||||
),
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--dry-run",
|
|
||||||
action="store_true",
|
|
||||||
help="walk and check, print the report to stdout, write nothing",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--out",
|
|
||||||
type=Path,
|
|
||||||
default=default_out,
|
|
||||||
help=f"report path (default: {default_out})",
|
|
||||||
)
|
|
||||||
return parser.parse_args(argv)
|
|
||||||
|
|
||||||
|
|
||||||
def main(
|
def main(
|
||||||
argv: list[str] | None = None, roots: list[Path] | None = None, out: Path = OUT
|
argv: list[str] | None = None, roots: list[Path] | None = None, out: Path = OUT
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Sweep, write (or with --dry-run print) the report, print the summary. Exit 0."""
|
"""Sweep, write the report, print the summary line. Always exit 0."""
|
||||||
args = parse_args(argv, out)
|
del argv
|
||||||
home = Path.home()
|
home = Path.home()
|
||||||
results = [run_one(r) for r in find_repos(roots or [home / "src", home])]
|
results = [run_one(r) for r in find_repos(roots or [home / "src", home])]
|
||||||
report = render(results)
|
out.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
out.write_text(render(results), encoding="utf-8")
|
||||||
passed = sum(r.passed for r in results)
|
passed = sum(r.passed for r in results)
|
||||||
summary = f"repo-contract sweep: {len(results)} repos, {passed} pass, {len(results) - passed} fail"
|
print(
|
||||||
if args.dry_run:
|
f"repo-contract sweep: {len(results)} repos, {passed} pass, {len(results) - passed} fail -> {out}"
|
||||||
sys.stdout.write(report)
|
)
|
||||||
print(f"{summary} (dry run, nothing written)", file=sys.stderr)
|
|
||||||
return 0
|
|
||||||
args.out.parent.mkdir(parents=True, exist_ok=True)
|
|
||||||
args.out.write_text(report, encoding="utf-8")
|
|
||||||
print(f"{summary} -> {args.out}")
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -40,81 +40,6 @@ def test_run_one_and_render(tmp_path: Path) -> None:
|
|||||||
def test_main_writes_report(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
def test_main_writes_report(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None:
|
||||||
make_repo(tmp_path / "r")
|
make_repo(tmp_path / "r")
|
||||||
out = tmp_path / "out" / "SWEEP.md"
|
out = tmp_path / "out" / "SWEEP.md"
|
||||||
assert main([], roots=[tmp_path], out=out) == 0
|
assert main(roots=[tmp_path], out=out) == 0
|
||||||
assert "1 repos, 0 pass, 1 fail" in capsys.readouterr().out
|
assert "1 repos, 0 pass, 1 fail" in capsys.readouterr().out
|
||||||
assert out.is_file()
|
assert out.is_file()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def no_walk(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
|
|
||||||
"""Redirect HOME and make any walk fail loudly; return the default out path."""
|
|
||||||
monkeypatch.setenv("HOME", str(tmp_path / "home"))
|
|
||||||
|
|
||||||
def boom(roots: list[Path]) -> list[Path]:
|
|
||||||
raise AssertionError(f"walked {roots}") # pragma: no cover - regression only
|
|
||||||
|
|
||||||
monkeypatch.setattr("repo_contract.sweep.find_repos", boom)
|
|
||||||
return tmp_path / "default" / "SWEEP.md"
|
|
||||||
|
|
||||||
|
|
||||||
def test_help_exits_0_without_walk_or_write(
|
|
||||||
no_walk: Path, capsys: pytest.CaptureFixture[str]
|
|
||||||
) -> None:
|
|
||||||
with pytest.raises(SystemExit) as exc:
|
|
||||||
main(["--help"], out=no_walk)
|
|
||||||
assert exc.value.code == 0
|
|
||||||
text = capsys.readouterr().out
|
|
||||||
assert "~/src" in text and "SWEEP.md" in text
|
|
||||||
assert "--dry-run" in text and "--out" in text
|
|
||||||
assert not no_walk.exists()
|
|
||||||
|
|
||||||
|
|
||||||
def test_unknown_arg_exits_2_without_walk_or_write(
|
|
||||||
no_walk: Path, capsys: pytest.CaptureFixture[str]
|
|
||||||
) -> None:
|
|
||||||
with pytest.raises(SystemExit) as exc:
|
|
||||||
main(["--bogus"], out=no_walk)
|
|
||||||
assert exc.value.code == 2
|
|
||||||
assert "usage:" in capsys.readouterr().err
|
|
||||||
assert not no_walk.exists()
|
|
||||||
|
|
||||||
|
|
||||||
def test_dry_run_prints_report_and_writes_nothing(
|
|
||||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
||||||
) -> None:
|
|
||||||
monkeypatch.setenv("HOME", str(tmp_path / "home"))
|
|
||||||
make_repo(tmp_path / "src" / "r")
|
|
||||||
out = tmp_path / "default" / "SWEEP.md"
|
|
||||||
assert main(["--dry-run"], roots=[tmp_path / "src"], out=out) == 0
|
|
||||||
cap = capsys.readouterr()
|
|
||||||
assert cap.out.startswith("# Repo contract sweep")
|
|
||||||
assert "| r | FAIL |" in cap.out
|
|
||||||
assert "1 repos, 0 pass, 1 fail" in cap.err and "dry run" in cap.err
|
|
||||||
assert not out.exists() and not out.parent.exists()
|
|
||||||
|
|
||||||
|
|
||||||
def test_out_flag_writes_given_path(
|
|
||||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
||||||
) -> None:
|
|
||||||
monkeypatch.setenv("HOME", str(tmp_path / "home"))
|
|
||||||
make_repo(tmp_path / "src" / "r")
|
|
||||||
default = tmp_path / "default" / "SWEEP.md"
|
|
||||||
given = tmp_path / "x" / "SWEEP.md"
|
|
||||||
assert main(["--out", str(given)], roots=[tmp_path / "src"], out=default) == 0
|
|
||||||
assert given.read_text(encoding="utf-8").startswith("# Repo contract sweep")
|
|
||||||
assert not default.exists()
|
|
||||||
assert f"-> {given}" in capsys.readouterr().out
|
|
||||||
|
|
||||||
|
|
||||||
def test_cli_help_runs_as_script(tmp_path: Path) -> None:
|
|
||||||
script = Path(__file__).resolve().parents[1] / "sweep.py"
|
|
||||||
env = {"HOME": str(tmp_path), "PATH": "/usr/bin:/bin"}
|
|
||||||
proc = subprocess.run(
|
|
||||||
["python3", str(script), "--help"],
|
|
||||||
check=False,
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
env=env,
|
|
||||||
)
|
|
||||||
assert proc.returncode == 0 and "--dry-run" in proc.stdout
|
|
||||||
assert not (tmp_path / "data").exists()
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user