| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Unit tests for the extracted explain-mode staleness detectors. |
| 4 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 5 |
# |
| 6 |
# License: ISC |
| 7 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
# purpose with or without fee is hereby granted, provided that the above |
| 9 |
# copyright notice and this permission notice appear in all copies. |
| 10 |
# . |
| 11 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 12 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 13 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 14 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 15 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 16 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 17 |
# PERFORMANCE OF THIS SOFTWARE. |
| 18 |
|
| 19 |
"""Direct unit tests for decopy.dep5doc.staleness. |
| 20 |
|
| 21 |
These functions were extracted from copyright_processor and used to be private, |
| 22 |
tested only through Copyright. Now that they are a module's public API, pin their |
| 23 |
contract directly. |
| 24 |
""" |
| 25 |
|
| 26 |
import debian.copyright as dc |
| 27 |
|
| 28 |
from decopy.dep5doc import staleness |
| 29 |
from decopy.dep5doc.delta import ( |
| 30 |
RELICENSED, |
| 31 |
STALE_PARAGRAPH, |
| 32 |
STALE_PATTERN, |
| 33 |
SUB_LICENSE, |
| 34 |
DeltaReport, |
| 35 |
) |
| 36 |
from decopy.fstree import nodes |
| 37 |
from decopy.grouping.grouping import GroupKey |
| 38 |
|
| 39 |
|
| 40 |
def test_record_relicensed_skips_packaging_files(): |
| 41 |
report = DeltaReport() |
| 42 |
fileinfo = nodes.FileInfo(parent=None, name="debian/rules") |
| 43 |
keys = ( |
| 44 |
GroupKey("Apache-2.0", None, "debian/rules"), |
| 45 |
GroupKey("GPL-2+", None, "debian/rules"), |
| 46 |
) |
| 47 |
staleness.record_relicensed(report, fileinfo, keys) |
| 48 |
assert report.is_empty() |
| 49 |
|
| 50 |
|
| 51 |
def test_record_relicensed_ignores_path_only_difference(): |
| 52 |
report = DeltaReport() |
| 53 |
fileinfo = nodes.FileInfo(parent=None, name="src/a.c") |
| 54 |
# Same license and copyright, different path -> not a relicense signal. |
| 55 |
keys = (GroupKey("MIT", None, "src/a.c"), GroupKey("MIT", None, "src/b.c")) |
| 56 |
staleness.record_relicensed(report, fileinfo, keys) |
| 57 |
assert report.is_empty() |
| 58 |
|
| 59 |
|
| 60 |
def test_record_relicensed_records_a_license_change(): |
| 61 |
report = DeltaReport() |
| 62 |
fileinfo = nodes.FileInfo(parent=None, name="src/a.c") |
| 63 |
keys = ( |
| 64 |
GroupKey("Apache-2.0", None, "src/a.c"), |
| 65 |
GroupKey("GPL-2+", None, "src/a.c"), |
| 66 |
) |
| 67 |
staleness.record_relicensed(report, fileinfo, keys) |
| 68 |
entries = report.by_kind()[RELICENSED] |
| 69 |
assert len(entries) == 1 |
| 70 |
entry = entries[0] |
| 71 |
assert entry.path == "src/a.c" |
| 72 |
assert entry.was == "GPL-2+" |
| 73 |
assert entry.now == "Apache-2.0" |
| 74 |
assert entry.sub_kind == SUB_LICENSE |
| 75 |
|
| 76 |
|
| 77 |
def test_record_stale_patterns_dedups_dead_patterns_in_order(): |
| 78 |
report = DeltaReport() |
| 79 |
paragraph = dc.FilesParagraph.create( |
| 80 |
["*.c", "*.h", "*.py"], "2020 Alice", dc.License("MIT") |
| 81 |
) |
| 82 |
# Only *.c matched; *.h and *.py are dead, one entry each, declared order. |
| 83 |
staleness.record_stale_patterns(report, paragraph, ["*.c"]) |
| 84 |
entries = report.by_kind()[STALE_PATTERN] |
| 85 |
assert [e.pattern for e in entries] == ["*.h", "*.py"] |
| 86 |
assert all(e.paragraph_license == "MIT" for e in entries) |
| 87 |
|
| 88 |
|
| 89 |
def test_record_stale_paragraph_attributes_matching_deletions(): |
| 90 |
report = DeltaReport() |
| 91 |
paragraph = dc.FilesParagraph.create(["docs/*"], "2020 Alice", dc.License("GPL-2+")) |
| 92 |
staleness.record_stale_paragraph( |
| 93 |
report, paragraph, deleted=("docs/old.txt", "src/keep.c") |
| 94 |
) |
| 95 |
entries = report.by_kind()[STALE_PARAGRAPH] |
| 96 |
assert len(entries) == 1 |
| 97 |
entry = entries[0] |
| 98 |
assert entry.paragraph_license == "GPL-2+" |
| 99 |
# Only the deleted path matching the paragraph's pattern is attributed. |
| 100 |
assert entry.deleted_files == ("docs/old.txt",) |