Skip to content

Commit 578bd8d

Browse files
Add function to create index from test rules folders
Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent ce19d7a commit 578bd8d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/licensedcode/cache.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,40 @@ def load_or_build(
138138
raise
139139

140140

141+
class MockCacheFromTestRules(LicenseCache):
142+
143+
@staticmethod
144+
def build_from_test_rules(test_rules_dir):
145+
146+
from licensedcode.models import licenses_data_dir as ldd
147+
from licensedcode.models import load_licenses
148+
149+
licenses_data_dir = licenses_data_dir or ldd
150+
rules_data_dir = test_rules_dir
151+
152+
licenses_db = load_licenses(licenses_data_dir=licenses_data_dir)
153+
154+
index = build_index(
155+
licenses_db=licenses_db,
156+
licenses_data_dir=licenses_data_dir,
157+
rules_data_dir=rules_data_dir,
158+
)
159+
160+
spdx_symbols = build_spdx_symbols(licenses_db=licenses_db)
161+
unknown_spdx_symbol = build_unknown_spdx_symbol(licenses_db=licenses_db)
162+
licensing = build_licensing(licenses_db=licenses_db)
163+
164+
license_cache = LicenseCache(
165+
db=licenses_db,
166+
index=index,
167+
licensing=licensing,
168+
spdx_symbols=spdx_symbols,
169+
unknown_spdx_symbol=unknown_spdx_symbol,
170+
)
171+
172+
return license_cache
173+
174+
141175
def build_index(
142176
licenses_db=None,
143177
licenses_data_dir=None,
@@ -353,6 +387,14 @@ def get_index(force=False, index_all_languages=False):
353387
return get_cache(force=force, index_all_languages=index_all_languages).index
354388

355389

390+
def get_index_with_test_rules(test_rules_dir):
391+
"""
392+
Return and eventually build and cache a LicenseIndex from a directory with
393+
a few test rules for a specific test.
394+
"""
395+
return MockCacheFromTestRules.build_from_test_rules(test_rules_dir).index
396+
397+
356398
get_cached_index = get_index
357399

358400

0 commit comments

Comments
 (0)