Skip to content

Commit 348e0d1

Browse files
committed
Fixed the crash error as token_string was not defined #125
Signed-off-by: Chin Yeung Li <tli@nexb.com>
1 parent a3c00c0 commit 348e0d1

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/license_expression/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@
8787

8888

8989
class ExpressionError(Exception):
90-
pass
90+
def __init__(self, message, token_string=None):
91+
super().__init__(message)
92+
self.token_string = token_string
9193

9294

9395
class ExpressionParseError(ParseError, ExpressionError):
@@ -1210,7 +1212,8 @@ def __init__(
12101212
raise ExpressionError(
12111213
"Invalid license key: the valid characters are: letters and "
12121214
"numbers, underscore, dot, colon or hyphen signs and "
1213-
f"spaces: {key!r}"
1215+
f"spaces: {key!r}",
1216+
token_string=f"{key!r}",
12141217
)
12151218

12161219
# normalize spaces
@@ -1219,7 +1222,8 @@ def __init__(
12191222
if key.lower() in KEYWORDS_STRINGS:
12201223
raise ExpressionError(
12211224
'Invalid license key: a key cannot be a reserved keyword: "or",'
1222-
f' "and" or "with": {key!r}'
1225+
f' "and" or "with": {key!r}',
1226+
token_string=f"{key!r}",
12231227
)
12241228

12251229
self.key = key

tests/test_license_expression.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,15 @@ def test_validation_invalid_license_key(self):
24222422
assert result.errors == ["Unknown license key(s): cool-license"]
24232423
assert result.invalid_symbols == ["cool-license"]
24242424

2425+
def test_validation_invalid_license_key_chara(self):
2426+
result = self.licensing.validate("cool,license")
2427+
assert result.original_expression == "cool,license"
2428+
assert not result.normalized_expression
2429+
assert result.errors == [
2430+
"Invalid license key: the valid characters are: letters and numbers, underscore, dot, colon or hyphen signs and spaces: 'cool,license'"
2431+
]
2432+
assert result.invalid_symbols == ["'cool,license'"]
2433+
24252434
def test_validate_exception(self):
24262435
result = self.licensing.validate("GPL-2.0-or-later WITH WxWindows-exception-3.1")
24272436
assert result.original_expression == "GPL-2.0-or-later WITH WxWindows-exception-3.1"

0 commit comments

Comments
 (0)