|
8 | 8 | syrupy = import_optional_dependency("syrupy") |
9 | 9 |
|
10 | 10 | # ruff: noqa: E402 |
| 11 | +from syrupy import __import_extension |
| 12 | +from syrupy.assertion import SnapshotAssertion |
11 | 13 | from syrupy.extensions.single_file import ( |
12 | 14 | SingleFileSnapshotExtension, |
13 | 15 | WriteMode, |
14 | 16 | ) |
| 17 | +from syrupy.location import PyTestLocation |
15 | 18 | from syrupy.types import ( |
16 | 19 | PropertyFilter, |
17 | 20 | PropertyMatcher, |
@@ -90,19 +93,67 @@ def serialize( |
90 | 93 | return np.array2string(data, threshold=np.inf) |
91 | 94 |
|
92 | 95 |
|
| 96 | +class MatchAnything: |
| 97 | + def __eq__(self, _): |
| 98 | + return True |
| 99 | + |
| 100 | + |
93 | 101 | # fixtures |
94 | 102 |
|
95 | 103 |
|
| 104 | +@pytest.fixture(scope="session") |
| 105 | +def snapshot_disable(pytestconfig) -> bool: |
| 106 | + return pytestconfig.getoption("--snapshot-disable") |
| 107 | + |
| 108 | + |
96 | 109 | @pytest.fixture |
97 | | -def array_snapshot(snapshot): |
98 | | - return snapshot.use_extension(BinaryArrayExtension) |
| 110 | +def snapshot(request, snapshot_disable) -> "SnapshotAssertion": |
| 111 | + return ( |
| 112 | + MatchAnything() |
| 113 | + if snapshot_disable |
| 114 | + else SnapshotAssertion( |
| 115 | + update_snapshots=request.config.option.update_snapshots, |
| 116 | + extension_class=__import_extension(request.config.option.default_extension), |
| 117 | + test_location=PyTestLocation(request.node), |
| 118 | + session=request.session.config._syrupy, |
| 119 | + ) |
| 120 | + ) |
99 | 121 |
|
100 | 122 |
|
101 | 123 | @pytest.fixture |
102 | | -def text_array_snapshot(snapshot): |
103 | | - return snapshot.use_extension(TextArrayExtension) |
| 124 | +def array_snapshot(snapshot, snapshot_disable): |
| 125 | + return ( |
| 126 | + MatchAnything() |
| 127 | + if snapshot_disable |
| 128 | + else snapshot.use_extension(BinaryArrayExtension) |
| 129 | + ) |
104 | 130 |
|
105 | 131 |
|
106 | 132 | @pytest.fixture |
107 | | -def readable_array_snapshot(snapshot): |
108 | | - return snapshot.use_extension(ReadableArrayExtension) |
| 133 | +def text_array_snapshot(snapshot, snapshot_disable): |
| 134 | + return ( |
| 135 | + MatchAnything() |
| 136 | + if snapshot_disable |
| 137 | + else snapshot.use_extension(TextArrayExtension) |
| 138 | + ) |
| 139 | + |
| 140 | + |
| 141 | +@pytest.fixture |
| 142 | +def readable_array_snapshot(snapshot, snapshot_disable): |
| 143 | + return ( |
| 144 | + MatchAnything() |
| 145 | + if snapshot_disable |
| 146 | + else snapshot.use_extension(ReadableArrayExtension) |
| 147 | + ) |
| 148 | + |
| 149 | + |
| 150 | +# pytest config hooks |
| 151 | + |
| 152 | + |
| 153 | +def pytest_addoption(parser): |
| 154 | + parser.addoption( |
| 155 | + "--snapshot-disable", |
| 156 | + action="store_true", |
| 157 | + default=False, |
| 158 | + help="Disable snapshot comparisons.", |
| 159 | + ) |
0 commit comments