Debian Codegraph @

tests/delta_test.py

raw
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 """Contract of the explain-mode delta data types.
18
19 Covers is_empty (fresh vs populated), the NullDelta no-op sink and by_kind
20 grouping with insertion order preserved within each kind's list.
21 """
22
23 import dataclasses
24
25 from decopy.dep5doc.delta import (
26 COPYRIGHT_DRIFT,
27 DRIFT_ADDED,
28 DRIFT_BOTH,
29 DRIFT_REMOVED,
30 NEW_FILE,
31 NEW_PARAGRAPH,
32 RELICENSED,
33 STALE_PARAGRAPH,
34 STALE_PATTERN,
35 SUB_BOTH,
36 SUB_COPYRIGHT,
37 SUB_LICENSE,
38 ChangedDelta,
39 DeltaEntry,
40 DeltaReport,
41 NullDelta,
42 PartialDelta,
43 is_advisory,
44 relicense_sub_kind,
45 )
46 from decopy.grouping.grouping import GroupKey
47
48
49 def test_kind_constants_have_spec_values():
50 assert STALE_PARAGRAPH == "stale-paragraph"
51 assert STALE_PATTERN == "stale-pattern"
52 assert RELICENSED == "relicensed"
53 assert NEW_FILE == "new-file"
54 assert NEW_PARAGRAPH == "new-paragraph"
55 assert COPYRIGHT_DRIFT == "copyright-drift"
56
57
58 def test_sub_kind_constants_have_spec_values():
59 assert SUB_LICENSE == "license-changed"
60 assert SUB_COPYRIGHT == "copyright-changed"
61 assert SUB_BOTH == "license-and-copyright-changed"
62
63
64 def test_drift_sub_kind_constants_are_distinct():
65 subs = [DRIFT_ADDED, DRIFT_REMOVED, DRIFT_BOTH]
66 assert len(set(subs)) == len(subs)
67
68
69 def test_delta_entry_is_frozen():
70 entry = DeltaEntry(kind=NEW_FILE, detail="src/a.c")
71 assert entry.kind == NEW_FILE
72 assert entry.detail == "src/a.c"
73 assert entry.paragraph is None
74 assert entry.pattern is None
75 assert entry.path is None
76 assert entry.was is None
77 assert entry.now is None
78 assert entry.sub_kind is None
79 # setattr via a non-literal name avoids both type checkers rejecting a
80 # frozen-field write, while still hitting the guard at runtime.
81 field_name = "kind"
82 try:
83 setattr(entry, field_name, STALE_PATTERN)
84 except dataclasses.FrozenInstanceError:
85 pass
86 else:
87 msg = "expected FrozenInstanceError"
88 raise AssertionError(msg)
89
90
91 def test_provenance_fields_default_to_none():
92 entry = DeltaEntry(kind=NEW_FILE, detail="src/a.c")
93 assert entry.license_source is None
94 assert entry.holder_sources is None
95 assert entry.files is None
96
97
98 def test_new_paragraph_carries_provenance_tuples():
99 entry = DeltaEntry(
100 kind=NEW_PARAGRAPH,
101 detail="GPL-2+",
102 now="GPL-2+",
103 files=("src/a.c", "src/b.c"),
104 license_source="detected in src/a.c",
105 holder_sources=(("alice@x", ("src/a.c", "src/b.c")),),
106 )
107 assert entry.files == ("src/a.c", "src/b.c")
108 assert entry.license_source == "detected in src/a.c"
109 assert entry.holder_sources == (("alice@x", ("src/a.c", "src/b.c")),)
110
111
112 def test_new_paragraph_is_staleness_not_advisory():
113 # NEW_PARAGRAPH behaves like new-file -- it fails the gate.
114 entry = DeltaEntry(kind=NEW_PARAGRAPH, detail="GPL-2+", now="GPL-2+")
115 assert is_advisory(entry) is False
116 report = DeltaReport()
117 report.add(entry)
118 assert report.has_staleness() is True
119
120
121 def test_partial_delta_keeps_new_paragraph():
122 # It concerns uncovered included files, so the subset keeps it.
123 sink = PartialDelta()
124 sink.add(DeltaEntry(kind=NEW_PARAGRAPH, detail="GPL-2+", now="GPL-2+"))
125 assert set(sink.by_kind()) == {NEW_PARAGRAPH}
126
127
128 def test_changed_delta_keeps_stale_and_file_signals_drops_drift():
129 # Changed mode keeps the whole-tree-sound stale-* signals (unlike a plain
130 # subset) and the scoped file-level signals, but still drops the unsound
131 # copyright drift.
132 sink = ChangedDelta()
133 for kind in (STALE_PARAGRAPH, STALE_PATTERN, RELICENSED, NEW_PARAGRAPH):
134 sink.add(DeltaEntry(kind=kind, detail="x"))
135 sink.add(DeltaEntry(kind=COPYRIGHT_DRIFT, detail="x", sub_kind=DRIFT_ADDED))
136 assert set(sink.by_kind()) == {
137 STALE_PARAGRAPH,
138 STALE_PATTERN,
139 RELICENSED,
140 NEW_PARAGRAPH,
141 }
142
143
144 def test_changed_delta_stale_paragraph_is_staleness():
145 # A kept stale paragraph fails the gate (exit 1), like in full explain.
146 sink = ChangedDelta()
147 sink.add(DeltaEntry(kind=STALE_PARAGRAPH, detail="Files: legacy/*"))
148 assert sink.has_staleness() is True
149
150
151 def test_fresh_report_is_empty():
152 report = DeltaReport()
153 assert report.is_empty() is True
154
155
156 def test_report_not_empty_after_add():
157 report = DeltaReport()
158 report.add(DeltaEntry(kind=NEW_FILE, detail="src/a.c"))
159 assert report.is_empty() is False
160
161
162 def test_by_kind_groups_and_preserves_order():
163 report = DeltaReport()
164 first = DeltaEntry(kind=NEW_FILE, detail="src/a.c")
165 second = DeltaEntry(kind=NEW_FILE, detail="src/b.c")
166 third = DeltaEntry(kind=RELICENSED, detail="src/util.c")
167 report.add(first)
168 report.add(second)
169 report.add(third)
170
171 grouped = report.by_kind()
172 assert set(grouped) == {NEW_FILE, RELICENSED}
173 assert grouped[NEW_FILE] == [first, second]
174 assert grouped[RELICENSED] == [third]
175
176
177 def test_null_delta_add_stores_nothing():
178 sink = NullDelta()
179 sink.add(DeltaEntry(kind=NEW_FILE, detail="src/a.c"))
180 sink.add(DeltaEntry(kind=RELICENSED, detail="src/util.c"))
181 assert sink.is_empty() is True
182 assert sink.by_kind() == {}
183
184
185 def test_null_delta_is_empty_without_adds():
186 assert NullDelta().is_empty() is True
187
188
189 def test_relicense_sub_kind_license_grouping_license_differs():
190 file_key = GroupKey("Apache-2.0", None, "src/util.c")
191 group_key = GroupKey("GPL-2+", None, "src/util.c")
192 assert relicense_sub_kind(file_key, group_key) == SUB_LICENSE
193
194
195 def test_relicense_sub_kind_license_grouping_license_same():
196 file_key = GroupKey("GPL-2+", None, "src/util.c")
197 group_key = GroupKey("GPL-2+", None, "src/util.c")
198 assert relicense_sub_kind(file_key, group_key) is None
199
200
201 def test_relicense_sub_kind_copyright_grouping_only_license_differs():
202 file_key = GroupKey("Apache-2.0", ("Foo",), "src/util.c")
203 group_key = GroupKey("GPL-2+", ("Foo",), "src/util.c")
204 assert relicense_sub_kind(file_key, group_key) == SUB_LICENSE
205
206
207 def test_relicense_sub_kind_copyright_grouping_only_copyright_differs():
208 file_key = GroupKey("GPL-2+", ("Bar",), "src/util.c")
209 group_key = GroupKey("GPL-2+", ("Foo",), "src/util.c")
210 assert relicense_sub_kind(file_key, group_key) == SUB_COPYRIGHT
211
212
213 def test_relicense_sub_kind_copyright_grouping_both_differ():
214 file_key = GroupKey("Apache-2.0", ("Bar",), "src/util.c")
215 group_key = GroupKey("GPL-2+", ("Foo",), "src/util.c")
216 assert relicense_sub_kind(file_key, group_key) == SUB_BOTH
217
218
219 def test_relicense_sub_kind_copyright_grouping_neither_differs():
220 file_key = GroupKey("GPL-2+", ("Foo",), "src/util.c")
221 group_key = GroupKey("GPL-2+", ("Foo",), "src/util.c")
222 assert relicense_sub_kind(file_key, group_key) is None
223
224
225 def test_relicense_sub_kind_path_only_difference_is_none():
226 file_key = GroupKey("GPL-2+", ("Foo",), "src/a.c")
227 group_key = GroupKey("GPL-2+", ("Foo",), "src/b.c")
228 assert relicense_sub_kind(file_key, group_key) is None
229
230
231 def _drift(sub_kind: str) -> DeltaEntry:
232 return DeltaEntry(kind=COPYRIGHT_DRIFT, detail="Files: src/*", sub_kind=sub_kind)
233
234
235 def test_is_advisory_only_for_removed_only_drift():
236 assert is_advisory(_drift(DRIFT_REMOVED)) is True
237 assert is_advisory(_drift(DRIFT_ADDED)) is False
238 assert is_advisory(_drift(DRIFT_BOTH)) is False
239 assert is_advisory(DeltaEntry(kind=NEW_FILE, detail="src/a.c")) is False
240
241
242 def test_has_staleness_true_for_added_or_both_drift():
243 for sub_kind in (DRIFT_ADDED, DRIFT_BOTH):
244 report = DeltaReport()
245 report.add(_drift(sub_kind))
246 assert report.has_staleness() is True
247
248
249 def test_has_staleness_false_for_removed_only_drift_but_not_empty():
250 report = DeltaReport()
251 report.add(_drift(DRIFT_REMOVED))
252 assert report.has_staleness() is False
253 assert report.is_empty() is False
254
255
256 def test_has_staleness_false_for_fresh_report():
257 assert DeltaReport().has_staleness() is False
258
259
260 def test_has_staleness_true_for_other_kinds():
261 report = DeltaReport()
262 report.add(DeltaEntry(kind=NEW_FILE, detail="src/a.c"))
263 assert report.has_staleness() is True
264
265
266 def test_partial_delta_keeps_relicensed_and_new_file():
267 sink = PartialDelta()
268 sink.add(DeltaEntry(kind=RELICENSED, detail="src/util.c"))
269 sink.add(DeltaEntry(kind=NEW_FILE, detail="src/a.c"))
270 assert set(sink.by_kind()) == {RELICENSED, NEW_FILE}
271
272
273 def test_partial_delta_drops_drift_and_stale_kinds():
274 sink = PartialDelta()
275 sink.add(_drift(DRIFT_ADDED))
276 sink.add(_drift(DRIFT_REMOVED))
277 sink.add(DeltaEntry(kind=STALE_PARAGRAPH, detail="Files: legacy/*"))
278 sink.add(
279 DeltaEntry(kind=STALE_PATTERN, detail="Files: src/*", pattern="src/old_*.c")
280 )
281 assert sink.is_empty() is True