fix(tests): anchor VCR redis cassette key to repo root

`os.path.relpath` with no `start` arg uses the current working
directory, so running pytest from a subdirectory produced a
different Redis key than running from the repo root. CI-recorded
cassettes and locally-replayed runs would silently miss each
other's cache.

Anchor the path to the repo root (derived from `__file__`) so the
key is stable regardless of CWD.

https://claude.ai/code/session_018uCx7pcrkdUJZrCVMaTdPx
This commit is contained in:
Claude 2026-05-01 23:09:18 +00:00 • committed by Cursor Agent
parent 628ce9d5ef
commit 6be7b77513
No known key found for this signature in database
2 changed files with 32 additions and 1 deletions

View file

@ -13,6 +13,8 @@ CASSETTE_REDIS_URL_ENV = "CASSETTE_REDIS_URL"
VCR_VERBOSE_ENV = "LITELLM_VCR_VERBOSE"
MAX_EPISODES_PER_CASSETTE = 50
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_log = logging.getLogger(__name__)
_passed_by_cassette_key: dict[str, bool] = {}
@ -22,7 +24,11 @@ def mark_test_outcome_for_cassette(cassette_path: str, passed: bool) -> None:
def redis_key_for(cassette_path: str) -> str:
rel = os.path.relpath(str(cassette_path))
abs_path = os.path.abspath(str(cassette_path))
try:
rel = os.path.relpath(abs_path, start=_REPO_ROOT)
except ValueError:
rel = os.path.basename(abs_path)
if rel.endswith(".yaml"):
rel = rel[: -len(".yaml")]
rel = rel.replace("/cassettes/", "/").lstrip("./")

View file

@ -81,6 +81,31 @@ def test_redis_key_normalizes_path_passed_by_pytest_recording():
)
def test_redis_key_is_stable_across_working_directories(tmp_path, monkeypatch):
repo_root = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
)
abs_cassette = os.path.join(
repo_root,
"tests/llm_translation/cassettes/test_anthropic/test_streaming.yaml",
)
monkeypatch.chdir(repo_root)
key_from_root = redis_key_for(abs_cassette)
monkeypatch.chdir(os.path.join(repo_root, "tests", "llm_translation"))
key_from_subdir = redis_key_for(abs_cassette)
monkeypatch.chdir(tmp_path)
key_from_tmp = redis_key_for(abs_cassette)
assert key_from_root == key_from_subdir == key_from_tmp
assert (
key_from_root
== "litellm:vcr:cassette:tests/llm_translation/test_anthropic/test_streaming"
)
class _FlakyRedis:
def __init__(self, inner, fail_on: str):
self._inner = inner