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)
| 313 | |
| 314 | |
| 315 | def 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 " |