Skip to content

Commit fe8767d

Browse files
authored
Merge pull request #355 from OJFord/fix-354
Fix use of -chdir with an absolute path
2 parents ca62417 + 9acfdf8 commit fe8767d

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

lib/tfenv-exec.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,39 @@
22

33
set -uo pipefail;
44

5+
function realpath-relative-to() {
6+
# A basic implementation of GNU `realpath --relative-to=$1 $2`
7+
# that can also be used on macOS.
8+
9+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
10+
readlink_f() {
11+
local target_file="${1}";
12+
local file_name;
13+
14+
while [ "${target_file}" != "" ]; do
15+
cd "$(dirname "$target_file")" || early_death "Failed to 'cd \$(${target_file%/*})'";
16+
file_name="${target_file##*/}" || early_death "Failed to '\"${target_file##*/}\"'";
17+
target_file="$(readlink "${file_name}")";
18+
done;
19+
20+
echo "$(pwd -P)/${file_name}";
21+
};
22+
23+
local relative_to="$(readlink_f "${1}")";
24+
local path="$(readlink_f "${2}")";
25+
26+
echo "${path#"${relative_to}/"}";
27+
return 0;
28+
}
29+
export -f realpath-relative-to;
30+
531
function tfenv-exec() {
632
for _arg in ${@:1}; do
733
if [[ "${_arg}" == -chdir=* ]]; then
8-
log 'debug' "Found -chdir arg. Setting TFENV_DIR to: ${_arg#-chdir=}";
9-
export TFENV_DIR="${PWD}/${_arg#-chdir=}";
34+
chdir="${_arg#-chdir=}";
35+
log 'debug' "Found -chdir arg: ${chdir}";
36+
export TFENV_DIR="${PWD}/$(realpath-relative-to "${PWD}" "${chdir}")";
37+
log 'debug' "Setting TFENV_DIR to: ${TFENV_DIR}";
1038
fi;
1139
done;
1240

test/test_use_minrequired.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ echo 'min-required' > .terraform-version;
8080
cleanup || log 'error' 'Cleanup failed?!';
8181

8282

83-
log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir';
83+
log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir with rel path';
8484

8585
minv='1.1.0';
8686

@@ -97,6 +97,24 @@ echo 'min-required' > chdir-dir/.terraform-version
9797

9898
cleanup || log 'error' 'Cleanup failed?!';
9999

100+
101+
log 'info' '### Install min-required with TFENV_AUTO_INSTALL & -chdir with abs path';
102+
103+
minv='1.2.3';
104+
105+
mkdir -p chdir-dir
106+
echo "terraform {
107+
required_version = \">=${minv}\"
108+
}" >> chdir-dir/min_required.tf;
109+
echo 'min-required' > chdir-dir/.terraform-version
110+
111+
(
112+
TFENV_AUTO_INSTALL=true terraform -chdir="${PWD}/chdir-dir" version;
113+
check_active_version "${minv}" chdir-dir;
114+
) || error_and_proceed 'Min required version from -chdir does not match';
115+
116+
cleanup || log 'error' 'Cleanup failed?!';
117+
100118
if [ "${#errors[@]}" -gt 0 ]; then
101119
log 'warn' '===== The following use_minrequired tests failed =====';
102120
for error in "${errors[@]}"; do

0 commit comments

Comments
 (0)