Skip to content

Commit 5a1c830

Browse files
committed
Add test for build_spdx_licensing() #56
* Make helper functions for loading license keys Signed-off-by: Jono Yang <[email protected]>
1 parent 25d29f1 commit 5a1c830

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

src/license_expression/__init__.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -673,27 +673,60 @@ class ExpressionInfo:
673673
return data
674674

675675

676-
def build_spdx_licensing(license_key_index_location=None):
676+
def get_license_key_info(license_key_index_location=None):
677677
"""
678-
Return a Licensing object that has been loaded with SPDX license keys
678+
Return a list of dictionaries that contain license key information from
679+
`license_key_index_location`
680+
681+
If `license_key_index_location` is not present, then we use a vendored copy
682+
of the license key index from https://scancode-licensedb.aboutcode.org/
679683
"""
680684
if license_key_index_location:
681685
with open(license_key_index_location, 'r') as f:
682-
license_info = json.load(f)
686+
license_key_info = json.load(f)
683687
else:
684-
# Use vendored license key index if `license_key_index_location` has not been provided
685688
curr_dir = dirname(abspath(__file__))
686689
data_dir = join(curr_dir, 'data')
687690
vendored_license_key_index_location = join(data_dir, 'license_key_index.json')
688691
with open(vendored_license_key_index_location, 'r') as f:
689-
license_info = json.load(f)
692+
license_key_info = json.load(f)
693+
return license_key_info
694+
695+
696+
def build_licensing(license_key_index_location=None):
697+
"""
698+
Return a Licensing object that has been loaded with license keys.
699+
700+
If `license_key_index_location` is present, then license key information
701+
will be loaded from `license_key_index_location`, otherwise license key
702+
information will come from a vendored license key index file.
703+
"""
704+
license_key_info = get_license_key_info(license_key_index_location)
705+
lics = [
706+
{
707+
'key': l.get('license_key', ''),
708+
'is_exception': l.get('is_exception', ''),
709+
} for l in license_key_info
710+
]
711+
syms = [LicenseSymbol(**l) for l in lics]
712+
return Licensing(syms)
713+
690714

715+
def build_spdx_licensing(license_key_index_location=None):
716+
"""
717+
Return a Licensing object that has been loaded with SPDX license keys.
718+
719+
If `license_key_index_location` is present, then license key information
720+
will be loaded from `license_key_index_location`, otherwise license key
721+
information will come from a vendored license key index file.
722+
"""
723+
license_key_info = get_license_key_info(license_key_index_location)
691724
lics = [
692725
{
693726
'key': l.get('spdx_license_key', ''),
694727
'aliases': l.get('other_spdx_license_keys', ''),
695728
'is_exception': l.get('is_exception', ''),
696-
} for l in license_info if l.get('spdx_license_key')
729+
} for l in license_key_info if l.get('spdx_license_key')
697730
]
698731
syms = [LicenseSymbol(**l) for l in lics]
699732
return Licensing(syms)

tests/test_license_expression.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@
4646
from license_expression import ParseError
4747
from license_expression import Token
4848

49+
from license_expression import build_licensing
4950
from license_expression import build_spdx_licensing
5051
from license_expression import build_token_groups_for_with_subexpression
52+
from license_expression import get_license_key_info
5153
from license_expression import validate_symbols
5254

5355
from license_expression import TOKEN_AND
@@ -2232,10 +2234,28 @@ def test_validation_invalid_license_exception(self):
22322234

22332235

22342236
class UtilTest(TestCase):
2237+
test_data_dir = join(dirname(__file__), 'data')
2238+
2239+
def test_build_licensing(self):
2240+
test_license_key_index_location = join(self.test_data_dir, 'test_license_key_index.json')
2241+
with open(test_license_key_index_location, 'r') as f:
2242+
license_info = json.load(f)
2243+
lics = [
2244+
{
2245+
'key': l.get('license_key', ''),
2246+
'is_exception': l.get('is_exception', ''),
2247+
} for l in license_info if l.get('spdx_license_key')
2248+
]
2249+
syms = [LicenseSymbol(**l) for l in lics]
2250+
expected = Licensing(syms)
2251+
2252+
result = build_licensing(test_license_key_index_location)
2253+
2254+
assert expected.known_symbols == result.known_symbols
2255+
assert expected.known_symbols_lowercase == result.known_symbols_lowercase
2256+
22352257
def test_build_spdx_licensing(self):
2236-
curr_dir = dirname(abspath(__file__))
2237-
data_dir = join(curr_dir, 'data')
2238-
test_license_key_index_location = join(data_dir, 'test_license_key_index.json')
2258+
test_license_key_index_location = join(self.test_data_dir, 'test_license_key_index.json')
22392259

22402260
with open(test_license_key_index_location, 'r') as f:
22412261
license_info = json.load(f)

0 commit comments

Comments
 (0)