Skip to content

Commit 7c73041

Browse files
authored
Improve missing cassette errors (#583)
* Improve missing cassette errors When the freeze file or the cassette file is missing, we get pretty werid errors. Let's try to make them more explicit. * Add back fixture with a comment. * Move check in freezer
1 parent 8d3abad commit 7c73041

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

tests/conftest.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ def pytest_bdd_before_step(request, feature, scenario, step, step_func):
6262

6363
context = tracer.get_call_context()
6464
span = tracer.start_span(
65-
step.type,
66-
resource=step.name,
67-
span_type=step.type,
68-
child_of=context,
65+
step.type, resource=step.name, span_type=step.type, child_of=context,
6966
)
7067
setattr(step_func, "__dd_span__", span)
7168

@@ -211,11 +208,9 @@ def context(vcr, unique, unique_lower, freezer):
211208
@pytest.fixture(scope="session")
212209
def record_mode(request):
213210
"""Manage compatibility with DD client libraries."""
214-
return {
215-
"false": "none",
216-
"true": "rewrite",
217-
"none": "new_episodes",
218-
}[os.getenv("RECORD", "false").lower()]
211+
return {"false": "none", "true": "rewrite", "none": "new_episodes",}[
212+
os.getenv("RECORD", "false").lower()
213+
]
219214

220215

221216
def _disable_recording():
@@ -225,7 +220,7 @@ def _disable_recording():
225220

226221
@pytest.fixture(scope="session")
227222
def disable_recording(request):
228-
"""Disable VCR.py integration."""
223+
"""Disable VCR.py integration. This overrides a pytest-recording fixture."""
229224
return _disable_recording()
230225

231226

@@ -257,9 +252,23 @@ def freezer(default_cassette_name, record_mode, vcr):
257252
with pathlib.Path(vcr._path).with_suffix(".frozen").open("w+") as f:
258253
f.write(freeze_at)
259254
else:
260-
with pathlib.Path(vcr._path).with_suffix(".frozen").open("r") as f:
255+
freeze_file = pathlib.Path(vcr._path).with_suffix(".frozen")
256+
if not freeze_file.exists():
257+
msg = (
258+
"Time file '{}' not found: create one setting `RECORD=true` or "
259+
"ignore it using `RECORD=none`".format(freeze_file)
260+
)
261+
raise RuntimeError(msg)
262+
with freeze_file.open("r") as f:
261263
freeze_at = f.readline().strip()
262264

265+
if not pathlib.Path(vcr._path).exists():
266+
msg = (
267+
"Cassette '{}' not found: create one setting `RECORD=true` or "
268+
"ignore it using `RECORD=none`".format(vcr._path)
269+
)
270+
raise RuntimeError(msg)
271+
263272
return freeze_time(parser.isoparse(freeze_at))
264273

265274

0 commit comments

Comments
 (0)