Debian Codegraph @

tests/spdx_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 licence in tests/testdata/licenses/.
18
19 For each licence the loop is
20
21 SPDX id --replace_spdx--> DEP-5 name --dep5_to_spdx--> SPDX id'
22
23 and ``spdx-name-roundtrip.tsv`` locks ``(DEP-5 name, SPDX id')`` for all 729
24 ids. This proves both conversion directions are covered and agree: a licence
25 with no row fails, a row with no licence fails, and any changed conversion
26 fails. A ``wrong:`` row still asserts its *recorded* values, so a known
27 asymmetry stays locked (and visible) until fixed rather than silently drifting.
28
29 Regenerate with ``scripts/audit-spdx-roundtrip.py``.
30 """
31
32 import unittest
33 from pathlib import Path
34
35 from decopy.licensing import spdx
36
37 LICENSES_DIR = Path("tests/testdata/licenses")
38 ROUNDTRIP_TSV = Path("tests/testdata/spdx-name-roundtrip.tsv")
39 _DEP5_COL, _BACK_COL, _STATUS_COL = 1, 2, 3
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 table[cols[0]] = (cols[_DEP5_COL], cols[_BACK_COL])
49 return table
50
51
52 class TestSpdxNameRoundtrip(unittest.TestCase):
53 table: dict[str, tuple[str, str]]
54 files: list[Path]
55
56 @classmethod
57 def setUpClass(cls):
58 cls.table = _load_table()
59 cls.files = sorted(LICENSES_DIR.glob("*.json"))
60
61 def test_every_licence_roundtrips(self):
62 for path in self.files:
63 spdx_id = path.stem
64 with self.subTest(licence=spdx_id):
65 self.assertIn(
66 spdx_id,
67 self.table,
68 msg="missing from spdx-name-roundtrip.tsv; "
69 "run scripts/audit-spdx-roundtrip.py",
70 )
71 dep5_name, back = self.table[spdx_id]
72 self.assertEqual(spdx.replace_spdx(spdx_id), dep5_name)
73 self.assertEqual(spdx.dep5_to_spdx(dep5_name), back)
74
75 def test_no_stale_table_rows(self):
76 present = {path.stem for path in self.files}
77 stale = sorted(set(self.table) - present)
78 self.assertEqual(stale, [], msg="rows with no licence file")
79
80 def test_no_unreviewed_wrong_rows(self):
81 """Every licence round-trips: no bare 'wrong' status remains.
82
83 A genuinely non-invertible row would carry an explicit
84 'wrong:<reason>' annotation; a bare 'wrong' is an unfixed --spdx
85 conversion asymmetry (a new SPDX data drop or a regression).
86 """
87 bare_wrong = []
88 for line in ROUNDTRIP_TSV.read_text().splitlines():
89 if line.startswith("#") or not line.strip():
90 continue
91 cols = line.split("\t")
92 if len(cols) > _STATUS_COL and cols[_STATUS_COL] == "wrong":
93 bare_wrong.append(cols[0])
94 self.assertEqual(bare_wrong, [], msg="unfixed --spdx asymmetries")
95
96
97 if __name__ == "__main__":
98 unittest.main()