From d02f6151dae073135a22d0123e8abdc6cbef7c50 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 11 Sep 2020 14:32:54 -0700 Subject: [PATCH] datasette --get status code for error pages, closes #947 --- datasette/cli.py | 2 ++ docs/getting_started.rst | 2 ++ tests/test_cli_serve_get.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/datasette/cli.py b/datasette/cli.py index 8b67048a..231ae8b7 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -444,6 +444,8 @@ def serve( client = TestClient(ds.app()) response = client.get(get) click.echo(response.text) + exit_code = 0 if response.status == 200 else 1 + sys.exit(exit_code) return # Start the server diff --git a/docs/getting_started.rst b/docs/getting_started.rst index 22cf6bb2..44617b8b 100644 --- a/docs/getting_started.rst +++ b/docs/getting_started.rst @@ -155,6 +155,8 @@ The ``--get`` option can specify the path to a page within Datasette and cause D } } +The exit code will be 0 if the request succeeds and 1 if the request produced an HTTP status code other than 200 - e.g. a 404 or 500 error. This means you can use ``datasette --get /`` to run tests against a Datasette application in a continuous integration environment such as GitHub Actions. + .. _getting_started_serve_help: datasette serve --help diff --git a/tests/test_cli_serve_get.py b/tests/test_cli_serve_get.py index 20934ab8..8f1665a9 100644 --- a/tests/test_cli_serve_get.py +++ b/tests/test_cli_serve_get.py @@ -39,6 +39,7 @@ def test_serve_with_get(tmp_path_factory): "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" @@ -48,3 +49,18 @@ def test_serve_with_get(tmp_path_factory): p for p in pm.get_plugins() if p.__name__ == "init_for_serve_with_get.py" ][0] pm.unregister(to_unregister) + + +def test_serve_with_get_exit_code_for_error(tmp_path_factory): + runner = CliRunner() + result = runner.invoke( + cli, + [ + "serve", + "--memory", + "--get", + "/this-is-404", + ], + ) + assert result.exit_code == 1 + assert "404" in result.output