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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
|
import ast
from inline_snapshot import external
from inline_snapshot import outsource
from inline_snapshot import snapshot
from inline_snapshot._external._find_external import ensure_import
from inline_snapshot._external._find_external import used_externals_in
from inline_snapshot._global_state import snapshot_env
from inline_snapshot.extra import raises
from inline_snapshot.testing import Example
from ..utils import apply_changes
def test_basic(check_update):
assert check_update(
"assert outsource('text') == snapshot()", flags="create"
) == snapshot(
"assert outsource('text') == snapshot(external(\"hash:982d9e3eb996*.txt\"))"
)
# def test_external():
# assert repr(external("11111111112222222222.txt")) == snapshot(
# 'external("111111111122*.txt")'
# )
def test_outsource():
Example(
"""\
from inline_snapshot import outsource, snapshot,external,register_format_alias
register_format_alias(".log",".txt")
def test_a():
assert outsource("test") == external()
assert outsource("test", suffix=".log") == external()
assert outsource(b"test") == external()
"""
).run_pytest(
["--inline-snapshot=create"],
changed_files=snapshot(
{
".inline-snapshot/external/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.bin": "test",
".inline-snapshot/external/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.log": "test",
".inline-snapshot/external/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.txt": "test",
"tests/__inline_snapshot__/test_something/test_a/e3e70682-c209-4cac-a29f-6fbed82c07cd.txt": "test",
"tests/__inline_snapshot__/test_something/test_a/eb1167b3-67a9-4378-bc65-c1e582e2e662.bin": "test",
"tests/__inline_snapshot__/test_something/test_a/f728b4fa-4248-4e3a-8a5d-2f346baa9455.log": "test",
"tests/test_something.py": """\
from inline_snapshot import outsource, snapshot,external,register_format_alias
register_format_alias(".log",".txt")
def test_a():
assert outsource("test") == external("uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt")
assert outsource("test", suffix=".log") == external("uuid:f728b4fa-4248-4e3a-8a5d-2f346baa9455.log")
assert outsource(b"test") == external("uuid:eb1167b3-67a9-4378-bc65-c1e582e2e662.bin")
""",
}
),
returncode=1,
).run_pytest(
["--inline-snapshot=disable"]
)
def test_diskstorage():
with snapshot_env():
assert outsource("test4") == snapshot(external("hash:a4e624d686e0*.txt"))
assert outsource("test5") == snapshot(external("hash:a140c0c1eda2*.txt"))
assert outsource("test6") == snapshot(external("hash:ed0cb90bdfa4*.txt"))
with raises(
snapshot(
"StorageLookupError: hash collision files=['a140c0c1eda2def2b830363ba362aa4d7d255c262960544821f556e16661b6ff.txt', 'a4e624d686e03ed2767c0abd85c14426b0b1157d2ce81d27bb4fe4f6f01d688a.txt']"
)
):
assert outsource("test4") == external("hash:a*.txt")
with raises(
snapshot(
"StorageLookupError: hash 'bbbbb*.txt' is not found in the HashStorage"
)
):
assert outsource("test4") == external("hash:bbbbb*.txt")
def test_update_legacy_external_names(project):
(
Example(
"""\
from inline_snapshot import outsource,snapshot
def test_something():
assert outsource("foo") == snapshot()
"""
)
.run_pytest(
["--inline-snapshot=create"],
changed_files=snapshot(
{
".inline-snapshot/external/2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae.txt": "foo",
"tests/test_something.py": """\
from inline_snapshot import outsource,snapshot
from inline_snapshot import external
def test_something():
assert outsource("foo") == snapshot(external("hash:2c26b46b68ff*.txt"))
""",
}
),
returncode=1,
)
.run_inline(reported_categories=snapshot([]))
.change_code(lambda code: code.replace("hash:", ""))
.run_inline(reported_categories=snapshot(["update"]))
.run_inline(
["--inline-snapshot=update"],
reported_categories=snapshot(["update"]),
changed_files=snapshot(
{
"tests/test_something.py": """\
from inline_snapshot import outsource,snapshot
from inline_snapshot import external
def test_something():
assert outsource("foo") == snapshot(external("hash:2c26b46b68ff*.txt"))
"""
}
),
)
)
def test_pytest_compare_external(project):
project.setup(
"""\
from inline_snapshot import external
def test_a():
s=snapshot()
assert outsource("test") == s
assert outsource("test2") == s
"""
)
result = project.run("--inline-snapshot=create")
result = project.run()
assert result.errorLines() == snapshot(
"""\
> assert outsource("test2") == s
E AssertionError: assert 'test2' == 'test'
E \n\
E - test
E + test2
E ? +
"""
)
def test_pytest_compare_external_bytes(project):
project.setup(
"""\
from inline_snapshot import external
def test_a():
assert outsource(b"test") == snapshot(
external("hash:9f86d081884c*.bin")
)
assert outsource(b"test2") == snapshot(
external("hash:9f86d081884c*.bin")
)
"""
)
result = project.run()
assert result.errorLines() == snapshot(
"""\
> assert outsource(b"test2") == snapshot(
E AssertionError: assert b'test2' == b'test'
E \n\
E Use -v to get more diff
"""
)
def test_pytest_existing_external_import(project):
project.setup(
"""\
from inline_snapshot import external
def test_a():
assert outsource("test") == snapshot()
"""
)
project.run("--inline-snapshot=create")
assert project.source == snapshot(
"""\
from inline_snapshot import external
def test_a():
assert outsource("test") == snapshot(external("hash:9f86d081884c*.txt"))
"""
)
def test_pytest_trim_external(project):
project.setup(
"""\
def test_a():
assert outsource("test") == snapshot()
# split
assert outsource("test2") == snapshot()
"""
)
project.run("--inline-snapshot=create")
assert project.storage() == snapshot(
[
"60303ae22b998861bce3b28f33eec1be758a213c86c93c076dbe9f558c11c752.txt",
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.txt",
]
)
assert project.source == snapshot(
"""\
from inline_snapshot import external
def test_a():
assert outsource("test") == snapshot(external("hash:9f86d081884c*.txt"))
# split
assert outsource("test2") == snapshot(external("hash:60303ae22b99*.txt"))
\
"""
)
project.setup(project.source.split("# split")[0])
result = project.run()
assert project.storage() == snapshot(
[
"60303ae22b998861bce3b28f33eec1be758a213c86c93c076dbe9f558c11c752.txt",
"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.txt",
]
)
assert result.report == snapshot("")
result = project.run("--inline-snapshot=trim")
assert project.storage() == snapshot(
["9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.txt"]
)
def test_pytest_new_external(project):
project.setup(
"""\
def test_a():
assert outsource("test") == snapshot()
"""
)
project.run()
assert project.storage() == snapshot(
["9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.txt"]
)
project.run("--inline-snapshot=create")
assert project.storage() == snapshot(
["9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08.txt"]
)
def test_pytest_config_hash_length(project):
project.setup(
"""\
def test_a():
assert outsource("test") == snapshot()
"""
)
project.run("--inline-snapshot=create")
default_result = project.source
# default config
project.pyproject(
"""
[tool.inline-snapshot]
"""
)
project.run("--inline-snapshot=create")
assert default_result == project.source
# set hash_length
project.pyproject(
"""
[tool.inline-snapshot]
hash-length=5
"""
)
project.run("--inline-snapshot=create")
assert project.source == snapshot(
"""\
from inline_snapshot import external
def test_a():
assert outsource("test") == snapshot(external("hash:9f86d081884c*.txt"))
"""
)
def test_errors():
with raises(snapshot("ValueError: suffix has to start with a '.' like '.png'")):
with snapshot_env():
outsource("test", suffix="blub")
with raises(
snapshot(
"UsageError: No format handler found for the given type 'test_errors.<locals>.C'."
)
):
class C: ...
with snapshot_env():
outsource(C())
with raises(snapshot("ValueError: 'invalid' is missing a suffix")):
with snapshot_env():
external("invalid")
def test_uses_external():
assert used_externals_in(
ast.parse("[external('hash:111*.txt')]"), check_import=False
) == snapshot({"hash:111*.txt"})
assert not used_externals_in(ast.parse("[external()]"), check_import=False)
assert not used_externals_in(ast.parse("[external]"), check_import=False)
assert used_externals_in(
ast.parse("[external('hash:111*.txt')]"), check_import=True
) == snapshot(set())
def test_no_imports(project):
project.setup(
"""\
# no imports
def test_something():
from inline_snapshot import outsource,snapshot
assert outsource("test") == snapshot()
test_something()
"""
)
result = project.run("--inline-snapshot=create")
result.assert_outcomes(errors=1, passed=1)
assert project.source == snapshot(
"""\
# no imports
from inline_snapshot import external
def test_something():
from inline_snapshot import outsource,snapshot
assert outsource("test") == snapshot(external("hash:9f86d081884c*.txt"))
test_something()
\
"""
)
def test_ensure_imports(tmp_path):
file = tmp_path / "file.py"
file.write_text(
"""\
from os import environ
from os import getcwd
""",
"utf-8",
)
with apply_changes() as recorder:
ensure_import(file, {"os": ["chdir", "environ"]}, recorder)
assert file.read_text("utf-8") == snapshot(
"""\
from os import environ
from os import getcwd
from os import chdir
"""
)
def test_ensure_imports_with_comment(tmp_path):
file = tmp_path / "file.py"
file.write_text(
"""\
from os import environ # comment
""",
"utf-8",
)
with apply_changes() as recorder:
ensure_import(file, {"os": ["chdir"]}, recorder)
assert file.read_text("utf-8") == snapshot(
"""\
from os import environ # comment
from os import chdir
"""
)
def test_new_externals(project):
project.setup(
"""
def test_something():
outsource("blub")
assert outsource("foo") == snapshot()
"""
)
project.run("--inline-snapshot=create")
assert project.storage() == snapshot(
[
"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae.txt",
"8dc140e6fe831481a2005ae152ffe32a9974aa92a260dfbac780d6a87154bb0b.txt",
]
)
assert project.source == snapshot(
"""\
from inline_snapshot import external
def test_something():
outsource("blub")
assert outsource("foo") == snapshot(external("hash:2c26b46b68ff*.txt"))
\
"""
)
project.run()
assert project.storage() == snapshot(
[
"2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae.txt",
"8dc140e6fe831481a2005ae152ffe32a9974aa92a260dfbac780d6a87154bb0b.txt",
]
)
def test_persist_twice():
Example(
"""\
from inline_snapshot import snapshot,outsource
def test_a():
assert outsource("testabc") == snapshot()
assert 1+1==snapshot()
"""
).run_pytest(["--inline-snapshot=create"], returncode=1).change_code(
lambda text: text.replace("snapshot(2)", "snapshot()")
).run_pytest(
["--inline-snapshot=create"], returncode=1
)
def test_disable():
Example(
"""
from inline_snapshot import external, snapshot,outsource
def test_something():
assert outsource("foo") == snapshot()
assert "foo" == external()
"""
).run_pytest(
["--inline-snapshot=create"],
changed_files=snapshot(
{
".inline-snapshot/external/2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae.txt": "foo",
"tests/__inline_snapshot__/test_something/test_something/e3e70682-c209-4cac-a29f-6fbed82c07cd.txt": "foo",
"tests/test_something.py": """\
from inline_snapshot import external, snapshot,outsource
def test_something():
assert outsource("foo") == snapshot(external("hash:2c26b46b68ff*.txt"))
assert "foo" == external("uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt")
""",
}
),
returncode=1,
).run_pytest(
["--inline-snapshot=disable"]
)
Example(
"""
from inline_snapshot import external
def test_something():
assert "foo" == external("hash:aaaaaaaaaaaa*.txt")
"""
).run_pytest(
["--inline-snapshot=disable"],
error=snapshot(
"""\
> assert "foo" == external("hash:aaaaaaaaaaaa*.txt")
> raise StorageLookupError(f"hash {name!r} is not found in the HashStorage")
E inline_snapshot._external._storage._protocol.StorageLookupError: hash 'aaaaaaaaaaaa*.txt' is not found in the HashStorage
"""
),
returncode=1,
)
def test_show_diff():
Example(
"""
from inline_snapshot import external
def test_a():
n=3
assert "\\n".join(map(str,range(n))) == external()
"""
).run_pytest(
["--inline-snapshot=create"],
report=snapshot(
"""\
------------------------------- Create snapshots -------------------------------
+-------------------------- tests/test_something.py ---------------------------+
| @@ -3,5 +3,5 @@ |
| |
| |
| def test_a(): |
| n=3 |
| - assert "\\n".join(map(str,range(n))) == external() |
| + assert "\\n".join(map(str,range(n))) == |
| external("uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt") |
+------------------------------------------------------------------------------+
+--------------- uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt ----------------+
| 0 |
| 1 |
| 2 |
+------------------------------------------------------------------------------+
These changes will be applied, because you used create\
"""
),
returncode=1,
).change_code(
lambda text: text.replace("n=3", "n=5")
).run_pytest(
["--inline-snapshot=fix"],
report=snapshot(
"""\
+--------------- uuid:e3e70682-c209-4cac-a29f-6fbed82c07cd.txt ----------------+
| @@ -1,3 +1,5 @@ |
| |
| 0 |
| 1 |
| 2 |
| +3 |
| +4 |
+------------------------------------------------------------------------------+
These changes will be applied, because you used fix\
"""
),
returncode=1,
)
def test_double_eq():
Example(
"""
from inline_snapshot import external
def test_a():
assert "hi" == external(".json") == external(".txt")
"""
).run_pytest(
["--inline-snapshot=create"],
error=snapshot(
"""\
> assert "hi" == external(".json") == external(".txt")
> raise UsageError("you can not compare external(...) with external(...)")
E inline_snapshot._exceptions.UsageError: you can not compare external(...) with external(...)
"""
),
returncode=1,
)
def test_external_eq_snapshot():
Example(
"""
from inline_snapshot import external, snapshot
def test_a():
assert "hi" == external(".json") == snapshot(".txt")
"""
).run_pytest(
["--inline-snapshot=create"],
error=snapshot(
"""\
> assert "hi" == external(".json") == snapshot(".txt")
> raise UsageError("you can not compare external(...) with snapshot(...)")
E inline_snapshot._exceptions.UsageError: you can not compare external(...) with snapshot(...)
"""
),
returncode=1,
)
def test_unknown_suffix():
Example(
"""
from inline_snapshot import external
def test_a():
assert "hi" == external("uuid:.blub")
"""
).run_pytest(
["--inline-snapshot=create"],
error=snapshot(
"""\
> assert "hi" == external("uuid:.blub")
> raise UsageError(
E inline_snapshot._exceptions.UsageError: No format handler found for the given type 'str' and suffix '.blub'.
"""
),
returncode=1,
)
def test_unknown_type():
Example(
"""
from inline_snapshot import external
class C:...
def test_a():
assert C() == external("uuid:")
"""
).run_pytest(
["--inline-snapshot=create"],
error=snapshot(
"""\
> assert C() == external("uuid:")
> raise UsageError(
E inline_snapshot._exceptions.UsageError: No format handler found for the given type 'C'.
"""
),
returncode=1,
)
def test_missing():
Example(
"""
from inline_snapshot import external
def test_a():
n=3
assert "hi" == external()
"""
).run_pytest(
["--inline-snapshot=short-report"],
error=snapshot(
"""\
> assert "hi" == external()
E assert 'hi' == external("uuid:")
E + where external("uuid:") = external()
"""
),
report=snapshot(
"""\
Error: one snapshot is missing a value (--inline-snapshot=create)
You can also use --inline-snapshot=review to approve the changes interactively\
"""
),
returncode=1,
).run_pytest(
["--inline-snapshot=disable"],
error=snapshot(
"""\
> assert "hi" == external()
> raise UsageError(
E inline_snapshot._exceptions.UsageError: can not load external object from an non existing location 'uuid:'
"""
),
report=snapshot(""),
returncode=1,
)
def test_tests_dir():
Example(
{
"pyproject.toml": "# empty",
"example1.py": """\
from inline_snapshot import external
def test_a():
assert "hi" == external("hash:")
""",
"tests/example2.py": """\
from inline_snapshot import external
def test_a():
assert "ho" == external("hash:")
""",
}
).run_pytest(
["--inline-snapshot=create", "example1.py", "tests/example2.py"],
changed_files=snapshot(
{
".inline-snapshot/external/8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4.txt": "hi",
".inline-snapshot/external/a821c62e8104f8519d639b4c0948aece641b143f6601fa145993bb2e2c7299d4.txt": "ho",
"example1.py": """\
from inline_snapshot import external
def test_a():
assert "hi" == external("hash:8f434346648f*.txt")
""",
"tests/example2.py": """\
from inline_snapshot import external
def test_a():
assert "ho" == external("hash:a821c62e8104*.txt")
""",
}
),
returncode=snapshot(1),
).run_pytest(
["--inline-snapshot=trim", "example1.py"],
changed_files=snapshot({}),
returncode=snapshot(0),
).run_pytest(
["--inline-snapshot=trim", "tests/example2.py"],
changed_files=snapshot(
{
".inline-snapshot/external/8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4.txt": None
}
),
returncode=snapshot(0),
)
|