From 5f380746adcb13f6af5dbd4d66b3b53e79b83e9d Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Thu, 10 Dec 2015 11:07:00 -0800 Subject: [PATCH 01/11] Let 'append_hash' be set by an environment variable. --- deploy.sh | 2 +- readme.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index d52a263..ce79eed 100755 --- a/deploy.sh +++ b/deploy.sh @@ -8,7 +8,7 @@ main() { fi #append commit hash to the end of message by default - append_hash=true + append_hash=${GIT_DEPLOY_APPEND_HASH:-true} # Parse arg flags while : ; do diff --git a/readme.md b/readme.md index 38e3d02..958bd00 100644 --- a/readme.md +++ b/readme.md @@ -17,6 +17,7 @@ You can also define any of variables using environment variables and configurati - `GIT_DEPLOY_DIR` - `GIT_DEPLOY_BRANCH` - `GIT_DEPLOY_REPO` +- `GIT_DEPLOY_APPEND_HASH` The script will set these variables in this order of preference: From ebae8a2f85a69d6dee0403434e4f7c9113afdf84 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Fri, 11 Dec 2015 09:19:23 -0800 Subject: [PATCH 02/11] Revert "Let 'append_hash' be set by an environment variable." Doesn't work if it's set in `config-file`. This reverts commit 5f380746adcb13f6af5dbd4d66b3b53e79b83e9d. --- deploy.sh | 2 +- readme.md | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/deploy.sh b/deploy.sh index ce79eed..d52a263 100755 --- a/deploy.sh +++ b/deploy.sh @@ -8,7 +8,7 @@ main() { fi #append commit hash to the end of message by default - append_hash=${GIT_DEPLOY_APPEND_HASH:-true} + append_hash=true # Parse arg flags while : ; do diff --git a/readme.md b/readme.md index 958bd00..38e3d02 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,6 @@ You can also define any of variables using environment variables and configurati - `GIT_DEPLOY_DIR` - `GIT_DEPLOY_BRANCH` - `GIT_DEPLOY_REPO` -- `GIT_DEPLOY_APPEND_HASH` The script will set these variables in this order of preference: From 676d33f965414a927c75a8b725b4252d998a5667 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Fri, 11 Dec 2015 16:33:33 -0800 Subject: [PATCH 03/11] Let 'append_hash' be set by an environment variable. This brings back commit 5f380746adcb13f6af5dbd4d66b3b53e79b83e9d except it actually works this time. It also establishes a pattern of "if it's an env-var, overwrite _that_ when parsing args, then set the internal var using the env-var/defaults". --- deploy.sh | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/deploy.sh b/deploy.sh index d52a263..44c57a0 100755 --- a/deploy.sh +++ b/deploy.sh @@ -7,10 +7,9 @@ main() { source .env fi - #append commit hash to the end of message by default - append_hash=true - # Parse arg flags + # If something is exposed as an environment variable, set/overwrite it + # here. Otherwise, set/overwrite the internal variable instead. while : ; do if [[ $1 = "-v" || $1 = "--verbose" ]]; then verbose=true @@ -22,7 +21,7 @@ main() { commit_message=$2 shift 2 elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then - append_hash=false + GIT_DEPLOY_APPEND_HASH=false shift elif [[ $1 = "-c" || $1 = "--config-file" ]]; then source "$2" @@ -32,7 +31,10 @@ main() { fi done - # Set default options + # Set internal option vars from the environment and arg flags. All internal + # vars should be declared here, with sane defaults if applicable. + + # Source directory & target branch. deploy_directory=${GIT_DEPLOY_DIR:-dist} deploy_branch=${GIT_DEPLOY_BRANCH:-gh-pages} @@ -42,7 +44,10 @@ main() { #repository to deploy to. must be readable and writable. repo=${GIT_DEPLOY_REPO:-origin} - + + #append commit hash to the end of message by default + append_hash=${GIT_DEPLOY_APPEND_HASH:-true} + enable_expanded_output if ! git diff --exit-code --quiet --cached; then From 399485745890a7ace0b3a0508910bc74e7892824 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Fri, 11 Dec 2015 16:49:20 -0800 Subject: [PATCH 04/11] Move '--config-file' out of the args-parsing loop. This should allow a config. file specified on the command-line to overwrite values set in '.env', which in turn ovewrite values set in the environment. Values set for these same vars in the args- parsing loop should then overwrite all of the above. Anything left unset by environment vars above are then covered when the "internal" vars are populated by the `var=${ENV_VAR:-default}` lines. --- deploy.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/deploy.sh b/deploy.sh index 44c57a0..9f34466 100755 --- a/deploy.sh +++ b/deploy.sh @@ -7,6 +7,12 @@ main() { source .env fi + # Set args from file specified on the command-line. + if [[ $1 = "-c" || $1 = "--config-file" ]]; then + source "$2" + shift 2 + fi + # Parse arg flags # If something is exposed as an environment variable, set/overwrite it # here. Otherwise, set/overwrite the internal variable instead. @@ -23,9 +29,6 @@ main() { elif [[ $1 = "-n" || $1 = "--no-hash" ]]; then GIT_DEPLOY_APPEND_HASH=false shift - elif [[ $1 = "-c" || $1 = "--config-file" ]]; then - source "$2" - shift 2 else break fi From 44a35e80d4cf780717b7bd674d18464d0b1785a1 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Tue, 15 Dec 2015 16:07:32 -0800 Subject: [PATCH 05/11] Move args-parsing to its own file. --- deploy.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/deploy.sh b/deploy.sh index 9f34466..160d32a 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -o errexit #abort if any command fails -main() { +parse_args() { # Set args from a local environment file. if [ -e ".env" ]; then source .env @@ -51,6 +51,11 @@ main() { #append commit hash to the end of message by default append_hash=${GIT_DEPLOY_APPEND_HASH:-true} +} + +main() { + parse_args "$@" + enable_expanded_output if ! git diff --exit-code --quiet --cached; then From cd03b06054c46d06e3834beebd4c564c25bec30c Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Tue, 15 Dec 2015 16:54:01 -0800 Subject: [PATCH 06/11] Add tests for environment variables. --- tests/env-vars.bats | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/env-vars.bats diff --git a/tests/env-vars.bats b/tests/env-vars.bats new file mode 100644 index 0000000..6df5434 --- /dev/null +++ b/tests/env-vars.bats @@ -0,0 +1,33 @@ +#!/usr/bin/env bats + +source lib/assert.bash +source deploy.sh --source-only + +setup() { + export GIT_DEPLOY_USERNAME=env-username + export GIT_DEPLOY_EMAIL=env-email + export GIT_DEPLOY_DIR=env-dir + export GIT_DEPLOY_BRANCH=env-branch + export GIT_DEPLOY_REPO=env-repo + export GIT_DEPLOY_APPEN_HASH=false +} + +@test 'environment variable: DEFAULT_USERNAME' { + assert that `parse_args && echo $default_username` = "env-username" +} +@test 'environment variable: DEFAULT_EMAIL' { + assert that `parse_args && echo $default_email` = "env-email" +} +@test 'environment variable: GIT_DEPLOY_DIR' { + assert that `parse_args && echo $deploy_directory` = "env-dir" +} +@test 'environment variable: GIT_DEPLOY_BRANCH' { + assert that `parse_args && echo $deploy_branch` = "env-branch" +} +@test 'environment variable: GIT_DEPLOY_REPO' { + assert that `parse_args && echo $repo` = "env-repo" +} +@test 'environment variable: GIT_DEPLOY_APPEND_HASH' { + assert that `parse_args && echo $append_hash` = "false" +} + From 68a726cb081f03a3ad5b98ac44fa93b2d366c990 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Tue, 15 Dec 2015 17:15:45 -0800 Subject: [PATCH 07/11] Add tests for '.env' files. --- tests/env-file.bats | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/env-file.bats diff --git a/tests/env-file.bats b/tests/env-file.bats new file mode 100644 index 0000000..427fc13 --- /dev/null +++ b/tests/env-file.bats @@ -0,0 +1,55 @@ +#!/usr/bin/env bats + +source lib/assert.bash +source deploy.sh --source-only + +envfile_backup=.env.bats-bak + +setup() { + export GIT_DEPLOY_USERNAME=env-username + export GIT_DEPLOY_EMAIL=env-email + export GIT_DEPLOY_DIR=env-dir + export GIT_DEPLOY_BRANCH=env-branch + export GIT_DEPLOY_REPO=env-repo + export GIT_DEPLOY_APPEND_HASH=env-hash + + if [ -e .env ]; then + mv .env $envfile_backup + fi + + cat <<-EOF > .env + GIT_DEPLOY_USERNAME=dotenv-username + GIT_DEPLOY_EMAIL=dotenv-email + GIT_DEPLOY_DIR=dotenv-dir + GIT_DEPLOY_BRANCH=dotenv-branch + GIT_DEPLOY_REPO=dotenv-repo + GIT_DEPLOY_APPEND_HASH=dotenv-hash + EOF +} + +teardown() { + rm .env + if [ -e "$envfile_backup" ]; then + mv $envfile_backup .env + fi +} + +@test 'dotenv variable: DEFAULT_USERNAME' { + assert that `parse_args && echo $default_username` = "dotenv-username" +} +@test 'dotenv variable: DEFAULT_EMAIL' { + assert that `parse_args && echo $default_email` = "dotenv-email" +} +@test 'dotenv variable: GIT_DEPLOY_DIR' { + assert that `parse_args && echo $deploy_directory` = "dotenv-dir" +} +@test 'dotenv variable: GIT_DEPLOY_BRANCH' { + assert that `parse_args && echo $deploy_branch` = "dotenv-branch" +} +@test 'dotenv variable: GIT_DEPLOY_REPO' { + assert that `parse_args && echo $repo` = "dotenv-repo" +} +@test 'dotenv variable: GIT_DEPLOY_APPEND_HASH' { + assert that `parse_args && echo $append_hash` = "dotenv-hash" +} + From a1e762a8f8570f7c6a8bfb225922cd88cd21df72 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Wed, 16 Dec 2015 13:15:49 -0800 Subject: [PATCH 08/11] Replace swath of redundant tests with working arg-parse test. --- arg-parsing-test.bats | 78 +++++++++++++++++++++++++++++++++++++++++++ tests/env-file.bats | 55 ------------------------------ tests/env-vars.bats | 33 ------------------ 3 files changed, 78 insertions(+), 88 deletions(-) create mode 100644 arg-parsing-test.bats delete mode 100644 tests/env-file.bats delete mode 100644 tests/env-vars.bats diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats new file mode 100644 index 0000000..42af766 --- /dev/null +++ b/arg-parsing-test.bats @@ -0,0 +1,78 @@ +#!/usr/bin/env bats + +source lib/assert.bash +source deploy.sh --source-only + +setup() { + run mktemp -dt deploy_test.XXXX + assert_success + tmp=$output + pushd "$tmp" >/dev/null +} + +teardown() { + popd >/dev/null + rm -rf "$tmp" +} + +set_env_vars() { + # Set environment variables. + export GIT_DEPLOY_USERNAME=env-username + export GIT_DEPLOY_EMAIL=env-email + export GIT_DEPLOY_APPEND_HASH=env-var +} + +write_env_file() { + # Write a '.env' file to override environment variables. + cat <<-EOF > .env + GIT_DEPLOY_EMAIL=dotenv-email + GIT_DEPLOY_APPEND_HASH=env-file + EOF +} + +write_conf_file() { + # Write a config-file to override '.env'. + cat <<-EOF > conf + GIT_DEPLOY_EMAIL=conf-email + GIT_DEPLOY_APPEND_HASH=conf-file + EOF +} + +@test 'Arg-parsing defaults to in-script values.' { + parse_args + assert that "$append_hash" = "true" +} + +@test ' overrides script defaults with environment variables.' { + set_env_vars + + parse_args + assert that "$append_hash" = "env-var" +} + +@test ' overrides environment variables with .env file.' { + set_env_vars + write_env_file + + parse_args + assert that "$append_hash" = "env-file" +} + +@test ' overrides .env with a file specified on the command-line.' { + set_env_vars + write_env_file + write_conf_file + + parse_args --config-file conf + assert that "$append_hash" = "conf-file" +} + +@test ' overrides everything with a command-line option.' { + set_env_vars + write_env_file + write_conf_file + + parse_args --config-file conf --no-hash + assert that "$append_hash" = "false" +} + diff --git a/tests/env-file.bats b/tests/env-file.bats deleted file mode 100644 index 427fc13..0000000 --- a/tests/env-file.bats +++ /dev/null @@ -1,55 +0,0 @@ -#!/usr/bin/env bats - -source lib/assert.bash -source deploy.sh --source-only - -envfile_backup=.env.bats-bak - -setup() { - export GIT_DEPLOY_USERNAME=env-username - export GIT_DEPLOY_EMAIL=env-email - export GIT_DEPLOY_DIR=env-dir - export GIT_DEPLOY_BRANCH=env-branch - export GIT_DEPLOY_REPO=env-repo - export GIT_DEPLOY_APPEND_HASH=env-hash - - if [ -e .env ]; then - mv .env $envfile_backup - fi - - cat <<-EOF > .env - GIT_DEPLOY_USERNAME=dotenv-username - GIT_DEPLOY_EMAIL=dotenv-email - GIT_DEPLOY_DIR=dotenv-dir - GIT_DEPLOY_BRANCH=dotenv-branch - GIT_DEPLOY_REPO=dotenv-repo - GIT_DEPLOY_APPEND_HASH=dotenv-hash - EOF -} - -teardown() { - rm .env - if [ -e "$envfile_backup" ]; then - mv $envfile_backup .env - fi -} - -@test 'dotenv variable: DEFAULT_USERNAME' { - assert that `parse_args && echo $default_username` = "dotenv-username" -} -@test 'dotenv variable: DEFAULT_EMAIL' { - assert that `parse_args && echo $default_email` = "dotenv-email" -} -@test 'dotenv variable: GIT_DEPLOY_DIR' { - assert that `parse_args && echo $deploy_directory` = "dotenv-dir" -} -@test 'dotenv variable: GIT_DEPLOY_BRANCH' { - assert that `parse_args && echo $deploy_branch` = "dotenv-branch" -} -@test 'dotenv variable: GIT_DEPLOY_REPO' { - assert that `parse_args && echo $repo` = "dotenv-repo" -} -@test 'dotenv variable: GIT_DEPLOY_APPEND_HASH' { - assert that `parse_args && echo $append_hash` = "dotenv-hash" -} - diff --git a/tests/env-vars.bats b/tests/env-vars.bats deleted file mode 100644 index 6df5434..0000000 --- a/tests/env-vars.bats +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env bats - -source lib/assert.bash -source deploy.sh --source-only - -setup() { - export GIT_DEPLOY_USERNAME=env-username - export GIT_DEPLOY_EMAIL=env-email - export GIT_DEPLOY_DIR=env-dir - export GIT_DEPLOY_BRANCH=env-branch - export GIT_DEPLOY_REPO=env-repo - export GIT_DEPLOY_APPEN_HASH=false -} - -@test 'environment variable: DEFAULT_USERNAME' { - assert that `parse_args && echo $default_username` = "env-username" -} -@test 'environment variable: DEFAULT_EMAIL' { - assert that `parse_args && echo $default_email` = "env-email" -} -@test 'environment variable: GIT_DEPLOY_DIR' { - assert that `parse_args && echo $deploy_directory` = "env-dir" -} -@test 'environment variable: GIT_DEPLOY_BRANCH' { - assert that `parse_args && echo $deploy_branch` = "env-branch" -} -@test 'environment variable: GIT_DEPLOY_REPO' { - assert that `parse_args && echo $repo` = "env-repo" -} -@test 'environment variable: GIT_DEPLOY_APPEND_HASH' { - assert that `parse_args && echo $append_hash` = "false" -} - From f1b23e1f10202b5cb4bac4d253654f79e8b90a6c Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Wed, 16 Dec 2015 13:50:44 -0800 Subject: [PATCH 09/11] Add docs about '-c' requiring it be first arg. --- deploy.sh | 6 +++--- readme.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deploy.sh b/deploy.sh index aee3d55..b20b952 100755 --- a/deploy.sh +++ b/deploy.sh @@ -3,7 +3,7 @@ set -o errexit #abort if any command fails me=$(basename "$0") help_message="\ -Usage: $me [] +Usage: $me [-c FILE] [] Deploy generated files to a git branch. Options: @@ -16,7 +16,8 @@ Options: -n, --no-hash Don't append the source commit's hash to the deploy commit's message. -c, --config-file PATH Override default & environment variables' values - with those in set in the file at 'PATH'. + with those in set in the file at 'PATH'. Must be the + first option specified. Variables: @@ -81,7 +82,6 @@ parse_args() { #append commit hash to the end of message by default append_hash=${GIT_DEPLOY_APPEND_HASH:-true} - } main() { diff --git a/readme.md b/readme.md index 7b449ed..b3e2427 100644 --- a/readme.md +++ b/readme.md @@ -39,7 +39,7 @@ Do this every time you want to deploy, or have your CI server do it. ### options `-h`, `--help`: show the program's help info. -`-c`, `--config-file`: specify a file that overrides the script's default configuration, or those values set in `.env`. The syntax for this file should be normal `var=value` declarations. +`-c`, `--config-file`: specify a file that overrides the script's default configuration, or those values set in `.env`. The syntax for this file should be normal `var=value` declarations. __This option _must_ come first on the command-line__. `-m`, `--message `: specify message to be used for the commit on `deploy_branch`. By default, the message is the title of the source commit, prepended with 'publish: '. From dae280a2569347160b1c094575c37d0550a7d21c Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Wed, 16 Dec 2015 19:17:12 -0800 Subject: [PATCH 10/11] Remove superfluous config bits from arg-parsing tests. --- arg-parsing-test.bats | 4 ---- 1 file changed, 4 deletions(-) diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats index 42af766..52a692a 100644 --- a/arg-parsing-test.bats +++ b/arg-parsing-test.bats @@ -17,15 +17,12 @@ teardown() { set_env_vars() { # Set environment variables. - export GIT_DEPLOY_USERNAME=env-username - export GIT_DEPLOY_EMAIL=env-email export GIT_DEPLOY_APPEND_HASH=env-var } write_env_file() { # Write a '.env' file to override environment variables. cat <<-EOF > .env - GIT_DEPLOY_EMAIL=dotenv-email GIT_DEPLOY_APPEND_HASH=env-file EOF } @@ -33,7 +30,6 @@ write_env_file() { write_conf_file() { # Write a config-file to override '.env'. cat <<-EOF > conf - GIT_DEPLOY_EMAIL=conf-email GIT_DEPLOY_APPEND_HASH=conf-file EOF } From b750ac341f4e096bcd1576cece3b2c50de8f6821 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Wed, 16 Dec 2015 19:41:23 -0800 Subject: [PATCH 11/11] Add test for '--message' with spaces in it. --- arg-parsing-test.bats | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arg-parsing-test.bats b/arg-parsing-test.bats index 52a692a..0600f49 100644 --- a/arg-parsing-test.bats +++ b/arg-parsing-test.bats @@ -71,4 +71,8 @@ write_conf_file() { parse_args --config-file conf --no-hash assert that "$append_hash" = "false" } +@test ' sets a commit message with spaces in it.' { + parse_args --message "a message" + assert that "$commit_message" = "a message" +}