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
|
from inline_snapshot import snapshot
from inline_snapshot.testing._example import Example
def test_external_file():
Example(
"""\
from inline_snapshot import external_file
def test_a():
assert "test1".upper() == external_file("test.txt"), "not equal"
"""
).run_inline(
raises=snapshot(
"""\
AssertionError:
not equal\
"""
)
).run_pytest(
["--inline-snapshot=create"],
changed_files=snapshot({"tests/test.txt": "TEST1"}),
report=snapshot(
"""\
+------------------------------- tests/test.txt -------------------------------+
| TEST1 |
+------------------------------------------------------------------------------+
These changes will be applied, because you used create\
"""
),
returncode=snapshot(1),
).replace(
"test1", "test2"
).run_inline(
raises=snapshot(
"""\
AssertionError:
not equal\
"""
)
).run_pytest(
["--inline-snapshot=fix"],
changed_files=snapshot({"tests/test.txt": "TEST2"}),
report=snapshot(
"""\
+------------------------------- tests/test.txt -------------------------------+
| @@ -1 +1 @@ |
| |
| -TEST1 |
| +TEST2 |
+------------------------------------------------------------------------------+
These changes will be applied, because you used fix\
"""
),
returncode=snapshot(1),
)
def test_compare_twice():
Example(
"""\
from inline_snapshot import external_file
def test_a():
assert "test1" == external_file("test.txt"), "not equal"
assert "test1" == external_file("test.txt"), "not equal"
"""
).run_inline(
["--inline-snapshot=create"],
changed_files=snapshot({"tests/test.txt": "test1"}),
).run_inline()
def test_external_in_other_dir(tmp_path):
file = tmp_path / "subdir" / "subsubdir" / "file.txt"
print("file", file)
Example(
f"""\
from inline_snapshot import external_file
def test_a():
assert "test" == external_file({str(file)!r})
"""
).run_pytest(
["--inline-snapshot=create"], changed_files=snapshot({}), returncode=1
).run_inline()
assert file.read_text() == "test"
def test_unused_external_file():
Example(
f"""\
from inline_snapshot import external_file
def test_a():
external_file("test.txt")
"""
).run_inline(["--inline-snapshot=create"], changed_files=snapshot({})).run_inline()
def test_register_format_alias():
Example(
f"""\
from inline_snapshot import external_file,register_format_alias
register_format_alias(".html",".txt")
def test_bar():
assert "text" ==external_file("a.html")
"""
).run_inline(
["--inline-snapshot=create"], changed_files=snapshot({"tests/a.html": "text"})
)
|