File tree Expand file tree Collapse file tree 2 files changed +16
-1
lines changed
localstack_snapshot/snapshots Expand file tree Collapse file tree 2 files changed +16
-1
lines changed Original file line number Diff line number Diff line change 11import logging
2+ import re
23
34from localstack_snapshot .snapshots import SnapshotMatchResult
45
2930 "underlined" : 4 ,
3031}
3132
33+ _special_json_path_chars_regex = re .compile ("[a-zA-Z0-9_-]+" )
34+
3235
3336class 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 _special_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
Original file line number Diff line number Diff 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
214222def test_sorting_transformer ():
You can’t perform that action at this time.
0 commit comments