Debian Codegraph @

tests/spdx_exception_roundtrip_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 """Full-loop coverage over every SPDX exception in tests/testdata/exceptions/.
18
19 For each exception the loop is
20
21 SPDX id --exception_to_dep5--> DEP-5 label --exception_to_spdx--> id'
22
23 and ``spdx-exception-roundtrip.tsv`` locks ``(label, id')`` for all 84 ids. An
24 exception with no row fails and a row with no exception fails, so a refreshed
25 SPDX exception set cannot slip coverage. Many-to-one collapses (the -generic /
26 -note / -short / dated variants share one label) and the single ``unmapped``
27 gap are recorded in the table rather than failing.
28
29 Regenerate with ``scripts/audit-spdx-roundtrip.py --exceptions``.
30 """
31
32 import unittest
33 from pathlib import Path
34
35 from decopy.licensing import spdx
36
37 EXCEPTIONS_DIR = Path("tests/testdata/exceptions")
38 ROUNDTRIP_TSV = Path("tests/testdata/spdx-exception-roundtrip.tsv")
39 _LABEL_COL, _BACK_COL = 1, 2
40
41
42 def _load_table() -> dict[str, tuple[str, str]]:
43 table: dict[str, tuple[str, str]] = {}
44 for line in ROUNDTRIP_TSV.read_text().splitlines():
45 if line.startswith("#") or not line.strip():
46 continue
47 cols = line.split("\t")
48 label = cols[_LABEL_COL] if len(cols) > _LABEL_COL else ""
49 back = cols[_BACK_COL] if len(cols) > _BACK_COL else ""
50 table[cols[0]] = (label, back)
51 return table
52
53
54 class TestSpdxExceptionRoundtrip(unittest.TestCase):
55 table: dict[str, tuple[str, str]]
56 files: list[Path]
57
58 @classmethod
59 def setUpClass(cls):
60 cls.table = _load_table()
61 cls.files = sorted(EXCEPTIONS_DIR.glob("*.json"))
62
63 def test_every_exception_roundtrips(self):
64 for path in self.files:
65 spdx_id = path.stem
66 with self.subTest(exception=spdx_id):
67 self.assertIn(
68 spdx_id,
69 self.table,
70 msg="missing from spdx-exception-roundtrip.tsv; "
71 "run scripts/audit-spdx-roundtrip.py --exceptions",
72 )
73 label, back = self.table[spdx_id]
74 # An empty label is the recorded "unmapped" gap.
75 self.assertEqual(spdx.exception_to_dep5(spdx_id) or "", label)
76 if label:
77 self.assertEqual(spdx.exception_to_spdx(label), back)
78
79 def test_no_stale_table_rows(self):
80 present = {path.stem for path in self.files}
81 stale = sorted(set(self.table) - present)
82 self.assertEqual(stale, [], msg="rows with no exception file")
83
84
85 if __name__ == "__main__":
86 unittest.main()