Skip to content

Commit ec79c08

Browse files
committed
sdk: tests: isolating xdg_state_home to address persistence files causing flaky tests
1 parent 455b1ec commit ec79c08

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

sdk/tests/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def pytest_runtest_setup(item) -> None:
4545
# ==== fixtures
4646

4747

48+
@pytest.fixture(autouse=True)
49+
def isolate_xdg_state(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
50+
"""Redirect XDG_STATE_HOME to a per-test tmpdir.
51+
52+
Prevents persistence files from accumulating across pytest sessions when
53+
pytest recycles its directory numbers (pytest-0, -1, -2, then back to -0),
54+
which would cause the same local_root hash to reuse stale upload records.
55+
"""
56+
monkeypatch.setenv("XDG_STATE_HOME", str(tmp_path / "state"))
57+
58+
4859
@pytest.fixture
4960
def anyio_backend():
5061
return "asyncio"

sdk/tests/test_uploads.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,9 @@ async def test_persistence_full_workflow(tmp_path: Path, client: Client) -> None
10911091
file1 = root / "file1.txt"
10921092
await file1.write_text("content1", encoding="utf-8")
10931093

1094+
file_exists = await file1.exists()
1095+
assert file_exists, "Test setup failed: file1 was not created successfully"
1096+
10941097
workload = UploadWorkload(client=client, local_root=Path(root), persist_state=True)
10951098

10961099
assert workload._persistence_manager is not None, (

0 commit comments

Comments
 (0)