Skip to content

Commit

Permalink
Fixed 'datasette plugins' command, with tests - closes #802
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jun 5, 2020
1 parent 033a1bb commit f786033
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 61 deletions.
4 changes: 2 additions & 2 deletions datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,9 +626,9 @@ def _versions(self):
},
}

def _plugins(self, request):
def _plugins(self, request=None, all=False):
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]
return [
{
Expand Down
2 changes: 1 addition & 1 deletion datasette/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def publish():
def plugins(all, plugins_dir):
"List currently available plugins"
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()
Expand Down
56 changes: 56 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,62 @@

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:
def __init__(self, status, headers, body):
Expand Down
58 changes: 2 additions & 56 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
generate_compound_rows,
generate_sortable_rows,
make_app_client,
EXPECTED_PLUGINS,
METADATA,
)
import json
Expand Down Expand Up @@ -1259,62 +1260,7 @@ def test_threads_json(app_client):

def test_plugins_json(app_client):
response = app_client.get("/-/plugins.json")
expected = [
{
"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"])
assert EXPECTED_PLUGINS == sorted(response.json, key=lambda p: p["name"])


def test_versions_json(app_client):
Expand Down
31 changes: 30 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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 click.testing import CliRunner
import io
Expand Down Expand Up @@ -50,6 +55,30 @@ def test_spatialite_error_if_attempt_to_open_spatialite():
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():
yaml_file = io.StringIO(
textwrap.dedent(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def foo(a, b):
({"foo": ["bar", "baz"]}, False),
({"foo": ("bar", "baz")}, False),
({"foo": "bar"}, True),
]
],
)
def test_multi_params(data, should_raise):
if should_raise:
Expand Down

0 comments on commit f786033

Please sign in to comment.