|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | +from asdf import open as asdf_open |
| 5 | +from asdf import versioning |
| 6 | +from asdf._tests._helpers import assert_tree_match |
| 7 | + |
| 8 | +_REFFILE_PATH = os.path.join(os.path.dirname(__file__), "..", "reference_files") |
| 9 | + |
| 10 | + |
| 11 | +def get_test_id(reference_file_path): |
| 12 | + """Helper function to return the informative part of a schema path""" |
| 13 | + path = os.path.normpath(str(reference_file_path)) |
| 14 | + return os.path.sep.join(path.split(os.path.sep)[-3:]) |
| 15 | + |
| 16 | + |
| 17 | +def collect_reference_files(): |
| 18 | + """Function used by pytest to collect ASDF reference files for testing.""" |
| 19 | + for version in versioning.supported_versions: |
| 20 | + version_dir = os.path.join(_REFFILE_PATH, str(version)) |
| 21 | + if os.path.exists(version_dir): |
| 22 | + for filename in os.listdir(version_dir): |
| 23 | + if filename.endswith(".asdf"): |
| 24 | + filepath = os.path.join(version_dir, filename) |
| 25 | + basename, _ = os.path.splitext(filepath) |
| 26 | + if os.path.exists(basename + ".yaml"): |
| 27 | + yield filepath |
| 28 | + |
| 29 | + |
| 30 | +def _compare_trees(name_without_ext): |
| 31 | + asdf_path = name_without_ext + ".asdf" |
| 32 | + yaml_path = name_without_ext + ".yaml" |
| 33 | + |
| 34 | + with asdf_open(asdf_path) as af_handle: |
| 35 | + af_handle.resolve_references() |
| 36 | + |
| 37 | + with asdf_open(yaml_path) as ref: |
| 38 | + assert_tree_match(af_handle.tree, ref.tree) |
| 39 | + |
| 40 | + |
| 41 | +def test_reference_files_exist(): |
| 42 | + assert list(collect_reference_files()) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.mark.parametrize("reference_file", collect_reference_files(), ids=get_test_id) |
| 46 | +def test_reference_file(reference_file): |
| 47 | + name_without_ext, _ = os.path.splitext(reference_file) |
| 48 | + _compare_trees(name_without_ext) |
0 commit comments