| 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 |
"""Tests for SPDX licence-expression parsing and DEP-5 rendering. |
| 18 |
|
| 19 |
DEP-5 (spec sec 7.2) uses lowercase or/and with `and` binding tighter than |
| 20 |
`or`, and a comma -- not parentheses -- to override priority. SPDX uses |
| 21 |
uppercase AND/OR/WITH and parentheses, with the same `AND > OR` precedence. |
| 22 |
""" |
| 23 |
|
| 24 |
import unittest |
| 25 |
|
| 26 |
|
| 27 |
class TestSpdxExpressionToDep5(unittest.TestCase): |
| 28 |
"""SPDX expression -> DEP-5 string.""" |
| 29 |
|
| 30 |
def test_simple_and_with(self): |
| 31 |
from decopy.licensing.spdx import spdx_expression_to_dep5 |
| 32 |
|
| 33 |
cases = [ |
| 34 |
("GPL-2.0-only", "GPL-2"), |
| 35 |
("GPL-2.0+", "GPL-2+"), |
| 36 |
( |
| 37 |
"GPL-2.0-or-later WITH Font-exception-2.0", |
| 38 |
"GPL-2+ with Font exception", |
| 39 |
), |
| 40 |
] |
| 41 |
for spdx, dep5 in cases: |
| 42 |
with self.subTest(spdx=spdx): |
| 43 |
self.assertEqual(spdx_expression_to_dep5(spdx), dep5) |
| 44 |
|
| 45 |
def test_disjunction_and_conjunction(self): |
| 46 |
from decopy.licensing.spdx import spdx_expression_to_dep5 |
| 47 |
|
| 48 |
cases = [ |
| 49 |
# spec sec 7.2 examples (dual-licence, combined-work) |
| 50 |
("GPL-1.0-or-later OR Artistic-2.0", "GPL-1+ or Artistic-2"), |
| 51 |
("GPL-2.0-only AND BSD-3-Clause", "GPL-2 and BSD-3-clause"), |
| 52 |
("MIT OR Apache-2.0 OR ISC", "Expat or Apache-2 or ISC"), |
| 53 |
# default `and > or`: no comma needed |
| 54 |
( |
| 55 |
"GPL-2.0-only OR Artistic-2.0 AND BSD-3-Clause", |
| 56 |
"GPL-2 or Artistic-2 and BSD-3-clause", |
| 57 |
), |
| 58 |
] |
| 59 |
for spdx, dep5 in cases: |
| 60 |
with self.subTest(spdx=spdx): |
| 61 |
self.assertEqual(spdx_expression_to_dep5(spdx), dep5) |
| 62 |
|
| 63 |
def test_grouping_renders_with_comma(self): |
| 64 |
from decopy.licensing.spdx import spdx_expression_to_dep5 |
| 65 |
|
| 66 |
# An `or` grouped under an `and` is the one case DEP-5 expresses with a |
| 67 |
# comma (a parenthesised SPDX `or` operand of an `and`). |
| 68 |
cases = [ |
| 69 |
("(GPL-2.0-only OR MIT) AND Apache-2.0", "GPL-2 or Expat, and Apache-2"), |
| 70 |
("Apache-2.0 AND (MIT OR ISC)", "Apache-2, and Expat or ISC"), |
| 71 |
( |
| 72 |
"(GPL-2.0-only OR MIT) AND (Apache-2.0 OR ISC)", |
| 73 |
"GPL-2 or Expat, and Apache-2 or ISC", |
| 74 |
), |
| 75 |
] |
| 76 |
for spdx, dep5 in cases: |
| 77 |
with self.subTest(spdx=spdx): |
| 78 |
self.assertEqual(spdx_expression_to_dep5(spdx), dep5) |
| 79 |
|
| 80 |
def test_with_in_compound_and_deprecated_token(self): |
| 81 |
from decopy.licensing.spdx import spdx_expression_to_dep5 |
| 82 |
|
| 83 |
cases = [ |
| 84 |
# WITH binds tighter than OR: no comma |
| 85 |
( |
| 86 |
"GPL-2.0-or-later WITH Classpath-exception-2.0 OR MIT", |
| 87 |
"GPL-2+ with ClassPath exception or Expat", |
| 88 |
), |
| 89 |
# deprecated combined single token survives as one leaf |
| 90 |
( |
| 91 |
"GPL-2.0-with-classpath-exception", |
| 92 |
"GPL-2 with ClassPath exception", |
| 93 |
), |
| 94 |
] |
| 95 |
for spdx, dep5 in cases: |
| 96 |
with self.subTest(spdx=spdx): |
| 97 |
self.assertEqual(spdx_expression_to_dep5(spdx), dep5) |
| 98 |
|
| 99 |
|
| 100 |
class TestSpdxLowercaseOperators(unittest.TestCase): |
| 101 |
"""Operators are accepted case-insensitively. |
| 102 |
|
| 103 |
The SPDX spec mandates uppercase AND/OR/WITH, but real-world source (e.g. |
| 104 |
ruff's `SPDX-License-Identifier: MIT or Apache-2.0`) uses lowercase. The |
| 105 |
detection regex already matches case-insensitively, so the parser must too; |
| 106 |
otherwise the expression fails to parse and canonicalization is skipped. |
| 107 |
""" |
| 108 |
|
| 109 |
def test_lowercase_operators_canonicalize(self): |
| 110 |
from decopy.licensing.spdx import spdx_expression_to_dep5 |
| 111 |
|
| 112 |
cases = [ |
| 113 |
("MIT or Apache-2.0", "Expat or Apache-2"), |
| 114 |
("GPL-2.0-only and BSD-3-Clause", "GPL-2 and BSD-3-clause"), |
| 115 |
( |
| 116 |
"GPL-2.0-or-later with Font-exception-2.0", |
| 117 |
"GPL-2+ with Font exception", |
| 118 |
), |
| 119 |
# mixed case |
| 120 |
("MIT Or Apache-2.0", "Expat or Apache-2"), |
| 121 |
] |
| 122 |
for spdx, dep5 in cases: |
| 123 |
with self.subTest(spdx=spdx): |
| 124 |
self.assertEqual(spdx_expression_to_dep5(spdx), dep5) |
| 125 |
|
| 126 |
def test_lowercase_operators_parse_same_tree(self): |
| 127 |
from decopy.licensing.license_expr import ( |
| 128 |
And, |
| 129 |
License, |
| 130 |
Or, |
| 131 |
parse_spdx_expression, |
| 132 |
) |
| 133 |
|
| 134 |
self.assertEqual( |
| 135 |
parse_spdx_expression("A or B and C"), |
| 136 |
Or((License("A"), And((License("B"), License("C"))))), |
| 137 |
) |
| 138 |
|
| 139 |
def test_uppercase_still_parses(self): |
| 140 |
from decopy.licensing.spdx import spdx_expression_to_dep5 |
| 141 |
|
| 142 |
# No regression on the conformant uppercase form. |
| 143 |
self.assertEqual( |
| 144 |
spdx_expression_to_dep5("MIT OR Apache-2.0"), "Expat or Apache-2" |
| 145 |
) |
| 146 |
|
| 147 |
|
| 148 |
class TestSpdxExpressionParse(unittest.TestCase): |
| 149 |
"""Parse tree shape and precedence.""" |
| 150 |
|
| 151 |
def test_and_binds_tighter_than_or(self): |
| 152 |
from decopy.licensing.license_expr import ( |
| 153 |
And, |
| 154 |
License, |
| 155 |
Or, |
| 156 |
parse_spdx_expression, |
| 157 |
) |
| 158 |
|
| 159 |
self.assertEqual( |
| 160 |
parse_spdx_expression("A OR B AND C"), |
| 161 |
Or((License("A"), And((License("B"), License("C"))))), |
| 162 |
) |
| 163 |
|
| 164 |
def test_with_binds_tighter_than_or(self): |
| 165 |
from decopy.licensing.license_expr import ( |
| 166 |
License, |
| 167 |
Or, |
| 168 |
With, |
| 169 |
parse_spdx_expression, |
| 170 |
) |
| 171 |
|
| 172 |
self.assertEqual( |
| 173 |
parse_spdx_expression("A WITH E OR B"), |
| 174 |
Or((With(License("A"), "E"), License("B"))), |
| 175 |
) |
| 176 |
|
| 177 |
def test_parentheses_override_precedence(self): |
| 178 |
from decopy.licensing.license_expr import ( |
| 179 |
And, |
| 180 |
License, |
| 181 |
Or, |
| 182 |
parse_spdx_expression, |
| 183 |
) |
| 184 |
|
| 185 |
self.assertEqual( |
| 186 |
parse_spdx_expression("(A OR B) AND C"), |
| 187 |
And((Or((License("A"), License("B"))), License("C"))), |
| 188 |
) |
| 189 |
|
| 190 |
|
| 191 |
class TestParseLicenseSynopsis(unittest.TestCase): |
| 192 |
"""parse_license_synopsis(): read a License field that is DEP-5 or SPDX. |
| 193 |
|
| 194 |
decopy emits SPDX (with parens) as the fallback for expressions DEP-5 cannot |
| 195 |
represent, so the reader must accept both and normalise to DEP-5 vocabulary. |
| 196 |
""" |
| 197 |
|
| 198 |
def test_dep5_field_parsed_as_dep5(self): |
| 199 |
from decopy.licensing.license_expr import And, License |
| 200 |
from decopy.licensing.spdx import parse_license_synopsis |
| 201 |
|
| 202 |
self.assertEqual( |
| 203 |
parse_license_synopsis("GPL-2+ and BSD-3-clause"), |
| 204 |
And((License("GPL-2+"), License("BSD-3-clause"))), |
| 205 |
) |
| 206 |
|
| 207 |
def test_spdx_field_parsed_and_converted_to_dep5_names(self): |
| 208 |
from decopy.licensing.license_expr import And, License, Or |
| 209 |
from decopy.licensing.spdx import parse_license_synopsis |
| 210 |
|
| 211 |
# Parens mark the SPDX fallback form; leaves convert to DEP-5 names. |
| 212 |
self.assertEqual( |
| 213 |
parse_license_synopsis("(MIT OR Apache-2.0) AND GPL-2.0-or-later"), |
| 214 |
And( |
| 215 |
( |
| 216 |
Or((License("Expat"), License("Apache-2"))), |
| 217 |
License("GPL-2+"), |
| 218 |
) |
| 219 |
), |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
if __name__ == "__main__": |
| 224 |
unittest.main() |