Skip to content

Commit

Permalink
Parse expression once in validate #56
Browse files Browse the repository at this point in the history
    * Add new test that uses license exception as normal license key

Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Jun 9, 2021
1 parent ac80a21 commit 8d86c4b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/license_expression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,20 +696,17 @@ def validate(self, expression, strict=True, **kwargs):
original_expression=str(expression)
)

# Check `expression` type
# Check `expression` type and syntax
try:
parsed_expression = self.parse(expression)
parsed_expression = self.parse(expression, strict=strict)
except ExpressionError as e:
expression_info.errors.append(str(e))
return expression_info

if strict:
# Check `expression` syntax
try:
parsed_expression = self.parse(expression, strict=strict)
except ExpressionParseError as e:
expression_info.errors.append(str(e))
error_code = e.error_code
# If we have these error codes, then we have a problematic license
# key that we need to append to `invalid_keys`
if error_code and error_code in (PARSE_INVALID_EXCEPTION, PARSE_INVALID_SYMBOL_AS_EXCEPTION):
expression_info.invalid_keys.append(e.token_string)
return expression_info

# Check `expression` keys (validate)
try:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_license_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,13 @@ def test_validation_exception_with_choice(self):
assert result.errors == []
assert result.invalid_keys == []

def test_validation_exception_as_regular_key(self):
result = self.licensing.validate('GPL-2.0-or-later AND WxWindows-exception-3.1')
assert result.original_expression == 'GPL-2.0-or-later AND WxWindows-exception-3.1'
assert not result.normalized_expression
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']
assert result.invalid_keys == ['WxWindows-exception-3.1']

def test_validation_bad_syntax(self):
result = self.licensing.validate('Apache-2.0 + MIT')
assert result.original_expression == 'Apache-2.0 + MIT'
Expand Down

0 comments on commit 8d86c4b

Please sign in to comment.