Skip to content

Commit

Permalink
fix: trigger no update for trailing comma changes
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed May 26, 2024
1 parent 56de34b commit 5fce096
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions inline_snapshot/_inline_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ._format import format_code
from ._sentinels import undefined
from ._utils import ignore_tokens
from ._utils import normalize_strings
from ._utils import normalize
from ._utils import simple_token
from ._utils import value_to_token

Expand Down Expand Up @@ -81,7 +81,7 @@ class GenericValue:
def _token_of_node(self, node):

return list(
normalize_strings(
normalize(
[
simple_token(t.type, t.string)
for t in self._source.asttokens().get_tokens(node)
Expand Down
16 changes: 16 additions & 0 deletions inline_snapshot/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ def normalize_strings(token_sequence):
yield simple_token(token.STRING, repr(current_string))


def skip_trailing_comma(token_sequence):
token_sequence = list(token_sequence)

for index, token in enumerate(token_sequence):
if index + 1 < len(token_sequence):
next_token = token_sequence[index + 1]

if token.string == "," and next_token.string in ("]", ")", "}"):
continue
yield token


def normalize(token_sequence):
return skip_trailing_comma(normalize_strings(token_sequence))


ignore_tokens = (token.NEWLINE, token.ENDMARKER, token.NL)


Expand Down
55 changes: 55 additions & 0 deletions tests/test_inline_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,3 +927,58 @@ def __eq__(self,other):
assert s.flags == snapshot({"create"})
s = s.run()
assert s.flags == snapshot(set())


def test_trailing_comma(project):

project.setup(
"""\
from inline_snapshot import external, snapshot
class X:
def __init__(self, *args):
self.args = args
def __repr__(self):
return f"X({', '.join(map(repr,self.args))})"
def __eq__(self,other):
if not isinstance(other,X):
return NotImplemented
return self.args == other.args
def test_thing():
assert X("a" * 40, "a" * 40) == snapshot()
"""
)

project.format()

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

assert result.report == snapshot(
"""\
------------------------------- Create snapshots -------------------------------
+-------------------------------- test_file.py --------------------------------+
| @@ -19,4 +19,9 @@ |
| |
| |
| |
| def test_thing(): |
| - assert X("a" * 40, "a" * 40) == snapshot() |
| + assert X("a" * 40, "a" * 40) == snapshot( |
| + X( |
| + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| + ) |
| + ) |
+------------------------------------------------------------------------------+
These changes will be applied, because you used --inline-snapshot=create
"""
)

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

assert result.report == snapshot("")

0 comments on commit 5fce096

Please sign in to comment.