mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Fixed 'datasette plugins' command, with tests - closes #802
This commit is contained in:
parent
033a1bb22c
commit
f786033a5f
6 changed files with 92 additions and 61 deletions
|
|
@ -626,9 +626,9 @@ class Datasette:
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
def _plugins(self, request):
|
def _plugins(self, request=None, all=False):
|
||||||
ps = list(get_plugins())
|
ps = list(get_plugins())
|
||||||
if not request.args.get("all"):
|
if all is False or (request is not None and request.args.get("all")):
|
||||||
ps = [p for p in ps if p["name"] not in DEFAULT_PLUGINS]
|
ps = [p for p in ps if p["name"] not in DEFAULT_PLUGINS]
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -126,7 +126,7 @@ pm.hook.publish_subcommand(publish=publish)
|
||||||
def plugins(all, plugins_dir):
|
def plugins(all, plugins_dir):
|
||||||
"List currently available plugins"
|
"List currently available plugins"
|
||||||
app = Datasette([], plugins_dir=plugins_dir)
|
app = Datasette([], plugins_dir=plugins_dir)
|
||||||
click.echo(json.dumps(app.plugins(all), indent=4))
|
click.echo(json.dumps(app._plugins(all=all), indent=4))
|
||||||
|
|
||||||
|
|
||||||
@cli.command()
|
@cli.command()
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,62 @@ TEMP_PLUGIN_SECRET_FILE = os.path.join(tempfile.gettempdir(), "plugin-secret")
|
||||||
|
|
||||||
PLUGINS_DIR = str(pathlib.Path(__file__).parent / "plugins")
|
PLUGINS_DIR = str(pathlib.Path(__file__).parent / "plugins")
|
||||||
|
|
||||||
|
EXPECTED_PLUGINS = [
|
||||||
|
{
|
||||||
|
"name": "messages_output_renderer.py",
|
||||||
|
"static": False,
|
||||||
|
"templates": False,
|
||||||
|
"version": None,
|
||||||
|
"hooks": ["register_output_renderer"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "my_plugin.py",
|
||||||
|
"static": False,
|
||||||
|
"templates": False,
|
||||||
|
"version": None,
|
||||||
|
"hooks": [
|
||||||
|
"actor_from_request",
|
||||||
|
"extra_body_script",
|
||||||
|
"extra_css_urls",
|
||||||
|
"extra_js_urls",
|
||||||
|
"extra_template_vars",
|
||||||
|
"permission_allowed",
|
||||||
|
"prepare_connection",
|
||||||
|
"prepare_jinja2_environment",
|
||||||
|
"register_facet_classes",
|
||||||
|
"render_cell",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "my_plugin_2.py",
|
||||||
|
"static": False,
|
||||||
|
"templates": False,
|
||||||
|
"version": None,
|
||||||
|
"hooks": [
|
||||||
|
"actor_from_request",
|
||||||
|
"asgi_wrapper",
|
||||||
|
"extra_js_urls",
|
||||||
|
"extra_template_vars",
|
||||||
|
"permission_allowed",
|
||||||
|
"render_cell",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "register_output_renderer.py",
|
||||||
|
"static": False,
|
||||||
|
"templates": False,
|
||||||
|
"version": None,
|
||||||
|
"hooks": ["register_output_renderer"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "view_name.py",
|
||||||
|
"static": False,
|
||||||
|
"templates": False,
|
||||||
|
"version": None,
|
||||||
|
"hooks": ["extra_template_vars"],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class TestResponse:
|
class TestResponse:
|
||||||
def __init__(self, status, headers, body):
|
def __init__(self, status, headers, body):
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ from .fixtures import ( # noqa
|
||||||
generate_compound_rows,
|
generate_compound_rows,
|
||||||
generate_sortable_rows,
|
generate_sortable_rows,
|
||||||
make_app_client,
|
make_app_client,
|
||||||
|
EXPECTED_PLUGINS,
|
||||||
METADATA,
|
METADATA,
|
||||||
)
|
)
|
||||||
import json
|
import json
|
||||||
|
|
@ -1259,62 +1260,7 @@ def test_threads_json(app_client):
|
||||||
|
|
||||||
def test_plugins_json(app_client):
|
def test_plugins_json(app_client):
|
||||||
response = app_client.get("/-/plugins.json")
|
response = app_client.get("/-/plugins.json")
|
||||||
expected = [
|
assert EXPECTED_PLUGINS == sorted(response.json, key=lambda p: p["name"])
|
||||||
{
|
|
||||||
"name": "messages_output_renderer.py",
|
|
||||||
"static": False,
|
|
||||||
"templates": False,
|
|
||||||
"version": None,
|
|
||||||
"hooks": ["register_output_renderer"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "my_plugin.py",
|
|
||||||
"static": False,
|
|
||||||
"templates": False,
|
|
||||||
"version": None,
|
|
||||||
"hooks": [
|
|
||||||
"actor_from_request",
|
|
||||||
"extra_body_script",
|
|
||||||
"extra_css_urls",
|
|
||||||
"extra_js_urls",
|
|
||||||
"extra_template_vars",
|
|
||||||
"permission_allowed",
|
|
||||||
"prepare_connection",
|
|
||||||
"prepare_jinja2_environment",
|
|
||||||
"register_facet_classes",
|
|
||||||
"render_cell",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "my_plugin_2.py",
|
|
||||||
"static": False,
|
|
||||||
"templates": False,
|
|
||||||
"version": None,
|
|
||||||
"hooks": [
|
|
||||||
"actor_from_request",
|
|
||||||
"asgi_wrapper",
|
|
||||||
"extra_js_urls",
|
|
||||||
"extra_template_vars",
|
|
||||||
"permission_allowed",
|
|
||||||
"render_cell",
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "register_output_renderer.py",
|
|
||||||
"static": False,
|
|
||||||
"templates": False,
|
|
||||||
"version": None,
|
|
||||||
"hooks": ["register_output_renderer"],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "view_name.py",
|
|
||||||
"static": False,
|
|
||||||
"templates": False,
|
|
||||||
"version": None,
|
|
||||||
"hooks": ["extra_template_vars"],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
assert expected == sorted(response.json, key=lambda p: p["name"])
|
|
||||||
|
|
||||||
|
|
||||||
def test_versions_json(app_client):
|
def test_versions_json(app_client):
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,9 @@
|
||||||
from .fixtures import app_client, make_app_client, TestClient as _TestClient
|
from .fixtures import (
|
||||||
|
app_client,
|
||||||
|
make_app_client,
|
||||||
|
TestClient as _TestClient,
|
||||||
|
EXPECTED_PLUGINS,
|
||||||
|
)
|
||||||
from datasette.cli import cli, serve
|
from datasette.cli import cli, serve
|
||||||
from click.testing import CliRunner
|
from click.testing import CliRunner
|
||||||
import io
|
import io
|
||||||
|
|
@ -50,6 +55,30 @@ def test_spatialite_error_if_attempt_to_open_spatialite():
|
||||||
assert "trying to load a SpatiaLite database" in result.output
|
assert "trying to load a SpatiaLite database" in result.output
|
||||||
|
|
||||||
|
|
||||||
|
def test_plugins_cli(app_client):
|
||||||
|
runner = CliRunner()
|
||||||
|
result1 = runner.invoke(cli, ["plugins"])
|
||||||
|
assert sorted(EXPECTED_PLUGINS, key=lambda p: p["name"]) == sorted(
|
||||||
|
json.loads(result1.output), key=lambda p: p["name"]
|
||||||
|
)
|
||||||
|
# Try with --all
|
||||||
|
result2 = runner.invoke(cli, ["plugins", "--all"])
|
||||||
|
names = [p["name"] for p in json.loads(result2.output)]
|
||||||
|
# Should have all the EXPECTED_PLUGINS
|
||||||
|
assert set(names).issuperset(set(p["name"] for p in EXPECTED_PLUGINS))
|
||||||
|
# And the following too:
|
||||||
|
assert set(names).issuperset(
|
||||||
|
[
|
||||||
|
"datasette.sql_functions",
|
||||||
|
"datasette.actor_auth_cookie",
|
||||||
|
"datasette.facets",
|
||||||
|
"datasette.publish.cloudrun",
|
||||||
|
"datasette.default_permissions",
|
||||||
|
"datasette.publish.heroku",
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_metadata_yaml():
|
def test_metadata_yaml():
|
||||||
yaml_file = io.StringIO(
|
yaml_file = io.StringIO(
|
||||||
textwrap.dedent(
|
textwrap.dedent(
|
||||||
|
|
|
||||||
|
|
@ -449,7 +449,7 @@ def test_call_with_supported_arguments():
|
||||||
({"foo": ["bar", "baz"]}, False),
|
({"foo": ["bar", "baz"]}, False),
|
||||||
({"foo": ("bar", "baz")}, False),
|
({"foo": ("bar", "baz")}, False),
|
||||||
({"foo": "bar"}, True),
|
({"foo": "bar"}, True),
|
||||||
]
|
],
|
||||||
)
|
)
|
||||||
def test_multi_params(data, should_raise):
|
def test_multi_params(data, should_raise):
|
||||||
if should_raise:
|
if should_raise:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue