| 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() returns atomic licence names, never a compound expression. |
| 18 |
|
| 19 |
A detected disjunction like "Expat or Apache-2" must come back as its atoms |
| 20 |
["Expat", "Apache-2"] so each gets its own standalone License stanza (with text), |
| 21 |
rather than a single opaque paragraph keyed by the whole expression string. |
| 22 |
""" |
| 23 |
|
| 24 |
import unittest |
| 25 |
|
| 26 |
from decopy.licensing import licenses |
| 27 |
|
| 28 |
|
| 29 |
def _detect(text: str) -> list[str]: |
| 30 |
return list(licenses.find_licenses(licenses.clean_comments(text))) |
| 31 |
|
| 32 |
|
| 33 |
class TestFindLicensesAtoms(unittest.TestCase): |
| 34 |
def test_spdx_disjunction_split_into_atoms(self): |
| 35 |
result = _detect("# SPDX-License-Identifier: MIT or Apache-2.0\n") |
| 36 |
self.assertEqual(result, ["Expat", "Apache-2"]) |
| 37 |
|
| 38 |
def test_spdx_conjunction_split_into_atoms(self): |
| 39 |
result = _detect("# SPDX-License-Identifier: MIT AND Apache-2.0\n") |
| 40 |
self.assertEqual(sorted(result), ["Apache-2", "Expat"]) |
| 41 |
|
| 42 |
def test_with_clause_stays_one_atom(self): |
| 43 |
# A `with` exception is a single grouping key, not two atoms. |
| 44 |
result = _detect( |
| 45 |
"# SPDX-License-Identifier: GPL-2.0-or-later WITH Font-exception-2.0\n" |
| 46 |
) |
| 47 |
self.assertEqual(result, ["GPL-2+ with Font exception"]) |
| 48 |
|
| 49 |
def test_atoms_are_deduplicated(self): |
| 50 |
result = _detect("# SPDX-License-Identifier: MIT or MIT\n") |
| 51 |
self.assertEqual(result, ["Expat"]) |
| 52 |
|
| 53 |
|
| 54 |
if __name__ == "__main__": |
| 55 |
unittest.main() |