MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_test_file_pairs

Function get_test_file_pairs

src/commoncode/testcase.py:315–393  ·  view source on GitHub ↗

Yield tuples of (data_file, test_file) from a test data `test_dir` directory. Raise exception for orphaned/dangling files. Each test consist of a pair of files: - a test file. - a data file with the same name as a test file and a '.yml' extension added. Each test file path s

(test_dir, template_to_generate_missing_yaml=None)

Source from the content-addressed store, hash-verified

313
314
315def get_test_file_pairs(test_dir, template_to_generate_missing_yaml=None):
316 """
317 Yield tuples of (data_file, test_file) from a test data `test_dir` directory.
318 Raise exception for orphaned/dangling files.
319 Each test consist of a pair of files:
320 - a test file.
321 - a data file with the same name as a test file and a '.yml' extension added.
322 Each test file path should be unique in the tree ignoring case.
323 """
324 # collect files with .yml extension and files with other extensions
325 data_files = {}
326 test_files = {}
327 dangling_test_files = set()
328 dangling_data_files = set()
329 paths_ignoring_case = defaultdict(list)
330
331 for top, _, files in os.walk(test_dir):
332 for tfile in files:
333 if tfile.endswith("~"):
334 continue
335 file_path = path.abspath(path.join(top, tfile))
336
337 if tfile.endswith(".yml"):
338 data_file_path = file_path
339 test_file_path = file_path.replace(".yml", "")
340 else:
341 test_file_path = file_path
342 data_file_path = test_file_path + ".yml"
343
344 if not path.exists(test_file_path):
345 dangling_test_files.add(test_file_path)
346
347 if not path.exists(data_file_path):
348 dangling_data_files.add(data_file_path)
349
350 paths_ignoring_case[file_path.lower()].append(file_path)
351
352 data_files[test_file_path] = data_file_path
353 test_files[test_file_path] = test_file_path
354
355 # ensure that we haev no dangling files
356 if dangling_test_files or dangling_data_files:
357 msg = ["Dangling missing test files without a YAML data file:"] + sorted(
358 dangling_test_files
359 )
360 msg += ["Dangling missing YAML data files without a test file"] + sorted(
361 dangling_data_files
362 )
363 msg = "\n".join(msg)
364 print(msg)
365 raise Exception(msg)
366
367 # ensure that each data file has a corresponding test file
368 diff = set(data_files.keys()).symmetric_difference(set(test_files.keys()))
369 if diff:
370 msg = [
371 "Orphaned copyright test file(s) found: "
372 "test file without its YAML test data file "

Callers 2

load_fromMethod · 0.90
load_copyright_testsFunction · 0.90

Calls 8

addMethod · 0.80
walkMethod · 0.45
joinMethod · 0.45
replaceMethod · 0.45
existsMethod · 0.45
appendMethod · 0.45
keysMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected