Debian Codegraph @

tests/license_texts_data_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 """Tests for the shipped license-text data (index snapshot, headers, texts)."""
18
19 from __future__ import annotations
20
21 import importlib.resources
22 import re
23
24 from decopy.licensing.data.common_licenses import COMMON_LICENSES
25 from decopy.licensing.data.loader import common_licenses_index
26 from decopy.licensing.spdx import canonical_license_name, exception_to_spdx
27
28 _COPYRIGHT_LINE = re.compile(
29 r"copyright\b.*?(\(c\)|©|reserved|\b\d{4}\b|\[|<)", re.IGNORECASE
30 )
31 # A line starting a copyright notice; and a leading heading line (the licence's
32 # own name), recognised structurally as a short non-sentence line - mirrors
33 # emit-license-data.clean_license.
34 _COPYRIGHT_START = re.compile(r"^copyright\b", re.IGNORECASE)
35 _TITLE_MAX_LEN = 55
36
37
38 def _is_title_line(line: str) -> bool:
39 stripped = line.strip()
40 return 0 < len(stripped) <= _TITLE_MAX_LEN and not stripped.endswith(".")
41
42
43 # A whole-line angle-bracketed instruction ("<one line to give ...>"); a bare
44 # <url> has no internal space and is not one.
45 _INSTRUCTION_LINE = re.compile(r"^<[^>]*\s[^>]*>$")
46 # The same "how to apply" instruction without the angle brackets, as the
47 # LGPL headers write it ("one line to give the library's name ...").
48 _INSTRUCTION_PHRASE = re.compile(r"^<?one line to give\b", re.IGNORECASE)
49 _TEMPLATE_BLANK = re.compile(r"_{3,}")
50 _RESERVED_LINE = re.compile(r"^all rights reserved\.?$", re.IGNORECASE)
51
52
53 def _header_text(name: str) -> str:
54 return (
55 importlib.resources.files("decopy.licensing.data")
56 .joinpath("headers", f"{name}.txt")
57 .read_text(encoding="utf-8")
58 )
59
60
61 def _header_files():
62 headers = importlib.resources.files("decopy.licensing.data").joinpath("headers")
63 return sorted(
64 (p for p in headers.iterdir() if p.name.endswith(".txt")), key=lambda p: p.name
65 )
66
67
68 def _license_text(name: str) -> str:
69 return (
70 importlib.resources.files("decopy.licensing.data")
71 .joinpath("licenses", f"{name}.txt")
72 .read_text(encoding="utf-8")
73 )
74
75
76 def _license_files():
77 lic = importlib.resources.files("decopy.licensing.data").joinpath("licenses")
78 return sorted(
79 (p for p in lic.iterdir() if p.name.endswith(".txt")), key=lambda p: p.name
80 )
81
82
83 def _exception_files():
84 exc = importlib.resources.files("decopy.licensing.data").joinpath("exceptions")
85 return sorted(
86 (p for p in exc.iterdir() if p.name.endswith(".txt")), key=lambda p: p.name
87 )
88
89
90 def test_index_snapshot_nonempty():
91 index = common_licenses_index()
92 assert index
93 assert {"GPL-2", "LGPL-2.1", "Apache-2.0"} <= index
94
95
96 def test_index_excludes_symlinks():
97 # The GPL/LGPL/GFDL entries in /usr/share/common-licenses are symlinks to a
98 # default version; the snapshot lists explicit versions only.
99 index = common_licenses_index()
100 assert {"GPL", "LGPL", "GFDL"}.isdisjoint(index)
101
102
103 def test_headers_present_for_copyleft():
104 # The +/only distinction lives in the header, so the variants must differ.
105 gpl2 = _header_text("GPL-2")
106 gpl2_plus = _header_text("GPL-2+")
107 assert gpl2.strip()
108 assert gpl2_plus.strip()
109 assert gpl2 != gpl2_plus
110
111
112 def test_gfdl_niv_header_kept_without_its_copyright_prefix():
113 # GFDL packs the copyright notice and the grant on one line; the notice
114 # prefix is stripped but the grant is kept (the whole line is not dropped).
115 text = _header_text("GFDL-NIV-1.3")
116 assert "Permission is granted" in text
117 assert not _COPYRIGHT_LINE.search(text)
118
119
120 def test_only_gfdl_no_invariants_headers_exist():
121 # The invariants-allowing GFDL notice carries unfillable "LIST ..."
122 # placeholders, so only the GFDL-NIV (no-invariants) variant gets a header.
123 gfdl = [f.name for f in _header_files() if f.name.startswith("GFDL")]
124 assert gfdl # some GFDL-NIV headers exist
125 assert all(name.startswith("GFDL-NIV-") for name in gfdl), gfdl
126 for f in _header_files():
127 assert "Invariant Sections being LIST" not in f.read_text(encoding="utf-8")
128
129
130 def test_headers_have_no_copyright_lines():
131 # The holder belongs in the Copyright field; a header must not carry a
132 # copyright/template notice ("Copyright (C) <year> <name>", "Portions ...").
133 for f in _header_files():
134 for line in f.read_text(encoding="utf-8").splitlines():
135 assert not _COPYRIGHT_LINE.search(line.strip()), (f.name, line)
136
137
138 def test_headers_have_no_instruction_placeholders():
139 for f in _header_files():
140 for line in f.read_text(encoding="utf-8").splitlines():
141 stripped = line.strip()
142 assert not _INSTRUCTION_LINE.match(stripped), (f.name, line)
143 assert not _INSTRUCTION_PHRASE.match(stripped), (f.name, line)
144
145
146 def test_headers_have_no_template_blanks():
147 for f in _header_files():
148 for line in f.read_text(encoding="utf-8").splitlines():
149 assert not _TEMPLATE_BLANK.search(line), (f.name, line)
150
151
152 def test_headers_have_no_all_rights_reserved_line():
153 for f in _header_files():
154 for line in f.read_text(encoding="utf-8").splitlines():
155 assert not _RESERVED_LINE.match(line.strip()), (f.name, line)
156
157
158 def test_headers_not_quote_wrapped():
159 for f in _header_files():
160 text = f.read_text(encoding="utf-8").strip()
161 assert text[:1] not in {'"', "'"}, (f.name, text[:40])
162 assert text[-1:] not in {'"', "'"}, (f.name, text[-40:])
163
164
165 def test_headers_non_empty():
166 for f in _header_files():
167 assert f.read_text(encoding="utf-8").strip(), f.name
168
169
170 def test_seed_embedded_present():
171 # The short/popular licences embedded as full verbatim text.
172 for name in ("Expat", "BSD-2-clause", "BSD-3-clause", "ISC", "Zlib", "X11"):
173 assert _license_text(name).strip(), name
174
175
176 def _exception_text(label: str) -> str:
177 return (
178 importlib.resources.files("decopy.licensing.data")
179 .joinpath("exceptions", f"{label}.txt")
180 .read_text(encoding="utf-8")
181 )
182
183
184 _EMBEDDED_EXCEPTIONS = (
185 "AutoConf",
186 "AutoConf-3",
187 "AutoConf-macro",
188 "Bison",
189 "Bootloader",
190 "ClassPath",
191 "Font",
192 "GCC",
193 "GCC-3.1",
194 "LLVM",
195 "Linux-syscall",
196 "OCaml-LGPL",
197 "PCRE2",
198 "Qt-GPL-1",
199 "Qt-LGPL-1.1",
200 )
201
202
203 def test_seed_exception_present():
204 # The curated exceptions embedded as full verbatim text (GPL-family seed
205 # plus the ranking-flagged additions).
206 for label in _EMBEDDED_EXCEPTIONS:
207 assert _exception_text(label).strip(), label
208
209
210 def _stem(path) -> str:
211 return path.name[: -len(".txt")]
212
213
214 def test_embedded_license_stems_canonical_and_not_common():
215 # The ladder is unambiguous: an embedded stem is a canonical DEP-5 name
216 # (so a detected licence keys straight to it) and is not also a
217 # common-licenses reference (which would win over the embedded text).
218 for f in _license_files():
219 stem = _stem(f)
220 assert canonical_license_name(stem) == stem, f.name
221 assert stem not in COMMON_LICENSES, f.name
222
223
224 def test_embedded_exception_stems_are_known_labels():
225 # Every shipped exception file names a DEP-5 label decopy can emit.
226 for f in _exception_files():
227 assert exception_to_spdx(_stem(f)) is not None, f.name
228
229
230 def test_header_stems_canonical_or_plus_variant():
231 # A header is keyed by the full variant name; the base (without a trailing
232 # "+") must be a canonical DEP-5 name.
233 for f in _header_files():
234 base = _stem(f).removesuffix("+")
235 assert canonical_license_name(base) == base, f.name
236
237
238 def test_embedded_keeps_license_terms():
239 # The substantive terms are kept verbatim; only title/copyright are dropped.
240 assert "Permission is hereby granted, free of charge" in _license_text("Expat")
241
242
243 def test_embedded_have_no_copyright_lines():
244 # The holder belongs in the Copyright field, not the shared licence text.
245 for f in _license_files():
246 for line in f.read_text(encoding="utf-8").splitlines():
247 assert not _COPYRIGHT_START.match(line.strip()), (f.name, line)
248
249
250 def test_embedded_have_no_title_line():
251 # The License field already names the licence; a leading heading (e.g.
252 # "MIT No Attribution", "UNICODE LICENSE V3") is redundant and must not
253 # lead the embedded text.
254 for f in _license_files():
255 lines = [ln for ln in f.read_text(encoding="utf-8").splitlines() if ln.strip()]
256 assert lines, f.name
257 assert not _is_title_line(lines[0]), (f.name, lines[0])
258
259
260 def test_embedded_have_no_reserved_line():
261 # A standalone "All rights reserved." is clerical noise, not licence terms.
262 for f in _license_files():
263 for line in f.read_text(encoding="utf-8").splitlines():
264 assert not _RESERVED_LINE.match(line.strip()), (f.name, line)