--port argument for datasette package, plus tests - closes #661

From pull request #663
This commit is contained in:
Simon Willison 2020-01-29 14:46:43 -08:00 committed by GitHub
commit 67fc9c5720
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 2 deletions

View file

@ -20,6 +20,8 @@ def pytest_collection_modifyitems(items):
move_to_front(items, "test_inspect_cli")
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")
move_to_front(items, "test_package_with_port")
def move_to_front(items, test_name):

53
tests/test_package.py Normal file
View file

@ -0,0 +1,53 @@
from click.testing import CliRunner
from datasette import cli
from unittest import mock
import pathlib
import json
class CaptureDockerfile:
def __call__(self, _):
self.captured = (pathlib.Path() / "Dockerfile").read_text()
EXPECTED_DOCKERFILE = """
FROM python:3.8
COPY . /app
WORKDIR /app
RUN pip install -U datasette
RUN datasette inspect test.db --inspect-file inspect-data.json
ENV PORT {port}
EXPOSE {port}
CMD datasette serve --host 0.0.0.0 -i test.db --cors --inspect-file inspect-data.json --port $PORT
""".strip()
@mock.patch("shutil.which")
@mock.patch("datasette.cli.call")
def test_package(mock_call, mock_which):
mock_which.return_value = True
runner = CliRunner()
capture = CaptureDockerfile()
mock_call.side_effect = capture
with runner.isolated_filesystem():
open("test.db", "w").write("data")
result = runner.invoke(cli.cli, ["package", "test.db"])
assert 0 == result.exit_code
mock_call.assert_has_calls([mock.call(["docker", "build", "."])])
assert EXPECTED_DOCKERFILE.format(port=8001) == capture.captured
@mock.patch("shutil.which")
@mock.patch("datasette.cli.call")
def test_package_with_port(mock_call, mock_which):
mock_which.return_value = True
capture = CaptureDockerfile()
mock_call.side_effect = capture
runner = CliRunner()
with runner.isolated_filesystem():
open("test.db", "w").write("data")
result = runner.invoke(cli.cli, ["package", "test.db", "-p", "8080"])
assert 0 == result.exit_code
assert EXPECTED_DOCKERFILE.format(port=8080) == capture.captured