| 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 |
"""find_licenses() de-duplicates names produced by more than one rule family. |
| 18 |
|
| 19 |
Different LICENSES_RES families are keyed separately, so two of them can resolve |
| 20 |
to the *same* DEP-5 name and the raw result would repeat it -- a disjunction like |
| 21 |
"CC-BY-4 or CC-BY-4". The detected names are already canonical, so identical |
| 22 |
names are the same licence and must collapse to one member. |
| 23 |
""" |
| 24 |
|
| 25 |
import json |
| 26 |
import unittest |
| 27 |
from pathlib import Path |
| 28 |
|
| 29 |
from decopy.licensing import licenses |
| 30 |
|
| 31 |
_LICENSES_DIR = Path("tests/testdata/licenses") |
| 32 |
|
| 33 |
|
| 34 |
def _detect(spdx_id: str) -> list[str]: |
| 35 |
text = json.loads((_LICENSES_DIR / f"{spdx_id}.json").read_text())["licenseText"] |
| 36 |
return list(licenses.find_licenses(licenses.clean_comments(text))) |
| 37 |
|
| 38 |
|
| 39 |
class TestFindLicensesDedup(unittest.TestCase): |
| 40 |
def test_community_spec_does_not_self_duplicate(self): |
| 41 |
"""Community-Spec-1.0 is itself published under CC-BY-4.0. |
| 42 |
|
| 43 |
Its text fires both the CC-BY text matcher and the embedded |
| 44 |
SPDX-License-Identifier tag, so CC-BY-4 came back twice alongside |
| 45 |
Community-Spec-1. The result must list each name once. |
| 46 |
""" |
| 47 |
result = _detect("Community-Spec-1.0") |
| 48 |
self.assertEqual(len(result), len(set(result)), msg=f"duplicates in {result}") |
| 49 |
self.assertEqual(sorted(result), ["CC-BY-4", "Community-Spec-1"]) |
| 50 |
|
| 51 |
|
| 52 |
if __name__ == "__main__": |
| 53 |
unittest.main() |