mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
register_commands() plugin hook, closes #1449
This commit is contained in:
parent
3655bb49a4
commit
30c18576d6
4 changed files with 109 additions and 1 deletions
|
|
@ -6,13 +6,15 @@ from .fixtures import (
|
|||
TEMP_PLUGIN_SECRET_FILE,
|
||||
TestClient as _TestClient,
|
||||
) # noqa
|
||||
from click.testing import CliRunner
|
||||
from datasette.app import Datasette
|
||||
from datasette import cli
|
||||
from datasette import cli, hookimpl
|
||||
from datasette.plugins import get_plugins, DEFAULT_PLUGINS, pm
|
||||
from datasette.utils.sqlite import sqlite3
|
||||
from datasette.utils import CustomRow
|
||||
from jinja2.environment import Template
|
||||
import base64
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
|
|
@ -902,3 +904,56 @@ def test_hook_get_metadata(app_client):
|
|||
assert "Hello from local metadata" == meta["databases"]["from-local"]["title"]
|
||||
assert "Hello from the plugin hook" == meta["databases"]["from-hook"]["title"]
|
||||
pm.hook.get_metadata = og_pm_hook_get_metadata
|
||||
|
||||
|
||||
def _extract_commands(output):
|
||||
lines = output.split("Commands:\n", 1)[1].split("\n")
|
||||
return {line.split()[0].replace("*", "") for line in lines if line.strip()}
|
||||
|
||||
|
||||
def test_hook_register_commands():
|
||||
# Without the plugin should have seven commands
|
||||
runner = CliRunner()
|
||||
result = runner.invoke(cli.cli, "--help")
|
||||
commands = _extract_commands(result.output)
|
||||
assert commands == {
|
||||
"serve",
|
||||
"inspect",
|
||||
"install",
|
||||
"package",
|
||||
"plugins",
|
||||
"publish",
|
||||
"uninstall",
|
||||
}
|
||||
|
||||
# Now install a plugin
|
||||
class VerifyPlugin:
|
||||
__name__ = "VerifyPlugin"
|
||||
|
||||
@hookimpl
|
||||
def register_commands(self, cli):
|
||||
@cli.command()
|
||||
def verify():
|
||||
pass
|
||||
|
||||
@cli.command()
|
||||
def unverify():
|
||||
pass
|
||||
|
||||
pm.register(VerifyPlugin(), name="verify")
|
||||
importlib.reload(cli)
|
||||
result2 = runner.invoke(cli.cli, "--help")
|
||||
commands2 = _extract_commands(result2.output)
|
||||
assert commands2 == {
|
||||
"serve",
|
||||
"inspect",
|
||||
"install",
|
||||
"package",
|
||||
"plugins",
|
||||
"publish",
|
||||
"uninstall",
|
||||
"verify",
|
||||
"unverify",
|
||||
}
|
||||
pm.unregister(name="verify")
|
||||
importlib.reload(cli)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue