Skip to content

Commit 8d86c4b

Browse files
committed
Parse expression once in validate #56
* Add new test that uses license exception as normal license key Signed-off-by: Jono Yang <[email protected]>
1 parent ac80a21 commit 8d86c4b

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/license_expression/__init__.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,20 +696,17 @@ def validate(self, expression, strict=True, **kwargs):
696696
original_expression=str(expression)
697697
)
698698

699-
# Check `expression` type
699+
# Check `expression` type and syntax
700700
try:
701-
parsed_expression = self.parse(expression)
701+
parsed_expression = self.parse(expression, strict=strict)
702702
except ExpressionError as e:
703703
expression_info.errors.append(str(e))
704-
return expression_info
705-
706-
if strict:
707-
# Check `expression` syntax
708-
try:
709-
parsed_expression = self.parse(expression, strict=strict)
710-
except ExpressionParseError as e:
711-
expression_info.errors.append(str(e))
704+
error_code = e.error_code
705+
# If we have these error codes, then we have a problematic license
706+
# key that we need to append to `invalid_keys`
707+
if error_code and error_code in (PARSE_INVALID_EXCEPTION, PARSE_INVALID_SYMBOL_AS_EXCEPTION):
712708
expression_info.invalid_keys.append(e.token_string)
709+
return expression_info
713710

714711
# Check `expression` keys (validate)
715712
try:

tests/test_license_expression.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,13 @@ def test_validation_exception_with_choice(self):
22652265
assert result.errors == []
22662266
assert result.invalid_keys == []
22672267

2268+
def test_validation_exception_as_regular_key(self):
2269+
result = self.licensing.validate('GPL-2.0-or-later AND WxWindows-exception-3.1')
2270+
assert result.original_expression == 'GPL-2.0-or-later AND WxWindows-exception-3.1'
2271+
assert not result.normalized_expression
2272+
assert result.errors == ['A license exception symbol can only be used as an exception in a "WITH exception" statement. for token: "WxWindows-exception-3.1" at position: 21']
2273+
assert result.invalid_keys == ['WxWindows-exception-3.1']
2274+
22682275
def test_validation_bad_syntax(self):
22692276
result = self.licensing.validate('Apache-2.0 + MIT')
22702277
assert result.original_expression == 'Apache-2.0 + MIT'

0 commit comments

Comments
 (0)