File: test_external_file.py

package info (click to toggle)
python-inline-snapshot 0.31.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,708 kB
  • sloc: python: 9,261; makefile: 39; sh: 32
file content (190 lines) | stat: -rw-r--r-- 5,444 bytes parent folder | download
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
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(
        ["--inline-snapshot=disable"],
        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"})
    )


def test_report():
    # see https://github.com/15r10nk/inline-snapshot/issues/298

    Example(
        """\

from inline_snapshot import external_file


def test_example():
    n=5
    assert sorted([n, 2]) == external_file("stored.json")


"""
    ).run_pytest(
        ["--inline-snapshot=report"],
        report=snapshot(
            """\
+----------------------------- tests/stored.json ------------------------------+
| [                                                                            |
|   2,                                                                         |
|   5                                                                          |
| ]                                                                            |
+------------------------------------------------------------------------------+
These changes are not applied.
Use --inline-snapshot=create to apply them, or use the interactive mode with
--inline-snapshot=review\
"""
        ),
        returncode=snapshot(1),
    ).run_inline(
        ["--inline-snapshot=create"],
        changed_files=snapshot(
            {
                "tests/stored.json": """\
[
  2,
  5
]\
"""
            }
        ),
    ).replace(
        "n=5", "n=8"
    ).run_pytest(
        ["--inline-snapshot=report"],
        report=snapshot(
            """\
+----------------------------- tests/stored.json ------------------------------+
| @@ -1,4 +1,4 @@                                                              |
|                                                                              |
|  [                                                                           |
|    2,                                                                        |
| -  5                                                                         |
| +  8                                                                         |
|  ]                                                                           |
+------------------------------------------------------------------------------+
These changes are not applied.
Use --inline-snapshot=fix to apply them, or use the interactive mode with
--inline-snapshot=review\
"""
        ),
        returncode=snapshot(1),
    )