mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-21 16:34:32 +02:00
fix: Plugins are still loaded when running tests (#719)
* fix: defer plugin entrypoint loading until runtime Fixes simonw/sqlite-utils#713 * Lazy-load plugins when listing them 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. --------- Co-authored-by: Simon Willison <swillison@gmail.com>
This commit is contained in:
parent
733a67490f
commit
f448b61f5e
4 changed files with 43 additions and 4 deletions
|
|
@ -16,7 +16,7 @@ from sqlite_utils.db import (
|
|||
NoTable,
|
||||
quote_identifier,
|
||||
)
|
||||
from sqlite_utils.plugins import pm, get_plugins
|
||||
from sqlite_utils.plugins import ensure_plugins_loaded, pm, get_plugins
|
||||
from sqlite_utils.utils import maximize_csv_field_size_limit
|
||||
from sqlite_utils import recipes
|
||||
import textwrap
|
||||
|
|
@ -3412,6 +3412,7 @@ def plugins_list():
|
|||
click.echo(json.dumps(get_plugins(), indent=2))
|
||||
|
||||
|
||||
ensure_plugins_loaded()
|
||||
pm.hook.register_commands(cli=cli)
|
||||
cli.add_command(migrate)
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ from typing import (
|
|||
Tuple,
|
||||
)
|
||||
import uuid
|
||||
from sqlite_utils.plugins import pm
|
||||
from sqlite_utils.plugins import ensure_plugins_loaded, pm
|
||||
|
||||
try:
|
||||
iterdump = importlib.import_module("sqlite_dump").iterdump
|
||||
|
|
@ -401,6 +401,7 @@ class Database:
|
|||
self._registered_functions: set = set()
|
||||
self.use_counts_table = use_counts_table
|
||||
if execute_plugins:
|
||||
ensure_plugins_loaded()
|
||||
pm.hook.prepare_connection(conn=self.conn)
|
||||
self.strict = strict
|
||||
|
||||
|
|
|
|||
|
|
@ -6,13 +6,19 @@ from . import hookspecs
|
|||
|
||||
pm: pluggy.PluginManager = pluggy.PluginManager("sqlite_utils")
|
||||
pm.add_hookspecs(hookspecs)
|
||||
_plugins_loaded = False
|
||||
|
||||
if not getattr(sys, "_called_from_test", False):
|
||||
# Only load plugins if not running tests
|
||||
|
||||
def ensure_plugins_loaded() -> None:
|
||||
global _plugins_loaded
|
||||
if _plugins_loaded or getattr(sys, "_called_from_test", False):
|
||||
return
|
||||
pm.load_setuptools_entrypoints("sqlite_utils")
|
||||
_plugins_loaded = True
|
||||
|
||||
|
||||
def get_plugins() -> List[Dict[str, Union[str, List[str]]]]:
|
||||
ensure_plugins_loaded()
|
||||
plugins: List[Dict[str, Union[str, List[str]]]] = []
|
||||
plugin_to_distinfo = dict(pm.list_plugin_distinfo())
|
||||
for plugin in pm.get_plugins():
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from click.testing import CliRunner
|
|||
import click
|
||||
import importlib
|
||||
import pytest
|
||||
import sys
|
||||
from sqlite_utils import cli, Database, hookimpl, plugins
|
||||
|
||||
|
||||
|
|
@ -16,6 +17,36 @@ def _supports_pragma_function_list():
|
|||
db.close()
|
||||
|
||||
|
||||
def test_get_plugins_loads_setuptools_entrypoints_once(monkeypatch):
|
||||
calls = []
|
||||
monkeypatch.delattr(sys, "_called_from_test", raising=False)
|
||||
monkeypatch.setattr(plugins, "_plugins_loaded", False)
|
||||
monkeypatch.setattr(
|
||||
plugins.pm,
|
||||
"load_setuptools_entrypoints",
|
||||
lambda group: calls.append(group) or 0,
|
||||
)
|
||||
|
||||
plugins.get_plugins()
|
||||
plugins.get_plugins()
|
||||
|
||||
assert calls == ["sqlite_utils"]
|
||||
|
||||
|
||||
def test_get_plugins_does_not_load_setuptools_entrypoints_in_tests(monkeypatch):
|
||||
calls = []
|
||||
monkeypatch.setattr(sys, "_called_from_test", True, raising=False)
|
||||
monkeypatch.setattr(plugins, "_plugins_loaded", False)
|
||||
monkeypatch.setattr(
|
||||
plugins.pm,
|
||||
"load_setuptools_entrypoints",
|
||||
lambda group: calls.append(group) or 0,
|
||||
)
|
||||
|
||||
assert plugins.get_plugins() == []
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_register_commands():
|
||||
importlib.reload(cli)
|
||||
assert plugins.get_plugins() == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue