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
|
"""Tests for the snapshot helper in :mod:`edify.testing.snapshots`."""
from pathlib import Path
import pytest
from edify import testing
from edify.testing import SnapshotMismatchError, SnapshotMissingError, assert_snapshot
_UPDATE_ENVIRONMENT_VARIABLE = "EDIFY_UPDATE_SNAPSHOTS"
def test_assert_snapshot_passes_when_actual_matches_committed_reference(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.delenv(_UPDATE_ENVIRONMENT_VARIABLE, raising=False)
snapshot_path = tmp_path / "identical.snapshot"
snapshot_path.write_text("hello\n")
assert_snapshot("hello\n", snapshot_path)
def test_assert_snapshot_raises_snapshot_mismatch_when_content_differs(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.delenv(_UPDATE_ENVIRONMENT_VARIABLE, raising=False)
snapshot_path = tmp_path / "drift.snapshot"
snapshot_path.write_text("expected\n")
with pytest.raises(SnapshotMismatchError) as excinfo:
assert_snapshot("actual\n", snapshot_path)
text = str(excinfo.value)
assert "snapshot mismatch" in text
assert "-expected" in text
assert "+actual" in text
assert "EDIFY_UPDATE_SNAPSHOTS=1" in text
def test_assert_snapshot_raises_snapshot_missing_when_reference_absent(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.delenv(_UPDATE_ENVIRONMENT_VARIABLE, raising=False)
absent_snapshot = tmp_path / "never_written.snapshot"
with pytest.raises(SnapshotMissingError, match="snapshot file missing"):
assert_snapshot("any actual value", absent_snapshot)
def test_update_mode_writes_the_snapshot_when_the_file_is_missing(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setenv(_UPDATE_ENVIRONMENT_VARIABLE, "1")
snapshot_path = tmp_path / "created.snapshot"
assert_snapshot("fresh content\n", snapshot_path)
assert snapshot_path.read_text() == "fresh content\n"
def test_update_mode_overwrites_the_snapshot_on_drift(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setenv(_UPDATE_ENVIRONMENT_VARIABLE, "1")
snapshot_path = tmp_path / "regenerated.snapshot"
snapshot_path.write_text("stale\n")
assert_snapshot("current\n", snapshot_path)
assert snapshot_path.read_text() == "current\n"
def test_update_mode_is_off_when_env_variable_is_set_to_other_value(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setenv(_UPDATE_ENVIRONMENT_VARIABLE, "0")
snapshot_path = tmp_path / "no_write.snapshot"
with pytest.raises(SnapshotMissingError):
assert_snapshot("actual", snapshot_path)
def test_update_mode_creates_missing_parent_directories(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.setenv(_UPDATE_ENVIRONMENT_VARIABLE, "1")
snapshot_path = tmp_path / "nested" / "dir" / "leaf.snapshot"
assert_snapshot("value", snapshot_path)
assert snapshot_path.read_text() == "value"
def test_snapshot_mismatch_error_names_the_snapshot_path(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):
monkeypatch.delenv(_UPDATE_ENVIRONMENT_VARIABLE, raising=False)
snapshot_path = tmp_path / "named.snapshot"
snapshot_path.write_text("was")
with pytest.raises(SnapshotMismatchError) as excinfo:
assert_snapshot("is", snapshot_path)
assert str(snapshot_path) in str(excinfo.value)
def test_snapshot_missing_error_is_an_assertion_error_subclass():
assert issubclass(SnapshotMissingError, AssertionError)
def test_snapshot_mismatch_error_is_an_assertion_error_subclass():
assert issubclass(SnapshotMismatchError, AssertionError)
def test_snapshot_helper_is_reachable_via_edify_testing_namespace(tmp_path: Path):
snapshot_path: Path = tmp_path / "namespace.snapshot"
snapshot_path.write_text("ok\n")
testing.assert_snapshot("ok\n", snapshot_path)
|