From a9c7e38b5c2c33aa2701bea08fec4714f0a0037f Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Sun, 16 Feb 2020 17:33:11 +0000 Subject: [PATCH] Fix tests for new logic --- lib/helpers.sh | 20 +++++++++++++++++--- libexec/tfenv-list-remote | 2 +- libexec/tfenv-version-name | 7 +++++-- test/test_install_and_use.sh | 17 ++++++++++++++--- test/test_symlink.sh | 2 +- test/test_uninstall.sh | 2 +- test/test_use_minrequired.sh | 4 ++-- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/helpers.sh b/lib/helpers.sh index 07477e7..cfddfba 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -47,11 +47,25 @@ function curlw () { } export -f curlw; -check_version() { - v="${1}"; +check_active_version() { + local v="${1}"; [ -n "$(${TFENV_ROOT}/bin/terraform --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ]; } -export -f check_version; +export -f check_active_version; + +check_installed_version() { + local v="${1}"; + local bin="${TFENV_ROOT}/versions/${v}/terraform"; + [ -n "$(${bin} --version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ]; +}; +export -f check_installed_version; + +check_default_version() { + local v="${1}"; + local def="$(cat "${TFENV_ROOT}/version")"; + [ "${def}" == "${v}" ]; +}; +export -f check_default_version; cleanup() { log 'info' 'Performing cleanup'; diff --git a/libexec/tfenv-list-remote b/libexec/tfenv-list-remote index 7b59d6b..8b04997 100755 --- a/libexec/tfenv-list-remote +++ b/libexec/tfenv-list-remote @@ -67,7 +67,7 @@ fi TFENV_REMOTE="${TFENV_REMOTE:-https://releases.hashicorp.com}" log 'debug' "TFENV_REMOTE: ${TFENV_REMOTE}"; declare remote_versions="$(curlw -sf "${TFENV_REMOTE}/terraform/")"; -log 'debug' "Remote versions available: ${remote_versions}"; +#log 'debug' "Remote versions available: ${remote_versions}"; # Even in debug mode this is too verbose curlw -sf "${TFENV_REMOTE}/terraform/" \ | grep -o -E "[0-9]+\.[0-9]+\.[0-9]+(-(rc|beta|alpha|oci)[0-9]*)?" \ | uniq; diff --git a/libexec/tfenv-version-name b/libexec/tfenv-version-name index 8e5f501..64bb7c0 100755 --- a/libexec/tfenv-version-name +++ b/libexec/tfenv-version-name @@ -71,11 +71,14 @@ TFENV_VERSION="$(cat "${TFENV_VERSION_FILE}" || true)" \ && log 'debug' "TFENV_VERSION specified in TFENV_VERSION_FILE: ${TFENV_VERSION}"; if [[ "${TFENV_VERSION}" =~ ^latest.*$ ]]; then - log 'debug' 'TFENV_VERSION uses "latest" keyword'; + log 'debug' "TFENV_VERSION uses 'latest' keyword: ${TFENV_VERSION}"; if [[ "${TFENV_VERSION}" =~ ^latest\:.*$ ]]; then regex="${TFENV_VERSION##*\:}"; - log 'debug' "\"latest\" keyword uses regex: ${regex}"; + log 'debug' "'latest' keyword uses regex: ${regex}"; + else + regex='.*'; # Just saves a seperate command below without the grep + log 'debug' "'latest' keyword does not use regex"; fi; version="$(\ls "${TFENV_ROOT}/versions" \ diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 3ed0181..8bea949 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -53,10 +53,21 @@ test_install_and_use() { local k="${2-""}"; local v="${1}"; tfenv install "${k}" || return 1; - check_version "${v}" || return 1; + check_installed_version "${v}" || return 1; + check_active_version "${v}" || return 1; return 0; }; +test_install_and_use_overridden() { + # Takes a static version and the optional keyword to install it with + local k="${2-""}"; + local v="${1}"; + tfenv install "${k}" || return 1; + check_installed_version "${v}" || return 1; + check_default_version "${v}" || return 1; + return 0; +} + declare -a errors=(); log 'info' '### Test Suite: Install and Use' @@ -120,14 +131,14 @@ test_install_and_use "${v1}" \ || error_and_proceed "## \${HOME}/.terraform-version Test 1/1: ( ${v1} ) failed"; log 'info' "## \${HOME}/.terraform-version Test 2/3: Override Install with Parameter ( ${v2} )"; -test_install_and_use "${v2}" "${v2}" \ +test_install_and_use_overridden "${v2}" "${v2}" \ && log info "## \${HOME}/.terraform-version Test 2/3: ( ${v2} ) succeeded" \ || error_and_proceed "## \${HOME}/.terraform-version Test 2/3: ( ${v2} ) failed"; log 'info' "## \${HOME}/.terraform-version Test 3/3: Override Use with Parameter ( ${v2} )"; ( tfenv use "${v2}" || exit 1; - check_version "${v2}" || exit 1; + check_default_version "${v2}" || exit 1; ) && log info "## \${HOME}/.terraform-version Test 3/3: ( ${v2} ) succeeded" \ || error_and_proceed "## \${HOME}/.terraform-version Test 3/3: ( ${v2} ) failed"; diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 3a5cc69..3c99bfd 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -67,7 +67,7 @@ log 'info' '## Using 0.8.2'; ${TFENV_BIN_DIR}/tfenv use 0.8.2 || error_and_proceed 'Use failed'; log 'info' '## Check-Version for 0.8.2'; -check_version 0.8.2 || error_and_proceed 'Version check failed'; +check_active_version 0.8.2 || error_and_proceed 'Version check failed'; if [ "${#errors[@]}" -gt 0 ]; then log 'warn' '===== The following symlink tests failed ====='; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index e03cc04..ccaad83 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -56,7 +56,7 @@ function test_uninstall() { tfenv install "${v}" || return 1; tfenv uninstall "${v}" || return 1; log 'info' 'Confirming uninstall success; an error indicates success:'; - check_version "${v}" && return 1 || return 0; + check_active_version "${v}" && return 1 || return 0; } log 'info' '### Test Suite: Uninstall Local Versions' diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index c0a8b11..a71041a 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -58,7 +58,7 @@ minv='0.8.0'; ( tfenv install "${v}" || true; tfenv use "${v}" || exit 1; - check_version "${v}" || exit 1; + check_active_version "${v}" || exit 1; ) || error_and_proceed "Installing specific version ${v}"; echo "terraform { @@ -69,7 +69,7 @@ echo "terraform { tfenv install min-required; tfenv use min-required; -check_version "${minv}" || error_and_proceed 'Min required version does not match'; +check_active_version "${minv}" || error_and_proceed 'Min required version does not match'; cleanup || log 'error' 'Cleanup failed?!';