From eade743b3bfd2085c4b39c7f3b7b086496e83b55 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:02:15 -0800 Subject: [PATCH 1/8] s/`_test_base`/`repo_abs_path` --- src/sentry/testutils/pytest/fixtures.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index c041745caa46f9..02431b2eac0418 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -179,7 +179,7 @@ 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) @@ -278,8 +278,8 @@ 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 :] + if source.startswith(repo_abs_path + os.path.sep): + source = source[len(repo_abs_path) + 1 :] if _snapshot_writeback == "new": reference_file += ".new" with open(reference_file, "w") as f: From 8c87d09494ead7b7fbeda601fa2f117af5f21b71 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:03:52 -0800 Subject: [PATCH 2/8] s/`_yaml_snap_re`/`SNAPSHOT_REGEX` --- src/sentry/testutils/pytest/fixtures.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index 02431b2eac0418..7e7593d4834795 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -182,7 +182,7 @@ def inner(active=True): 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 @@ -202,7 +202,7 @@ def ignore_aliases(self, data) -> bool: def read_snapshot_file(reference_file: str) -> tuple[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() From 2a601b6258a9bf2973aa5697a7a22d9261e6505e Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:08:26 -0800 Subject: [PATCH 3/8] s/`source`/`test_file` --- src/sentry/testutils/pytest/fixtures.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index 7e7593d4834795..2a08926a757c64 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -277,9 +277,9 @@ 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(repo_abs_path + os.path.sep): - source = source[len(repo_abs_path) + 1 :] + test_file = os.path.realpath(str(request.node.fspath)) + if test_file.startswith(repo_abs_path + os.path.sep): + test_file = test_file[len(repo_abs_path) + 1 :] if _snapshot_writeback == "new": reference_file += ".new" with open(reference_file, "w") as f: @@ -290,7 +290,7 @@ def inner( { "created": timezone.now().isoformat(), "creator": "sentry", - "source": source, + "source": test_file, }, indent=2, default_flow_style=False, From 4aa53f3e67361791d563a15fb995d15eb4bb20bc Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:04:24 -0800 Subject: [PATCH 4/8] stop capturing header in snapshot regex --- src/sentry/testutils/pytest/fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index 2a08926a757c64..d9798de060e536 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -182,7 +182,7 @@ def inner(active=True): repo_abs_path = os.path.realpath( os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sentry.__file__)))) ) -SNAPSHOT_REGEX = 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 From 5aa3262060d0fdbf58816937826c0e336405ab52 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:09:00 -0800 Subject: [PATCH 5/8] only return snapshot output from `read_snapshot_file` --- src/sentry/testutils/pytest/fixtures.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index d9798de060e536..b34492b0aca8b6 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -200,14 +200,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 = 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 +266,7 @@ def inner( ) try: - _, refval = read_snapshot_file(reference_file) + refval = read_snapshot_file(reference_file) except OSError: refval = "" From bd2448e768c661f4f25bc31bd7e69e7ae6a29241 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:11:07 -0800 Subject: [PATCH 6/8] use `replace` for stripping repo path from test file path --- src/sentry/testutils/pytest/fixtures.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index b34492b0aca8b6..af55d656a918d2 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -278,7 +278,7 @@ def inner( os.makedirs(os.path.dirname(reference_file), exist_ok=True) test_file = os.path.realpath(str(request.node.fspath)) if test_file.startswith(repo_abs_path + os.path.sep): - test_file = test_file[len(repo_abs_path) + 1 :] + 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: From 980de8b0fd3b1b6886087f1f590be536282f448d Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:51:40 -0800 Subject: [PATCH 7/8] only include source in snapshot header --- src/sentry/testutils/pytest/fixtures.py | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index af55d656a918d2..37440843f48ee7 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 @@ -282,21 +281,8 @@ def inner( 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": test_file, - }, - indent=2, - default_flow_style=False, - ).rstrip(), - output, - ) - ) + header = f"---\nsource: {test_file}\n---" + f.write(f"{header}\n{output}\n") elif is_unequal: __tracebackhide__ = True if isinstance(is_unequal, str): From 10327cd55f9ebe986a11d3eb06a2be82b656812e Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Tue, 11 Nov 2025 18:52:04 -0800 Subject: [PATCH 8/8] include test name is source header --- src/sentry/testutils/pytest/fixtures.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sentry/testutils/pytest/fixtures.py b/src/sentry/testutils/pytest/fixtures.py index 37440843f48ee7..05e777a45c5dc1 100644 --- a/src/sentry/testutils/pytest/fixtures.py +++ b/src/sentry/testutils/pytest/fixtures.py @@ -276,12 +276,13 @@ def inner( if _snapshot_writeback is not None and is_unequal: os.makedirs(os.path.dirname(reference_file), exist_ok=True) 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: - header = f"---\nsource: {test_file}\n---" + header = f"---\nsource: {test_file}::{test_name}\n---" f.write(f"{header}\n{output}\n") elif is_unequal: __tracebackhide__ = True