Repository navigation
fix(tests): fix sys.modules leaks causing 47 cross-file test errors (#752) - #756
Merged
Merged
Conversation
…752) test_ip_rate_limit_fix.py was unconditionally overwriting sys.modules["fastapi"] with a stripped stub (no `status` attr) and never restoring it, breaking every subsequent test that imports any module doing `from fastapi import status`. Converted _load_public_router() to accept monkeypatch and use monkeypatch.setitem for all sys.modules mutations so pytest auto-restores state after each test. Added missing stubs (dependencies, models, services.upload_service) to prevent the transitive import chain from reaching the real dependencies.py. test_sync_health_service.py was failing when run after test_fleet_status_resilience.py because the latter installs services.agent_client as a SimpleNamespace without AgentClient. Fixed by evicting and re-stubbing services.agent_client in the tmp_db fixture via monkeypatch. Fixes #752 Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
4 tasks
Merged
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
test_ip_rate_limit_fix.pypermanently overwrotesys.modules[\"fastapi\"]with a stripped stub (nostatus,security, or other attrs) and never restored it, causingImportError: cannot import name 'status' from 'fastapi'in every test that ran after it in the same session — 47 errors across the 7 files listed in fix(tests): update check_login_rate_limit import across 7 test files #752test_sync_health_service.pyfailed aftertest_fleet_status_resilience.pybecause the latter installed aSimpleNamespacestub forservices.agent_clientwithoutAgentClient, breakingsync_health_service.py's module-level importChanges
tests/test_ip_rate_limit_fix.py: Refactored_load_public_router()to acceptmonkeypatchand usemonkeypatch.setitemfor allsys.modulesmutations (fastapi, fastapi.*, database, dependencies, models, all service stubs). Added missingdependencies,models, andservices.upload_servicestubs that were causing the transitive import chain to fail. Added autouseload_modulefixture toTestPerTokenRateLimit(previously called_load_public_router()directly with no cleanup).tests/unit/test_sync_health_service.py: Extendedtmp_dbfixture to evict and re-stubservices.agent_clientviamonkeypatch, ensuring a cleanAgentClient-bearing stub is always present whensync_health_service.pyis imported, regardless of what prior test files installed.Test Plan
pytest tests/test_ip_rate_limit_fix.py tests/unit/test_login_rate_limit_split.py tests/unit/test_webhook_rate_limit_inprocess.py tests/unit/test_webhook_rate_limit_toctou.py tests/unit/test_fleet_status_resilience.py tests/unit/test_sync_health_service.py tests/test_github_pat_propagation_unit.pypytest tests/ --collect-onlyreports 0 collection errors for all 7 files (only 3 pre-existing unrelated errors remain: trinity_cli ×2, twilio ×1)Fixes #752
Generated with Claude Code