Skip to content

Commit 15cbff3

Browse files
committed
Use new SCANCODE_REGEN_TEST_FIXTURES env var
SCANCODE_REGEN_TEST_FIXTURES sets the flag regen=True in tests and will regenerate the test data fixtures (typically expected YAML or JSON outputs) Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
1 parent dfd906c commit 15cbff3

File tree

80 files changed

+693
-564
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+693
-564
lines changed

CHANGELOG.rst

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ Important API changes:
4848
column to "path". The "copyright_holder" has been ranmed to "holder"
4949

5050

51-
Development environment changes:
52-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53-
54-
- The license cache consistency is not checked anymore when you are using a Git
55-
checkout. The SCANCODE_DEV_MODE tag file has been removed entirely. Use
56-
instead the --reindex-licenses option to rebuild the license index.
57-
58-
5951
Copyright detection:
6052
~~~~~~~~~~~~~~~~~~~~
6153

@@ -122,7 +114,6 @@ License detection:
122114
- Small, two-words matches that overlap the previous or next match by
123115
by the word "license" and assimilated are now filtered as false matches.
124116

125-
126117
- The new --licenses-reference option adds a new "licenses_reference" top
127118
level attribute to a scan when using the JSON and YAML outputs. This contains
128119
all the details and the full text of every licenses seen in a file or
@@ -251,13 +242,26 @@ Changes:
251242
- add resource-level attribute `for_packages`.
252243
- remove `package-data` attribute `root_path`.
253244

245+
254246
Documentation Update
255247
~~~~~~~~~~~~~~~~~~~~~~~~
256248

257249
- Various documentations have been updated to reflects API changes and
258250
correct minor documentation issues.
259251

260252

253+
Development environment changes:
254+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
255+
256+
- The license cache consistency is not checked anymore when you are using a Git
257+
checkout. The SCANCODE_DEV_MODE tag file has been removed entirely. Use
258+
instead the --reindex-licenses option to rebuild the license index.
259+
260+
- We can now regenerate updated test fixtures using the new SCANCODE_REGEN_TEST_FIXTURES
261+
environemnt variable. There is no need to replace the regen=False with regen=True
262+
in the code.
263+
264+
261265
30.1.0 - 2021-09-25
262266
--------------------
263267

src/scancode_config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,7 @@ def _create_dir(location):
170170
_create_dir(__scancode_temp_base_dir)
171171
_prefix = 'scancode-tk-' + __version__ + '-'
172172
scancode_temp_dir = tempfile.mkdtemp(prefix=_prefix, dir=__scancode_temp_base_dir)
173+
174+
175+
# Used for tests to regenerate fixtures with regen=True
176+
REGEN_TEST_FIXTURES = os.getenv('SCANCODE_REGEN_TEST_FIXTURES', False)

tests/cluecode/cluecode_test_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
from cluecode.copyrights import detect_copyrights
2323
from cluecode.copyrights import Detection
24+
from scancode_config import REGEN_TEST_FIXTURES
25+
2426

2527
"""
2628
Data-driven Copyright test utilities.
@@ -166,7 +168,7 @@ def make_copyright_test_functions(
166168
test,
167169
index,
168170
test_data_dir=test_env.test_data_dir,
169-
regen=False,
171+
regen=REGEN_TEST_FIXTURES,
170172
):
171173
"""
172174
Build and return a test function closing on tests arguments and the function
@@ -234,7 +236,7 @@ def build_tests(
234236
copyright_tests,
235237
clazz,
236238
test_data_dir=test_env.test_data_dir,
237-
regen=False,
239+
regen=REGEN_TEST_FIXTURES,
238240
):
239241
"""
240242
Dynamically build test methods from a sequence of CopyrightTest and attach

tests/cluecode/test_copyrights.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010

1111
import pytest
1212

13+
from commoncode.testcase import FileBasedTesting
14+
1315
from cluecode_test_utils import build_tests
1416
from cluecode_test_utils import load_copyright_tests
15-
from commoncode.testcase import FileBasedTesting
17+
from scancode_config import REGEN_TEST_FIXTURES
18+
1619

1720
pytestmark = pytest.mark.scanslow
1821

@@ -28,4 +31,4 @@ class TestCopyrightDataDriven(FileBasedTesting):
2831
pass
2932

3033

31-
build_tests(copyright_tests=load_copyright_tests(), clazz=TestCopyrightDataDriven, regen=False)
34+
build_tests(copyright_tests=load_copyright_tests(), clazz=TestCopyrightDataDriven, regen=REGEN_TEST_FIXTURES)

tests/cluecode/test_plugin_email_url.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
import os
1111

12-
import click
13-
1412
from commoncode.testcase import FileDrivenTesting
1513

1614
from scancode.cli_test_utils import check_json_scan
1715
from scancode.cli_test_utils import run_scan_click
16+
from scancode_config import REGEN_TEST_FIXTURES
17+
1818

1919
test_env = FileDrivenTesting()
2020
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
@@ -25,28 +25,28 @@ def test_scan_email():
2525
result_file = test_env.get_temp_file('json')
2626
args = ['--email', '--strip-root', test_dir, '--json', result_file]
2727
run_scan_click(args)
28-
check_json_scan(test_env.get_test_loc('plugin_email_url/emails.expected.json'), result_file, regen=False)
28+
check_json_scan(test_env.get_test_loc('plugin_email_url/emails.expected.json'), result_file, regen=REGEN_TEST_FIXTURES)
2929

3030

3131
def test_scan_email_with_threshold():
3232
test_dir = test_env.get_test_loc('plugin_email_url/files')
3333
result_file = test_env.get_temp_file('json')
3434
args = ['--email', '--strip-root', '--max-email', '2', test_dir, '--json', result_file]
3535
run_scan_click(args)
36-
check_json_scan(test_env.get_test_loc('plugin_email_url/emails-threshold.expected.json'), result_file, regen=False)
36+
check_json_scan(test_env.get_test_loc('plugin_email_url/emails-threshold.expected.json'), result_file, regen=REGEN_TEST_FIXTURES)
3737

3838

3939
def test_scan_url():
4040
test_dir = test_env.get_test_loc('plugin_email_url/files')
4141
result_file = test_env.get_temp_file('json')
4242
args = ['--url', '--strip-root', test_dir, '--json', result_file]
4343
run_scan_click(args)
44-
check_json_scan(test_env.get_test_loc('plugin_email_url/urls.expected.json'), result_file, regen=False)
44+
check_json_scan(test_env.get_test_loc('plugin_email_url/urls.expected.json'), result_file, regen=REGEN_TEST_FIXTURES)
4545

4646

4747
def test_scan_url_with_threshold():
4848
test_dir = test_env.get_test_loc('plugin_email_url/files')
4949
result_file = test_env.get_temp_file('json')
5050
args = ['--url', '--strip-root', '--max-url', '2', test_dir, '--json', result_file]
5151
run_scan_click(args)
52-
check_json_scan(test_env.get_test_loc('plugin_email_url/urls-threshold.expected.json'), result_file, regen=False)
52+
check_json_scan(test_env.get_test_loc('plugin_email_url/urls-threshold.expected.json'), result_file, regen=REGEN_TEST_FIXTURES)

tests/cluecode/test_plugin_filter_clues.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from scancode.cli_test_utils import check_json_scan
1919
from scancode.cli_test_utils import run_scan_click
2020

21+
from scancode_config import REGEN_TEST_FIXTURES
22+
23+
2124
test_env = FileDrivenTesting()
2225
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
2326

@@ -41,7 +44,7 @@ def test_scan_plugin_filter_clues_for_rule():
4144
args = ['-clieu', '--filter-clues', test_dir, '--json', result_file]
4245
run_scan_click(args)
4346
expected = test_env.get_test_loc('plugin_filter_clues/filtered-expected.json')
44-
check_json_scan(expected, result_file, remove_file_date=True, regen=False)
47+
check_json_scan(expected, result_file, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
4548

4649

4750
def test_scan_plugin_filter_clues_does_not_filter_incorrectly():
@@ -53,7 +56,7 @@ def test_scan_plugin_filter_clues_does_not_filter_incorrectly():
5356
args = ['-clieu', '--filter-clues', test_dir, '--json', result_file]
5457
run_scan_click(args)
5558
expected = test_env.get_test_loc('plugin_filter_clues/filtered-expected2.json')
56-
check_json_scan(expected, result_file, remove_file_date=True, regen=False)
59+
check_json_scan(expected, result_file, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
5760

5861

5962
# Regression on types tracked in https://github.com/nexB/typecode/issues/21
@@ -66,4 +69,4 @@ def test_scan_plugin_filter_clues_for_license():
6669
args = ['-clieu', '--filter-clues', test_dir, '--json', result_file]
6770
run_scan_click(args)
6871
expected = test_env.get_test_loc('plugin_filter_clues/filtered-expected3.json')
69-
check_json_scan(expected, result_file, remove_file_date=True, regen=False)
72+
check_json_scan(expected, result_file, remove_file_date=True, regen=REGEN_TEST_FIXTURES)

tests/cluecode/test_plugin_ignore_copyrights.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
from os.path import join
1212

1313
from commoncode.testcase import FileDrivenTesting
14+
15+
from cluecode.plugin_ignore_copyrights import is_ignored
1416
from scancode.cli_test_utils import run_scan_click
1517
from scancode.cli_test_utils import check_json_scan
16-
from cluecode.plugin_ignore_copyrights import is_ignored
18+
from scancode_config import REGEN_TEST_FIXTURES
19+
20+
1721

1822

1923
class TestIgnoreCopyrights(FileDrivenTesting):
@@ -37,11 +41,11 @@ def test_ignore_holders(self):
3741
result_file = self.get_temp_file('json')
3842
expected_file = self.get_test_loc('plugin_ignore_copyrights/holders.expected.json')
3943
run_scan_click(['-c', '--ignore-copyright-holder', 'Regents', '--json-pp', result_file, test_dir])
40-
check_json_scan(expected_file, result_file, remove_file_date=True, regen=False)
44+
check_json_scan(expected_file, result_file, remove_file_date=True, regen=REGEN_TEST_FIXTURES)
4145

4246
def test_ignore_authors(self):
4347
test_dir = self.extract_test_tar('plugin_ignore_copyrights/basic.tgz')
4448
result_file = self.get_temp_file('json')
4549
expected_file = self.get_test_loc('plugin_ignore_copyrights/authors.expected.json')
4650
run_scan_click(['-c', '--ignore-author', 'Berkeley', '--json-pp', result_file, test_dir])
47-
check_json_scan(expected_file, result_file, remove_file_date=True, regen=False)
51+
check_json_scan(expected_file, result_file, remove_file_date=True, regen=REGEN_TEST_FIXTURES)

tests/formattedcode/test_output_csv.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
import os
1515

1616
import pytest
17-
1817
from commoncode.testcase import FileDrivenTesting
18+
1919
from formattedcode.output_csv import flatten_scan
2020
from scancode.cli_test_utils import run_scan_click
2121
from scancode.cli_test_utils import run_scan_plain
22+
from scancode_config import REGEN_TEST_FIXTURES
2223

2324

2425
test_env = FileDrivenTesting()
@@ -38,7 +39,7 @@ def load_scan(json_input):
3839
return scan_results
3940

4041

41-
def check_json(result, expected_file, regen=False):
42+
def check_json(result, expected_file, regen=REGEN_TEST_FIXTURES):
4243
if regen:
4344
with io.open(expected_file, 'w', encoding='utf-8') as reg:
4445
reg.write(json.dumps(result, indent=4, separators=(',', ': ')))
@@ -49,7 +50,7 @@ def check_json(result, expected_file, regen=False):
4950

5051
def check_csvs(result_file, expected_file,
5152
ignore_keys=('date', 'file_type', 'mime_type', 'package_uuid'),
52-
regen=False):
53+
regen=REGEN_TEST_FIXTURES):
5354
"""
5455
Load and compare two CSVs.
5556
`ignore_keys` is a tuple of keys that will be ignored in the comparisons.
@@ -93,7 +94,7 @@ def test_flatten_scan_minimal():
9394
])
9495
result = list(flatten_scan(scan, headers))
9596
expected_file = test_env.get_test_loc('csv/flatten_scan/minimal.json-expected')
96-
check_json(result, expected_file, regen=False)
97+
check_json(result, expected_file, regen=REGEN_TEST_FIXTURES)
9798

9899

99100
def test_flatten_scan_can_process_path_with_and_without_leading_slash():
@@ -119,7 +120,7 @@ def test_can_process_live_scan_for_packages_with_root():
119120
args = ['--package', test_dir, '--csv', result_file]
120121
run_scan_plain(args)
121122
expected_file = test_env.get_test_loc('csv/packages/expected.csv')
122-
check_csvs(result_file, expected_file, regen=False)
123+
check_csvs(result_file, expected_file, regen=REGEN_TEST_FIXTURES)
123124

124125

125126
def test_output_can_handle_non_ascii_paths():
@@ -234,7 +235,7 @@ def test_can_process_live_scan_with_all_options():
234235
args = ['-clip', '--email', '--url', '--strip-root', test_dir, '--csv', result_file]
235236
run_scan_plain(args)
236237
expected_file = test_env.get_test_loc('csv/livescan/expected.csv')
237-
check_csvs(result_file, expected_file, regen=False)
238+
check_csvs(result_file, expected_file, regen=REGEN_TEST_FIXTURES)
238239

239240

240241
@pytest.mark.scanslow
@@ -244,7 +245,7 @@ def test_can_process_live_scan_for_packages_strip_root():
244245
args = ['--package', '--strip-root', test_dir, '--csv', result_file]
245246
run_scan_plain(args)
246247
expected_file = test_env.get_test_loc('csv/packages/expected-no-root.csv')
247-
check_csvs(result_file, expected_file, regen=False)
248+
check_csvs(result_file, expected_file, regen=REGEN_TEST_FIXTURES)
248249

249250

250251
@pytest.mark.scanslow
@@ -254,7 +255,7 @@ def test_output_contains_license_expression():
254255
args = ['--from-json', test_file, '--csv', result_file]
255256
run_scan_plain(args)
256257
expected_file = test_env.get_test_loc('csv/expressions/expected.csv')
257-
check_csvs(result_file, expected_file, regen=False)
258+
check_csvs(result_file, expected_file, regen=REGEN_TEST_FIXTURES)
258259

259260

260261
@pytest.mark.scanslow
@@ -264,4 +265,4 @@ def test_output_handles_non_standard_data():
264265
args = ['--from-json', test_file, '--csv', result_file]
265266
run_scan_plain(args)
266267
expected_file = test_env.get_test_loc('csv/non-standard/identified.csv')
267-
check_csvs(result_file, expected_file, regen=False)
268+
check_csvs(result_file, expected_file, regen=REGEN_TEST_FIXTURES)

tests/formattedcode/test_output_cyclonedx.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414

1515
import saneyaml
1616
from lxml import etree
17-
1817
from commoncode.testcase import FileDrivenTesting
18+
1919
from formattedcode.output_cyclonedx import CycloneDxComponent
2020
from formattedcode.output_cyclonedx import CycloneDxExternalRef
2121
from formattedcode.output_cyclonedx import CycloneDxHashObject
2222
from formattedcode.output_cyclonedx import CycloneDxLicenseExpression
2323
from formattedcode.output_cyclonedx import CycloneDxMetadata
2424
from formattedcode.output_cyclonedx import get_author_from_parties
2525
from scancode.cli_test_utils import run_scan_click
26+
from scancode_config import REGEN_TEST_FIXTURES
27+
2628

2729
test_env = FileDrivenTesting()
2830
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
@@ -36,7 +38,7 @@ def load_and_clean_json(result_file):
3638
return result
3739

3840

39-
def check_cyclone_output(expected_file, result_file, regen=False):
41+
def check_cyclone_output(expected_file, result_file, regen=REGEN_TEST_FIXTURES):
4042
"""
4143
Check that expected and result_file are equal. Ignore headers.
4244
If `regen` is True the expected_file is overwritten with `results_file`.
@@ -57,7 +59,7 @@ def check_cyclone_output(expected_file, result_file, regen=False):
5759
assert result == expected
5860

5961

60-
def check_cyclone_xml_output(expected_file, result_file, regen=False):
62+
def check_cyclone_xml_output(expected_file, result_file, regen=REGEN_TEST_FIXTURES):
6163
"""
6264
Check that expected and result_file are equal. Ignore headers.
6365
If `regen` is True the expected_file is overwritten with `results_file`.
@@ -220,12 +222,12 @@ def test_cyclonedx_plugin_json():
220222
result_file = test_env.get_temp_file('cyclonedx.json')
221223
run_scan_click(['-p', test_dir, '--cyclonedx', result_file])
222224
expected_file = test_env.get_test_loc('cyclonedx/expected.json')
223-
check_cyclone_output(expected_file, result_file, regen=False)
225+
check_cyclone_output(expected_file, result_file, regen=REGEN_TEST_FIXTURES)
224226

225227

226228
def test_cyclonedx_plugin_xml_components_and_dependencies_are_serialized_correctly():
227229
test_dir = test_env.get_test_loc('cyclonedx/simple')
228230
result_file = test_env.get_temp_file('cyclonedx.xml')
229231
run_scan_click(['-p', test_dir, '--cyclonedx-xml', result_file])
230232
expected_file = test_env.get_test_loc('cyclonedx/expected.xml')
231-
check_cyclone_xml_output(expected_file, result_file, regen=False)
233+
check_cyclone_xml_output(expected_file, result_file, regen=REGEN_TEST_FIXTURES)

tests/formattedcode/test_output_debian.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
import shutil
1313

1414
from commoncode.testcase import FileDrivenTesting
15+
1516
from scancode.cli_test_utils import run_scan_plain
17+
from scancode_config import REGEN_TEST_FIXTURES
18+
1619

1720
test_env = FileDrivenTesting()
1821
test_env.test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
1922

2023

21-
def check_debian_copyright_output(test_dir, expected_file, regen=False):
24+
def check_debian_copyright_output(test_dir, expected_file, regen=REGEN_TEST_FIXTURES):
2225
"""
2326
Rrun a scan on ``test_dir`` with a debian output and check if the created
2427
file matches ``expected_file``.
@@ -49,10 +52,10 @@ def check_debian_copyright_output(test_dir, expected_file, regen=False):
4952
def test_debian_basic():
5053
test_dir = test_env.get_test_loc('debian/basic/scan')
5154
expected_file = test_env.get_test_loc('debian/basic/expected.copyright')
52-
check_debian_copyright_output(test_dir, expected_file, regen=False)
55+
check_debian_copyright_output(test_dir, expected_file, regen=REGEN_TEST_FIXTURES)
5356

5457

5558
def test_debian_multiple_files():
5659
test_dir = test_env.get_test_loc('debian/multiple_files/scan')
5760
expected_file = test_env.get_test_loc('debian/multiple_files/expected.copyright')
58-
check_debian_copyright_output(test_dir, expected_file, regen=False)
61+
check_debian_copyright_output(test_dir, expected_file, regen=REGEN_TEST_FIXTURES)

0 commit comments

Comments
 (0)