Skip to content

Commit d2d6e25

Browse files
committed
fix: warn by default for expired cassettes
1 parent 57d4f29 commit d2d6e25

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

tests/conftest.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import json
33
import os
4+
import warnings
45
from typing import (
56
Any,
67
Dict,
@@ -70,26 +71,28 @@ def partner_prod_api_key():
7071
easypost.api_key = default_key
7172

7273

73-
def check_expired_cassettes(expiration_days: int = 180):
74+
@pytest.fixture(autouse=True)
75+
def check_expired_cassettes(expiration_days: int = 180, throw_error: bool = False):
7476
"""Checks for expired cassettes and throws errors if they are too old and must be re-recorded."""
7577
test_name = os.environ.get("PYTEST_CURRENT_TEST").split(":")[-1].split(" ")[0] # type: ignore
7678
cassette_filepath = os.path.join("tests", "cassettes", f"{test_name}.yaml")
7779

7880
if os.path.exists(cassette_filepath):
79-
expiration_age = datetime.datetime.now() - datetime.timedelta(days=expiration_days)
80-
cassette_age = datetime.datetime.fromtimestamp(os.stat(cassette_filepath).st_mtime)
81-
82-
if cassette_age < expiration_age:
83-
raise Exception(
84-
f"{cassette_filepath} is older than {expiration_days} and has expired. Please re-record the cassette."
85-
)
81+
cassette_timestamp = datetime.datetime.fromtimestamp(os.stat(cassette_filepath).st_mtime)
82+
expiration_timestamp = cassette_timestamp + datetime.timedelta(days=expiration_days)
83+
current_timestamp = datetime.datetime.now()
84+
85+
if current_timestamp > expiration_timestamp:
86+
error_message = f"{cassette_filepath} is older than {expiration_days} days and has expired. Please re-record the cassette." # noqa
87+
if throw_error:
88+
raise Exception(error_message)
89+
else:
90+
warnings.warn(error_message)
8691

8792

8893
@pytest.fixture(scope="session")
8994
def vcr_config():
9095
"""Setup the VCR config for the test suite."""
91-
check_expired_cassettes()
92-
9396
return {
9497
"match_on": [
9598
"body",

0 commit comments

Comments
 (0)