diff --git a/localstack_snapshot/snapshots/report.py b/localstack_snapshot/snapshots/report.py index 450b4a5..4b1de3f 100644 --- a/localstack_snapshot/snapshots/report.py +++ b/localstack_snapshot/snapshots/report.py @@ -1,4 +1,5 @@ import logging +import re from localstack_snapshot.snapshots import SnapshotMatchResult @@ -29,6 +30,8 @@ "underlined": 4, } +_regular_json_path_chars_regex = re.compile("[a-zA-Z0-9_-]+") + class PatchPath(str): """ @@ -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 += "." diff --git a/tests/test_snapshots.py b/tests/test_snapshots.py index 4b986b4..e4436fe 100644 --- a/tests/test_snapshots.py +++ b/tests/test_snapshots.py @@ -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():