Skip to content

Commit 09ef305

Browse files
committed
Fixed "datasette publish now ... --alias=x"
The --alias argument can now be passed more than once. Also updated our Travis configuration to use this. Fixes #459
1 parent a0d4448 commit 09ef305

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

.travis.yml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,15 @@ jobs:
2727
- npm install -g now
2828
- python tests/fixtures.py fixtures.db fixtures.json
2929
- export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7`
30-
- echo "{\"name\":\"datasette-latest-$ALIAS\",\"alias\":\"latest.datasette.io\"}" > now.json
31-
- datasette publish now fixtures.db -m fixtures.json --token=$NOW_TOKEN --branch=$TRAVIS_COMMIT --version-note=$TRAVIS_COMMIT --name=datasette-latest-$ALIAS
32-
- now --target production --token=$NOW_TOKEN
33-
- echo "{\"name\":\"datasette-latest-$ALIAS\",\"alias\":\"$ALIAS.datasette.io\"}" > now.json
34-
- now --target production --token=$NOW_TOKEN
30+
- datasette publish now fixtures.db -m fixtures.json --token=$NOW_TOKEN --branch=$TRAVIS_COMMIT --version-note=$TRAVIS_COMMIT --name=datasette-latest-$ALIAS --alias=latest.datasette.io --alias=$ALIAS.datasette.io
3531
- stage: release tagged version
3632
if: tag IS present
3733
python: 3.6
3834
script:
3935
- npm install -g now
4036
- export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7`
4137
- export TAG=`echo $TRAVIS_TAG | sed 's/\./-/g' | sed 's/.*/v&/'`
42-
- echo "{\"name\":\"datasette-latest-$ALIAS\",\"alias\":\"$TAG.datasette.io\"}" > now.json
43-
- now --target production --token=$NOW_TOKEN
38+
- now alias $ALIAS.datasette.io $TAG.datasette.io --token=$NOW_TOKEN
4439
# Build and release to Docker Hub
4540
- docker login -u $DOCKER_USER -p $DOCKER_PASS
4641
- export REPO=datasetteproject/datasette

datasette/publish/now.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datasette import hookimpl
22
import click
33
import json
4-
from subprocess import call
4+
from subprocess import run, PIPE
55

66
from .common import (
77
add_common_publish_arguments_and_options,
@@ -22,7 +22,7 @@ def publish_subcommand(publish):
2222
)
2323
@click.option("--force", is_flag=True, help="Pass --force option to now")
2424
@click.option("--token", help="Auth token to use for deploy")
25-
@click.option("--alias", help="Desired alias e.g. yoursite.now.sh")
25+
@click.option("--alias", multiple=True, help="Desired alias e.g. yoursite.now.sh")
2626
@click.option("--spatialite", is_flag=True, help="Enable SpatialLite extension")
2727
def now(
2828
files,
@@ -77,20 +77,23 @@ def now(
7777
},
7878
):
7979
now_json = {"version": 1}
80-
if alias:
81-
now_json["alias"] = alias
82-
open("now.json", "w").write(json.dumps(now_json))
80+
open("now.json", "w").write(json.dumps(now_json, indent=4))
8381
args = []
8482
if force:
8583
args.append("--force")
8684
if token:
8785
args.append("--token={}".format(token))
8886
if args:
89-
call(["now"] + args)
87+
done = run(["now"] + args, stdout=PIPE)
9088
else:
91-
call("now")
89+
done = run("now", stdout=PIPE)
90+
deployment_url = done.stdout
9291
if alias:
93-
alias_args = ["alias"]
94-
if token:
95-
alias_args.append("--token={}".format(token))
96-
call(["now"] + alias_args)
92+
# I couldn't get --target=production working, so I call
93+
# 'now alias' with arguments directly instead - but that
94+
# means I need to figure out what URL it was deployed to.
95+
for single_alias in alias:
96+
# Because --alias can be specified multiple times
97+
run(["now", "alias", deployment_url, single_alias])
98+
else:
99+
print(deployment_url.decode("latin1"))

tests/test_publish_heroku.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,3 @@ def test_publish_heroku(mock_call, mock_check_output, mock_which):
6060
mock_call.assert_called_once_with(
6161
["heroku", "builds:create", "-a", "f", "--include-vcs-ignore"]
6262
)
63-
64-
65-
@mock.patch("shutil.which")
66-
@mock.patch("datasette.publish.now.call")
67-
def test_publish_now_force_token(mock_call, mock_which):
68-
mock_which.return_value = True
69-
runner = CliRunner()
70-
with runner.isolated_filesystem():
71-
open("test.db", "w").write("data")
72-
result = runner.invoke(
73-
cli.cli, ["publish", "now", "test.db", "--force", "--token=X"]
74-
)
75-
assert 0 == result.exit_code
76-
mock_call.assert_called_once_with(["now", "--force", "--token=X"])

tests/test_publish_now.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from click.testing import CliRunner
22
from datasette import cli
33
from unittest import mock
4+
import subprocess
45

56

67
@mock.patch("shutil.which")
@@ -24,20 +25,20 @@ def test_publish_now_invalid_database(mock_which):
2425

2526

2627
@mock.patch("shutil.which")
27-
@mock.patch("datasette.publish.now.call")
28-
def test_publish_now(mock_call, mock_which):
28+
@mock.patch("datasette.publish.now.run")
29+
def test_publish_now(mock_run, mock_which):
2930
mock_which.return_value = True
3031
runner = CliRunner()
3132
with runner.isolated_filesystem():
3233
open("test.db", "w").write("data")
3334
result = runner.invoke(cli.cli, ["publish", "now", "test.db"])
3435
assert 0 == result.exit_code
35-
mock_call.assert_called_once_with("now")
36+
mock_run.assert_called_once_with("now", stdout=subprocess.PIPE)
3637

3738

3839
@mock.patch("shutil.which")
39-
@mock.patch("datasette.publish.now.call")
40-
def test_publish_now_force_token(mock_call, mock_which):
40+
@mock.patch("datasette.publish.now.run")
41+
def test_publish_now_force_token(mock_run, mock_which):
4142
mock_which.return_value = True
4243
runner = CliRunner()
4344
with runner.isolated_filesystem():
@@ -46,4 +47,28 @@ def test_publish_now_force_token(mock_call, mock_which):
4647
cli.cli, ["publish", "now", "test.db", "--force", "--token=X"]
4748
)
4849
assert 0 == result.exit_code
49-
mock_call.assert_called_once_with(["now", "--force", "--token=X"])
50+
mock_run.assert_called_once_with(
51+
["now", "--force", "--token=X"], stdout=subprocess.PIPE
52+
)
53+
54+
55+
@mock.patch("shutil.which")
56+
@mock.patch("datasette.publish.now.run")
57+
def test_publish_now_multiple_aliases(mock_run, mock_which):
58+
mock_which.return_value = True
59+
mock_run.return_value = mock.Mock(0)
60+
mock_run.return_value.stdout = b"https://demo.example.com/"
61+
runner = CliRunner()
62+
with runner.isolated_filesystem():
63+
open("test.db", "w").write("data")
64+
runner.invoke(
65+
cli.cli,
66+
["publish", "now", "test.db", "--alias", "alias1", "--alias", "alias2"],
67+
)
68+
mock_run.assert_has_calls(
69+
[
70+
mock.call("now", stdout=subprocess.PIPE),
71+
mock.call(["now", "alias", b"https://demo.example.com/", "alias1"]),
72+
mock.call(["now", "alias", b"https://demo.example.com/", "alias2"]),
73+
]
74+
)

0 commit comments

Comments
 (0)