Debian Codegraph @

tests/spdx_test.py

raw
1 #!/usr/bin/env python3
2 # Copyright: 2016, 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 import io
18 import sys
19 import typing
20 import unittest
21 from unittest.mock import patch
22
23 from decopy import cmdoptions
24 from decopy.common import core_types
25 from decopy.common.core_types import Options
26 from decopy.decopy import run_pipeline
27 from decopy.dep5doc import dep5
28 from decopy.dep5doc.renderer import Dep5Renderer
29 from decopy.fstree import nodes
30 from decopy.grouping.group import TallyTracker
31 from decopy.licensing.spdx import dep5_to_spdx
32
33
34 class TestDep5ToSpdx(unittest.TestCase):
35 # --- Name aliases ---
36
37 def test_mit_x11_alias(self):
38 self.assertEqual(dep5_to_spdx("MIT/X11"), "MIT")
39
40 def test_expat_alias(self):
41 # Expat is Debian's DEP-5 name for the MIT licence
42 self.assertEqual(dep5_to_spdx("Expat"), "MIT")
43
44 def test_cc0_alias(self):
45 self.assertEqual(dep5_to_spdx("CC0"), "CC0-1.0")
46
47 def test_reverse_aliases_exact(self):
48 # decopy renames these SPDX ids to a DEP-5 name on read; --spdx must
49 # restore the SPDX id rather than emit decopy's name verbatim. Exact
50 # 1:1 renames with no version component. (bzip2/SMLNJ are many-to-one;
51 # the canonical/current SPDX id is the chosen representative.)
52 cases = {
53 "MIT-like-icu": "ICU",
54 "MIT-Imlib2": "Imlib2",
55 "MIT-like-SMLNJ": "SMLNJ",
56 "BSD-like-Entessa": "Entessa",
57 "BSD-like-Mup": "Mup",
58 "BSD-like-Saxpath": "Saxpath",
59 "BSD-like-TMate": "TMate",
60 "BSD-like-TOSL": "TOSL",
61 "BSD-like-bzip2": "bzip2-1.0.6",
62 "mpich": "mpich2",
63 "BSD-2-clause-AMPAS": "AMPAS",
64 }
65 for dep5_name, spdx_id in cases.items():
66 with self.subTest(name=dep5_name):
67 self.assertEqual(dep5_to_spdx(dep5_name), spdx_id)
68
69 def test_reverse_aliases_per_licence(self):
70 # Per-licence reverses with no shared pattern: multi-component .0.0,
71 # a version before a non-version suffix, and mid-string renames. Each
72 # DEP-5 name is unique to one SPDX id, so an exact alias is safe.
73 cases = {
74 "Artistic-1-Perl": "Artistic-1.0-Perl",
75 "Artistic-1-cl8": "Artistic-1.0-cl8",
76 "BSD-3-clause-no-nuclear": "BSD-3-Clause-No-Nuclear-License",
77 "BSD-3-clause-no-nuclear-2014": "BSD-3-Clause-No-Nuclear-License-2014",
78 "BlueOak-1.0": "BlueOak-1.0.0",
79 "CAL-1-Combined-Work-Exception": "CAL-1.0-Combined-Work-Exception",
80 "FSL-1.1-Expat": "FSL-1.1-MIT",
81 "LiLiQ-R+-1.1": "LiLiQ-Rplus-1.1",
82 "MPL-2-no-copyleft-exception": "MPL-2.0-no-copyleft-exception",
83 "OFL-1-RFN": "OFL-1.0-RFN",
84 "OFL-1-no-RFN": "OFL-1.0-no-RFN",
85 "Parity-6.0": "Parity-6.0.0",
86 "Parity-7.0": "Parity-7.0.0",
87 "PolyForm-Noncommercial-1.0": "PolyForm-Noncommercial-1.0.0",
88 "PolyForm-Small-Business-1.0": "PolyForm-Small-Business-1.0.0",
89 "QPL-1-INRIA-2004": "QPL-1.0-INRIA-2004",
90 "RSCPL-1": "RSCPL",
91 "copyleft-next-0.3": "copyleft-next-0.3.0",
92 }
93 for dep5_name, spdx_id in cases.items():
94 with self.subTest(name=dep5_name):
95 self.assertEqual(dep5_to_spdx(dep5_name), spdx_id)
96
97 def test_reverse_aliases_versioned(self):
98 # Prefix renames whose version must survive: the family prefix reverts
99 # and the existing .0 restoration then runs (Academic-2 -> AFL-2 ->
100 # AFL-2.0; CeCILL-1 -> CECILL-1 -> CECILL-1.0).
101 cases = {
102 "Academic-1.1": "AFL-1.1",
103 "Academic-2": "AFL-2.0",
104 "Academic-2.1": "AFL-2.1",
105 "Academic-3": "AFL-3.0",
106 "CeCILL-1": "CECILL-1.0",
107 "CeCILL-1.1": "CECILL-1.1",
108 "CeCILL-2": "CECILL-2.0",
109 "CeCILL-2.1": "CECILL-2.1",
110 "CeCILL-B": "CECILL-B",
111 "CeCILL-C": "CECILL-C",
112 }
113 for dep5_name, spdx_id in cases.items():
114 with self.subTest(name=dep5_name):
115 self.assertEqual(dep5_to_spdx(dep5_name), spdx_id)
116
117 # --- Non-copyleft version restoration (bare integer → add .0) ---
118
119 def test_apache_version_restoration(self):
120 self.assertEqual(dep5_to_spdx("Apache-2"), "Apache-2.0")
121 # Apache-1.1 already has a minor version - no .0 appended
122 self.assertEqual(dep5_to_spdx("Apache-1.1"), "Apache-1.1")
123
124 def test_mpl_version_restoration(self):
125 self.assertEqual(dep5_to_spdx("MPL-2"), "MPL-2.0")
126 self.assertEqual(dep5_to_spdx("MPL-1"), "MPL-1.0")
127
128 def test_epl_version_restoration(self):
129 self.assertEqual(dep5_to_spdx("EPL-1"), "EPL-1.0")
130 self.assertEqual(dep5_to_spdx("EPL-2"), "EPL-2.0")
131
132 def test_cddl_version_restoration(self):
133 self.assertEqual(dep5_to_spdx("CDDL-1"), "CDDL-1.0")
134
135 def test_artistic_version_restoration(self):
136 self.assertEqual(dep5_to_spdx("Artistic-1"), "Artistic-1.0")
137 self.assertEqual(dep5_to_spdx("Artistic-2"), "Artistic-2.0")
138
139 def test_ofl_version_restoration(self):
140 self.assertEqual(dep5_to_spdx("OFL-1"), "OFL-1.0")
141 # OFL-1.1 already has minor version - unchanged
142 self.assertEqual(dep5_to_spdx("OFL-1.1"), "OFL-1.1")
143
144 def test_eupl_version_restoration(self):
145 # EUPL uses -only/-or-later in practice but SPDX does not - treat as non-copyleft # noqa: E501
146 self.assertEqual(dep5_to_spdx("EUPL-1"), "EUPL-1.0")
147 self.assertEqual(dep5_to_spdx("EUPL-1.1"), "EUPL-1.1")
148
149 # --- Trailing number that is a year/date/zero is NOT a version: no .0 ---
150
151 def test_year_suffix_not_restored(self):
152 # A 4-digit year, an 8-digit date, a 2-digit year and a bare 0 are not
153 # versions, so dep5_to_spdx must not append .0 (which would yield an
154 # invalid SPDX id like Adobe-2006.0). Only a single-digit major version
155 # is restored.
156 for name in (
157 "Adobe-2006",
158 "Arphic-1999",
159 "HP-1986",
160 "NAIST-2003",
161 "Sun-PPP-2000",
162 "Unicode-DFS-2016",
163 "W3C-19980720",
164 "Spencer-86",
165 "MIT-0",
166 "NTP-0",
167 ):
168 with self.subTest(name=name):
169 self.assertEqual(dep5_to_spdx(name), name)
170 # ... while a genuine single-digit major version still gains its .0.
171 self.assertEqual(dep5_to_spdx("Apache-2"), "Apache-2.0")
172 self.assertEqual(dep5_to_spdx("MPL-2"), "MPL-2.0")
173
174 # --- Copyleft -only (GPL / LGPL / AGPL / GFDL) ---
175
176 def test_gpl_only(self):
177 self.assertEqual(dep5_to_spdx("GPL-1"), "GPL-1.0-only")
178 self.assertEqual(dep5_to_spdx("GPL-2"), "GPL-2.0-only")
179 self.assertEqual(dep5_to_spdx("GPL-3"), "GPL-3.0-only")
180
181 def test_lgpl_only(self):
182 self.assertEqual(dep5_to_spdx("LGPL-2"), "LGPL-2.0-only")
183 # LGPL-2.1 already has a minor version - just add -only
184 self.assertEqual(dep5_to_spdx("LGPL-2.1"), "LGPL-2.1-only")
185 self.assertEqual(dep5_to_spdx("LGPL-3"), "LGPL-3.0-only")
186
187 def test_agpl_only(self):
188 self.assertEqual(dep5_to_spdx("AGPL-3"), "AGPL-3.0-only")
189 self.assertEqual(dep5_to_spdx("AGPL-1"), "AGPL-1.0-only")
190
191 def test_gfdl_only(self):
192 self.assertEqual(dep5_to_spdx("GFDL-1.3"), "GFDL-1.3-only")
193 self.assertEqual(dep5_to_spdx("GFDL-1.2"), "GFDL-1.2-only")
194
195 # --- Copyleft -or-later (GPL / LGPL / AGPL / GFDL with + suffix) ---
196
197 def test_gpl_or_later(self):
198 self.assertEqual(dep5_to_spdx("GPL-1+"), "GPL-1.0-or-later")
199 self.assertEqual(dep5_to_spdx("GPL-2+"), "GPL-2.0-or-later")
200 self.assertEqual(dep5_to_spdx("GPL-3+"), "GPL-3.0-or-later")
201
202 def test_lgpl_or_later(self):
203 self.assertEqual(dep5_to_spdx("LGPL-2+"), "LGPL-2.0-or-later")
204 self.assertEqual(dep5_to_spdx("LGPL-2.1+"), "LGPL-2.1-or-later")
205 self.assertEqual(dep5_to_spdx("LGPL-3+"), "LGPL-3.0-or-later")
206
207 def test_agpl_or_later(self):
208 self.assertEqual(dep5_to_spdx("AGPL-3+"), "AGPL-3.0-or-later")
209
210 def test_gfdl_or_later(self):
211 self.assertEqual(dep5_to_spdx("GFDL-1.2+"), "GFDL-1.2-or-later")
212 self.assertEqual(dep5_to_spdx("GFDL-1.3+"), "GFDL-1.3-or-later")
213
214 # --- BSD clause capitalisation ---
215
216 def test_bsd_2_clause(self):
217 self.assertEqual(dep5_to_spdx("BSD-2-clause"), "BSD-2-Clause")
218
219 def test_bsd_3_clause(self):
220 self.assertEqual(dep5_to_spdx("BSD-3-clause"), "BSD-3-Clause")
221
222 def test_bsd_clause_with_suffix(self):
223 # A suffix with no exact alias still gets generic Clause capitalisation.
224 # (no-nuclear now has an exact alias -- see test_reverse_aliases_per_licence.)
225 self.assertEqual(dep5_to_spdx("BSD-2-clause-FreeBSD"), "BSD-2-Clause-FreeBSD")
226
227 def test_suffix_word_recapitalisation(self):
228 # The read path lower-cases Clause/Attribution/Protection/Source-Code;
229 # --spdx must restore exactly those words (and not neighbouring words
230 # SPDX keeps lower-case: disclaimer, acknowledgment, no-conversion).
231 cases = {
232 "BSD-3-clause-attribution": "BSD-3-Clause-Attribution",
233 "BSD-attribution-HPND-disclaimer": "BSD-Attribution-HPND-disclaimer",
234 "BSD-protection": "BSD-Protection",
235 "BSD-source-code": "BSD-Source-Code",
236 "Brian-Gladman-2-clause": "Brian-Gladman-2-Clause",
237 "Brian-Gladman-3-clause": "Brian-Gladman-3-Clause",
238 "Brian-Gladman-3-clause-no-conversion": (
239 "Brian-Gladman-3-Clause-no-conversion"
240 ),
241 "DEC-3-clause": "DEC-3-Clause",
242 "Mackerras-3-clause": "Mackerras-3-Clause",
243 "Mackerras-3-clause-acknowledgment": "Mackerras-3-Clause-acknowledgment",
244 }
245 for dep5_name, spdx_id in cases.items():
246 with self.subTest(name=dep5_name):
247 self.assertEqual(dep5_to_spdx(dep5_name), spdx_id)
248
249 def test_gfdl_invariants(self):
250 # The -invariants / -no-invariants suffix stops the copyleft rule from
251 # re-adding -only / -or-later, so a dedicated rule must (the read path
252 # turned -only into "" and -or-later into "+").
253 cases = {}
254 for ver in ("1.1", "1.2", "1.3"):
255 for inv in ("invariants", "no-invariants"):
256 cases[f"GFDL-{ver}-{inv}"] = f"GFDL-{ver}-{inv}-only"
257 cases[f"GFDL-{ver}-{inv}+"] = f"GFDL-{ver}-{inv}-or-later"
258 for dep5_name, spdx_id in cases.items():
259 with self.subTest(name=dep5_name):
260 self.assertEqual(dep5_to_spdx(dep5_name), spdx_id)
261 # A plain GFDL (no invariants suffix) still goes through the copyleft rule.
262 self.assertEqual(dep5_to_spdx("GFDL-1.3"), "GFDL-1.3-only")
263 self.assertEqual(dep5_to_spdx("GFDL-1.2+"), "GFDL-1.2-or-later")
264
265 def test_cc_by_jurisdiction_ports(self):
266 # A jurisdiction port keeps its 2-3 letter suffix, so the major's .0 was
267 # stripped mid-string on read; --spdx must restore it (CC-BY-3-AT ->
268 # CC-BY-3.0-AT) for every BY / NC / SA / ND modifier combination.
269 cases = {
270 "CC-BY-3-AT": "CC-BY-3.0-AT",
271 "CC-BY-3-IGO": "CC-BY-3.0-IGO",
272 "CC-BY-NC-3-DE": "CC-BY-NC-3.0-DE",
273 "CC-BY-NC-SA-2-DE": "CC-BY-NC-SA-2.0-DE",
274 "CC-BY-NC-SA-2-UK": "CC-BY-NC-SA-2.0-UK",
275 "CC-BY-NC-ND-3-IGO": "CC-BY-NC-ND-3.0-IGO",
276 "CC-BY-SA-2-UK": "CC-BY-SA-2.0-UK",
277 "CC-BY-ND-3-DE": "CC-BY-ND-3.0-DE",
278 }
279 for dep5_name, spdx_id in cases.items():
280 with self.subTest(name=dep5_name):
281 self.assertEqual(dep5_to_spdx(dep5_name), spdx_id)
282 # A non-port CC-BY (no jurisdiction suffix) still gains .0 the usual way.
283 self.assertEqual(dep5_to_spdx("CC-BY-4"), "CC-BY-4.0")
284 self.assertEqual(dep5_to_spdx("CC-BY-SA-4"), "CC-BY-SA-4.0")
285
286 # --- Pass-through: already SPDX-compatible or no mapping available ---
287
288 def test_passthrough_common(self):
289 for name in (
290 "ISC",
291 "Zlib",
292 "Unlicense",
293 "X11",
294 "WTFPL",
295 "Libpng",
296 "JSON",
297 "curl",
298 ):
299 with self.subTest(name=name):
300 self.assertEqual(dep5_to_spdx(name), name)
301
302 def test_passthrough_exceptions_and_custom(self):
303 # Exception labels with no SPDX mapping pass through unchanged (the
304 # uppercase "QT-1.x" forms are not decopy's labels - Qt1.1 etc. are).
305 self.assertEqual(
306 dep5_to_spdx("GPL-2+3 with QT-1.3 exception"),
307 "GPL-2+3 with QT-1.3 exception",
308 )
309 self.assertEqual(
310 dep5_to_spdx("LGPL-2.1 with QT-1.1 exception"),
311 "LGPL-2.1 with QT-1.1 exception",
312 )
313
314 def test_passthrough_unknown(self):
315 self.assertEqual(dep5_to_spdx("Unknown"), "Unknown")
316
317 # --- Compound DEP-5 'A or B' → SPDX 'A OR B' ---
318
319 def test_compound_two_gpl(self):
320 self.assertEqual(dep5_to_spdx("GPL-2 or GPL-3"), "GPL-2.0-only OR GPL-3.0-only")
321
322 def test_compound_gpl_or_later_and_apache(self):
323 self.assertEqual(
324 dep5_to_spdx("GPL-2+ or Apache-2"), "GPL-2.0-or-later OR Apache-2.0"
325 )
326
327 def test_compound_with_alias(self):
328 self.assertEqual(dep5_to_spdx("Apache-2 or MIT/X11"), "Apache-2.0 OR MIT")
329
330 def test_compound_three_way(self):
331 self.assertEqual(
332 dep5_to_spdx("GPL-2 or GPL-3 or Apache-2"),
333 "GPL-2.0-only OR GPL-3.0-only OR Apache-2.0",
334 )
335
336
337 class TestSpdxOption(unittest.TestCase):
338 """Tests for the --spdx / --no-spdx CLI option and Options.spdx field."""
339
340 _BASE_ARGS: typing.ClassVar[list[str]] = ["--no-progress", "--root", "."]
341
342 def test_spdx_defaults_to_false(self):
343 opts = cmdoptions.process_options(self._BASE_ARGS)
344 self.assertFalse(opts.spdx)
345
346 def test_spdx_flag_enables_spdx(self):
347 opts = cmdoptions.process_options([*self._BASE_ARGS, "--spdx"])
348 self.assertTrue(opts.spdx)
349
350 def test_no_spdx_flag_disables_spdx(self):
351 opts = cmdoptions.process_options([*self._BASE_ARGS, "--no-spdx"])
352 self.assertFalse(opts.spdx)
353
354 def test_spdx_last_flag_wins(self):
355 opts = cmdoptions.process_options([*self._BASE_ARGS, "--spdx", "--no-spdx"])
356 self.assertFalse(opts.spdx)
357
358 opts = cmdoptions.process_options([*self._BASE_ARGS, "--no-spdx", "--spdx"])
359 self.assertTrue(opts.spdx)
360
361 def test_options_dataclass_has_spdx_field(self):
362 # Verify the field exists and defaults to False when not supplied
363 opts = cmdoptions.process_options(self._BASE_ARGS)
364 self.assertIsInstance(opts, Options)
365 self.assertIs(type(opts.spdx), bool)
366
367
368 class TestCopyrightBlockSpdx(unittest.TestCase):
369 """Unit tests for Dep5Renderer.file_block() with options.spdx."""
370
371 def _opts(self, *, spdx: bool):
372 flag = "--spdx" if spdx else "--no-spdx"
373 return cmdoptions.process_options(["--no-progress", flag])
374
375 def _block(self, group, *, spdx: bool = False):
376 opts = self._opts(spdx=spdx)
377 if opts.glob:
378 file_patterns = group.files.get_patterns(TallyTracker())
379 else:
380 file_patterns = group.files.sorted_members()
381 return Dep5Renderer().file_block(group, opts, file_patterns)
382
383 def _group(self, filename: str, *license_names: str):
384 group = dep5.Group()
385 group.files.add_file(nodes.FileInfo(parent=None, name=filename))
386 for name in license_names:
387 group.licenses[name] = core_types.License(name)
388 return group
389
390 def test_no_options_leaves_dep5_name(self):
391 # options=None (default) must not change anything
392 group = self._group("src/foo.c", "GPL-2")
393 block = self._block(group, spdx=False)
394 self.assertIn("License: GPL-2\n", block + "\n")
395 self.assertNotIn("GPL-2.0-only", block)
396
397 def test_no_spdx_option_leaves_dep5_name(self):
398 group = self._group("src/foo.c", "GPL-2")
399 block = self._block(group, spdx=False)
400 self.assertIn("License: GPL-2\n", block + "\n")
401 self.assertNotIn("GPL-2.0-only", block)
402
403 def test_spdx_converts_gpl_only(self):
404 group = self._group("src/foo.c", "GPL-2")
405 block = self._block(group, spdx=True)
406 self.assertIn("License: GPL-2.0-only", block)
407
408 def test_spdx_converts_gpl_or_later(self):
409 group = self._group("src/foo.c", "GPL-2+")
410 block = self._block(group, spdx=True)
411 self.assertIn("License: GPL-2.0-or-later", block)
412
413 def test_spdx_converts_apache(self):
414 group = self._group("src/foo.c", "Apache-2")
415 block = self._block(group, spdx=True)
416 self.assertIn("License: Apache-2.0", block)
417
418 def test_spdx_converts_mit_x11(self):
419 group = self._group("src/foo.c", "MIT/X11")
420 block = self._block(group, spdx=True)
421 self.assertIn("License: MIT", block)
422
423 def test_spdx_compound_or_becomes_uppercase_or(self):
424 # "Apache-2 or GPL-2" (sorted) → "Apache-2.0 OR GPL-2.0-only"
425 group = self._group("src/foo.c", "GPL-2", "Apache-2")
426 block = self._block(group, spdx=True)
427 self.assertIn("License: Apache-2.0 OR GPL-2.0-only", block)
428
429
430 class TestOutputSpdx(unittest.TestCase):
431 """Integration tests: full pipeline output with/without --spdx."""
432
433 def _run(self, path: str, extra_flags: list[str] | None = None) -> str:
434 if extra_flags is None:
435 extra_flags = []
436 flags = ["--no-progress", "--root", path, *extra_flags]
437 options = cmdoptions.process_options(flags)
438 buf = io.StringIO()
439 with patch.object(sys, "stdout", buf):
440 run_pipeline(options)
441 return buf.getvalue()
442
443 def test_no_spdx_keeps_dep5_names_in_files_paragraphs(self):
444 out = self._run("tests/testdata/four", ["--no-spdx"])
445 self.assertIn("License: GPL-2+", out)
446 self.assertIn("License: LGPL-2+", out)
447 self.assertNotIn("GPL-2.0-or-later", out)
448 self.assertNotIn("LGPL-2.0-or-later", out)
449
450 def test_spdx_converts_files_paragraphs(self):
451 out = self._run("tests/testdata/four", ["--spdx"])
452 # Files paragraph License: field uses SPDX identifiers
453 self.assertIn("License: GPL-2.0-or-later", out)
454 self.assertIn("License: LGPL-2.0-or-later", out)
455 # No raw DEP-5 names in the output
456 self.assertNotIn("License: GPL-2+", out)
457 self.assertNotIn("License: LGPL-2+", out)
458
459 def test_spdx_converts_standalone_stanza_headers(self):
460 out = self._run("tests/testdata/four", ["--spdx"])
461 # The standalone License: stanza headers must use SPDX names too.
462 # Each stanza occupies a blank-line-separated block; the header line
463 # starts at the beginning of its block.
464 stanza_headers = [
465 line for line in out.splitlines() if line.startswith("License:")
466 ]
467 self.assertIn("License: GPL-2.0-or-later", stanza_headers)
468 self.assertIn("License: LGPL-2.0-or-later", stanza_headers)
469
470 def test_spdx_preserves_license_body_text(self):
471 # Switching to SPDX must not discard the stored licence body text.
472 out = self._run("tests/testdata/four", ["--spdx"])
473 self.assertIn("This program is free software", out)
474
475
476 class TestThreeFormatNormalisation(unittest.TestCase):
477 """dep5_to_spdx() accepts DEP-5, old/deprecated SPDX, and new canonical SPDX."""
478
479 def test_agpl_all_formats(self):
480 self.assertEqual(dep5_to_spdx("AGPL-3"), "AGPL-3.0-only") # DEP-5
481 self.assertEqual(dep5_to_spdx("AGPL-3.0"), "AGPL-3.0-only") # old SPDX
482 self.assertEqual(dep5_to_spdx("AGPL-3.0-only"), "AGPL-3.0-only") # canonical
483
484 def test_gpl_all_formats(self):
485 self.assertEqual(dep5_to_spdx("GPL-2"), "GPL-2.0-only")
486 self.assertEqual(dep5_to_spdx("GPL-2.0"), "GPL-2.0-only")
487 self.assertEqual(dep5_to_spdx("GPL-2.0-only"), "GPL-2.0-only")
488 self.assertEqual(dep5_to_spdx("GPL-2+"), "GPL-2.0-or-later")
489 self.assertEqual(dep5_to_spdx("GPL-2.0+"), "GPL-2.0-or-later")
490 self.assertEqual(dep5_to_spdx("GPL-2.0-or-later"), "GPL-2.0-or-later")
491
492 def test_lgpl_all_formats(self):
493 self.assertEqual(dep5_to_spdx("LGPL-2.1"), "LGPL-2.1-only")
494 self.assertEqual(dep5_to_spdx("LGPL-2.1-only"), "LGPL-2.1-only")
495
496 def test_gfdl_all_formats(self):
497 self.assertEqual(dep5_to_spdx("GFDL-1.3"), "GFDL-1.3-only")
498 self.assertEqual(dep5_to_spdx("GFDL-1.3-only"), "GFDL-1.3-only")
499
500
501 class TestReplaceSpdx(unittest.TestCase):
502 """replace_spdx() converts SPDX identifiers → DEP-5 names."""
503
504 def test_only_suffix_removed(self):
505 from decopy.licensing.res import replace_spdx
506
507 self.assertEqual(replace_spdx("GPL-2.0-only"), "GPL-2")
508 self.assertEqual(replace_spdx("AGPL-3.0-only"), "AGPL-3")
509
510 def test_or_later_becomes_plus(self):
511 from decopy.licensing.res import replace_spdx
512
513 self.assertEqual(replace_spdx("GPL-2.0-or-later"), "GPL-2+")
514
515 def test_mit_becomes_expat(self):
516 from decopy.licensing.res import replace_spdx
517
518 self.assertEqual(replace_spdx("MIT"), "Expat")
519
520 def test_mit_x11_unchanged(self):
521 # MIT/X11 is a valid DEP-5 compound name; the MIT -> Expat rule must
522 # not fire on it.
523 from decopy.licensing.res import replace_spdx
524
525 self.assertEqual(replace_spdx("MIT/X11"), "MIT/X11")
526 self.assertEqual(replace_spdx("MIT-CMU"), "MIT-CMU")
527
528 def test_dep5_license_names_unchanged(self):
529 # DEP-5 names produced by decopy's matchers must pass through unmodified.
530 from decopy.licensing.res import replace_spdx
531
532 for name in (
533 "Expat",
534 "CC0",
535 "GPL-2",
536 "GPL-2+",
537 "GPL-3",
538 "GPL-3+",
539 "LGPL-2",
540 "LGPL-2+",
541 "LGPL-2.1",
542 "LGPL-2.1+",
543 "LGPL-3",
544 "LGPL-3+",
545 "AGPL-3",
546 "AGPL-3+",
547 "GFDL-1.2",
548 "GFDL-1.2+",
549 "GFDL-1.3",
550 "GFDL-1.3+",
551 "Apache-2",
552 "BSD-2-clause",
553 "BSD-3-clause",
554 "CeCILL-2",
555 "CC-BY-4",
556 ):
557 with self.subTest(name=name):
558 self.assertEqual(replace_spdx(name), name)
559
560 def test_dep5_exception_strings_unchanged(self):
561 # DEP-5 "… with … exception" strings must pass through unmodified.
562 from decopy.licensing.res import replace_spdx
563
564 for name in (
565 "GPL-2+ with Font exception",
566 "GPL-2+ with ClassPath exception",
567 "LGPL-2.1 with Qt1.1 exception",
568 "GPL-2 with OpenSSL exception",
569 ):
570 with self.subTest(name=name):
571 self.assertEqual(replace_spdx(name), name)
572
573 def test_lgpl_spdx_forms(self):
574 from decopy.licensing.res import replace_spdx
575
576 self.assertEqual(replace_spdx("LGPL-2.0-only"), "LGPL-2")
577 self.assertEqual(replace_spdx("LGPL-2.1-only"), "LGPL-2.1")
578 self.assertEqual(replace_spdx("LGPL-2.1-or-later"), "LGPL-2.1+")
579 self.assertEqual(replace_spdx("LGPL-3.0-only"), "LGPL-3")
580 self.assertEqual(replace_spdx("LGPL-3.0-or-later"), "LGPL-3+")
581
582 def test_gfdl_spdx_forms(self):
583 from decopy.licensing.res import replace_spdx
584
585 self.assertEqual(replace_spdx("GFDL-1.3-only"), "GFDL-1.3")
586 self.assertEqual(replace_spdx("GFDL-1.3-or-later"), "GFDL-1.3+")
587
588 def test_cc0_and_apache_spdx_forms(self):
589 from decopy.licensing.res import replace_spdx
590
591 self.assertEqual(replace_spdx("CC0-1.0"), "CC0")
592 self.assertEqual(replace_spdx("Apache-2.0"), "Apache-2")
593
594 def test_with_expression_font(self):
595 from decopy.licensing.res import replace_spdx
596
597 self.assertEqual(
598 replace_spdx("GPL-2.0-or-later WITH Font-exception-2.0"),
599 "GPL-2+ with Font exception",
600 )
601
602 def test_with_expression_classpath(self):
603 from decopy.licensing.res import replace_spdx
604
605 self.assertEqual(
606 replace_spdx("GPL-2.0-only WITH Classpath-exception-2.0"),
607 "GPL-2 with ClassPath exception",
608 )
609
610 def test_with_expression_gcc_version_specific(self):
611 from decopy.licensing.res import replace_spdx
612
613 self.assertEqual(
614 replace_spdx("GPL-3.0-only WITH GCC-exception-3.1"),
615 "GPL-3 with GCC-3.1 exception",
616 )
617
618 def test_with_expression_unmapped_passes_through(self):
619 from decopy.licensing.res import replace_spdx
620
621 self.assertEqual(
622 replace_spdx("GPL-2.0-or-later WITH Some-unknown-exception"),
623 "GPL-2.0-or-later WITH Some-unknown-exception",
624 )
625
626 def test_deprecated_non_copyleft_ids(self):
627 # Deprecated SPDX IDs whose current spelling decopy renames; the
628 # deprecated form must reach the same DEP-5 name.
629 from decopy.licensing.res import replace_spdx
630
631 self.assertEqual(replace_spdx("StandardML-NJ"), "MIT-like-SMLNJ")
632 self.assertEqual(replace_spdx("bzip2-1.0.5"), "BSD-like-bzip2")
633 # Current forms already map (regression guard):
634 self.assertEqual(replace_spdx("SMLNJ"), "MIT-like-SMLNJ")
635 self.assertEqual(replace_spdx("bzip2-1.0.6"), "BSD-like-bzip2")
636
637 def test_deprecated_with_exception_ids(self):
638 # Deprecated combined GPL-with-exception license IDs convert to DEP-5
639 # "with … exception" form (version-aware: GPL-3.0 selects the v3 variant).
640 from decopy.licensing.res import replace_spdx
641
642 self.assertEqual(
643 replace_spdx("GPL-2.0-with-GCC-exception"), "GPL-2 with GCC exception"
644 )
645 self.assertEqual(
646 replace_spdx("GPL-2.0-with-autoconf-exception"),
647 "GPL-2 with AutoConf exception",
648 )
649 self.assertEqual(
650 replace_spdx("GPL-2.0-with-bison-exception"), "GPL-2 with Bison exception"
651 )
652 self.assertEqual(
653 replace_spdx("GPL-2.0-with-classpath-exception"),
654 "GPL-2 with ClassPath exception",
655 )
656 self.assertEqual(
657 replace_spdx("GPL-2.0-with-font-exception"), "GPL-2 with Font exception"
658 )
659 self.assertEqual(
660 replace_spdx("GPL-3.0-with-GCC-exception"), "GPL-3 with GCC-3.1 exception"
661 )
662 self.assertEqual(
663 replace_spdx("GPL-3.0-with-autoconf-exception"),
664 "GPL-3 with AutoConf-3 exception",
665 )
666
667
668 class TestExceptionForward(unittest.TestCase):
669 """exception_to_spdx(): DEP-5 exception label → SPDX exception ID."""
670
671 def test_known_labels(self):
672 from decopy.licensing.spdx import exception_to_spdx
673
674 self.assertEqual(exception_to_spdx("Font"), "Font-exception-2.0")
675 self.assertEqual(exception_to_spdx("ClassPath"), "Classpath-exception-2.0")
676 self.assertEqual(exception_to_spdx("GCC"), "GCC-exception-2.0")
677 self.assertEqual(exception_to_spdx("GCC-3.1"), "GCC-exception-3.1")
678 self.assertEqual(exception_to_spdx("AutoConf"), "Autoconf-exception-2.0")
679 self.assertEqual(exception_to_spdx("AutoConf-3"), "Autoconf-exception-3.0")
680 self.assertEqual(exception_to_spdx("LLVM"), "LLVM-exception")
681
682 def test_unknown_label(self):
683 from decopy.licensing.spdx import exception_to_spdx
684
685 self.assertIsNone(exception_to_spdx("NoSuchException"))
686
687 def test_round_trip_via_public_api(self):
688 # Each label converts to an SPDX id (forward) that converts back to the
689 # same label (reverse, via replace_spdx). Covers every collision label
690 # whose representative id is pinned. The exhaustive forward/reverse
691 # consistency invariant is enforced inside spdx.py at import time.
692 from decopy.licensing.spdx import exception_to_spdx, replace_spdx
693
694 labels = [
695 "Font",
696 "ClassPath",
697 "GCC",
698 "GCC-3.1",
699 "AutoConf",
700 "AutoConf-3",
701 "GStreamer",
702 "LLVM",
703 "Universal-FOSS",
704 ]
705 for label in labels:
706 with self.subTest(label=label):
707 spdx_id = exception_to_spdx(label)
708 self.assertIsNotNone(spdx_id)
709 self.assertEqual(
710 replace_spdx(f"GPL-2.0-only WITH {spdx_id}"),
711 f"GPL-2 with {label} exception",
712 )
713
714
715 class TestDep5ToSpdxWithExpressions(unittest.TestCase):
716 """dep5_to_spdx() emits SPDX WITH expressions for 'with … exception' names."""
717
718 def test_font_exception(self):
719 self.assertEqual(
720 dep5_to_spdx("GPL-2+ with Font exception"),
721 "GPL-2.0-or-later WITH Font-exception-2.0",
722 )
723
724 def test_classpath_exception(self):
725 self.assertEqual(
726 dep5_to_spdx("GPL-2 with ClassPath exception"),
727 "GPL-2.0-only WITH Classpath-exception-2.0",
728 )
729
730 def test_gcc_version_in_label(self):
731 # decopy's label encodes the variant: GCC -> 2.0, GCC-3.1 -> 3.1.
732 self.assertEqual(
733 dep5_to_spdx("GPL-2+ with GCC exception"),
734 "GPL-2.0-or-later WITH GCC-exception-2.0",
735 )
736 self.assertEqual(
737 dep5_to_spdx("GPL-3 with GCC-3.1 exception"),
738 "GPL-3.0-only WITH GCC-exception-3.1",
739 )
740
741 def test_autoconf_version_in_label(self):
742 self.assertEqual(
743 dep5_to_spdx("GPL-2+ with AutoConf exception"),
744 "GPL-2.0-or-later WITH Autoconf-exception-2.0",
745 )
746 self.assertEqual(
747 dep5_to_spdx("GPL-3 with AutoConf-3 exception"),
748 "GPL-3.0-only WITH Autoconf-exception-3.0",
749 )
750
751 def test_exception_within_disjunction(self):
752 # "with … exception" handled per term inside an 'or' expression.
753 self.assertEqual(
754 dep5_to_spdx("GPL-2+ with Font exception or Expat"),
755 "GPL-2.0-or-later WITH Font-exception-2.0 OR MIT",
756 )
757
758 def test_unmapped_exception_passes_through(self):
759 self.assertEqual(
760 dep5_to_spdx("GPL-2+ with UnknownWidget exception"),
761 "GPL-2+ with UnknownWidget exception",
762 )
763
764
765 class TestSpdxLicenseIdentifierTag(unittest.TestCase):
766 """find_licenses() converts SPDX-License-Identifier tags to DEP-5 atoms.
767
768 A compound tag is canonicalised and split into its atomic licences, so each
769 gets its own standalone License stanza; the field's and/or/comma rendering is
770 a separate concern (Group.license_field / license_expr).
771 """
772
773 @staticmethod
774 def _detect(tag: str) -> list[str]:
775 from decopy.licensing import licenses
776
777 return list(licenses.find_licenses(licenses.clean_comments(tag)))
778
779 def test_single(self):
780 self.assertEqual(
781 self._detect("SPDX-License-Identifier: GPL-2.0-only"), ["GPL-2"]
782 )
783
784 def test_disjunction(self):
785 # Canonicalised and split into atoms, one stanza per licence.
786 self.assertEqual(
787 self._detect("SPDX-License-Identifier: Apache-2.0 OR MIT"),
788 ["Apache-2", "Expat"],
789 )
790
791 def test_with_clause(self):
792 self.assertEqual(
793 self._detect(
794 "SPDX-License-Identifier: GPL-2.0-or-later WITH Classpath-exception-2.0"
795 ),
796 ["GPL-2+ with ClassPath exception"],
797 )
798
799 def test_grouped_expression_split_into_atoms(self):
800 # A grouped compound splits into every atom it mentions (encounter order).
801 self.assertEqual(
802 self._detect(
803 "SPDX-License-Identifier: (GPL-2.0-only OR MIT) AND Apache-2.0"
804 ),
805 ["GPL-2", "Expat", "Apache-2"],
806 )
807
808 def test_trailing_comment_text_is_not_consumed(self):
809 # clean_comments() flattens lines, so prose can follow on the same line;
810 # the operator-shaped capture stops at the first non-operator token.
811 self.assertEqual(
812 self._detect("SPDX-License-Identifier: Apache-2.0 OR MIT see LICENSE file"),
813 ["Apache-2", "Expat"],
814 )
815
816 def test_single_letter_capture_is_rejected(self):
817 # ruff fixture pycodestyle/E501.py:89 has
818 # "# SPDX-License-Identifier: a very long license identifier ...";
819 # the lone "a" is not an SPDX id and must not be reported.
820 self.assertNotIn("a", self._detect("SPDX-License-Identifier: a very long id"))
821
822 def test_marker_like_capture_is_rejected(self):
823 # ruff mentions these markers in code/fixtures (it handles SPDX itself);
824 # a token with a trailing colon is never an SPDX id.
825 self.assertNotIn("fmt:", self._detect("SPDX-License-Identifier: fmt:"))
826 self.assertNotIn(
827 "SPDX-FileCopyrightText:",
828 self._detect("SPDX-License-Identifier: SPDX-FileCopyrightText:"),
829 )
830
831
832 if __name__ == "__main__":
833 unittest.main()