From 934a002629c1c9cb73c78558cede392b24ae866c Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 27 Jul 2022 19:22:26 +0100 Subject: [PATCH 1/3] Add test to reproduce #354 --- test/test_use_minrequired.sh | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index b53b2fa..c39ea99 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -121,7 +121,7 @@ echo 'min-required' > .terraform-version; cleanup || log 'error' 'Cleanup failed?!'; -log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir'; +log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir with rel path'; minv='1.1.0'; @@ -138,6 +138,24 @@ echo 'min-required' > chdir-dir/.terraform-version cleanup || log 'error' 'Cleanup failed?!'; + +log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir with abs path'; + +minv='1.2.3'; + +mkdir -p chdir-dir +echo "terraform { + required_version = \">=${minv}\" +}" >> chdir-dir/min_required.tf; +echo 'min-required' > chdir-dir/.terraform-version + +( + TFENV_AUTO_INSTALL=true terraform -chdir="${PWD}/chdir-dir" version; + check_active_version "${minv}" chdir-dir; +) || error_and_proceed 'Min required version from -chdir does not match'; + +cleanup || log 'error' 'Cleanup failed?!'; + if [ "${#errors[@]}" -gt 0 ]; then log 'warn' '===== The following use_minrequired tests failed ====='; for error in "${errors[@]}"; do From b5782f81ad09b5a6b7b2e63b13082fff34b83a0b Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 27 Jul 2022 19:35:04 +0100 Subject: [PATCH 2/3] Fix use of -chdir with an absolute path Fixes #354. --- lib/tfenv-exec.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/tfenv-exec.sh b/lib/tfenv-exec.sh index 189d222..6649df7 100644 --- a/lib/tfenv-exec.sh +++ b/lib/tfenv-exec.sh @@ -5,8 +5,10 @@ set -uo pipefail; function tfenv-exec() { for _arg in ${@:1}; do if [[ "${_arg}" == -chdir=* ]]; then - log 'debug' "Found -chdir arg. Setting TFENV_DIR to: ${_arg#-chdir=}"; - export TFENV_DIR="${PWD}/${_arg#-chdir=}"; + chdir="${_arg#-chdir=}"; + log 'debug' "Found -chdir arg: ${chdir}"; + export TFENV_DIR="${PWD}/$(realpath --relative-to="${PWD}" "$chdir")"; + log 'debug' "Setting TFENV_DIR to: ${TFENV_DIR}"; fi; done; From 9ea34f6e8be4d209edd5de20f9b55fa48eb4981f Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Fri, 4 Nov 2022 22:05:45 +0000 Subject: [PATCH 3/3] Fix realpath not available on macOS --- lib/tfenv-exec.sh | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/tfenv-exec.sh b/lib/tfenv-exec.sh index 6649df7..66af4cd 100644 --- a/lib/tfenv-exec.sh +++ b/lib/tfenv-exec.sh @@ -2,12 +2,38 @@ set -uo pipefail; +function realpath-relative-to() { + # A basic implementation of GNU `realpath --relative-to=$1 $2` + # that can also be used on macOS. + + # 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 \$(${target_file%/*})'"; + file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + local relative_to="$(readlink_f "${1}")"; + local path="$(readlink_f "${2}")"; + + echo "${path#"${relative_to}/"}"; + return 0; +} +export -f realpath-relative-to; + function tfenv-exec() { for _arg in ${@:1}; do if [[ "${_arg}" == -chdir=* ]]; then chdir="${_arg#-chdir=}"; log 'debug' "Found -chdir arg: ${chdir}"; - export TFENV_DIR="${PWD}/$(realpath --relative-to="${PWD}" "$chdir")"; + export TFENV_DIR="${PWD}/$(realpath-relative-to "${PWD}" "${chdir}")"; log 'debug' "Setting TFENV_DIR to: ${TFENV_DIR}"; fi; done;