Skip to content

[Test Improver] test: replace gc.collect+sleep teardown with shutil.rmtree(ignore_errors=True)#395

Draft
danielmeppiel wants to merge 1 commit intomainfrom
test-assist/fix-teardown-gc-sleep-23368659522-c9b9bd0a62f0be46
Draft

[Test Improver] test: replace gc.collect+sleep teardown with shutil.rmtree(ignore_errors=True)#395
danielmeppiel wants to merge 1 commit intomainfrom
test-assist/fix-teardown-gc-sleep-23368659522-c9b9bd0a62f0be46

Conversation

@danielmeppiel
Copy link
Collaborator

🤖 Test Improver — automated AI assistant for test improvements

Closes #185

Goal and Rationale

Several test classes used gc.collect() + time.sleep(0.1) in tearDown as a workaround for Windows file-lock timing issues with tempfile.TemporaryDirectory. This pattern was flagged during PR #181 review as fragile and adds unnecessary test latency (~100–500ms per test class).

This PR replaces that pattern with tempfile.mkdtemp() + shutil.rmtree(ignore_errors=True), which is:

  • Faster: no artificial delays
  • Cleaner: no gc hacks
  • Correct: ignore_errors=True silently handles locked files on Windows without retrying or sleeping

Approach

For each affected class:

  1. setUp: Replace tempfile.TemporaryDirectory() with tempfile.mkdtemp() (returns path string directly, no .name needed)
  2. tearDown: Replace gc.collect() + sleep + .cleanup() with shutil.rmtree(path, ignore_errors=True)

Affected files:

File Changes
tests/unit/test_deps.py 1 class: TestLoadApmConfig
tests/unit/test_env_variables.py 1 class: TestEnvironmentVariablesHandling
tests/unit/test_helpers.py Removed empty tearDown (no temp dir was used)
tests/unit/test_vscode_adapter.py 4 classes fixed
tests/unit/workflow/test_workflow.py Removed safe_rmdir helper; simplified 2 tearDowns

Test Status

✅ All 2261 tests pass

python3 -m uv run pytest tests/unit/ --no-header -q
# 2261 passed in 11.13s

Generated by Daily Test Improver ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@b87234850bf9664d198f28a02df0f937d0447295

…ors=True)

Closes #185

Replace fragile tearDown patterns (gc.collect + time.sleep) used to work
around Windows file-lock timing issues with TemporaryDirectory. Switch to
tempfile.mkdtemp() + shutil.rmtree(ignore_errors=True) which is cleaner,
faster, and avoids unnecessary test latency.

Affected files:
- tests/unit/test_deps.py
- tests/unit/test_env_variables.py
- tests/unit/test_helpers.py (removed empty tearDown)
- tests/unit/test_vscode_adapter.py (4 test classes)
- tests/unit/workflow/test_workflow.py (removed safe_rmdir helper)

All 2261 tests pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Clean up tearDown patterns in unit tests (gc.collect + sleep)

1 participant