fix(smoke): force-override OMNIVOICE_DATA_DIR + purge cached backend modules - #95
Conversation
…modules The smoke test used `os.environ.setdefault()` to point at the frozen fixture, which silently skipped when a prior test in the suite had already set the env var. Combined with `core.config` caching `DB_PATH` at module import time, this left smoke tests pointed at the wrong DB once Wave 1's services tests pre-imported `main` with their own temp state. Exposed by Wave 2's additional tests (PR #94) pushing collection order past the tipping point, but the underlying pollution existed since Wave 1 merged — Wave 3 CI passed only by collection-order luck. Fix mirrors the `sys.modules` purge pattern that `tests/backend/services/conftest.py` already uses. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughThe smoke test fixture is hardened for isolation: ChangesTest Isolation Fixture
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@tests/smoke/test_boot_smoke.py`:
- Around line 47-50: The test currently sets os.environ["OMNIVOICE_DATA_DIR"] =
str(_FIXTURE_COPY) at import time and never restores it; change this to scope
the override to the fixture lifetime by moving the environment assignment into
the test fixture (or using pytest's monkeypatch.setenv) and restore the original
value on teardown; reference the same symbols OMNIVOICE_DATA_DIR, _FIXTURE_COPY
and os.environ when implementing the fix and, if the code under test reads
core.config at import time, ensure the env is set before importing (or reload
core.config after setting) so the temporary value is used only for that fixture
and not left as a process-global.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 8584789f-d3cb-4aea-a2e5-51e557f10cfc
📒 Files selected for processing (1)
tests/smoke/test_boot_smoke.py
| # Point backend.core.config.get_app_data_dir() at the COPY. Force-override | ||
| # (not setdefault) — earlier tests in a full-suite run may have set it to | ||
| # their own temp dir, and core.config caches DB_PATH at first import. | ||
| os.environ["OMNIVOICE_DATA_DIR"] = str(_FIXTURE_COPY) |
There was a problem hiding this comment.
Scope OMNIVOICE_DATA_DIR override to fixture lifetime to avoid cross-test leakage.
Line 50 mutates process-global env at import time and never restores it. That can pollute later tests if collection/order changes, recreating the same class of flake in reverse.
Proposed fix
-# Point backend.core.config.get_app_data_dir() at the COPY. Force-override
-# (not setdefault) — earlier tests in a full-suite run may have set it to
-# their own temp dir, and core.config caches DB_PATH at first import.
-os.environ["OMNIVOICE_DATA_DIR"] = str(_FIXTURE_COPY)
-
-
`@pytest.fixture`(scope="module")
def client():
+ prev_data_dir = os.environ.get("OMNIVOICE_DATA_DIR")
+ os.environ["OMNIVOICE_DATA_DIR"] = str(_FIXTURE_COPY)
# Purge any cached backend modules from earlier tests in the suite —
# `core.config` reads OMNIVOICE_DATA_DIR at import time and caches DB_PATH,
# so a prior import with a different value would survive the env-var
# override above. Same pattern as tests/backend/services/conftest.py.
for mod in list(sys.modules):
if mod == "main" or mod == "core" or mod.startswith("core.") or mod.startswith("api.") or mod.startswith("services."):
sys.modules.pop(mod, None)
from fastapi.testclient import TestClient
from main import app
- return TestClient(app, client=("127.0.0.1", 50000))
+ try:
+ yield TestClient(app, client=("127.0.0.1", 50000))
+ finally:
+ if prev_data_dir is None:
+ os.environ.pop("OMNIVOICE_DATA_DIR", None)
+ else:
+ os.environ["OMNIVOICE_DATA_DIR"] = prev_data_dir🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@tests/smoke/test_boot_smoke.py` around lines 47 - 50, The test currently sets
os.environ["OMNIVOICE_DATA_DIR"] = str(_FIXTURE_COPY) at import time and never
restores it; change this to scope the override to the fixture lifetime by moving
the environment assignment into the test fixture (or using pytest's
monkeypatch.setenv) and restore the original value on teardown; reference the
same symbols OMNIVOICE_DATA_DIR, _FIXTURE_COPY and os.environ when implementing
the fix and, if the code under test reads core.config at import time, ensure the
env is set before importing (or reload core.config after setting) so the
temporary value is used only for that fixture and not left as a process-global.
Summary
The smoke test used
os.environ.setdefault()to point at the frozen fixture, which silently skipped when a prior test in the suite had already set the env var. Combined withcore.configcachingDB_PATHat module import time, this left smoke tests pointed at the wrong DB once Wave 1's services tests pre-importedmainwith their own temp state.sys.modulespurge pattern thattests/backend/services/conftest.pyalready uses.Test plan
pytest tests/smoke/→ 4 passed (no regression vs Phase 0 smoke matrix)pytest tests/backend/services tests/smoke/→ 31 passed (used to fail at the smoke step)pytest tests/ -q --ignore=tests/manual→ 292 passed, 6 skipped, 12 xfailed, 1 xpassed, 0 failures🤖 Generated with Claude Code
Summary by CodeRabbit