diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index c041745caa46f9..05e777a45c5dc1 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -18,7 +18,6 @@ import requests import yaml from django.core.cache import cache -from django.utils import timezone import sentry from sentry.types.activity import ActivityType @@ -179,10 +178,10 @@ def inner(active=True): _snapshot_writeback = "overwrite" elif _snapshot_writeback != "new": _snapshot_writeback = None -_test_base = os.path.realpath( +repo_abs_path = os.path.realpath( os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sentry.__file__)))) ) -_yaml_snap_re = re.compile(r"^---\r?\n(.*?)\r?\n---\r?\n(.*)$", re.DOTALL) +SNAPSHOT_REGEX = re.compile(r"^---\r?\n.*?\r?\n---\r?\n(.*)$", re.DOTALL) @pytest.fixture @@ -200,14 +199,13 @@ def ignore_aliases(self, data) -> bool: return True -def read_snapshot_file(reference_file: str) -> tuple[str, str]: +def read_snapshot_file(reference_file: str) -> str: with open(reference_file, encoding="utf-8") as f: - match = _yaml_snap_re.match(f.read()) + match = SNAPSHOT_REGEX.match(f.read()) if match is None: raise OSError() - header, refval = match.groups() - return (header, refval) + return match.group(1) InequalityComparator = Callable[[str, str], bool | str] @@ -267,7 +265,7 @@ def inner( ) try: - _, refval = read_snapshot_file(reference_file) + refval = read_snapshot_file(reference_file) except OSError: refval = "" @@ -277,27 +275,15 @@ def inner( if _snapshot_writeback is not None and is_unequal: os.makedirs(os.path.dirname(reference_file), exist_ok=True) - source = os.path.realpath(str(request.node.fspath)) - if source.startswith(_test_base + os.path.sep): - source = source[len(_test_base) + 1 :] + test_file = os.path.realpath(str(request.node.fspath)) + test_name = request.node.originalname + if test_file.startswith(repo_abs_path + os.path.sep): + test_file = test_file.replace(repo_abs_path + os.path.sep, "") if _snapshot_writeback == "new": reference_file += ".new" with open(reference_file, "w") as f: - f.write( - "---\n%s\n---\n%s\n" - % ( - yaml.safe_dump( - { - "created": timezone.now().isoformat(), - "creator": "sentry", - "source": source, - }, - indent=2, - default_flow_style=False, - ).rstrip(), - output, - ) - ) + header = f"---\nsource: {test_file}::{test_name}\n---" + f.write(f"{header}\n{output}\n") elif is_unequal: __tracebackhide__ = True if isinstance(is_unequal, str):