Skip to content

Commit

Permalink
Fixed "datasette publish now ... --alias=x"
Browse files Browse the repository at this point in the history
The --alias argument can now be passed more than once.

Also updated our Travis configuration to use this.

Fixes #459
  • Loading branch information
simonw committed May 11, 2019
1 parent a0d4448 commit 09ef305
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Expand Up @@ -27,20 +27,15 @@ jobs:
- npm install -g now - npm install -g now
- python tests/fixtures.py fixtures.db fixtures.json - python tests/fixtures.py fixtures.db fixtures.json
- export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7` - export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7`
- echo "{\"name\":\"datasette-latest-$ALIAS\",\"alias\":\"latest.datasette.io\"}" > now.json - 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
- datasette publish now fixtures.db -m fixtures.json --token=$NOW_TOKEN --branch=$TRAVIS_COMMIT --version-note=$TRAVIS_COMMIT --name=datasette-latest-$ALIAS
- now --target production --token=$NOW_TOKEN
- echo "{\"name\":\"datasette-latest-$ALIAS\",\"alias\":\"$ALIAS.datasette.io\"}" > now.json
- now --target production --token=$NOW_TOKEN
- stage: release tagged version - stage: release tagged version
if: tag IS present if: tag IS present
python: 3.6 python: 3.6
script: script:
- npm install -g now - npm install -g now
- export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7` - export ALIAS=`echo $TRAVIS_COMMIT | cut -c 1-7`
- export TAG=`echo $TRAVIS_TAG | sed 's/\./-/g' | sed 's/.*/v&/'` - export TAG=`echo $TRAVIS_TAG | sed 's/\./-/g' | sed 's/.*/v&/'`
- echo "{\"name\":\"datasette-latest-$ALIAS\",\"alias\":\"$TAG.datasette.io\"}" > now.json - now alias $ALIAS.datasette.io $TAG.datasette.io --token=$NOW_TOKEN
- now --target production --token=$NOW_TOKEN
# Build and release to Docker Hub # Build and release to Docker Hub
- docker login -u $DOCKER_USER -p $DOCKER_PASS - docker login -u $DOCKER_USER -p $DOCKER_PASS
- export REPO=datasetteproject/datasette - export REPO=datasetteproject/datasette
Expand Down
25 changes: 14 additions & 11 deletions datasette/publish/now.py
@@ -1,7 +1,7 @@
from datasette import hookimpl from datasette import hookimpl
import click import click
import json import json
from subprocess import call from subprocess import run, PIPE


from .common import ( from .common import (
add_common_publish_arguments_and_options, add_common_publish_arguments_and_options,
Expand All @@ -22,7 +22,7 @@ def publish_subcommand(publish):
) )
@click.option("--force", is_flag=True, help="Pass --force option to now") @click.option("--force", is_flag=True, help="Pass --force option to now")
@click.option("--token", help="Auth token to use for deploy") @click.option("--token", help="Auth token to use for deploy")
@click.option("--alias", help="Desired alias e.g. yoursite.now.sh") @click.option("--alias", multiple=True, help="Desired alias e.g. yoursite.now.sh")
@click.option("--spatialite", is_flag=True, help="Enable SpatialLite extension") @click.option("--spatialite", is_flag=True, help="Enable SpatialLite extension")
def now( def now(
files, files,
Expand Down Expand Up @@ -77,20 +77,23 @@ def now(
}, },
): ):
now_json = {"version": 1} now_json = {"version": 1}
if alias: open("now.json", "w").write(json.dumps(now_json, indent=4))
now_json["alias"] = alias
open("now.json", "w").write(json.dumps(now_json))
args = [] args = []
if force: if force:
args.append("--force") args.append("--force")
if token: if token:
args.append("--token={}".format(token)) args.append("--token={}".format(token))
if args: if args:
call(["now"] + args) done = run(["now"] + args, stdout=PIPE)
else: else:
call("now") done = run("now", stdout=PIPE)
deployment_url = done.stdout
if alias: if alias:
alias_args = ["alias"] # I couldn't get --target=production working, so I call
if token: # 'now alias' with arguments directly instead - but that
alias_args.append("--token={}".format(token)) # means I need to figure out what URL it was deployed to.
call(["now"] + alias_args) for single_alias in alias:
# Because --alias can be specified multiple times
run(["now", "alias", deployment_url, single_alias])
else:
print(deployment_url.decode("latin1"))
14 changes: 0 additions & 14 deletions tests/test_publish_heroku.py
Expand Up @@ -60,17 +60,3 @@ def test_publish_heroku(mock_call, mock_check_output, mock_which):
mock_call.assert_called_once_with( mock_call.assert_called_once_with(
["heroku", "builds:create", "-a", "f", "--include-vcs-ignore"] ["heroku", "builds:create", "-a", "f", "--include-vcs-ignore"]
) )


@mock.patch("shutil.which")
@mock.patch("datasette.publish.now.call")
def test_publish_now_force_token(mock_call, mock_which):
mock_which.return_value = True
runner = CliRunner()
with runner.isolated_filesystem():
open("test.db", "w").write("data")
result = runner.invoke(
cli.cli, ["publish", "now", "test.db", "--force", "--token=X"]
)
assert 0 == result.exit_code
mock_call.assert_called_once_with(["now", "--force", "--token=X"])
37 changes: 31 additions & 6 deletions tests/test_publish_now.py
@@ -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 subprocess




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




@mock.patch("shutil.which") @mock.patch("shutil.which")
@mock.patch("datasette.publish.now.call") @mock.patch("datasette.publish.now.run")
def test_publish_now(mock_call, mock_which): def test_publish_now(mock_run, mock_which):
mock_which.return_value = True mock_which.return_value = True
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
open("test.db", "w").write("data") open("test.db", "w").write("data")
result = runner.invoke(cli.cli, ["publish", "now", "test.db"]) result = runner.invoke(cli.cli, ["publish", "now", "test.db"])
assert 0 == result.exit_code assert 0 == result.exit_code
mock_call.assert_called_once_with("now") mock_run.assert_called_once_with("now", stdout=subprocess.PIPE)




@mock.patch("shutil.which") @mock.patch("shutil.which")
@mock.patch("datasette.publish.now.call") @mock.patch("datasette.publish.now.run")
def test_publish_now_force_token(mock_call, mock_which): def test_publish_now_force_token(mock_run, mock_which):
mock_which.return_value = True mock_which.return_value = True
runner = CliRunner() runner = CliRunner()
with runner.isolated_filesystem(): with runner.isolated_filesystem():
Expand All @@ -46,4 +47,28 @@ def test_publish_now_force_token(mock_call, mock_which):
cli.cli, ["publish", "now", "test.db", "--force", "--token=X"] cli.cli, ["publish", "now", "test.db", "--force", "--token=X"]
) )
assert 0 == result.exit_code assert 0 == result.exit_code
mock_call.assert_called_once_with(["now", "--force", "--token=X"]) mock_run.assert_called_once_with(
["now", "--force", "--token=X"], stdout=subprocess.PIPE
)


@mock.patch("shutil.which")
@mock.patch("datasette.publish.now.run")
def test_publish_now_multiple_aliases(mock_run, mock_which):
mock_which.return_value = True
mock_run.return_value = mock.Mock(0)
mock_run.return_value.stdout = b"https://demo.example.com/"
runner = CliRunner()
with runner.isolated_filesystem():
open("test.db", "w").write("data")
runner.invoke(
cli.cli,
["publish", "now", "test.db", "--alias", "alias1", "--alias", "alias2"],
)
mock_run.assert_has_calls(
[
mock.call("now", stdout=subprocess.PIPE),
mock.call(["now", "alias", b"https://demo.example.com/", "alias1"]),
mock.call(["now", "alias", b"https://demo.example.com/", "alias2"]),
]
)

0 comments on commit 09ef305

Please sign in to comment.