Skip to content

Commit 2d90024

Browse files
authored
Merge pull request #303 from OJFord/fix-301
Fix #301: find min-required version through -chdir
2 parents e289caf + 256bb81 commit 2d90024

4 files changed

Lines changed: 54 additions & 10 deletions

File tree

lib/helpers.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,20 @@ export -f curlw;
8484

8585
function check_active_version() {
8686
local v="${1}";
87-
[ -n "$(${TFENV_ROOT}/bin/terraform version | grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?$")" ];
87+
local maybe_chdir=;
88+
if [ -n "${2:-}" ]; then
89+
maybe_chdir="-chdir=${2}";
90+
fi;
91+
92+
local active_version="$(${TFENV_ROOT}/bin/terraform ${maybe_chdir} version | grep '^Terraform')";
93+
94+
if ! grep -E "^Terraform v${v}((-dev)|( \([a-f0-9]+\)))?\$" <(echo "${active_version}"); then
95+
log 'debug' "Expected version ${v} but found ${active_version}";
96+
return 1;
97+
fi;
98+
99+
log 'debug' "Active version ${v} as expected";
100+
return 0;
88101
};
89102
export -f check_active_version;
90103

@@ -113,6 +126,8 @@ function cleanup() {
113126
rm -rf ./.terraform-version;
114127
log 'debug' "Deleting ${pwd}/min_required.tf";
115128
rm -rf ./min_required.tf;
129+
log 'debug' "Deleting ${pwd}/chdir-dir";
130+
rm -rf ./chdir-dir;
116131
};
117132
export -f cleanup;
118133

lib/tfenv-exec.sh

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
set -uo pipefail;
44

55
function tfenv-exec() {
6+
for _arg in ${@:1}; do
7+
if [[ "${_arg}" == -chdir=* ]]; then
8+
log 'debug' "Found -chdir arg: ${_arg#-chdir=}";
9+
export TFENV_DIR="${PWD}/${_arg#-chdir=}";
10+
break;
11+
else
12+
export TFENV_DIR="${PWD}";
13+
fi;
14+
log 'info' "Set TFENV_DIR to ${TFENV_DIR}";
15+
done;
16+
617
log 'debug' 'Getting version from tfenv-version-name';
718
TFENV_VERSION="$(tfenv-version-name)" \
8-
&& log 'debug' "TFENV_VERSION is ${TFENV_VERSION}" \
9-
|| {
10-
# Errors will be logged from tfenv-version name,
11-
# we don't need to trouble STDERR with repeat information here
12-
log 'debug' 'Failed to get version from tfenv-version-name';
13-
return 1;
14-
};
19+
&& log 'debug' "TFENV_VERSION is ${TFENV_VERSION}" \
20+
|| {
21+
# Errors will be logged from tfenv-version name,
22+
# we don't need to trouble STDERR with repeat information here
23+
log 'debug' 'Failed to get version from tfenv-version-name';
24+
return 1;
25+
};
1526
export TFENV_VERSION;
1627

1728
if [ ! -d "${TFENV_CONFIG_DIR}/versions/${TFENV_VERSION}" ]; then
@@ -20,7 +31,7 @@ function tfenv-exec() {
2031
TFENV_VERSION_SOURCE="$(tfenv-version-file)";
2132
else
2233
TFENV_VERSION_SOURCE='TFENV_TERRAFORM_VERSION';
23-
fi
34+
fi;
2435
log 'info' "version '${TFENV_VERSION}' is not installed (set by ${TFENV_VERSION_SOURCE}). Installing now as TFENV_AUTO_INSTALL==true";
2536
tfenv-install;
2637
else

lib/tfenv-min-required.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
set -uo pipefail;
44

55
function tfenv-min-required() {
6-
local path="${1:-.}";
6+
local path="${1:-${TFENV_DIR:-.}}";
77

88
local versions="$( echo $(cat ${path}/{*.tf,*.tf.json} 2>/dev/null | grep -Eh '^\s*[^#]*\s*required_version') | grep -o '[~=!<>]\{0,2\}\s*\([0-9]\+\.\?\)\{2,3\}\(-[a-z]\+[0-9]\+\)\?')";
99

test/test_use_minrequired.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ echo 'min-required' > .terraform-version;
120120

121121
cleanup || log 'error' 'Cleanup failed?!';
122122

123+
124+
log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir';
125+
126+
minv='1.1.0';
127+
128+
mkdir -p chdir-dir
129+
echo "terraform {
130+
required_version = \">=${minv}\"
131+
}" >> chdir-dir/min_required.tf;
132+
echo 'min-required' > chdir-dir/.terraform-version
133+
134+
(
135+
TFENV_AUTO_INSTALL=true terraform -chdir=chdir-dir version;
136+
check_active_version "${minv}" chdir-dir;
137+
) || error_and_proceed 'Min required version from -chdir does not match';
138+
139+
cleanup || log 'error' 'Cleanup failed?!';
140+
123141
if [ "${#errors[@]}" -gt 0 ]; then
124142
log 'warn' '===== The following use_minrequired tests failed =====';
125143
for error in "${errors[@]}"; do

0 commit comments

Comments
 (0)