Skip to content

Commit

Permalink
Fix tests for new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Zordrak committed Feb 16, 2020
1 parent b7f72a5 commit a9c7e38
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
20 changes: 17 additions & 3 deletions lib/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion libexec/tfenv-list-remote
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 5 additions & 2 deletions libexec/tfenv-version-name
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand Down
17 changes: 14 additions & 3 deletions test/test_install_and_use.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion test/test_symlink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 =====';
Expand Down
2 changes: 1 addition & 1 deletion test/test_uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 2 additions & 2 deletions test/test_use_minrequired.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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?!';

Expand Down

0 comments on commit a9c7e38

Please sign in to comment.