From f553a670216960043ec7e62ff35d42db78903ff0 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 1 May 2019 22:09:03 -0700 Subject: [PATCH] Don't load setuptools plugins during test runs Uses pattern from https://docs.pytest.org/en/latest/example/simple.html#detect-if-running-from-within-a-pytest-run Closes #438 --- datasette/plugins.py | 11 ++++++----- tests/test_api.py | 22 ++++++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/datasette/plugins.py b/datasette/plugins.py index 2d2c62e4..8981ace7 100644 --- a/datasette/plugins.py +++ b/datasette/plugins.py @@ -1,15 +1,16 @@ import importlib import pluggy +import sys from . import hookspecs -DEFAULT_PLUGINS = ( - "datasette.publish.heroku", - "datasette.publish.now", -) +DEFAULT_PLUGINS = ("datasette.publish.heroku", "datasette.publish.now") pm = pluggy.PluginManager("datasette") pm.add_hookspecs(hookspecs) -pm.load_setuptools_entrypoints("datasette") + +if not hasattr(sys, "_called_from_test"): + # Only load plugins if not running tests + pm.load_setuptools_entrypoints("datasette") # Load default plugins for plugin in DEFAULT_PLUGINS: diff --git a/tests/test_api.py b/tests/test_api.py index bf8b87b9..07974e26 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1060,18 +1060,16 @@ def test_inspect_json(app_client): def test_plugins_json(app_client): - response = app_client.get( - "/-/plugins.json" - ) - # This will include any plugins that have been installed into the - # current virtual environment, so we only check for the presence of - # the one we know will definitely be There - assert { - 'name': 'my_plugin.py', - 'static': False, - 'templates': False, - 'version': None, - } in response.json + response = app_client.get("/-/plugins.json") + assert [ + {"name": "my_plugin.py", "static": False, "templates": False, "version": None}, + { + "name": "my_plugin_2.py", + "static": False, + "templates": False, + "version": None, + } + ] == sorted(response.json, key=lambda p: p["name"]) def test_versions_json(app_client):