Skip to content

Commit 34d102e

Browse files
lobsterkatieandrewshie-sentry
authored andcommitted
ref(tests): Refactor snapshotter and simplify header (#103243)
This is a small refactor to the snapshotter we use in tests, including the following changes: - Rename a few variables for clarity. - Stop capturing the header when reading a snapshot file, since we never use it. - Remove timestamp and creator from the header. The timestamp often causes merge conflicts (and the same information can be gleaned from the commit date), and the creator is hard-coded as 'sentry', which... yeah, okay, but is that news? - Add test name to `source` entry in header. Yes, by default the test name is included in the snapshot file's path, but that's configurable, so might not always be true. Putting it in the header guarantees the information is easily available. Note that this PR doesn't actually change the contents of any snapshots. Because snapshot files are only written when they change, and the header isn't actually anything we match on, we have the option to let the dates be removed organically, or to remove them in batches in separate PRs, but either way, this change won't make any current snapshot tests fail in the meantime.
1 parent 0a635e9 commit 34d102e

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

src/sentry/testutils/pytest/fixtures.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import requests
1919
import yaml
2020
from django.core.cache import cache
21-
from django.utils import timezone
2221

2322
import sentry
2423
from sentry.types.activity import ActivityType
@@ -179,10 +178,10 @@ def inner(active=True):
179178
_snapshot_writeback = "overwrite"
180179
elif _snapshot_writeback != "new":
181180
_snapshot_writeback = None
182-
_test_base = os.path.realpath(
181+
repo_abs_path = os.path.realpath(
183182
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sentry.__file__))))
184183
)
185-
_yaml_snap_re = re.compile(r"^---\r?\n(.*?)\r?\n---\r?\n(.*)$", re.DOTALL)
184+
SNAPSHOT_REGEX = re.compile(r"^---\r?\n.*?\r?\n---\r?\n(.*)$", re.DOTALL)
186185

187186

188187
@pytest.fixture
@@ -200,14 +199,13 @@ def ignore_aliases(self, data) -> bool:
200199
return True
201200

202201

203-
def read_snapshot_file(reference_file: str) -> tuple[str, str]:
202+
def read_snapshot_file(reference_file: str) -> str:
204203
with open(reference_file, encoding="utf-8") as f:
205-
match = _yaml_snap_re.match(f.read())
204+
match = SNAPSHOT_REGEX.match(f.read())
206205
if match is None:
207206
raise OSError()
208207

209-
header, refval = match.groups()
210-
return (header, refval)
208+
return match.group(1)
211209

212210

213211
InequalityComparator = Callable[[str, str], bool | str]
@@ -267,7 +265,7 @@ def inner(
267265
)
268266

269267
try:
270-
_, refval = read_snapshot_file(reference_file)
268+
refval = read_snapshot_file(reference_file)
271269
except OSError:
272270
refval = ""
273271

@@ -277,27 +275,15 @@ def inner(
277275

278276
if _snapshot_writeback is not None and is_unequal:
279277
os.makedirs(os.path.dirname(reference_file), exist_ok=True)
280-
source = os.path.realpath(str(request.node.fspath))
281-
if source.startswith(_test_base + os.path.sep):
282-
source = source[len(_test_base) + 1 :]
278+
test_file = os.path.realpath(str(request.node.fspath))
279+
test_name = request.node.originalname
280+
if test_file.startswith(repo_abs_path + os.path.sep):
281+
test_file = test_file.replace(repo_abs_path + os.path.sep, "")
283282
if _snapshot_writeback == "new":
284283
reference_file += ".new"
285284
with open(reference_file, "w") as f:
286-
f.write(
287-
"---\n%s\n---\n%s\n"
288-
% (
289-
yaml.safe_dump(
290-
{
291-
"created": timezone.now().isoformat(),
292-
"creator": "sentry",
293-
"source": source,
294-
},
295-
indent=2,
296-
default_flow_style=False,
297-
).rstrip(),
298-
output,
299-
)
300-
)
285+
header = f"---\nsource: {test_file}::{test_name}\n---"
286+
f.write(f"{header}\n{output}\n")
301287
elif is_unequal:
302288
__tracebackhide__ = True
303289
if isinstance(is_unequal, str):

0 commit comments

Comments
 (0)