diff --git a/datasette/cli.py b/datasette/cli.py index 5c81489e..2c70cde4 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -416,12 +416,6 @@ def serve( ds = Datasette(files, **kwargs) - if get: - client = TestClient(ds.app()) - response = client.get(get) - click.echo(response.text) - return - if return_instance: # Private utility mechanism for writing unit tests return ds @@ -432,6 +426,12 @@ def serve( # Run async sanity checks - but only if we're not under pytest asyncio.get_event_loop().run_until_complete(check_databases(ds)) + if get: + client = TestClient(ds.app()) + response = client.get(get) + click.echo(response.text) + return + # Start the server if root: print("http://{}:{}/-/auth-token?token={}".format(host, port, ds._root_token)) diff --git a/tests/conftest.py b/tests/conftest.py index 320aa45b..db9eb0c8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,9 +25,11 @@ def pytest_unconfigure(config): def pytest_collection_modifyitems(items): - # Ensure test_black.py and test_inspect.py run first before any asyncio code kicks in + # Ensure test_cli.py and test_black.py and test_inspect.py run first before any asyncio code kicks in + move_to_front(items, "test_cli") move_to_front(items, "test_black") move_to_front(items, "test_inspect_cli") + move_to_front(items, "test_serve_with_get") move_to_front(items, "test_inspect_cli_writes_to_file") move_to_front(items, "test_spatialite_error_if_attempt_to_open_spatialite") move_to_front(items, "test_package") diff --git a/tests/test_cli.py b/tests/test_cli.py index 4dda4a71..38bb8834 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -50,20 +50,6 @@ def test_serve_with_inspect_file_prepopulates_table_counts_cache(): assert {"hithere": 44} == db.cached_table_counts -def test_serve_with_get(): - runner = CliRunner() - result = runner.invoke( - cli, - ["serve", "--memory", "--get", "/:memory:.json?sql=select+sqlite_version()"], - ) - assert 0 == result.exit_code, result.output - assert { - "database": ":memory:", - "truncated": False, - "columns": ["sqlite_version()"], - }.items() <= json.loads(result.output).items() - - def test_spatialite_error_if_attempt_to_open_spatialite(): runner = CliRunner() result = runner.invoke( diff --git a/tests/test_cli_serve_get.py b/tests/test_cli_serve_get.py new file mode 100644 index 00000000..20934ab8 --- /dev/null +++ b/tests/test_cli_serve_get.py @@ -0,0 +1,50 @@ +from datasette.cli import cli, serve +from datasette.plugins import pm +from click.testing import CliRunner +import textwrap +import json + + +def test_serve_with_get(tmp_path_factory): + plugins_dir = tmp_path_factory.mktemp("plugins_for_serve_with_get") + (plugins_dir / "init_for_serve_with_get.py").write_text( + textwrap.dedent( + """ + from datasette import hookimpl + + @hookimpl + def startup(datasette): + open("{}", "w").write("hello") + """.format( + str(plugins_dir / "hello.txt") + ), + ), + "utf-8", + ) + runner = CliRunner() + result = runner.invoke( + cli, + [ + "serve", + "--memory", + "--plugins-dir", + str(plugins_dir), + "--get", + "/:memory:.json?sql=select+sqlite_version()", + ], + ) + assert 0 == result.exit_code, result.output + assert { + "database": ":memory:", + "truncated": False, + "columns": ["sqlite_version()"], + }.items() <= json.loads(result.output).items() + # The plugin should have created hello.txt + assert (plugins_dir / "hello.txt").read_text() == "hello" + + # Annoyingly that new test plugin stays resident - we need + # to manually unregister it to avoid conflict with other tests + to_unregister = [ + p for p in pm.get_plugins() if p.__name__ == "init_for_serve_with_get.py" + ][0] + pm.unregister(to_unregister)