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
|
import platform
import re
import sys
from inline_snapshot import snapshot
from inline_snapshot.extra import transformation
from inline_snapshot.testing import Example
executable = sys.executable.replace("\\", "\\\\")
def test_black_formatting_error(mocker):
def custom_format_str(*a, **ka):
raise Exception()
mocker.patch("black.format_str", custom_format_str)
Example(
"""\
from inline_snapshot import snapshot
def test_something():
assert 1==snapshot()
assert 1==snapshot(2)
assert list(range(20)) == snapshot()
"""
).run_inline(
["--inline-snapshot=fix,create"],
changed_files=snapshot(
{
"tests/test_something.py": """\
from inline_snapshot import snapshot
def test_something():
assert 1==snapshot(1)
assert 1==snapshot(1)
assert list(range(20)) == snapshot([0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ])
"""
}
),
report=snapshot(
"""\
FAIL: your snapshot is missing 2 values.
If you just created this value with --inline-snapshot=create, the value is now \n\
created and you can ignore this message.
FAIL: some snapshots in this test have incorrect values.
If you just created this value with --inline-snapshot=create, the value is now \n\
created and you can ignore this message.
═══════════════════════════════ inline-snapshot ════════════════════════════════
------------------------------- Create snapshots -------------------------------
+-------------------------- tests/test_something.py ---------------------------+
| @@ -1,6 +1,6 @@ |
| |
| from inline_snapshot import snapshot |
| |
| def test_something(): |
| - assert 1==snapshot() |
| + assert 1==snapshot(1) |
| assert 1==snapshot(2) |
| - assert list(range(20)) == snapshot() |
| + assert list(range(20)) == snapshot([0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 |
| ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ]) |
+------------------------------------------------------------------------------+
These changes will be applied, because you used create
-------------------------------- Fix snapshots ---------------------------------
+-------------------------- tests/test_something.py ---------------------------+
| @@ -2,5 +2,5 @@ |
| |
| |
| def test_something(): |
| assert 1==snapshot(1) |
| - assert 1==snapshot(2) |
| + assert 1==snapshot(1) |
| assert list(range(20)) == snapshot([0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 |
| ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ]) |
+------------------------------------------------------------------------------+
These changes will be applied, because you used fix
----------------------------------- Problems -----------------------------------
black could not format your code, which might be caused by this issue:
https://github.com/15r10nk/inline-snapshot/issues/138
"""
),
)
def test_fstring_139():
Example(
"""
from inline_snapshot import snapshot
snapshot(f'')
snapshot(rf'')
snapshot(Rf'')
snapshot(F'')
snapshot(rF'')
snapshot(RF'')
def test_a():
return None
"""
).run_pytest(returncode=0)
def test_format_command():
Example(
{
"fmt_cmd.py": """\
from sys import stdin,stdout
import re
text=stdin.read()
text=re.sub("#.*","",text)
stdout.buffer.write(text.encode("utf-8"))
""",
"pyproject.toml": f"""\
[tool.inline-snapshot]
format-command="{executable} fmt_cmd.py {{filename}}"
""",
"test_a.py": """\
from inline_snapshot import snapshot
# some comment
def test_a():
assert "5" == snapshot('''3''')# abc
""",
}
).run_pytest(
["--inline-snapshot=fix"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert "5" == snapshot('5')
"""
}
),
returncode=1,
)
def test_format_command_fail():
@transformation
def NoPaths(text):
path_re = "/[^ ]*/" if platform.system() != "Windows" else r".:\\[^ ]*\\"
text = re.sub(
"The.format_command.*following.error:",
lambda m: re.sub(" *\n *", " ", m[0]),
text,
flags=re.MULTILINE | re.DOTALL,
)
text = re.sub(path_re, "/.../", text, flags=re.MULTILINE)
text = re.sub(r"python(\.exe|\d\.\d*|\d)", "python", text)
return text
Example(
{
"fmt_cmd.py": """
import sys
sys.stdout.buffer.write(b"some problem\\n")
sys.exit(1)
""",
"pyproject.toml": f"""\
[tool.inline-snapshot]
format-command="{executable} fmt_cmd.py {{filename}}"
""",
"tests/test_a.py": """
from inline_snapshot import snapshot
def test_a():
assert "5" == snapshot('''3''')
""",
}
).run_pytest(
["--inline-snapshot=fix"],
term_columns=200,
changed_files=snapshot(
{
"tests/test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert "5" == snapshot('5')
"""
}
),
report=NoPaths(
snapshot(
"""\
-------------------------------------------------------------------------------------------- Fix snapshots ---------------------------------------------------------------------------------------------
+------------------------------------------------------------------------------------------ tests/test_a.py -------------------------------------------------------------------------------------------+
| @@ -2,4 +2,4 @@ |
| |
| from inline_snapshot import snapshot |
| |
| def test_a(): |
| - assert "5" == snapshot('''3''') |
| + assert "5" == snapshot('5') |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
These changes will be applied, because you used fix
----------------------------------------------------------------------------------------------- Problems -----------------------------------------------------------------------------------------------
The format_command '/.../python fmt_cmd.py /.../test_a.py' caused the following error:
some problem\
"""
)
),
returncode=1,
)
def test_no_black(mocker):
mocker.patch.dict(sys.modules, {"black": None})
Example(
{
"test_a.py": """
from inline_snapshot import snapshot
def test_a():
assert "5" == snapshot('''3''')
""",
}
).run_inline(
["--inline-snapshot=fix"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert "5" == snapshot('5')
"""
}
),
report=snapshot(
"""\
FAIL: some snapshots in this test have incorrect values.
If you just created this value with --inline-snapshot=create, the value is now \n\
created and you can ignore this message.
═══════════════════════════════ inline-snapshot ════════════════════════════════
-------------------------------- Fix snapshots ---------------------------------
+--------------------------------- test_a.py ----------------------------------+
| @@ -2,4 +2,4 @@ |
| |
| from inline_snapshot import snapshot |
| |
| def test_a(): |
| - assert "5" == snapshot('''3''') |
| + assert "5" == snapshot('5') |
+------------------------------------------------------------------------------+
These changes will be applied, because you used fix
----------------------------------- Problems -----------------------------------
inline-snapshot is not able to format your code.
This issue can be solved by:
* installing inline-snapshot[black] which gives you the same formatting like in
older versions
* adding a `format-command` to your pyproject.toml (see \n\
https://15r10nk.github.io/inline-snapshot/latest/configuration/#format-command \n\
for more information).
"""
),
)
def test_black_config():
Example(
{
"pyproject.toml": """
[tool.black]
skip_magic_trailing_comma=true
skip_string_normalization=true
preview=true
""",
"test_a.py": """
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(3)
""",
}
).run_pytest(
["--inline-snapshot=fix"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert 1+1 == snapshot(2)
"""
}
),
returncode=1,
)
def test_black_does_not_trim_spaces():
# https://github.com/15r10nk/inline-snapshot/issues/301
Example(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert " a " == snapshot("")
""",
}
).run_pytest(
["--inline-snapshot=fix"],
changed_files=snapshot(
{
"test_a.py": """\
from inline_snapshot import snapshot
def test_a():
assert " a " == snapshot(" a ")
"""
}
),
returncode=1,
).run_inline()
|