Replace all uses of runner.isolated_filesystem, refs #1406

This commit is contained in:
Simon Willison 2021-07-31 11:49:08 -07:00
commit ff253f5242
3 changed files with 284 additions and 292 deletions

View file

@ -1,6 +1,7 @@
from click.testing import CliRunner from click.testing import CliRunner
from datasette import cli from datasette import cli
from unittest import mock from unittest import mock
import os
import pathlib import pathlib
import pytest import pytest
@ -32,7 +33,7 @@ def test_package(mock_call, mock_which, tmp_path_factory):
runner = CliRunner() runner = CliRunner()
capture = CaptureDockerfile() capture = CaptureDockerfile()
mock_call.side_effect = capture mock_call.side_effect = capture
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke(cli.cli, ["package", "test.db", "--secret", "sekrit"]) result = runner.invoke(cli.cli, ["package", "test.db", "--secret", "sekrit"])
@ -48,7 +49,7 @@ def test_package_with_port(mock_call, mock_which, tmp_path_factory):
capture = CaptureDockerfile() capture = CaptureDockerfile()
mock_call.side_effect = capture mock_call.side_effect = capture
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(

View file

@ -2,6 +2,7 @@ from click.testing import CliRunner
from datasette import cli from datasette import cli
from unittest import mock from unittest import mock
import json import json
import os
import pytest import pytest
import textwrap import textwrap
@ -11,7 +12,7 @@ import textwrap
def test_publish_cloudrun_requires_gcloud(mock_which, tmp_path_factory): def test_publish_cloudrun_requires_gcloud(mock_which, tmp_path_factory):
mock_which.return_value = False mock_which.return_value = False
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke(cli.cli, ["publish", "cloudrun", "test.db"]) result = runner.invoke(cli.cli, ["publish", "cloudrun", "test.db"])
@ -42,26 +43,19 @@ def test_publish_cloudrun_prompts_for_service(
mock_output.return_value = "myproject" mock_output.return_value = "myproject"
mock_which.return_value = True mock_which.return_value = True
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(
cli.cli, ["publish", "cloudrun", "test.db"], input="input-service" cli.cli, ["publish", "cloudrun", "test.db"], input="input-service"
) )
assert ( assert (
""" "Please provide a service name for this deployment\n\n"
Please provide a service name for this deployment "Using an existing service name will over-write it\n\n"
"Your existing services:\n\n"
Using an existing service name will over-write it " existing - created 2019-01-01 - http://www.example.com/\n\n"
"Service name: input-service"
Your existing services: ) == result.output.strip()
existing - created 2019-01-01 - http://www.example.com/
Service name: input-service
""".strip()
== result.output.strip()
)
assert 0 == result.exit_code assert 0 == result.exit_code
tag = "gcr.io/myproject/datasette" tag = "gcr.io/myproject/datasette"
mock_call.assert_has_calls( mock_call.assert_has_calls(
@ -85,7 +79,7 @@ def test_publish_cloudrun(mock_call, mock_output, mock_which, tmp_path_factory):
mock_output.return_value = "myproject" mock_output.return_value = "myproject"
mock_which.return_value = True mock_which.return_value = True
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(
@ -126,7 +120,7 @@ def test_publish_cloudrun_memory(
mock_output.return_value = "myproject" mock_output.return_value = "myproject"
mock_which.return_value = True mock_which.return_value = True
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(
@ -162,7 +156,7 @@ def test_publish_cloudrun_plugin_secrets(
mock_output.return_value = "myproject" mock_output.return_value = "myproject"
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
with open("metadata.yml", "w") as fp: with open("metadata.yml", "w") as fp:
@ -225,9 +219,9 @@ def test_publish_cloudrun_plugin_secrets(
"title": "Hello from metadata YAML", "title": "Hello from metadata YAML",
"plugins": { "plugins": {
"datasette-auth-github": { "datasette-auth-github": {
"foo": "bar",
"client_id": {"$env": "DATASETTE_AUTH_GITHUB_CLIENT_ID"}, "client_id": {"$env": "DATASETTE_AUTH_GITHUB_CLIENT_ID"},
} "foo": "bar",
},
}, },
} == json.loads(metadata) } == json.loads(metadata)
@ -243,7 +237,7 @@ def test_publish_cloudrun_apt_get_install(
mock_output.return_value = "myproject" mock_output.return_value = "myproject"
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(
@ -312,7 +306,7 @@ def test_publish_cloudrun_extra_options(
mock_output.return_value = "myproject" mock_output.return_value = "myproject"
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(

View file

@ -1,6 +1,7 @@
from click.testing import CliRunner from click.testing import CliRunner
from datasette import cli from datasette import cli
from unittest import mock from unittest import mock
import os
import pytest import pytest
@ -9,7 +10,7 @@ import pytest
def test_publish_heroku_requires_heroku(mock_which, tmp_path_factory): def test_publish_heroku_requires_heroku(mock_which, tmp_path_factory):
mock_which.return_value = False mock_which.return_value = False
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke(cli.cli, ["publish", "heroku", "test.db"]) result = runner.invoke(cli.cli, ["publish", "heroku", "test.db"])
@ -27,7 +28,7 @@ def test_publish_heroku_installs_plugin(
mock_which.return_value = True mock_which.return_value = True
mock_check_output.side_effect = lambda s: {"['heroku', 'plugins']": b""}[repr(s)] mock_check_output.side_effect = lambda s: {"['heroku', 'plugins']": b""}[repr(s)]
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("t.db", "w") as fp: with open("t.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke(cli.cli, ["publish", "heroku", "t.db"], input="y\n") result = runner.invoke(cli.cli, ["publish", "heroku", "t.db"], input="y\n")
@ -61,12 +62,10 @@ def test_publish_heroku(mock_call, mock_check_output, mock_which, tmp_path_facto
"['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}', "['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}',
}[repr(s)] }[repr(s)]
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(cli.cli, ["publish", "heroku", "test.db", "--tar", "gtar"])
cli.cli, ["publish", "heroku", "test.db", "--tar", "gtar"]
)
assert 0 == result.exit_code, result.output assert 0 == result.exit_code, result.output
mock_call.assert_has_calls( mock_call.assert_has_calls(
[ [
@ -99,7 +98,7 @@ def test_publish_heroku_plugin_secrets(
"['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}', "['heroku', 'apps:create', 'datasette', '--json']": b'{"name": "f"}',
}[repr(s)] }[repr(s)]
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(tmp_path_factory.mktemp("runner")): os.chdir(tmp_path_factory.mktemp("runner"))
with open("test.db", "w") as fp: with open("test.db", "w") as fp:
fp.write("data") fp.write("data")
result = runner.invoke( result = runner.invoke(
@ -126,8 +125,6 @@ def test_publish_heroku_plugin_secrets(
"DATASETTE_AUTH_GITHUB_CLIENT_ID=x-client-id", "DATASETTE_AUTH_GITHUB_CLIENT_ID=x-client-id",
] ]
), ),
mock.call( mock.call(["heroku", "builds:create", "-a", "f", "--include-vcs-ignore"]),
["heroku", "builds:create", "-a", "f", "--include-vcs-ignore"]
),
] ]
) )