Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion localstack_snapshot/snapshots/report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re

from localstack_snapshot.snapshots import SnapshotMatchResult

Expand Down Expand Up @@ -29,6 +30,8 @@
"underlined": 4,
}

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


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

Expand Down
8 changes: 8 additions & 0 deletions tests/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ def test_json_diff_format():
assert _format_json_path(path) == '"$.."'
path = [1, 1, 0, "SomeKey"]
assert _format_json_path(path) == '"$..SomeKey"'
path = ["Some:Key"]
assert _format_json_path(path) == "\"$..'Some:Key'\""
path = ["Some.Key"]
assert _format_json_path(path) == "\"$..'Some.Key'\""
path = ["Some-Key"]
assert _format_json_path(path) == '"$..Some-Key"'
path = ["Some0Key"]
assert _format_json_path(path) == '"$..Some0Key"'


def test_sorting_transformer():
Expand Down