From 6be7b77513993c0656249fa1922b05f2f42a3ed8 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 1 May 2026 23:09:18 +0000 Subject: [PATCH] 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 --- tests/_vcr_redis_persister.py | 8 +++++- .../test_vcr_redis_persister.py | 25 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/_vcr_redis_persister.py b/tests/_vcr_redis_persister.py index 4d72a1142bb..a6ed448f1cb 100644 --- a/tests/_vcr_redis_persister.py +++ b/tests/_vcr_redis_persister.py @@ -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("./") diff --git a/tests/llm_translation/test_vcr_redis_persister.py b/tests/llm_translation/test_vcr_redis_persister.py index 853558150c1..6e62e4491cb 100644 --- a/tests/llm_translation/test_vcr_redis_persister.py +++ b/tests/llm_translation/test_vcr_redis_persister.py @@ -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