fix(tests): order-independence pin for S7 migration preflight (#763) - #780
Merged
Conversation
Root cause
----------
`tests/test_validation.py:35` and `tests/test_watchdog_unit.py:54`
unconditionally execute
sys.modules["utils.helpers"] = _helpers_mod
at module import time. The stub has `utc_now`, `utc_now_iso`,
`to_utc_iso`, `parse_iso_timestamp` — but is missing `iso_cutoff`. The
top-level `tests/conftest.py` preload installs the real `utils.helpers`
first, but either polluter overwrites it permanently when collected
ahead of `tests/git-sync/`.
`tests/git-sync/test_s7_reserve_instance_id.py::TestMigrationPreflight`
later does
sys.modules.pop("db.migrations", None)
from db.migrations import _find_duplicate_working_branches
The reimport chain hits `db/__init__.py` → `db/schedules.py` →
`from utils.helpers import iso_cutoff, ...` → ImportError, which the
test surfaces as `pytest.fail("S7 migration helper ... is missing")`.
All 4 preflight tests then fail in the full suite. They pass in
isolation because no polluter has run.
Fix
---
Per the issue's recommendation, add an order-independence pin in
`tests/git-sync/conftest.py`:
1. Autouse fixture restores the real `src/backend/utils/helpers.py`
module if the current `sys.modules["utils.helpers"]` lacks
`iso_cutoff` (the discriminator for the stub).
2. Same fixture evicts the cached `db` / `db.*` modules so the next
`from db.migrations import ...` reloads `db/__init__.py` and
`db/schedules.py` against the restored real `utils.helpers`.
Both steps are idempotent (no-op when state is already clean) and
cheap. They run before every git-sync test, defending against
arbitrary polluter ordering.
This is the integration-tier counterpart to the unit-tier pin added by
issue #714.
Related to #763.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
vybe
approved these changes
May 11, 2026
vybe
left a comment
Contributor
There was a problem hiding this comment.
LGTM — clean, minimal test infra fix. Root cause well-documented, fix is idempotent and correctly scoped to git-sync/.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
4 tests in
tests/git-sync/test_s7_reserve_instance_id.py::TestMigrationPreflightfail in the full integration suite but pass in isolation. Root cause is asys.modulespollution + reimport ordering bug. Fixed intests/git-sync/conftest.pywith an autouse pin that restores the realutils.helpersand evicts the cacheddb.*subtree before every git-sync test.Related to #763.
Root cause
tests/test_validation.py:35andtests/test_watchdog_unit.py:54execute, at module import time:The stub has
utc_now/utc_now_iso/to_utc_iso/parse_iso_timestamp— but noiso_cutoff. The top-leveltests/conftest.pypreload installs the realutils.helpersfirst, but either polluter overwrites it permanently when collected ahead oftests/git-sync/.TestMigrationPreflightthen does:The reimport chain hits
db/__init__.py→db/schedules.py→from utils.helpers import iso_cutoff, ...→ImportError, surfaced by the test aspytest.fail("S7 migration helper ... is missing").Verified by reproducing the exact ImportError outside pytest with
python -c "...install stub then reimport db.migrations...".Fix
Per the issue's recommendation, an order-independence pin in
tests/git-sync/conftest.py:utils.helpersif the currentsys.modulesentry lacksiso_cutoff(the discriminator for the stub).db/db.*modules so the nextfrom db.migrations import …reloadsdb/__init__.pyanddb/schedules.pyagainst the restored realutils.helpers.Both steps are idempotent (no-op when state is already clean), cheap, and run before every git-sync test. Integration-tier counterpart to the unit-tier pin added by #714.
Test plan
test_s7_reserve_instance_id.py::TestMigrationPreflightpasses in isolation —4 passedtest_validation.py → test_s7passes —23 passed, 3 skipped(was 4 failures pre-fix)test_watchdog_unit.py → test_s7passes —39 passedtest_s7_reserve_instance_id.pystill passes —12 passedtest_validation.py/test_watchdog_unit.pycollection errors are pre-existing and unrelated (verified viagit stash)Files
tests/git-sync/conftest.py_restore_real_utils_helpers()and_evict_db_module_cache()helpers + autouse_pin_module_state_for_db_reimportsfixture🤖 Generated with Claude Code