datasette --get status code for error pages, closes #947

pull/977/head
Simon Willison 2020-09-11 14:32:54 -07:00
rodzic 77521c6cd7
commit d02f6151da
3 zmienionych plików z 20 dodań i 0 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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

Wyświetl plik

@ -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