| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Characterization tests for the EXCEPTION_SUBS_MAP families via find_licenses. |
| 4 |
# Copyright: 2026, Maximiliano Curia <[email protected]> |
| 5 |
# |
| 6 |
# License: ISC |
| 7 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 8 |
# purpose with or without fee is hereby granted, provided that the above |
| 9 |
# copyright notice and this permission notice appear in all copies. |
| 10 |
# . |
| 11 |
# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH |
| 12 |
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 13 |
# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 14 |
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 15 |
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
| 16 |
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 17 |
# PERFORMANCE OF THIS SOFTWARE. |
| 18 |
|
| 19 |
"""Pin the EXCEPTION_SUBS_MAP families end-to-end through find_licenses. |
| 20 |
|
| 21 |
The five families keyed in licenses.EXCEPTION_SUBS_MAP (Apache, QPL, MIT/X11, |
| 22 |
CC-BY, BSD) have their exception SUBS applied by find_licenses itself, via the |
| 23 |
EXCEPTION_SUBS_MAP[family] lookup after detail resolution. The existing |
| 24 |
TestSpdxExceptions cases drive those SUBS tables directly through the family |
| 25 |
matchers (parse_apache, parse_qpl, parse_exceptions(..., SUBS)), which bypass |
| 26 |
that lookup -- so nothing currently exercises the find_licenses path that the |
| 27 |
Item-3 migration (EXCEPTION_SUBS_MAP -> per-row exception_subs) rewrites. |
| 28 |
|
| 29 |
These cases feed a base licence text plus a real exception clause through the |
| 30 |
whole find_licenses pipeline, so moving the map onto row data cannot drop or |
| 31 |
mis-wire a family's exceptions silently. |
| 32 |
""" |
| 33 |
|
| 34 |
import json |
| 35 |
import unittest |
| 36 |
from pathlib import Path |
| 37 |
|
| 38 |
from decopy.licensing import licenses |
| 39 |
|
| 40 |
_LICENSES_DIR = Path("tests/testdata/licenses") |
| 41 |
_EXCEPTIONS_DIR = Path("tests/testdata/exceptions") |
| 42 |
|
| 43 |
|
| 44 |
def _license_text(spdx_id: str) -> str: |
| 45 |
data = json.loads((_LICENSES_DIR / f"{spdx_id}.json").read_text()) |
| 46 |
return data.get("licenseText") or data.get("standardLicenseHeader") or "" |
| 47 |
|
| 48 |
|
| 49 |
def _exception_text(filename: str) -> str: |
| 50 |
data = json.loads((_EXCEPTIONS_DIR / filename).read_text()) |
| 51 |
return data["licenseExceptionText"] |
| 52 |
|
| 53 |
|
| 54 |
def _detect(text: str) -> str: |
| 55 |
return " or ".join(sorted(licenses.find_licenses(licenses.clean_comments(text)))) |
| 56 |
|
| 57 |
|
| 58 |
class FindLicensesExceptionSubsTest(unittest.TestCase): |
| 59 |
# (family, base licence id, exception file, expected find_licenses output) |
| 60 |
CASES = ( |
| 61 |
("Apache", "Apache-2.0", "LLVM-exception.json", "Apache-2 with LLVM exception"), |
| 62 |
( |
| 63 |
"QPL", |
| 64 |
"QPL-1.0", |
| 65 |
"QPL-1.0-INRIA-2004-exception.json", |
| 66 |
"QPL-1 with QPL-INRIA-2004 exception", |
| 67 |
), |
| 68 |
("MIT/X11", "MIT", "fmt-exception.json", "Expat with fmt exception"), |
| 69 |
( |
| 70 |
"CC-BY", |
| 71 |
"CC-BY-SA-4.0", |
| 72 |
"GNOME-examples-exception.json", |
| 73 |
"CC-BY-SA-4 with GNOME-examples exception", |
| 74 |
), |
| 75 |
( |
| 76 |
"BSD", |
| 77 |
"BSD-3-Clause", |
| 78 |
"PCRE2-exception.json", |
| 79 |
"BSD-3-clause with PCRE2 exception", |
| 80 |
), |
| 81 |
) |
| 82 |
|
| 83 |
def test_exception_subs_applied_through_find_licenses(self): |
| 84 |
for family, base, exception_file, expected in self.CASES: |
| 85 |
with self.subTest(family=family): |
| 86 |
text = _license_text(base) + "\n\n" + _exception_text(exception_file) |
| 87 |
self.assertEqual(_detect(text), expected) |
| 88 |
|
| 89 |
|
| 90 |
if __name__ == "__main__": |
| 91 |
unittest.main() |