Try passing a directory to isolated_filesystem(), refs #1406

This commit is contained in:
Simon Willison 2021-07-29 18:16:58 -07:00
commit e55cd9dc3f
3 changed files with 34 additions and 40 deletions

View file

@ -6,12 +6,11 @@ import pytest
import textwrap
@pytest.mark.serial
@mock.patch("shutil.which")
def test_publish_cloudrun_requires_gcloud(mock_which):
def test_publish_cloudrun_requires_gcloud(mock_which, tmp_path_factory):
mock_which.return_value = False
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(cli.cli, ["publish", "cloudrun", "test.db"])
@ -28,13 +27,12 @@ def test_publish_cloudrun_invalid_database(mock_which):
assert "Path 'woop.db' does not exist" in result.output
@pytest.mark.serial
@mock.patch("shutil.which")
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
@mock.patch("datasette.publish.cloudrun.get_existing_services")
def test_publish_cloudrun_prompts_for_service(
mock_get_existing_services, mock_call, mock_output, mock_which
mock_get_existing_services, mock_call, mock_output, mock_which, tmp_path_factory
):
mock_get_existing_services.return_value = [
{"name": "existing", "created": "2019-01-01", "url": "http://www.example.com/"}
@ -42,7 +40,7 @@ def test_publish_cloudrun_prompts_for_service(
mock_output.return_value = "myproject"
mock_which.return_value = True
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(
@ -77,15 +75,14 @@ Service name: input-service
)
@pytest.mark.serial
@mock.patch("shutil.which")
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
def test_publish_cloudrun(mock_call, mock_output, mock_which):
def test_publish_cloudrun(mock_call, mock_output, mock_which, tmp_path_factory):
mock_output.return_value = "myproject"
mock_which.return_value = True
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(
@ -106,7 +103,6 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which):
)
@pytest.mark.serial
@mock.patch("shutil.which")
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
@ -121,12 +117,12 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which):
],
)
def test_publish_cloudrun_memory(
mock_call, mock_output, mock_which, memory, should_fail
mock_call, mock_output, mock_which, memory, should_fail, tmp_path_factory
):
mock_output.return_value = "myproject"
mock_which.return_value = True
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(
@ -151,16 +147,17 @@ def test_publish_cloudrun_memory(
)
@pytest.mark.serial
@mock.patch("shutil.which")
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
def test_publish_cloudrun_plugin_secrets(mock_call, mock_output, mock_which):
def test_publish_cloudrun_plugin_secrets(
mock_call, mock_output, mock_which, tmp_path_factory
):
mock_which.return_value = True
mock_output.return_value = "myproject"
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
with open("metadata.yml", "w") as fp:
@ -230,16 +227,17 @@ def test_publish_cloudrun_plugin_secrets(mock_call, mock_output, mock_which):
} == json.loads(metadata)
@pytest.mark.serial
@mock.patch("shutil.which")
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
def test_publish_cloudrun_apt_get_install(mock_call, mock_output, mock_which):
def test_publish_cloudrun_apt_get_install(
mock_call, mock_output, mock_which, tmp_path_factory
):
mock_which.return_value = True
mock_output.return_value = "myproject"
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(
@ -286,7 +284,6 @@ def test_publish_cloudrun_apt_get_install(mock_call, mock_output, mock_which):
assert expected == dockerfile
@pytest.mark.serial
@mock.patch("shutil.which")
@mock.patch("datasette.publish.cloudrun.check_output")
@mock.patch("datasette.publish.cloudrun.check_call")
@ -302,13 +299,13 @@ def test_publish_cloudrun_apt_get_install(mock_call, mock_output, mock_which):
],
)
def test_publish_cloudrun_extra_options(
mock_call, mock_output, mock_which, extra_options, expected
mock_call, mock_output, mock_which, extra_options, expected, tmp_path_factory
):
mock_which.return_value = True
mock_output.return_value = "myproject"
runner = CliRunner()
with runner.isolated_filesystem():
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")):
with open("test.db", "w") as fp:
fp.write("data")
result = runner.invoke(