Debian Codegraph @

tests/explain_renderer_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 """Unit tests for ExplainRenderer (pure DeltaReport -> str rendering).
18
19 Reports are built by hand and the rendered string is asserted, so these tests
20 never touch the pipeline or stdout. They lock the section order, counts,
21 singular/plural agreement, per-kind line formats, --spdx rendering and the
22 holders-under-copyright-grouping rule from the interactions spec.
23 """
24
25 from decopy import cmdoptions
26 from decopy.dep5doc.delta import (
27 COPYRIGHT_DRIFT,
28 DRIFT_ADDED,
29 DRIFT_BOTH,
30 DRIFT_REMOVED,
31 NEW_PARAGRAPH,
32 RELICENSED,
33 STALE_PARAGRAPH,
34 STALE_PATTERN,
35 SUB_BOTH,
36 SUB_COPYRIGHT,
37 SUB_LICENSE,
38 DeltaEntry,
39 DeltaReport,
40 )
41 from decopy.dep5doc.explain_renderer import ExplainRenderer
42
43
44 def _options(args):
45 base = ["--no-progress", "--root", "tests/testdata/one"]
46 return cmdoptions.process_options([*base, *args])
47
48
49 _PARAGRAPH_DUMP = "Files: legacy/*\nLicense: GPL-2+\n"
50
51
52 def _new_paragraph(license_name, files=("src/a.c",), source=None, holders=None):
53 return DeltaEntry(
54 kind=NEW_PARAGRAPH,
55 detail=license_name,
56 now=license_name,
57 files=tuple(files),
58 license_source=source or f"detected in {files[0]}",
59 holder_sources=holders,
60 )
61
62
63 def _relicensed(path, sub_kind, licenses, copyrights=(None, None)):
64 was, now = licenses
65 was_copyright, now_copyright = copyrights
66 return DeltaEntry(
67 kind=RELICENSED,
68 detail=path,
69 path=path,
70 sub_kind=sub_kind,
71 was=was,
72 now=now,
73 was_copyright=was_copyright,
74 now_copyright=now_copyright,
75 )
76
77
78 _DRIFT_DUMP = "Files: u*.c\nCopyright: 2010, Someone Else\nLicense: Apache-2.0\n"
79
80
81 def _drift(sub_kind, was, now, dump=_DRIFT_DUMP):
82 return DeltaEntry(
83 kind=COPYRIGHT_DRIFT,
84 detail=dump,
85 paragraph=dump,
86 sub_kind=sub_kind,
87 was_copyright=was,
88 now_copyright=now,
89 )
90
91
92 def _stale_paragraph(
93 dump=_PARAGRAPH_DUMP, paragraph_license="GPL-2+", deleted_files=None
94 ):
95 return DeltaEntry(
96 kind=STALE_PARAGRAPH,
97 detail=dump,
98 paragraph=dump,
99 paragraph_license=paragraph_license,
100 deleted_files=deleted_files,
101 )
102
103
104 def _stale_pattern(pattern, dump=_PARAGRAPH_DUMP, paragraph_license="GPL-2+"):
105 return DeltaEntry(
106 kind=STALE_PATTERN,
107 detail=dump,
108 pattern=pattern,
109 paragraph_license=paragraph_license,
110 )
111
112
113 def _explain(report, args=()):
114 return ExplainRenderer().explain(report, _options(list(args)))
115
116
117 def test_lead_in_is_first_non_empty_line():
118 report = DeltaReport()
119 report.add(_new_paragraph("Expat"))
120 text = _explain(report)
121 first = next(line for line in text.splitlines() if line.strip())
122 assert first == "debian/copyright is out of date:"
123
124
125 def test_empty_report_is_clean_message():
126 text = _explain(DeltaReport())
127 assert text == "debian/copyright is up to date.\n"
128
129
130 def test_section_order_with_all_kinds():
131 report = DeltaReport()
132 report.add(_new_paragraph("Expat"))
133 report.add(_relicensed("src/util.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
134 report.add(_stale_paragraph())
135 report.add(_stale_pattern("legacy/old_*.c"))
136 text = _explain(report)
137
138 new_idx = text.index("new paragraph")
139 reli_idx = text.index("no longer match")
140 para_idx = text.index("matches no files")
141 pat_idx = text.index("stale pattern")
142 assert reli_idx < new_idx < para_idx < pat_idx
143
144
145 def test_section_skipped_when_empty():
146 report = DeltaReport()
147 report.add(_new_paragraph("Expat"))
148 text = _explain(report)
149 assert "no longer match" not in text
150 assert "matches no files" not in text
151 assert "stale pattern" not in text
152
153
154 def test_count_singular():
155 report = DeltaReport()
156 report.add(_new_paragraph("Expat"))
157 text = _explain(report)
158 assert "1 new paragraph would be added:" in text
159
160
161 def test_count_plural():
162 report = DeltaReport()
163 report.add(_new_paragraph("Expat", ("src/a.c",)))
164 report.add(_new_paragraph("Expat", ("src/b.c",)))
165 report.add(_new_paragraph("Expat", ("src/c.c",)))
166 text = _explain(report)
167 assert "3 new paragraphs would be added:" in text
168
169
170 def test_relicensed_header_singular_and_plural():
171 one = DeltaReport()
172 one.add(_relicensed("src/util.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
173 assert "1 file no longer matches its paragraph:" in _explain(one)
174
175 many = DeltaReport()
176 many.add(_relicensed("src/util.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
177 many.add(_relicensed("src/io.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
178 assert "2 files no longer match their paragraph:" in _explain(many)
179
180
181 def test_stale_paragraph_header_singular_and_plural():
182 one = DeltaReport()
183 one.add(_stale_paragraph())
184 assert "1 paragraph matches no files:" in _explain(one)
185
186 many = DeltaReport()
187 many.add(_stale_paragraph())
188 many.add(_stale_paragraph())
189 assert "2 paragraphs match no files:" in _explain(many)
190
191
192 def test_stale_pattern_header_singular_and_plural():
193 one = DeltaReport()
194 one.add(_stale_pattern("legacy/old_*.c"))
195 assert "1 stale pattern:" in _explain(one)
196
197 many = DeltaReport()
198 many.add(_stale_pattern("legacy/old_*.c"))
199 many.add(_stale_pattern("legacy/gone_*.c"))
200 assert "2 stale patterns:" in _explain(many)
201
202
203 def test_new_paragraph_block_format():
204 report = DeltaReport()
205 report.add(
206 _new_paragraph("Expat", ("docs/guide.rst",), "detected in docs/guide.rst")
207 )
208 text = _explain(report)
209 # The would-be paragraph lists its file and names where the license came from.
210 assert " Files: docs/guide.rst" in text
211 assert " License: Expat (detected in docs/guide.rst)" in text
212 # A new paragraph has no prior state, so there is no "was: (none)" line.
213 assert "was: (none)" not in text
214
215
216 def test_new_paragraph_license_honours_spdx():
217 report = DeltaReport()
218 report.add(_new_paragraph("Expat", ("src/a.c",)))
219 text = _explain(report, ["--spdx"])
220 assert "License: MIT (detected in src/a.c)" in text
221 # --spdx maps the license, not the source phrase (which names a file).
222 assert "License: Expat" not in text
223
224
225 def test_new_paragraph_lists_holders_with_sources():
226 report = DeltaReport()
227 report.add(
228 _new_paragraph(
229 "Expat",
230 ("src/a.c", "src/b.c"),
231 "detected in src/a.c",
232 holders=(("Acme Inc", ("src/a.c", "src/b.c")),),
233 )
234 )
235 text = _explain(report)
236 assert " Files: src/a.c" in text
237 # Continuation file aligns under the first.
238 assert " src/b.c" in text
239 assert " Copyright: Acme Inc (in src/a.c, src/b.c)" in text
240
241
242 def test_was_column_aligned_across_relicensed_rows():
243 paths = ["a.c", "long/path/file.c"]
244 report = DeltaReport()
245 for path in paths:
246 report.add(_relicensed(path, SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
247 rows = [line for line in _explain(report).splitlines() if "was:" in line]
248 assert len(rows) == len(paths)
249 assert rows[0].index("was:") == rows[1].index("was:")
250
251
252 def test_relicensed_line_format():
253 report = DeltaReport()
254 report.add(_relicensed("src/parse.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
255 line = next(line for line in _explain(report).splitlines() if "src/parse.c" in line)
256 assert "src/parse.c" in line
257 assert "was: GPL-2+ -> now: Apache-2.0" in line
258 # The sub-kind token and tab columns are gone from the visible line.
259 assert "\t" not in line
260 assert "license-changed" not in line
261
262
263 def test_relicensed_license_honours_spdx():
264 report = DeltaReport()
265 report.add(_relicensed("src/parse.c", SUB_LICENSE, ("Expat", "GPL-2")))
266 line = next(
267 line
268 for line in _explain(report, ["--spdx"]).splitlines()
269 if "src/parse.c" in line
270 )
271 assert "was: MIT -> now: GPL-2.0-only" in line
272
273
274 def test_holders_shown_under_copyright_grouping():
275 report = DeltaReport()
276 report.add(
277 _relicensed(
278 "src/util.c",
279 SUB_BOTH,
280 ("GPL-2+", "Apache-2.0"),
281 (("Foo",), ("Bar", "Baz")),
282 )
283 )
284 text = _explain(report, ["--group-by", "copyright"])
285 # License stays on the item line; holders move to +/- delta lines below it.
286 assert "src/util.c was: GPL-2+ -> now: Apache-2.0" in text
287 assert " + Bar" in text
288 assert " + Baz" in text
289 assert " - Foo" in text
290
291
292 def test_drift_and_relicensed_share_holder_delta_syntax():
293 drift = DeltaReport()
294 drift.add(_drift(DRIFT_ADDED, ("X",), ("X", "Y")))
295 reli = DeltaReport()
296 reli.add(
297 _relicensed(
298 "src/a.c", SUB_COPYRIGHT, ("GPL-2+", "GPL-2+"), (("X",), ("X", "Y"))
299 )
300 )
301 assert " + Y" in _explain(drift)
302 assert " + Y" in _explain(reli, ["--group-by", "copyright"])
303
304
305 def test_holders_omitted_under_license_grouping():
306 report = DeltaReport()
307 report.add(
308 _relicensed(
309 "src/util.c",
310 SUB_LICENSE,
311 ("GPL-2+", "Apache-2.0"),
312 (("Foo",), ("Bar",)),
313 )
314 )
315 line = next(
316 line
317 for line in _explain(report, ["--group-by", "license"]).splitlines()
318 if "src/util.c" in line
319 )
320 assert "(" not in line
321 assert "was: GPL-2+ -> now: Apache-2.0" in line
322
323
324 def test_copyright_changed_sub_kind_renders():
325 report = DeltaReport()
326 report.add(
327 _relicensed(
328 "src/io.c",
329 SUB_COPYRIGHT,
330 ("GPL-2+", "GPL-2+"),
331 (("Foo",), ("Bar",)),
332 )
333 )
334 text = _explain(report, ["--group-by", "copyright"])
335 # A copyright-only change: identical-sided license line plus holder deltas.
336 assert "src/io.c was: GPL-2+ -> now: GPL-2+" in text
337 assert " + Bar" in text
338 assert " - Foo" in text
339
340
341 def test_drift_header_singular_and_plural():
342 one = DeltaReport()
343 one.add(_drift(DRIFT_ADDED, ("Someone Else",), ("Foo Bar", "Someone Else")))
344 assert "1 paragraph has copyright drift:" in _explain(one)
345
346 many = DeltaReport()
347 many.add(_drift(DRIFT_ADDED, ("Someone Else",), ("Foo Bar", "Someone Else")))
348 many.add(_drift(DRIFT_REMOVED, ("A", "B"), ("A",)))
349 assert "2 paragraphs have copyright drift:" in _explain(many)
350
351
352 def test_drift_added_line():
353 report = DeltaReport()
354 report.add(_drift(DRIFT_ADDED, ("Someone Else",), ("Foo Bar", "Someone Else")))
355 text = _explain(report)
356 assert " Files: u*.c" in text
357 assert "+ Foo Bar" in text
358 assert "- " not in text
359
360
361 def test_drift_removed_line_is_advisory():
362 report = DeltaReport()
363 report.add(_drift(DRIFT_REMOVED, ("Foo Bar", "Someone Else"), ("Foo Bar",)))
364 text = _explain(report)
365 assert "- Someone Else [advisory]" in text
366 assert "+ " not in text
367 # The terse tag is explained once, by a footnote under the section header.
368 assert "[advisory] holders are kept on regeneration" in text
369
370
371 def test_drift_both_shows_added_and_removed():
372 report = DeltaReport()
373 report.add(_drift(DRIFT_BOTH, ("Someone Else",), ("Foo Bar",)))
374 text = _explain(report)
375 assert "+ Foo Bar" in text
376 assert "- Someone Else [advisory]" in text
377
378
379 def test_drift_added_holder_shows_source_removed_none():
380 # An added holder names the files it was found in; a removed holder is
381 # absent from every file, so it carries no source.
382 report = DeltaReport()
383 report.add(
384 DeltaEntry(
385 kind=COPYRIGHT_DRIFT,
386 detail=_DRIFT_DUMP,
387 paragraph=_DRIFT_DUMP,
388 sub_kind=DRIFT_BOTH,
389 was_copyright=("Old",),
390 now_copyright=("New",),
391 holder_sources=(("New", ("a.c", "b.c")),),
392 )
393 )
394 text = _explain(report)
395 assert " + New (in a.c, b.c)" in text
396 assert " - Old [advisory]" in text
397 assert "- Old (in" not in text
398
399
400 def test_relicensed_added_holder_shows_source():
401 # The relicensed +/- lines share the holder grammar, so they get sources too.
402 report = DeltaReport()
403 report.add(
404 DeltaEntry(
405 kind=RELICENSED,
406 detail="src/util.c",
407 path="src/util.c",
408 sub_kind=SUB_COPYRIGHT,
409 was="GPL-2+",
410 now="GPL-2+",
411 was_copyright=("Foo",),
412 now_copyright=("Foo", "Bar"),
413 holder_sources=(("Bar", ("src/util.c",)),),
414 )
415 )
416 text = _explain(report, ["--group-by", "copyright"])
417 assert " + Bar (in src/util.c)" in text
418
419
420 def test_holder_source_truncates_past_cap():
421 report = DeltaReport()
422 files = ("a.c", "b.c", "c.c", "d.c", "e.c")
423 report.add(
424 DeltaEntry(
425 kind=COPYRIGHT_DRIFT,
426 detail=_DRIFT_DUMP,
427 paragraph=_DRIFT_DUMP,
428 sub_kind=DRIFT_ADDED,
429 was_copyright=(),
430 now_copyright=("New",),
431 holder_sources=(("New", files),),
432 )
433 )
434 text = _explain(report)
435 assert "+ New (in a.c, b.c, c.c +2 more)" in text
436
437
438 def test_added_holder_without_sources_has_no_suffix():
439 # No holder_sources (older entries / license grouping) -> + line unchanged.
440 report = DeltaReport()
441 report.add(_drift(DRIFT_ADDED, ("X",), ("X", "Y")))
442 text = _explain(report)
443 assert " + Y" in text
444 assert "(in" not in text
445
446
447 def test_advisory_tag_is_drift_only():
448 # A relicensed-copyright removed holder is a real move (the file regrouped),
449 # not advisory, so it must not carry the [advisory] tag or the footnote.
450 report = DeltaReport()
451 report.add(
452 _relicensed(
453 "src/a.c", SUB_COPYRIGHT, ("GPL-2+", "GPL-2+"), (("Foo",), ("Bar",))
454 )
455 )
456 text = _explain(report, ["--group-by", "copyright"])
457 assert "- Foo" in text
458 assert "[advisory]" not in text
459
460
461 def test_drift_added_only_has_no_advisory_footnote():
462 report = DeltaReport()
463 report.add(_drift(DRIFT_ADDED, ("Someone Else",), ("Foo Bar", "Someone Else")))
464 text = _explain(report)
465 assert "[advisory]" not in text
466
467
468 def test_drift_section_placed_between_relicensed_and_new_paragraph():
469 report = DeltaReport()
470 report.add(_new_paragraph("Expat"))
471 report.add(_relicensed("src/util.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
472 report.add(_drift(DRIFT_ADDED, ("Someone Else",), ("Foo Bar", "Someone Else")))
473 report.add(_stale_paragraph())
474 report.add(_stale_pattern("legacy/old_*.c"))
475 text = _explain(report)
476
477 new_idx = text.index("new paragraph")
478 reli_idx = text.index("no longer match")
479 drift_idx = text.index("copyright drift")
480 para_idx = text.index("matches no files")
481 pat_idx = text.index("stale pattern")
482 assert reli_idx < drift_idx < new_idx < para_idx < pat_idx
483
484
485 def test_new_paragraph_without_files_is_skipped():
486 # Defensive: a new-paragraph entry with no files must not render an empty
487 # block nor inflate the count; the valid entry alone drives the section.
488 report = DeltaReport()
489 report.add(_new_paragraph("Expat", ("src/good.c",), "detected in src/good.c"))
490 report.add(DeltaEntry(kind=NEW_PARAGRAPH, detail="Expat", now="Expat", files=None))
491 text = _explain(report)
492 assert "1 new paragraph would be added:" in text
493 assert " Files: src/good.c" in text
494
495
496 def test_stale_paragraph_without_dump_is_skipped():
497 # Defensive: a stale-paragraph entry missing `paragraph` must not render an
498 # empty block nor inflate the count.
499 report = DeltaReport()
500 report.add(_stale_paragraph("Files: legacy/*\nLicense: GPL-2+\n"))
501 report.add(DeltaEntry(kind=STALE_PARAGRAPH, detail="", paragraph=None))
502 text = _explain(report)
503 assert "1 paragraph matches no files:" in text
504 assert "Files: legacy/*" in text
505
506
507 def test_stale_pattern_line_format():
508 report = DeltaReport()
509 report.add(
510 _stale_pattern("legacy/old_*.c", "Files: legacy/*\nLicense: Expat\n", "Expat")
511 )
512 line = next(
513 line for line in _explain(report).splitlines() if "legacy/old_*.c" in line
514 )
515 assert "legacy/old_*.c" in line
516 assert "matches nothing" in line
517 assert "[License: Expat]" in line
518 assert "(in the" not in line
519
520
521 def test_stale_pattern_uses_paragraph_license_field_not_dump():
522 # The renderer reads the explicit paragraph_license field, not a re-parse of
523 # the serialised dump: a dump with no License line still renders the license.
524 report = DeltaReport()
525 report.add(
526 DeltaEntry(
527 kind=STALE_PATTERN,
528 detail="Files: legacy/*\n",
529 pattern="legacy/old_*.c",
530 paragraph_license="Expat",
531 )
532 )
533 line = next(
534 line for line in _explain(report).splitlines() if "legacy/old_*.c" in line
535 )
536 assert "[License: Expat]" in line
537
538
539 def test_stale_paragraph_shows_files_with_license_tag():
540 report = DeltaReport()
541 report.add(_stale_paragraph("Files: legacy/*\nLicense: GPL-2+\n"))
542 text = _explain(report)
543 # The license rides as a tag on the Files line; no separate License: line.
544 assert "Files: legacy/* [License: GPL-2+]" in text
545 assert not any(line.strip().startswith("License:") for line in text.splitlines())
546
547
548 def test_stale_paragraph_names_deleted_culprits():
549 report = DeltaReport()
550 report.add(
551 _stale_paragraph(
552 "Files: legacy/*\nLicense: GPL-2+\n", deleted_files=("legacy/old.c",)
553 )
554 )
555 text = _explain(report)
556 assert "Files: legacy/* [License: GPL-2+]" in text
557 assert " legacy/old.c (deleted)" in text
558
559
560 def test_stale_paragraph_without_deletions_has_no_provenance_line():
561 report = DeltaReport()
562 report.add(_stale_paragraph("Files: legacy/*\nLicense: GPL-2+\n"))
563 text = _explain(report)
564 assert "(deleted)" not in text
565
566
567 def test_stale_paragraph_lists_each_culprit_on_its_own_line():
568 report = DeltaReport()
569 report.add(
570 _stale_paragraph(
571 "Files: legacy/*\n", deleted_files=("legacy/a.c", "legacy/b.c")
572 )
573 )
574 text = _explain(report)
575 assert " legacy/a.c (deleted)" in text
576 assert " legacy/b.c (deleted)" in text
577
578
579 def test_stale_license_tags_honour_spdx():
580 report = DeltaReport()
581 report.add(_stale_pattern("legacy/old_*.c", "Files: legacy/*\n", "GPL-2+"))
582 report.add(_stale_paragraph("Files: gone/*\n", "GPL-2+"))
583 text = _explain(report, ["--spdx"])
584 assert "[License: GPL-2.0-or-later]" in text
585 assert "[License: GPL-2+]" not in text
586
587
588 def test_deterministic_across_calls():
589 report = DeltaReport()
590 report.add(_new_paragraph("Expat"))
591 report.add(_relicensed("src/util.c", SUB_LICENSE, ("GPL-2+", "Apache-2.0")))
592 report.add(_stale_paragraph())
593 report.add(_stale_pattern("legacy/old_*.c"))
594 renderer = ExplainRenderer()
595 options = _options([])
596 assert renderer.explain(report, options) == renderer.explain(report, options)