From a1e762a8f8570f7c6a8bfb225922cd88cd21df72 Mon Sep 17 00:00:00 2001 From: Matt Pearson Date: Wed, 16 Dec 2015 13:15:49 -0800 Subject: [PATCH] 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" -} -