| 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 |
"""Contract of canonical_license_name(): the single licence-name normaliser. |
| 18 |
|
| 19 |
It maps any SPDX-or-DEP-5 single licence id to decopy's canonical DEP-5 name, |
| 20 |
is idempotent, and leaves names it does not recognise unchanged. The read path |
| 21 |
and the SPDX-License-Identifier path both normalise through it, so these |
| 22 |
properties are what lets every entry point agree. |
| 23 |
""" |
| 24 |
|
| 25 |
import unittest |
| 26 |
|
| 27 |
from decopy.licensing.spdx import canonical_license_name |
| 28 |
|
| 29 |
# (input, canonical DEP-5 name) for the recognised mappings the read path relies |
| 30 |
# on. SPDX -only / -or-later / .0 spellings collapse onto decopy's short names. |
| 31 |
_RECOGNISED = ( |
| 32 |
("GPL-2.0-only", "GPL-2"), |
| 33 |
("GPL-2.0", "GPL-2"), |
| 34 |
("GPL-2", "GPL-2"), |
| 35 |
("GPL-2.0-or-later", "GPL-2+"), |
| 36 |
("LGPL-2.1-only", "LGPL-2.1"), |
| 37 |
("AGPL-3.0", "AGPL-3"), |
| 38 |
("Apache-2.0", "Apache-2"), |
| 39 |
("MIT", "Expat"), |
| 40 |
("CC0-1.0", "CC0"), |
| 41 |
("BSD-3-Clause", "BSD-3-clause"), |
| 42 |
) |
| 43 |
|
| 44 |
# Names canonical_license_name() must pass through untouched: decopy's own |
| 45 |
# canonical spellings (already normalised) and ids it does not recognise. The |
| 46 |
# identity-on-unknown property is the safety guarantee -- it must never mangle a |
| 47 |
# bespoke name a maintainer wrote by hand. |
| 48 |
_PASSTHROUGH = ( |
| 49 |
"Expat", |
| 50 |
"GPL-2", |
| 51 |
"GPL-2+", |
| 52 |
"MIT/X11", |
| 53 |
"ISC", |
| 54 |
"Some-Bespoke-1.2", |
| 55 |
"Totally-Made-Up-License", |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
class TestCanonicalLicenseName(unittest.TestCase): |
| 60 |
def test_recognised_mappings(self): |
| 61 |
for raw, expected in _RECOGNISED: |
| 62 |
with self.subTest(name=raw): |
| 63 |
self.assertEqual(canonical_license_name(raw), expected) |
| 64 |
|
| 65 |
def test_identity_on_unknown_and_already_canonical(self): |
| 66 |
for name in _PASSTHROUGH: |
| 67 |
with self.subTest(name=name): |
| 68 |
self.assertEqual(canonical_license_name(name), name) |
| 69 |
|
| 70 |
def test_idempotent(self): |
| 71 |
names = [raw for raw, _ in _RECOGNISED] + list(_PASSTHROUGH) |
| 72 |
for name in names: |
| 73 |
with self.subTest(name=name): |
| 74 |
once = canonical_license_name(name) |
| 75 |
self.assertEqual(canonical_license_name(once), once) |
| 76 |
|
| 77 |
|
| 78 |
if __name__ == "__main__": |
| 79 |
unittest.main() |