File: test_diff.py

package info (click to toggle)
python-asdf 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,940 kB
  • sloc: python: 23,812; makefile: 123
file content (124 lines) | stat: -rw-r--r-- 3,591 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import io
import sys

import pytest

import asdf
from asdf._commands import diff, main


@pytest.fixture(autouse=True)
def force_isatty(monkeypatch):
    def _isatty():
        return True

    monkeypatch.setattr(sys.stdout, "isatty", _isatty)
    yield


def _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False, ignore=None):
    iostream = io.StringIO()

    file_paths = [test_data_path / name for name in filenames]
    diff(file_paths, minimal=minimal, iostream=iostream, ignore=ignore)
    iostream.seek(0)

    result_path = test_data_path / result_file
    with open(result_path) as handle:
        assert handle.read() == iostream.read()


def test_diff(test_data_path):
    filenames = ["frames0.asdf", "frames1.asdf"]
    result_file = "frames.diff"
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False)


def test_diff_minimal(test_data_path):
    filenames = ["frames0.asdf", "frames1.asdf"]
    result_file = "frames_minimal.diff"
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=True)


@pytest.mark.parametrize(
    ("result_file", "ignore"),
    [
        ("frames_ignore_asdf_library.diff", ["asdf_library"]),
        ("frames_ignore_reference_frame.diff", ["frames[*].reference_frame"]),
        ("frames_ignore_both.diff", ["asdf_library", "frames[*].reference_frame"]),
    ],
)
def test_diff_ignore(test_data_path, result_file, ignore):
    filenames = ["frames0.asdf", "frames1.asdf"]
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False, ignore=ignore)


def test_diff_ndarray(test_data_path):
    filenames = ["ndarray0.asdf", "ndarray1.asdf"]
    result_file = "ndarrays.diff"
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False)


def test_diff_ndarray_in_list(test_data_path):
    filenames = ["ndarray_in_list0.asdf", "ndarray_in_list1.asdf"]
    result_file = "ndarray_in_list.diff"
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False)


def test_diff_block(test_data_path):
    filenames = ["block0.asdf", "block1.asdf"]
    result_file = "blocks.diff"
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False)


def test_diff_simple_inline_array(test_data_path):
    filenames = ["simple_inline_array0.asdf", "simple_inline_array1.asdf"]
    result_file = "simple_inline_array.diff"
    _assert_diffs_equal(test_data_path, filenames, result_file, minimal=False)


@pytest.mark.filterwarnings("ignore:unclosed file .*")
def test_file_not_found(test_data_path):
    # Try to open files that exist but are not valid asdf
    filenames = ["frames.diff", "blocks.diff"]
    with pytest.raises(
        RuntimeError,
        match=r"Does not appear to be a ASDF file.",
    ):
        diff([test_data_path / name for name in filenames], False)


def test_diff_command(test_data_path):
    filenames = ["frames0.asdf", "frames1.asdf"]
    path_strings = [str(test_data_path / name) for name in filenames]

    assert main.main_from_args(["diff", *path_strings]) == 0


def test_recursive_diff(tmp_path):
    fn0 = tmp_path / "a.asdf"
    fn1 = tmp_path / "b.asdf"

    t0 = {}
    t0["a"] = t0

    t1 = {"a": []}
    t1["a"].append(t1)

    asdf.dump(t0, fn0)
    asdf.dump(t1, fn1)

    assert main.main_from_args(["diff", str(fn0), str(fn1)]) == 0


def test_int_key_diff(tmp_path):
    fn0 = tmp_path / "a.asdf"
    fn1 = tmp_path / "b.asdf"

    t0 = {1: "a"}
    t1 = {2: "b"}

    asdf.dump(t0, fn0)
    asdf.dump(t1, fn1)

    assert main.main_from_args(["diff", str(fn0), str(fn1)]) == 0