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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
|
import sys
from pathlib import Path
import pytest
@pytest.fixture
def testcases_initial():
return {
"test_used": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, 2, 3])
def test_used(snapshot, actual):
assert snapshot == actual
def test_used1(snapshot):
assert snapshot == 'unused'
assert 'unused' == snapshot
"""
),
"test_updated_1": (
"""
def test_updated_1(snapshot):
assert snapshot == ['this', 'will', 'be', 'updated']
"""
),
"test_updated_2": (
"""
def test_updated_2(snapshot):
assert ['this', 'will', 'be', 'updated'] == snapshot
"""
),
"test_updated_3": (
"""
def test_updated_3(snapshot):
assert snapshot == ['this', 'will', 'be', 'updated']
"""
),
"test_updated_4": (
"""
def test_updated_4(snapshot):
assert snapshot == "single line change"
"""
),
"test_updated_5": (
"""
def test_updated_5(snapshot):
assert snapshot == '''
multiple line changes
with some lines staying the same
intermittent changes that have to be ignore by the differ output
because when there are a lot of changes you only want to see changes
you do not want to see this line
or this line
this line should not show up because new lines are normalised\\r\\n
\x1b[38;5;1mthis line should show up because it changes color\x1b[0m
'''
"""
),
}
@pytest.fixture
def testcases_updated(testcases_initial):
updates = {
"test_updated_1": (
"""
def test_updated_1(snapshot):
assert snapshot == ['this', 'will', 'not', 'match']
"""
),
"test_updated_2": (
"""
def test_updated_2(snapshot):
assert ['this', 'will', 'fail'] == snapshot
"""
),
"test_updated_3": (
"""
def test_updated_3(snapshot):
assert snapshot == ['this', 'will', 'be', 'too', 'much']
"""
),
"test_updated_4": (
"""
def test_updated_4(snapshot):
assert snapshot == "sing line changeling"
"""
),
"test_updated_5": (
"""
def test_updated_5(snapshot):
assert snapshot == '''
multiple line changes
with some lines not staying the same
intermittent changes so unchanged lines have to be ignored by the differ
cause when there are a lot of changes you only want to see what changed
you do not want to see this line
or this line
this line should not show up because new lines are normalised\\n
\x1b[38;5;3mthis line should show up because it changes color\x1b[0m
and this line does not exist in the first one
'''
"""
),
}
return {**testcases_initial, **updates}
@pytest.fixture
def run_testcases(testdir, testcases_initial):
sys.path.append(str(testdir.tmpdir))
testdir.makepyfile(**testcases_initial)
result = testdir.runpytest("-v", "--snapshot-update")
result.stdout.re_match_lines((r"10 snapshots generated.",))
assert "Can not relate snapshot name" not in result.stdout.str()
return result, testdir, testcases_initial
def test_update_failure_shows_snapshot_diff(
run_testcases, testcases_updated, plugin_args_fails_xdist
):
testdir = run_testcases[1]
testdir.makepyfile(**testcases_updated)
result = testdir.runpytest("-vv", *plugin_args_fails_xdist)
result.stdout.re_match_lines(
(
r".*assert snapshot == \['this', 'will', 'not', 'match'\]",
r".*AssertionError: assert \[- snapshot\] == \[\+ received\]",
r".* list\(\[",
r".* ...",
r".* 'will',",
r".* - 'be',",
r".* - 'updated',",
r".* \+ 'not',",
r".* \+ 'match',",
r".* \]",
r".*assert \['this', 'will', 'fail'\] == snapshot",
r".*AssertionError: assert \[\+ received\] == \[- snapshot\]",
r".* list\(\[",
r".* ...",
r".* 'will',",
r".* - 'be',",
r".* \+ 'fail',",
r".* - 'updated',",
r".* \]",
r".*assert snapshot == \['this', 'will', 'be', 'too', 'much'\]",
r".*AssertionError: assert \[- snapshot\] == \[\+ received\]",
r".* list\(\[",
r".* ...",
r".* 'be',",
r".* - 'updated',",
r".* \+ 'too',",
r".* \+ 'much',",
r".* \]",
r".*assert snapshot == \"sing line changeling\"",
r".*AssertionError: assert \[- snapshot\] == \[\+ received\]",
r".* - 'single line change'",
r".* \+ 'sing line changeling'",
r".*AssertionError: assert \[- snapshot\] == \[\+ received\]",
r".* '",
r".* ...",
r".* multiple line changes",
r".* - with some lines staying the same",
r".* \+ with some lines not staying the same",
r".* - intermittent changes that have to be ignore by the differ out",
r".* \+ intermittent changes so unchanged lines have to be ignored b",
r".* - because when there are a lot of changes you only want to see ",
r".* \+ cause when there are a lot of changes you only want to see w",
r".* you do not want to see this line",
r".* ...",
r".* ",
r".* - \[38;5;1mthis line should show up because it changes color",
r".* \+ \[38;5;3mthis line should show up because it changes color",
r".* \+ and this line does not exist in the first one",
r".* ",
r".* '",
)
)
assert result.ret == 1
def test_update_success_shows_snapshot_report(
run_testcases, testcases_updated, plugin_args_fails_xdist
):
testdir = run_testcases[1]
testdir.makepyfile(**testcases_updated)
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines((r"5 snapshots passed\. 5 snapshots updated\.",))
assert result.ret == 0
def test_update_targets_only_selected_parametrized_tests_for_update_dash_m(
run_testcases, plugin_args_fails_xdist
):
updated_tests = {
"test_used": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, "2"])
def test_used(snapshot, actual):
assert snapshot == actual
"""
),
}
testdir = run_testcases[1]
testdir.makepyfile(**updated_tests)
result = testdir.runpytest(
"-v", "--snapshot-update", *plugin_args_fails_xdist, "-m", "parametrize"
)
result.stdout.re_match_lines(
(
r"1 snapshot passed\. 1 snapshot updated\. 1 unused snapshot deleted\.",
r"Deleted test_used\[3\] \(__snapshots__[\\/]test_used.ambr\)",
)
)
snapshot_path = [testdir.tmpdir, "__snapshots__"]
assert Path(*snapshot_path, "test_used.ambr").exists()
assert Path(*snapshot_path, "test_updated_1.ambr").exists()
def test_update_targets_only_selected_parametrized_tests_for_update_dash_k(
run_testcases, plugin_args_fails_xdist
):
updated_tests = {
"test_used": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, "2", 3])
def test_used(snapshot, actual):
assert snapshot == actual
"""
),
}
testdir = run_testcases[1]
testdir.makepyfile(**updated_tests)
result = testdir.runpytest(
"-v", "--snapshot-update", *plugin_args_fails_xdist, "-k", "test_used[2]"
)
result.stdout.re_match_lines((r"1 snapshot updated\.",))
assert "Deleted" not in result.stdout.str()
snapshot_path = [testdir.tmpdir, "__snapshots__"]
assert Path(*snapshot_path, "test_used.ambr").exists()
assert Path(*snapshot_path, "test_updated_1.ambr").exists()
def test_update_targets_only_selected_parametrized_tests_for_removal_dash_k(
run_testcases, plugin_args_fails_xdist
):
updated_tests = {
"test_used": (
"""
import pytest
@pytest.mark.parametrize("actual", [1, 2])
def test_used(snapshot, actual):
assert snapshot == actual
"""
),
}
testdir = run_testcases[1]
testdir.makepyfile(**updated_tests)
result = testdir.runpytest(
"-v", "--snapshot-update", *plugin_args_fails_xdist, "-k", "test_used["
)
result.stdout.re_match_lines(
(
r"2 snapshots passed\. 1 unused snapshot deleted\.",
r"Deleted test_used\[3\] \(__snapshots__[\\/]test_used\.ambr\)",
)
)
snapshot_path = [testdir.tmpdir, "__snapshots__"]
assert Path(*snapshot_path, "test_used.ambr").exists()
assert Path(*snapshot_path, "test_updated_1.ambr").exists()
def test_update_targets_only_selected_class_tests_dash_k(
testdir, plugin_args_fails_xdist
):
test_content = """
import pytest
class TestClass:
def test_case_1(self, snapshot):
assert snapshot == 1
def test_case_2(self, snapshot):
assert snapshot == 2
"""
testdir.makepyfile(test_content=test_content)
testdir.runpytest("-v", "--snapshot-update")
assert Path(testdir.tmpdir, "__snapshots__", "test_content.ambr").exists()
result = testdir.runpytest(
"test_content.py", "-v", *plugin_args_fails_xdist, "-k", "test_case_2"
)
result.stdout.re_match_lines((r"1 snapshot passed\.",))
assert "snapshot unused" not in result.stdout.str()
def test_update_targets_only_selected_module_tests_dash_k(
testdir, plugin_args_fails_xdist
):
test_content = """
import pytest
def test_case_1(snapshot):
assert snapshot == 1
def test_case_2(snapshot):
assert snapshot == 2
"""
testdir.makepyfile(test_content=test_content)
testdir.runpytest("-v", "--snapshot-update")
assert Path(testdir.tmpdir, "__snapshots__", "test_content.ambr").exists()
result = testdir.runpytest(
"test_content.py", "-v", *plugin_args_fails_xdist, "-k", "test_case_2"
)
result.stdout.re_match_lines((r"1 snapshot passed\.",))
assert "snapshot unused" not in result.stdout.str()
def test_update_targets_only_selected_module_tests_nodes(
run_testcases, plugin_args_fails_xdist
):
testdir = run_testcases[1]
snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr")
testdir.makefile(".ambr", **{str(snapfile_empty): ""})
testfile = Path(testdir.tmpdir, "test_used.py")
result = testdir.runpytest(
"-v", f"{testfile}::test_used", "--snapshot-update", *plugin_args_fails_xdist
)
result.stdout.re_match_lines((r"3 snapshots passed\.",))
assert "unused" not in result.stdout.str()
assert "updated" not in result.stdout.str()
assert "deleted" not in result.stdout.str()
assert result.ret == 0
assert snapfile_empty.exists()
def test_update_targets_only_selected_module_tests_nodes_pyargs(
run_testcases, plugin_args_fails_xdist
):
testdir = run_testcases[1]
snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr")
testdir.makefile(".ambr", **{str(snapfile_empty): ""})
result = testdir.runpytest(
"-v",
"--snapshot-update",
*plugin_args_fails_xdist,
"--pyargs",
"test_used::test_used",
)
result.stdout.re_match_lines((r"3 snapshots passed\.",))
assert "unused" not in result.stdout.str()
assert "updated" not in result.stdout.str()
assert "deleted" not in result.stdout.str()
assert result.ret == 0
assert snapfile_empty.exists()
def test_update_targets_only_selected_module_tests_file_for_update(
run_testcases, plugin_args_fails_xdist
):
testdir = run_testcases[1]
snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr")
testdir.makefile(".ambr", **{str(snapfile_empty): ""})
testdir.makepyfile(
test_used=(
"""
import pytest
@pytest.mark.parametrize("actual", [1, 2, 3])
def test_used(snapshot, actual):
assert snapshot == actual
"""
)
)
result = testdir.runpytest(
"-v", "test_used.py", "--snapshot-update", *plugin_args_fails_xdist
)
result.stdout.re_match_lines(
(
r"3 snapshots passed\. 2 unused snapshots deleted\.",
r"Deleted test_used1, test_used1\.1 \(__snapshots__[\\/]test_used\.ambr\)",
)
)
assert result.ret == 0
assert snapfile_empty.exists()
assert Path("__snapshots__", "test_used.ambr").exists()
def test_update_targets_only_selected_module_tests_file_for_removal(
run_testcases, plugin_args_fails_xdist
):
testdir = run_testcases[1]
testdir.makepyfile(
test_used=(
"""
def test_used(snapshot):
assert True
"""
),
)
snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr")
testdir.makefile(".ambr", **{str(snapfile_empty): ""})
result = testdir.runpytest(
"-v", "test_used.py", "--snapshot-update", *plugin_args_fails_xdist
)
result.stdout.re_match_lines(
(
r"5 unused snapshots deleted\.",
r"Deleted test_used1, test_used1\.1, test_used\[1\], test_used\[2\]"
r", test_used\[3\] \(__snapshots__[\\/]test_used\.ambr\)",
)
)
assert result.ret == 0
assert snapfile_empty.exists()
assert not Path("__snapshots__", "test_used.ambr").exists()
def test_update_removes_empty_snapshot_collection_only(
run_testcases, plugin_args_fails_xdist
):
testdir = run_testcases[1]
snapfile_empty = Path("__snapshots__", "empty_snapfile.ambr")
testdir.makefile(".ambr", **{str(snapfile_empty): ""})
assert snapfile_empty.exists()
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines(
(
r"10 snapshots passed\. 1 unused snapshot deleted\.",
r"Deleted empty snapshot collection "
r"\(__snapshots__[\\/]empty_snapfile\.ambr\)",
)
)
assert result.ret == 0
assert not snapfile_empty.exists()
assert Path("__snapshots__", "test_used.ambr").exists()
def test_update_removes_hanging_snapshot_collection_file(
run_testcases, plugin_args_fails_xdist
):
testdir = run_testcases[1]
snapfile_used = Path("__snapshots__", "test_used.ambr")
snapfile_hanging = Path("__snapshots__", "hanging_snapfile.abc")
testdir.makefile(".abc", **{str(snapfile_hanging): ""})
assert snapfile_hanging.exists()
result = testdir.runpytest("-v", "--snapshot-update", *plugin_args_fails_xdist)
result.stdout.re_match_lines(
(
r"10 snapshots passed\. 1 unused snapshot deleted\.",
r"Deleted unknown snapshot collection "
r"\(__snapshots__[\\/]hanging_snapfile\.abc\)",
)
)
assert f"{snapfile_used}" not in result.stdout.str()
assert result.ret == 0
assert snapfile_used.exists()
assert not snapfile_hanging.exists()
|