| 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 |
"""Coverage harness over every SPDX licence in tests/testdata/licenses/. |
| 18 |
|
| 19 |
``license-coverage.tsv`` is the source of truth. Each row is |
| 20 |
``spdx_id <TAB> expected <TAB> status <TAB> fields``: |
| 21 |
|
| 22 |
* ``expected`` - the DEP-5 name find_licenses() must produce (empty = a known |
| 23 |
coverage gap: a licence decopy does not yet recognise); |
| 24 |
* ``status`` - annotations (ok / undetected / wrong:<note> / ...); |
| 25 |
* ``fields`` - comma-separated SPDX text fields to validate against (default |
| 26 |
``licenseText``). Some ids share an identical ``licenseText`` across their |
| 27 |
``-only`` / ``-or-later`` variants but differ in ``standardLicenseHeader``; |
| 28 |
those rows validate against ``standardLicenseHeader`` instead. |
| 29 |
|
| 30 |
The test locks find_licenses() output for all 729 files, so neither a matcher |
| 31 |
change nor refreshed SPDX data can shift coverage silently: a file with no row |
| 32 |
fails, a row with no file fails, and any changed result fails. |
| 33 |
|
| 34 |
Regenerate with ``scripts/audit-license-coverage.py``. |
| 35 |
""" |
| 36 |
|
| 37 |
import json |
| 38 |
import unittest |
| 39 |
from pathlib import Path |
| 40 |
|
| 41 |
from decopy.licensing import licenses |
| 42 |
|
| 43 |
LICENSES_DIR = Path("tests/testdata/licenses") |
| 44 |
COVERAGE_TSV = Path("tests/testdata/license-coverage.tsv") |
| 45 |
DEFAULT_FIELDS = ("licenseText",) |
| 46 |
_EXPECTED_COL = 1 |
| 47 |
_FIELDS_COL = 3 |
| 48 |
|
| 49 |
|
| 50 |
def _load_table() -> dict[str, tuple[str, tuple[str, ...]]]: |
| 51 |
table: dict[str, tuple[str, tuple[str, ...]]] = {} |
| 52 |
for line in COVERAGE_TSV.read_text().splitlines(): |
| 53 |
if line.startswith("#") or not line.strip(): |
| 54 |
continue |
| 55 |
cols = line.split("\t") |
| 56 |
spdx_id = cols[0] |
| 57 |
expected = cols[_EXPECTED_COL] if len(cols) > _EXPECTED_COL else "" |
| 58 |
raw_fields = cols[_FIELDS_COL] if len(cols) > _FIELDS_COL else "" |
| 59 |
fields = tuple(f for f in raw_fields.split(",") if f) or DEFAULT_FIELDS |
| 60 |
table[spdx_id] = (expected, fields) |
| 61 |
return table |
| 62 |
|
| 63 |
|
| 64 |
def _detect(text: str) -> str: |
| 65 |
return " or ".join(sorted(licenses.find_licenses(licenses.clean_comments(text)))) |
| 66 |
|
| 67 |
|
| 68 |
class TestLicenseCoverage(unittest.TestCase): |
| 69 |
table: dict[str, tuple[str, tuple[str, ...]]] |
| 70 |
files: list[Path] |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def setUpClass(cls): |
| 74 |
cls.table = _load_table() |
| 75 |
cls.files = sorted(LICENSES_DIR.glob("*.json")) |
| 76 |
|
| 77 |
def test_every_licence_matches_table(self): |
| 78 |
for path in self.files: |
| 79 |
spdx_id = path.stem |
| 80 |
with self.subTest(licence=spdx_id): |
| 81 |
self.assertIn( |
| 82 |
spdx_id, |
| 83 |
self.table, |
| 84 |
msg="missing from license-coverage.tsv; " |
| 85 |
"run scripts/audit-license-coverage.py", |
| 86 |
) |
| 87 |
expected, fields = self.table[spdx_id] |
| 88 |
data = json.loads(path.read_text()) |
| 89 |
for field in fields: |
| 90 |
text = data.get(field, "") |
| 91 |
if not text: |
| 92 |
continue |
| 93 |
self.assertEqual(_detect(text), expected, msg=f"field={field}") |
| 94 |
|
| 95 |
def test_no_stale_table_rows(self): |
| 96 |
present = {path.stem for path in self.files} |
| 97 |
stale = sorted(set(self.table) - present) |
| 98 |
self.assertEqual(stale, [], msg="rows with no licence file") |
| 99 |
|
| 100 |
|
| 101 |
if __name__ == "__main__": |
| 102 |
unittest.main() |