fix: Plugins are still loaded when running tests#719
Merged
Conversation
Keep plugin entrypoints from loading at module import time, but preserve the existing behavior of get_plugins() by loading entrypoints the first time plugins are listed outside tests.
There was a problem hiding this comment.
Pull request overview
This PR fixes leakage of system-installed plugins into the test suite by deferring setuptools entrypoint loading until plugin hooks are actually needed, and skipping that loading entirely when tests are running (sys._called_from_test).
Changes:
- Introduces
ensure_plugins_loaded()with a one-time_plugins_loadedguard and calls it fromget_plugins(). - Ensures plugins are loaded (when appropriate) immediately before
prepare_connectionandregister_commandshooks are invoked. - Adds targeted tests verifying entrypoints load once in non-test mode and never load during tests.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tests/test_plugins.py | Adds regression tests for one-time entrypoint loading and “never load in tests” behavior. |
| sqlite_utils/plugins.py | Moves entrypoint loading behind ensure_plugins_loaded() and calls it from get_plugins(). |
| sqlite_utils/db.py | Loads plugins right before invoking prepare_connection hooks in Database.__init__(). |
| sqlite_utils/cli.py | Loads plugins right before invoking register_commands hooks during CLI module initialization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Plugin entrypoints were being loaded at module import time, before tests set
sys._called_from_test, which allowed system-installed plugins to leak into test runs. This change defers entrypoint loading until plugin hooks are actually used, and only when not running tests.Changes
ensure_plugins_loaded()and a one-time_plugins_loadedguard insqlite_utils/plugins.pyDatabase.__init__()to callensure_plugins_loaded()beforeprepare_connectionhookscli.pyto callensure_plugins_loaded()beforeregister_commandshooksTesting
pytest -q tests/test_plugins.py2 passedFixes #713
📚 Documentation preview 📚: https://sqlite-utils--719.org.readthedocs.build/en/719/