Skip to content

Commit ec575d7

Browse files
bentskutiurin
andauthored
improve reporting suggestion for snapshot skips (#14)
Co-authored-by: Misha Tiurin <[email protected]>
1 parent b302a67 commit ec575d7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

localstack_snapshot/snapshots/report.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23

34
from localstack_snapshot.snapshots import SnapshotMatchResult
45

@@ -29,6 +30,8 @@
2930
"underlined": 4,
3031
}
3132

33+
_regular_json_path_chars_regex = re.compile("[a-zA-Z0-9_-]+")
34+
3235

3336
class PatchPath(str):
3437
"""
@@ -52,7 +55,11 @@ def _format_json_path(path: list):
5255
json_str = "$.."
5356
for idx, elem in enumerate(path):
5457
if not isinstance(elem, int):
55-
json_str += str(elem)
58+
_elem = str(elem)
59+
# we want to wrap in single quotes parts with special characters so that users can copy-paste them directly
60+
if not _regular_json_path_chars_regex.fullmatch(_elem):
61+
_elem = f"'{_elem}'"
62+
json_str += _elem
5663
if idx < len(path) - 1 and not json_str.endswith(".."):
5764
json_str += "."
5865

tests/test_snapshots.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,14 @@ def test_json_diff_format():
209209
assert _format_json_path(path) == '"$.."'
210210
path = [1, 1, 0, "SomeKey"]
211211
assert _format_json_path(path) == '"$..SomeKey"'
212+
path = ["Some:Key"]
213+
assert _format_json_path(path) == "\"$..'Some:Key'\""
214+
path = ["Some.Key"]
215+
assert _format_json_path(path) == "\"$..'Some.Key'\""
216+
path = ["Some-Key"]
217+
assert _format_json_path(path) == '"$..Some-Key"'
218+
path = ["Some0Key"]
219+
assert _format_json_path(path) == '"$..Some0Key"'
212220

213221

214222
def test_sorting_transformer():

0 commit comments

Comments
 (0)