Skip to content

Commit a6cc1f8

Browse files
committed
Run validation of licenses in two tests
This makes tests run in roughly 900s vs 1400s before when using -n15 on a laptop Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent 9aaf1b1 commit a6cc1f8

File tree

2 files changed

+105
-9
lines changed

2 files changed

+105
-9
lines changed

azure-pipelines.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ jobs:
7575
tests/licensedcode/test_detection_validate.py \
7676
-k TestValidateLicenseExtended5
7777
78+
79+
license_validate_ignorables_1: |
80+
venv/bin/pytest -n 3 -vvs --test-suite=validate \
81+
tests/licensedcode/test_detection_validate.py \
82+
-k TestValidateLicenseIgnorableClues1
83+
84+
license_validate_ignorables_2: |
85+
venv/bin/pytest -n 3 -vvs --test-suite=validate \
86+
tests/licensedcode/test_detection_validate.py \
87+
-k TestValidateLicenseIgnorableClues2
88+
89+
license_validate_ignorables_3: |
90+
venv/bin/pytest -n 3 -vvs --test-suite=validate \
91+
tests/licensedcode/test_detection_validate.py \
92+
-k TestValidateLicenseIgnorableClues3
93+
94+
license_validate_ignorables_4: |
95+
venv/bin/pytest -n 3 -vvs --test-suite=validate \
96+
tests/licensedcode/test_detection_validate.py \
97+
-k TestValidateLicenseIgnorableClues4
98+
99+
license_validate_ignorables_5: |
100+
venv/bin/pytest -n 3 -vvs --test-suite=validate \
101+
tests/licensedcode/test_detection_validate.py \
102+
-k TestValidateLicenseIgnorableClues5
103+
104+
78105
license_cache: |
79106
venv/bin/pytest -n 3 -vvs --test-suite=all \
80107
tests/licensedcode/test_zzzz_cache.py --reruns 2

tests/licensedcode/test_detection_validate.py

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ def make_validation_test(rule, test_name, regen=REGEN_TEST_FIXTURES):
4242

4343
def closure_test_function(*args, **kwargs):
4444
check_special_rule_cannot_be_detected(rule)
45-
4645
else:
4746

4847
def closure_test_function(*args, **kwargs):
4948
check_rule_or_license_can_be_self_detected_exactly(rule)
50-
check_ignorable_clues(rule, regen=regen)
5149

5250
closure_test_function.__name__ = test_name
5351
closure_test_function.funcname = test_name
@@ -107,6 +105,25 @@ def check_rule_or_license_can_be_self_detected_exactly(rule):
107105
assert '\n'.join(failure_trace) == '\n'.join(expected)
108106

109107

108+
def make_ignorable_clues_test(rule, test_name, regen=REGEN_TEST_FIXTURES):
109+
"""
110+
Build and return a test function closing on tests arguments.
111+
"""
112+
if isinstance(test_name, bytes):
113+
test_name = test_name.decode('utf-8')
114+
115+
if rule.is_false_positive:
116+
return
117+
118+
def closure_test_function(*args, **kwargs):
119+
check_ignorable_clues(rule, regen=regen)
120+
121+
closure_test_function.__name__ = test_name
122+
closure_test_function.funcname = test_name
123+
124+
return closure_test_function
125+
126+
110127
def check_ignorable_clues(licensish, regen=REGEN_TEST_FIXTURES, verbose=False):
111128
"""
112129
Validate that all expected ignorable clues declared in a `licensish` License
@@ -152,7 +169,13 @@ def check_ignorable_clues(licensish, regen=REGEN_TEST_FIXTURES, verbose=False):
152169
assert saneyaml.dump(result) == saneyaml.dump(expected)
153170

154171

155-
def build_validation_tests(rules, test_classes, regen=REGEN_TEST_FIXTURES):
172+
def build_validation_tests(
173+
rules,
174+
test_classes,
175+
test_func_creator=make_validation_test,
176+
test_name_prefix="test_validate_detect_",
177+
regen=REGEN_TEST_FIXTURES,
178+
):
156179
"""
157180
Dynamically build an individual test method for each rule texts in a
158181
``rules`` iterable of Rule objects then attach the test methods to the
@@ -178,16 +201,14 @@ def build_validation_tests(rules, test_classes, regen=REGEN_TEST_FIXTURES):
178201
# as they are not included in the standard indexing
179202
if rule.language != 'en':
180203
continue
181-
test_name = (
182-
'test_validate_detect_' +
183-
text.python_safe_name(rule.identifier)
184-
)
185-
test_method = make_validation_test(
204+
test_name = f"{test_name_prefix }{text.python_safe_name(rule.identifier)}"
205+
test_method = test_func_creator(
186206
rule=rule,
187207
test_name=test_name,
188208
regen=regen,
189209
)
190-
setattr(cls, test_name, test_method)
210+
if test_method:
211+
setattr(cls, test_name, test_method)
191212

192213

193214
class TestValidateLicenseBasic(unittest.TestCase):
@@ -232,6 +253,54 @@ class TestValidateLicenseExtended5(unittest.TestCase):
232253
TestValidateLicenseExtended4,
233254
TestValidateLicenseExtended5,
234255
],
256+
test_func_creator=make_validation_test,
257+
test_name_prefix="test_validate_detect_",
258+
regen=REGEN_TEST_FIXTURES,
259+
)
260+
261+
262+
class TestValidateLicenseIgnorableCluesBasic(unittest.TestCase):
263+
# Test functions are attached to this class at import time
264+
pytestmark = pytest.mark.scanslow
265+
266+
267+
class TestValidateLicenseIgnorableClues1(unittest.TestCase):
268+
# Test functions are attached to this class at import time
269+
pytestmark = pytest.mark.scanvalidate
270+
271+
272+
class TestValidateLicenseIgnorableClues2(unittest.TestCase):
273+
# Test functions are attached to this class at import time
274+
pytestmark = pytest.mark.scanvalidate
275+
276+
277+
class TestValidateLicenseIgnorableClues3(unittest.TestCase):
278+
# Test functions are attached to this class at import time
279+
pytestmark = pytest.mark.scanvalidate
280+
281+
282+
class TestValidateLicenseIgnorableClues4(unittest.TestCase):
283+
# Test functions are attached to this class at import time
284+
pytestmark = pytest.mark.scanvalidate
285+
286+
287+
class TestValidateLicenseIgnorableClues5(unittest.TestCase):
288+
# Test functions are attached to this class at import time
289+
pytestmark = pytest.mark.scanvalidate
290+
291+
292+
build_validation_tests(
293+
_rules,
294+
test_classes=[
295+
TestValidateLicenseIgnorableCluesBasic,
296+
TestValidateLicenseIgnorableClues1,
297+
TestValidateLicenseIgnorableClues2,
298+
TestValidateLicenseIgnorableClues3,
299+
TestValidateLicenseIgnorableClues4,
300+
TestValidateLicenseIgnorableClues5,
301+
],
302+
test_func_creator=make_ignorable_clues_test,
303+
test_name_prefix="test_ignorables_in_license_",
235304
regen=REGEN_TEST_FIXTURES,
236305
)
237306

0 commit comments

Comments
 (0)