| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 3 |
# |
| 4 |
# License: ISC |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# . |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 10 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 14 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 |
# PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
"""Capture or check decopy's full output for a corpus (golden-output workflow). |
| 18 |
|
| 19 |
The committed regression net for the in-repo fixtures lives in |
| 20 |
tests/golden_output_test.py. This script is its counterpart for large external |
| 21 |
trees (e.g. the ruff checkout) that are too big to commit: capture a golden |
| 22 |
once, then `check` after each optimisation to prove the output is unchanged. |
| 23 |
|
| 24 |
# before optimising: |
| 25 |
python3 scripts/golden_output.py capture ~/debian/python/ruff /tmp/ruff.golden |
| 26 |
# after a change: |
| 27 |
python3 scripts/golden_output.py check ~/debian/python/ruff /tmp/ruff.golden |
| 28 |
|
| 29 |
Extra decopy options (after `--`) are passed through and recorded, so the same |
| 30 |
flags are used for capture and check: |
| 31 |
|
| 32 |
python3 scripts/golden_output.py capture SRC OUT -- --group-by copyright |
| 33 |
""" |
| 34 |
|
| 35 |
from __future__ import annotations |
| 36 |
|
| 37 |
import argparse |
| 38 |
import difflib |
| 39 |
import subprocess |
| 40 |
import sys |
| 41 |
import tempfile |
| 42 |
from pathlib import Path |
| 43 |
|
| 44 |
ROOT = Path(__file__).resolve().parent.parent |
| 45 |
sys.path.insert(0, str(ROOT)) |
| 46 |
|
| 47 |
from decopy import cmdoptions # noqa: E402 |
| 48 |
from decopy.decopy import run_pipeline # noqa: E402 |
| 49 |
|
| 50 |
_BASE_OPTIONS = ["--no-progress"] |
| 51 |
|
| 52 |
|
| 53 |
def _git_rev(path: Path) -> str: |
| 54 |
try: |
| 55 |
out = subprocess.run( # noqa: S603 |
| 56 |
["git", "-C", str(path), "rev-parse", "--short", "HEAD"], # noqa: S607 |
| 57 |
check=True, |
| 58 |
capture_output=True, |
| 59 |
text=True, |
| 60 |
) |
| 61 |
return out.stdout.strip() |
| 62 |
except (subprocess.CalledProcessError, OSError): |
| 63 |
return "unknown" |
| 64 |
|
| 65 |
|
| 66 |
def render(corpus: Path, extra: list[str], out_path: Path) -> str: |
| 67 |
args = [*_BASE_OPTIONS, *extra, "--root", str(corpus), "-o", str(out_path)] |
| 68 |
run_pipeline(cmdoptions.process_options(args)) |
| 69 |
return out_path.read_text(encoding="utf-8") |
| 70 |
|
| 71 |
|
| 72 |
def capture(corpus: Path, golden: Path, extra: list[str]) -> int: |
| 73 |
text = render(corpus, extra, golden) |
| 74 |
print( |
| 75 |
f"Captured {len(text)} bytes to {golden}\n" |
| 76 |
f" corpus: {corpus} (rev {_git_rev(corpus)})\n" |
| 77 |
f" decopy: rev {_git_rev(ROOT)} options: {extra or '(defaults)'}", |
| 78 |
file=sys.stderr, |
| 79 |
) |
| 80 |
return 0 |
| 81 |
|
| 82 |
|
| 83 |
def check(corpus: Path, golden: Path, extra: list[str]) -> int: |
| 84 |
if not golden.exists(): |
| 85 |
print(f"No golden at {golden}; run `capture` first.", file=sys.stderr) |
| 86 |
return 2 |
| 87 |
with tempfile.TemporaryDirectory() as tmp: |
| 88 |
got = render(corpus, extra, Path(tmp) / "copyright") |
| 89 |
want = golden.read_text(encoding="utf-8") |
| 90 |
if got == want: |
| 91 |
print(f"OK: output matches {golden}", file=sys.stderr) |
| 92 |
return 0 |
| 93 |
diff = difflib.unified_diff( |
| 94 |
want.splitlines(keepends=True), |
| 95 |
got.splitlines(keepends=True), |
| 96 |
fromfile=str(golden), |
| 97 |
tofile="current", |
| 98 |
) |
| 99 |
sys.stdout.writelines(diff) |
| 100 |
print(f"\nMISMATCH: output drifted from {golden}", file=sys.stderr) |
| 101 |
return 1 |
| 102 |
|
| 103 |
|
| 104 |
def main() -> int: |
| 105 |
parser = argparse.ArgumentParser(description=__doc__) |
| 106 |
parser.add_argument("mode", choices=("capture", "check")) |
| 107 |
parser.add_argument("corpus", help="source tree to render") |
| 108 |
parser.add_argument("golden", help="golden file to write (capture) or compare") |
| 109 |
parser.add_argument( |
| 110 |
"extra", |
| 111 |
nargs="*", |
| 112 |
help="extra decopy options after `--`, e.g. -- --group-by copyright", |
| 113 |
) |
| 114 |
args = parser.parse_args() |
| 115 |
|
| 116 |
corpus = Path(args.corpus) |
| 117 |
golden = Path(args.golden) |
| 118 |
if args.mode == "capture": |
| 119 |
return capture(corpus, golden, args.extra) |
| 120 |
return check(corpus, golden, args.extra) |
| 121 |
|
| 122 |
|
| 123 |
if __name__ == "__main__": |
| 124 |
sys.exit(main()) |