From 36a44bffbf5185a58fcd2548e8de94e9490a7aea Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 18 Feb 2021 10:05:27 -0800 Subject: [PATCH] Validation for --port, closes #1226 --- datasette/cli.py | 2 ++ tests/test_cli.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/datasette/cli.py b/datasette/cli.py index 0a355edbe4..815f971867 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -223,6 +223,7 @@ def plugins(all, plugins_dir): "-p", "--port", default=8001, + type=click.IntRange(1, 65535), help="Port to run the server on, defaults to 8001", ) @click.option("--title", help="Title for metadata") @@ -329,6 +330,7 @@ def uninstall(packages, yes): "-p", "--port", default=8001, + type=click.IntRange(0, 65535), help="Port for server, defaults to 8001. Use -p 0 to automatically assign an available port.", ) @click.option( diff --git a/tests/test_cli.py b/tests/test_cli.py index 2ca500cfdc..4bcf615bb9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -194,6 +194,14 @@ def test_version(): assert result.output == f"cli, version {__version__}\n" +@pytest.mark.parametrize("invalid_port", ["-1", "0.5", "dog", "65536"]) +def test_serve_invalid_ports(ensure_eventloop, invalid_port): + runner = CliRunner(mix_stderr=False) + result = runner.invoke(cli, ["--port", invalid_port]) + assert result.exit_code == 2 + assert "Invalid value for '-p'" in result.stderr + + def test_setting(ensure_eventloop): runner = CliRunner() result = runner.invoke(