| 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 |
"""Deletion provenance: a stale paragraph names the deleted file that emptied it. |
| 18 |
|
| 19 |
Unit-level over Copyright.process with the deleted set injected directly (no git |
| 20 |
needed): fnmatch of the deleted paths against the stale paragraph's patterns. |
| 21 |
""" |
| 22 |
|
| 23 |
from __future__ import annotations |
| 24 |
|
| 25 |
import tempfile |
| 26 |
import unittest |
| 27 |
from pathlib import Path |
| 28 |
|
| 29 |
from decopy import cmdoptions |
| 30 |
from decopy.dep5doc import dep5 |
| 31 |
from decopy.dep5doc.delta import STALE_PARAGRAPH, DeltaReport |
| 32 |
from decopy.fstree import tree |
| 33 |
|
| 34 |
# u*.c stays alive via util.c so only legacy/* is stale. |
| 35 |
_COPYRIGHT = """\ |
| 36 |
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ |
| 37 |
Source: TODO |
| 38 |
|
| 39 |
Files: u*.c |
| 40 |
License: Expat |
| 41 |
|
| 42 |
Files: legacy/* |
| 43 |
License: GPL-2+ |
| 44 |
""" |
| 45 |
|
| 46 |
|
| 47 |
def _stale_entries(root: Path, deleted: tuple[str, ...] = ()) -> list: |
| 48 |
(root / "util.c").write_text("int x;\n", encoding="utf-8") |
| 49 |
debian = root / "debian" |
| 50 |
debian.mkdir(exist_ok=True) |
| 51 |
(debian / "copyright").write_text(_COPYRIGHT, encoding="utf-8") |
| 52 |
options = cmdoptions.process_options( |
| 53 |
["--no-progress", "--no-expand-archives", "--root", str(root)] |
| 54 |
) |
| 55 |
filetree = tree.RootInfo.build(options) |
| 56 |
copyright_ = dep5.read_copyright(options) |
| 57 |
report = DeltaReport() |
| 58 |
copyright_.process(filetree, report, deleted) |
| 59 |
return report.by_kind().get(STALE_PARAGRAPH, []) |
| 60 |
|
| 61 |
|
| 62 |
class DeletionProvenanceTest(unittest.TestCase): |
| 63 |
def test_matching_deleted_path_is_named_as_culprit(self): |
| 64 |
with tempfile.TemporaryDirectory() as tmp: |
| 65 |
entries = _stale_entries(Path(tmp), ("legacy/old.c",)) |
| 66 |
self.assertEqual(len(entries), 1) |
| 67 |
self.assertEqual(entries[0].deleted_files, ("legacy/old.c",)) |
| 68 |
|
| 69 |
def test_no_deleted_set_leaves_provenance_none(self): |
| 70 |
with tempfile.TemporaryDirectory() as tmp: |
| 71 |
entries = _stale_entries(Path(tmp)) |
| 72 |
self.assertEqual(len(entries), 1) |
| 73 |
self.assertIsNone(entries[0].deleted_files) |
| 74 |
|
| 75 |
def test_non_matching_deleted_path_leaves_provenance_none(self): |
| 76 |
with tempfile.TemporaryDirectory() as tmp: |
| 77 |
entries = _stale_entries(Path(tmp), ("src/other.c",)) |
| 78 |
self.assertEqual(len(entries), 1) |
| 79 |
self.assertIsNone(entries[0].deleted_files) |
| 80 |
|
| 81 |
def test_multiple_culprits_sorted(self): |
| 82 |
with tempfile.TemporaryDirectory() as tmp: |
| 83 |
entries = _stale_entries( |
| 84 |
Path(tmp), ("legacy/z.c", "legacy/a.c", "src/keep.c") |
| 85 |
) |
| 86 |
self.assertEqual(entries[0].deleted_files, ("legacy/a.c", "legacy/z.c")) |