From a913cfd1f5e0ea4307a4bfcd744b6e6f12f7c66e Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Thu, 14 Jul 2022 19:07:46 +0100 Subject: [PATCH] Fix use when passed a latest --- libexec/tfenv-resolve-version | 5 +++++ libexec/tfenv-use | 8 +++++++- test/test_install_and_use.sh | 16 +++++++++++++--- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/libexec/tfenv-resolve-version b/libexec/tfenv-resolve-version index 265e856..ce5cfb0 100755 --- a/libexec/tfenv-resolve-version +++ b/libexec/tfenv-resolve-version @@ -118,6 +118,11 @@ if [[ "${version_requested}" =~ ^min-required$ ]]; then min_required="$(tfenv-min-required)" \ || log 'error' 'tfenv-min-required failed'; + if [ -z "${min_required}" ]; then + log 'debug' 'It was not possible to detect a minimum-required version. Do you have a required_version line present?'; + exit; + fi; + log 'debug' "Minimum required version detected: ${min_required}"; version_requested="${min_required}"; fi; diff --git a/libexec/tfenv-use b/libexec/tfenv-use index 67680f7..0c411cc 100755 --- a/libexec/tfenv-use +++ b/libexec/tfenv-use @@ -89,6 +89,8 @@ fi; log debug "Resolving version with: tfenv-resolve-version ${requested_arg}"; declare resolved="$(tfenv-resolve-version ${requested_arg})"; +[ -z "${resolved}" ] && log 'error' "Failure to resolve version from ${requested_arg}"; + log debug "Resolved to: ${resolved}"; declare version="${resolved%%\:*}"; @@ -111,7 +113,11 @@ if [ -n "${installed_version}" ]; then else if [ "${auto_install}" == 'true' ]; then log 'info' "No installed versions of terraform matched '${resolved}'. Trying to install a matching version since TFENV_AUTO_INSTALL=true"; - tfenv-install "${resolved}" || log 'error' 'Installing a matching version failed'; + + declare install_version=''; + [ "${version}" == 'latest' ] && install_version="${version}:${regex}" || install_version="${version}"; + + tfenv-install "${install_version}" || log 'error' 'Installing a matching version failed'; installed_version="$(\find "${TFENV_CONFIG_DIR}/versions/" -type d -exec basename {} \; \ | tail -n +2 \ diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index ca7a542..fd2b6f0 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -196,14 +196,24 @@ if [ -f "${HOME}/.terraform-version.bup" ]; then mv "${HOME}/.terraform-version.bup" "${HOME}/.terraform-version"; fi; -log 'info' 'Use with no input and TFENV_AUTO_INSTALL:'; +log 'info' '## Use Auto-Install Test 1/2: (No Input)'; cleanup || log 'error' 'Cleanup failed?!'; ( tfenv use || exit 1; check_default_version "$(tfenv list-remote | grep -e "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | head -n 1)" || exit 1; -) && log info '## Use (No Input) Test 1/1: succeeded' \ - || error_and_proceed '## Use (No Input) Test 1/1: failed'; +) && log info '## Use Auto-Install Test 1/2: (No Input) succeeded' \ + || error_and_proceed '## Use Auto-Install Test 1/2: (No Input) failed'; + +log 'info' '## Use Auto-Install Test 2/2: (Specific version)'; +cleanup || log 'error' 'Cleanup failed?!'; + +( + tfenv use 1.0.1 || exit 1; + check_default_version 1.0.1 || exit 1; +) && log info '## Use Auto-Install Test 2/2: (Specific version) succeeded' \ + || error_and_proceed '## Use Auto-Install Test 2/2: (Specific version) failed'; + log 'info' 'Install invalid specific version'; cleanup || log 'error' 'Cleanup failed?!';