Skip to content

Commit

Permalink
fix: solved f-string bug which appeared with pytest-8.3.4 (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Dec 4, 2024
1 parent 689907a commit ae72954
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
41 changes: 41 additions & 0 deletions changelog.d/20241204_073038_15r10nk-git.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Removed
- A bullet item for the Removed category.
-->
<!--
### Added
- A bullet item for the Added category.
-->
<!--
### Changed
- A bullet item for the Changed category.
-->
<!--
### Deprecated
- A bullet item for the Deprecated category.
-->
### Fixed

- handling f-string like `snapshot(f"")`
it first appeared with pytest-8.3.4, but already existed before for cpython-3.11

<!--
### Security
- A bullet item for the Security category.
-->
8 changes: 8 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ scripts.update="cog -r docs/**.md"

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.13", "3.12", "3.11", "3.10", "3.9", "3.8","pypy3.8","pypy3.9","pypy3.10"]
pytest=["8.3.3","8.3.4"]

[tool.hatch.envs.hatch-test.overrides]
matrix.pytest.dependencies = [
{ value = "pytest==8.3.3", if = ["8.3.3"] },
{ value = "pytest>=8.3.4", if = ["8.3.4"] },
]


[tool.hatch.envs.hatch-test]
extra-dependencies = [
Expand Down
3 changes: 3 additions & 0 deletions src/inline_snapshot/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class simple_token(namedtuple("simple_token", "type,string")):

def __eq__(self, other):
if self.type == other.type == 3:
if self.string[0] == "f" or other.string[0] == "f":
return False

return ast.literal_eval(self.string) == ast.literal_eval(
other.string
) and self.string.replace("'", '"') == other.string.replace("'", '"')
Expand Down
13 changes: 13 additions & 0 deletions tests/test_bugs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from inline_snapshot.testing import Example


def test_fstring_139():
Example(
"""
from inline_snapshot import snapshot
snapshot(f'')
def test_a():
return None
"""
).run_pytest(returncode=0)
28 changes: 25 additions & 3 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from inline_snapshot import snapshot
from inline_snapshot.testing import Example

from .utils import pytest_compatible


def test_help_message(testdir):
result = testdir.runpytest_subprocess("--help")
Expand Down Expand Up @@ -115,7 +117,16 @@ def test_a():

result.assert_outcomes(passed=1)

assert result.report == snapshot("")
assert result.report == (
snapshot(
"""\
Info: one snapshot changed its representation (--inline-snapshot=update)
You can also use --inline-snapshot=review to approve the changes interactively
"""
)
if pytest_compatible
else snapshot("")
)

result = project.run("--inline-snapshot=update")

Expand Down Expand Up @@ -202,12 +213,23 @@ def test_a():

result.assert_outcomes(failed=1, errors=1)

assert result.report == snapshot(
"""\
assert result.report == (
snapshot(
"""\
Error: one snapshot has incorrect values (--inline-snapshot=fix)
Info: one snapshot can be trimmed (--inline-snapshot=trim)
Info: one snapshot changed its representation (--inline-snapshot=update)
You can also use --inline-snapshot=review to approve the changes interactively
"""
)
if pytest_compatible
else snapshot(
"""\
Error: one snapshot has incorrect values (--inline-snapshot=fix)
Info: one snapshot can be trimmed (--inline-snapshot=trim)
You can also use --inline-snapshot=review to approve the changes interactively
"""
)
)

result = project.run("--inline-snapshot=trim,fix")
Expand Down
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import sys
from contextlib import contextmanager

import inline_snapshot._config as _config
Expand All @@ -7,8 +8,11 @@
from inline_snapshot._rewrite_code import ChangeRecorder
from inline_snapshot.testing._example import snapshot_env


__all__ = ("snapshot_env",)

pytest_compatible = sys.version_info >= (3, 11) and pytest.version_tuple >= (8, 3, 4)


@contextlib.contextmanager
def config(**args):
Expand Down

0 comments on commit ae72954

Please sign in to comment.