diff --git a/lib/helpers.sh b/lib/helpers.sh index 535232d..0f0de24 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -118,10 +118,17 @@ export -f check_default_version; function cleanup() { log 'info' 'Performing cleanup'; local pwd="$(pwd)"; - log 'debug' "Deleting ${pwd}/version"; - rm -rf ./version; - log 'debug' "Deleting ${pwd}/versions"; - rm -rf ./versions; + + # Safety check to ensure TFENV_CONFIG_DIR is set and not empty + if [ -z "${TFENV_CONFIG_DIR:-""}" ]; then + log 'error' 'TFENV_CONFIG_DIR is not set, cannot perform cleanup safely'; + return 1; + fi; + + log 'debug' "Deleting ${TFENV_CONFIG_DIR}/version"; + rm -rf "${TFENV_CONFIG_DIR}/version"; + log 'debug' "Deleting ${TFENV_CONFIG_DIR}/versions"; + rm -rf "${TFENV_CONFIG_DIR}/versions"; log 'debug' "Deleting ${pwd}/.terraform-version"; rm -rf ./.terraform-version; log 'debug' "Deleting ${pwd}/latest_allowed.tf"; diff --git a/test/run.sh b/test/run.sh index 0e1d362..cfec2fc 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,54 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - errors=(); if [ "${#}" -ne 0 ]; then targets="$@"; diff --git a/test/test_common.sh b/test/test_common.sh new file mode 100644 index 0000000..2797137 --- /dev/null +++ b/test/test_common.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +# Common test setup header +# Source this file at the beginning of test scripts to set up the test environment + +set -uo pipefail; + +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac + readlink_f() { + local target_file="${1}"; + local file_name; + + while [ "${target_file}" != "" ]; do + cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; +else + TFENV_ROOT="${TFENV_ROOT%/}"; +fi; +export TFENV_ROOT; + +if [ -n "${TFENV_HELPERS:-""}" ]; then + log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; +else + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; + if source "${TFENV_ROOT}/lib/helpers.sh"; then + log 'debug' 'Helpers sourced successfully'; + else + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; + fi; +fi; + +# Ensure local tfenv binaries take precedence +export PATH="${TFENV_ROOT}/bin:${PATH}"; diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 879b62d..e5d13d8 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -1,47 +1,7 @@ #!/usr/bin/env bash -set -uo pipefail; -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # diff --git a/test/test_list.sh b/test/test_list.sh index bcb8d57..a9a171b 100755 --- a/test/test_list.sh +++ b/test/test_list.sh @@ -1,48 +1,7 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 5529c9e..ac47eac 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -1,48 +1,7 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # @@ -52,11 +11,11 @@ declare -a errors=(); log 'info' '### Testing symlink functionality'; -TFENV_BIN_DIR='/tmp/tfenv-test'; -log 'info' "## Creating/clearing ${TFENV_BIN_DIR}" -rm -rf "${TFENV_BIN_DIR}" && mkdir "${TFENV_BIN_DIR}"; -log 'info' "## Symlinking ${PWD}/bin/* into ${TFENV_BIN_DIR}"; -ln -s "${PWD}"/bin/* "${TFENV_BIN_DIR}"; +TFENV_BIN_DIR="$(mktemp -d)"; +log 'info' "## Using temporary directory ${TFENV_BIN_DIR}"; +trap 'rm -rf "${TFENV_BIN_DIR}"' EXIT; +log 'info' "## Symlinking ${TFENV_ROOT}/bin/* into ${TFENV_BIN_DIR}"; +ln -s "${TFENV_ROOT}"/bin/* "${TFENV_BIN_DIR}"; cleanup || log 'error' 'Cleanup failed?!'; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index 70fe803..f75e4ae 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -1,48 +1,7 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # @@ -94,11 +53,11 @@ cleanup || error_and_die "Cleanup failed?!" ( tfenv install 0.12.1 || exit 1 tfenv install 0.12.2 || exit 1 - [ -d "./versions" ] || exit 1 + [ -d "${TFENV_CONFIG_DIR}/versions" ] || exit 1 tfenv uninstall 0.12.1 || exit 1 - [ -d "./versions" ] || exit 1 + [ -d "${TFENV_CONFIG_DIR}/versions" ] || exit 1 tfenv uninstall 0.12.2 || exit 1 - [ -d "./versions" ] && exit 1 || exit 0 + [ -d "${TFENV_CONFIG_DIR}/versions" ] && exit 1 || exit 0 ) || error_and_proceed "Removing last version deletes versions directory" if [ "${#errors[@]}" -gt 0 ]; then diff --git a/test/test_use_latestallowed.sh b/test/test_use_latestallowed.sh index 42f8d4f..bc4a07d 100755 --- a/test/test_use_latestallowed.sh +++ b/test/test_use_latestallowed.sh @@ -1,48 +1,7 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index b915d78..8e03a3e 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -1,48 +1,7 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac - readlink_f() { - local target_file="${1}"; - local file_name; - - while [ "${target_file}" != "" ]; do - cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body #