From e02fe084e26861b4422572c7ac877f05bd58e9f1 Mon Sep 17 00:00:00 2001 From: Benjamin Simon Date: Fri, 20 Jun 2025 23:51:33 +0200 Subject: [PATCH] improve reporting suggestion for snapshot skips --- localstack_snapshot/snapshots/report.py | 9 ++++++++- tests/test_snapshots.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/localstack_snapshot/snapshots/report.py b/localstack_snapshot/snapshots/report.py index 450b4a5..7ddb338 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, } +_special_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 _special_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():