Added pluggy and first hook, register_commands - refs #569, #567

This commit is contained in:
Simon Willison 2023-07-22 12:04:31 -07:00
commit b379a2a0c3
11 changed files with 224 additions and 2 deletions

View file

@ -8,6 +8,12 @@ create table Gosh2 (c1 text, c2 text, c3 text);
"""
def pytest_configure(config):
import sys
sys._called_from_test = True
@pytest.fixture
def fresh_db():
return Database(memory=True)

View file

@ -11,7 +11,9 @@ recipes_re = re.compile(r"r\.(\w+)\(")
@pytest.fixture(scope="session")
def documented_commands():
rst = (docs_path / "cli.rst").read_text()
rst = ""
for doc in ("cli.rst", "plugins.rst"):
rst += (docs_path / doc).read_text()
return {
command
for command in commands_re.findall(rst)

37
tests/test_plugins.py Normal file
View file

@ -0,0 +1,37 @@
from click.testing import CliRunner
import click
import importlib
from sqlite_utils import cli, hookimpl, plugins
def test_register_commands():
importlib.reload(cli)
assert plugins.get_plugins() == []
class HelloWorldPlugin:
__name__ = "HelloWorldPlugin"
@hookimpl
def register_commands(self, cli):
@cli.command(name="hello-world")
def hello_world():
"Print hello world"
click.echo("Hello world!")
try:
plugins.pm.register(HelloWorldPlugin(), name="HelloWorldPlugin")
importlib.reload(cli)
assert plugins.get_plugins() == [
{"name": "HelloWorldPlugin", "hooks": ["register_commands"]}
]
runner = CliRunner()
result = runner.invoke(cli.cli, ["hello-world"])
assert result.exit_code == 0
assert result.output == "Hello world!\n"
finally:
plugins.pm.unregister(name="HelloWorldPlugin")
importlib.reload(cli)
assert plugins.get_plugins() == []