Skip to content

improve reporting suggestion for snapshot skips #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,
}

_special_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 _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 += "."

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