fix(cleanup): isolate tempdir in cache invalidation test to prevent cross-test pollution
CI / benchmark-publish (pull_request) Has been skipped
CI / lint (pull_request) Successful in 28s
CI / typecheck (pull_request) Successful in 46s
CI / quality (pull_request) Successful in 49s
CI / security (pull_request) Successful in 1m31s
CI / build (pull_request) Successful in 28s
CI / helm (pull_request) Successful in 25s
CI / push-validation (pull_request) Successful in 18s
CI / integration_tests (pull_request) Successful in 4m12s
CI / e2e_tests (pull_request) Successful in 4m34s
CI / unit_tests (pull_request) Successful in 8m55s
CI / coverage (pull_request) Successful in 13m33s
CI / docker (pull_request) Successful in 1m25s
CI / status-check (pull_request) Successful in 1s
CI / benchmark-regression (pull_request) Successful in 1h0m55s

The 'scan after purge returns empty stale items' scenario was failing in CI
because _get_sandbox_dirs() re-reads tempfile.gettempdir() after cache
invalidation, which returns the nox session temp dir containing sandbox
directories from other concurrently-running tests.

Fix: patch tempfile.gettempdir to return the test's isolated temp directory
throughout the entire scan -> purge -> scan cycle, preventing unrelated
sandbox directories from appearing in the second scan's results.
This commit is contained in:
2026-04-14 10:37:10 +00:00
parent 0534130fc7
commit 05ac727028
@@ -117,16 +117,28 @@ def step_cache_inv_scan_purge_scan(context: Context) -> None:
svc = context.cache_inv_service
real_dirs = context.cache_inv_real_dirs
# First scan: inject the real dirs into the cache
svc._sandbox_dirs_cache = list(real_dirs)
context.cache_inv_first_scan = svc.scan()
tmp = context.cache_inv_tmp
# Purge: actually delete the directories; cache should be invalidated
context.cache_inv_purge_report = svc.purge()
# Patch tempfile.gettempdir to return our isolated temp dir throughout
# the entire scan -> purge -> scan cycle. This prevents _get_sandbox_dirs()
# from picking up unrelated sandbox directories created by other tests
# running in the same nox session.
with patch(
"cleveragents.application.services.cleanup_service.tempfile"
) as mock_tmp:
mock_tmp.gettempdir.return_value = str(tmp)
# Second scan: cache was invalidated, so _get_sandbox_dirs re-reads /tmp
# The real dirs are now gone, so the scan should find nothing stale.
context.cache_inv_second_scan = svc.scan()
# First scan: inject the real dirs into the cache
svc._sandbox_dirs_cache = list(real_dirs)
context.cache_inv_first_scan = svc.scan()
# Purge: actually delete the directories; cache should be invalidated
context.cache_inv_purge_report = svc.purge()
# Second scan: cache was invalidated, so _get_sandbox_dirs re-reads
# our isolated temp dir. The real dirs are now gone, so the scan
# should find nothing stale.
context.cache_inv_second_scan = svc.scan()
@when("cache invalidation calls purge then scan")