| 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 |
"""Golden-output regression for the full decopy pipeline. |
| 18 |
|
| 19 |
Runs decopy end to end over the small in-repo fixture trees and asserts the |
| 20 |
rendered debian/copyright matches a committed golden file byte for byte. This |
| 21 |
is the safety net for performance refactors (e.g. a license-detection |
| 22 |
prefilter): the output must not change, or the diff must be explained and the |
| 23 |
golden regenerated deliberately. |
| 24 |
|
| 25 |
Regenerate the golden files after an intended change with: |
| 26 |
|
| 27 |
DECOPY_UPDATE_GOLDEN=1 python3 -m pytest tests/golden_output_test.py |
| 28 |
""" |
| 29 |
|
| 30 |
from __future__ import annotations |
| 31 |
|
| 32 |
import os |
| 33 |
import subprocess |
| 34 |
import sys |
| 35 |
import tempfile |
| 36 |
import unittest |
| 37 |
from pathlib import Path |
| 38 |
|
| 39 |
_REPO_ROOT = Path(__file__).resolve().parent.parent |
| 40 |
_TESTDATA = Path(__file__).parent / "testdata" |
| 41 |
_GOLDEN_DIR = _TESTDATA / "golden" |
| 42 |
|
| 43 |
# Fixture trees rendered as goldens. All are pure text, so detection does not |
| 44 |
# depend on optional external tools (exiftool, *cat) and the output is portable. |
| 45 |
_CORPORA = ("one", "two", "three") |
| 46 |
|
| 47 |
# Pinned, explicit options so the golden does not silently track default flips. |
| 48 |
_OPTIONS = ["--no-progress", "--no-expand-archives"] |
| 49 |
|
| 50 |
|
| 51 |
def render_corpus(root: Path, out_path: Path, extra: list[str] | None = None) -> str: |
| 52 |
"""Render `root` to `out_path` via the real CLI in a fresh subprocess. |
| 53 |
|
| 54 |
A subprocess exercises the real `decopy` CLI entry point end to end, so the |
| 55 |
golden reflects exactly what users get, independent of test execution order. |
| 56 |
""" |
| 57 |
args = [ |
| 58 |
sys.executable, |
| 59 |
"-c", |
| 60 |
"from decopy.decopy import main; raise SystemExit(main())", |
| 61 |
*_OPTIONS, |
| 62 |
*(extra or []), |
| 63 |
"--root", |
| 64 |
str(root), |
| 65 |
"-o", |
| 66 |
str(out_path), |
| 67 |
] |
| 68 |
env = {**os.environ, "PYTHONPATH": str(_REPO_ROOT)} |
| 69 |
subprocess.run(args, check=True, capture_output=True, env=env) # noqa: S603 |
| 70 |
return out_path.read_text(encoding="utf-8") |
| 71 |
|
| 72 |
|
| 73 |
class TestGoldenOutput(unittest.TestCase): |
| 74 |
"""The rendered copyright for each fixture tree matches its golden.""" |
| 75 |
|
| 76 |
def _render(self, corpus: str, extra: list[str] | None = None) -> str: |
| 77 |
with tempfile.TemporaryDirectory() as tmp: |
| 78 |
return render_corpus(_TESTDATA / corpus, Path(tmp) / "copyright", extra) |
| 79 |
|
| 80 |
def test_matches_golden(self): |
| 81 |
update = os.environ.get("DECOPY_UPDATE_GOLDEN") == "1" |
| 82 |
if update: |
| 83 |
_GOLDEN_DIR.mkdir(parents=True, exist_ok=True) |
| 84 |
for corpus in _CORPORA: |
| 85 |
with self.subTest(corpus=corpus): |
| 86 |
got = self._render(corpus) |
| 87 |
golden = _GOLDEN_DIR / f"{corpus}.copyright" |
| 88 |
if update: |
| 89 |
golden.write_text(got, encoding="utf-8") |
| 90 |
continue |
| 91 |
self.assertTrue( |
| 92 |
golden.exists(), |
| 93 |
f"Missing golden {golden}; regenerate with DECOPY_UPDATE_GOLDEN=1", |
| 94 |
) |
| 95 |
self.assertEqual( |
| 96 |
got, |
| 97 |
golden.read_text(encoding="utf-8"), |
| 98 |
f"Output for {corpus} drifted from {golden}", |
| 99 |
) |
| 100 |
|
| 101 |
def test_output_is_deterministic(self): |
| 102 |
# Locks the property the perf work relies on: the threaded pipeline's |
| 103 |
# output does not depend on task completion order. |
| 104 |
for corpus in _CORPORA: |
| 105 |
with self.subTest(corpus=corpus): |
| 106 |
self.assertEqual(self._render(corpus), self._render(corpus)) |
| 107 |
|
| 108 |
|
| 109 |
if __name__ == "__main__": |
| 110 |
unittest.main() |