Skip to content

Commit 6e0116f

Browse files
authored
Merge branch 'master' into patch-1
2 parents 28de4b0 + f6cc330 commit 6e0116f

10 files changed

+90
-319
lines changed

lib/helpers.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,17 @@ export -f check_default_version;
118118
function cleanup() {
119119
log 'info' 'Performing cleanup';
120120
local pwd="$(pwd)";
121-
log 'debug' "Deleting ${pwd}/version";
122-
rm -rf ./version;
123-
log 'debug' "Deleting ${pwd}/versions";
124-
rm -rf ./versions;
121+
122+
# Safety check to ensure TFENV_CONFIG_DIR is set and not empty
123+
if [ -z "${TFENV_CONFIG_DIR:-""}" ]; then
124+
log 'error' 'TFENV_CONFIG_DIR is not set, cannot perform cleanup safely';
125+
return 1;
126+
fi;
127+
128+
log 'debug' "Deleting ${TFENV_CONFIG_DIR}/version";
129+
rm -rf "${TFENV_CONFIG_DIR}/version";
130+
log 'debug' "Deleting ${TFENV_CONFIG_DIR}/versions";
131+
rm -rf "${TFENV_CONFIG_DIR}/versions";
125132
log 'debug' "Deleting ${pwd}/.terraform-version";
126133
rm -rf ./.terraform-version;
127134
log 'debug' "Deleting ${pwd}/latest_allowed.tf";

libexec/tfenv-resolve-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ if [[ "${version_requested}" =~ ^latest-allowed$ ]]; then
142142
;;
143143
'~>'*)
144144
version_without_rightmost="${version_num%.*}";
145-
version_requested="latest:^${version_without_rightmost}";
145+
version_requested="latest:^${version_without_rightmost}\.";
146146
;;
147147
*)
148148
log 'error' "Unsupported version spec: '${version_spec}', only >, >=, <=, and ~> are supported.";

test/run.sh

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,12 @@
11
#!/usr/bin/env bash
2-
set -uo pipefail;
32

4-
####################################
5-
# Ensure we can execute standalone #
6-
####################################
7-
8-
function early_death() {
9-
echo "[FATAL] ${0}: ${1}" >&2;
10-
exit 1;
11-
};
12-
13-
if [ -z "${TFENV_ROOT:-""}" ]; then
14-
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
15-
readlink_f() {
16-
local target_file="${1}";
17-
local file_name;
18-
19-
while [ "${target_file}" != "" ]; do
20-
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
21-
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
22-
target_file="$(readlink "${file_name}")";
23-
done;
24-
25-
echo "$(pwd -P)/${file_name}";
26-
};
27-
28-
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
29-
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
30-
else
31-
TFENV_ROOT="${TFENV_ROOT%/}";
32-
fi;
33-
export TFENV_ROOT;
34-
35-
if [ -n "${TFENV_HELPERS:-""}" ]; then
36-
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
37-
else
38-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
39-
if source "${TFENV_ROOT}/lib/helpers.sh"; then
40-
log 'debug' 'Helpers sourced successfully';
41-
else
42-
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
43-
fi;
44-
fi;
3+
# Source common test setup
4+
source "$(dirname "${0}")/test_common.sh";
455

466
#####################
477
# Begin Script Body #
488
#####################
499

50-
export PATH="${TFENV_ROOT}/bin:${PATH}";
51-
5210
errors=();
5311
if [ "${#}" -ne 0 ]; then
5412
targets="$@";

test/test_common.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
3+
# Common test setup header
4+
# Source this file at the beginning of test scripts to set up the test environment
5+
6+
set -uo pipefail;
7+
8+
####################################
9+
# Ensure we can execute standalone #
10+
####################################
11+
12+
function early_death() {
13+
echo "[FATAL] ${0}: ${1}" >&2;
14+
exit 1;
15+
};
16+
17+
if [ -z "${TFENV_ROOT:-""}" ]; then
18+
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
19+
readlink_f() {
20+
local target_file="${1}";
21+
local file_name;
22+
23+
while [ "${target_file}" != "" ]; do
24+
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
25+
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
26+
target_file="$(readlink "${file_name}")";
27+
done;
28+
29+
echo "$(pwd -P)/${file_name}";
30+
};
31+
32+
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
33+
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
34+
else
35+
TFENV_ROOT="${TFENV_ROOT%/}";
36+
fi;
37+
export TFENV_ROOT;
38+
39+
if [ -n "${TFENV_HELPERS:-""}" ]; then
40+
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
41+
else
42+
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
43+
if source "${TFENV_ROOT}/lib/helpers.sh"; then
44+
log 'debug' 'Helpers sourced successfully';
45+
else
46+
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
47+
fi;
48+
fi;
49+
50+
# Ensure local tfenv binaries take precedence
51+
export PATH="${TFENV_ROOT}/bin:${PATH}";

test/test_install_and_use.sh

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
11
#!/usr/bin/env bash
2-
set -uo pipefail;
32

4-
####################################
5-
# Ensure we can execute standalone #
6-
####################################
7-
8-
function early_death() {
9-
echo "[FATAL] ${0}: ${1}" >&2;
10-
exit 1;
11-
};
12-
13-
if [ -z "${TFENV_ROOT:-""}" ]; then
14-
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
15-
readlink_f() {
16-
local target_file="${1}";
17-
local file_name;
18-
19-
while [ "${target_file}" != "" ]; do
20-
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
21-
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
22-
target_file="$(readlink "${file_name}")";
23-
done;
24-
25-
echo "$(pwd -P)/${file_name}";
26-
};
27-
28-
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
29-
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
30-
else
31-
TFENV_ROOT="${TFENV_ROOT%/}";
32-
fi;
33-
export TFENV_ROOT;
34-
35-
if [ -n "${TFENV_HELPERS:-""}" ]; then
36-
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
37-
else
38-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
39-
if source "${TFENV_ROOT}/lib/helpers.sh"; then
40-
log 'debug' 'Helpers sourced successfully';
41-
else
42-
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
43-
fi;
44-
fi;
3+
# Source common test setup
4+
source "$(dirname "${0}")/test_common.sh";
455

466
#####################
477
# Begin Script Body #

test/test_list.sh

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,7 @@
11
#!/usr/bin/env bash
22

3-
set -uo pipefail;
4-
5-
####################################
6-
# Ensure we can execute standalone #
7-
####################################
8-
9-
function early_death() {
10-
echo "[FATAL] ${0}: ${1}" >&2;
11-
exit 1;
12-
};
13-
14-
if [ -z "${TFENV_ROOT:-""}" ]; then
15-
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
16-
readlink_f() {
17-
local target_file="${1}";
18-
local file_name;
19-
20-
while [ "${target_file}" != "" ]; do
21-
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
22-
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
23-
target_file="$(readlink "${file_name}")";
24-
done;
25-
26-
echo "$(pwd -P)/${file_name}";
27-
};
28-
29-
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
30-
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
31-
else
32-
TFENV_ROOT="${TFENV_ROOT%/}";
33-
fi;
34-
export TFENV_ROOT;
35-
36-
if [ -n "${TFENV_HELPERS:-""}" ]; then
37-
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
38-
else
39-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
40-
if source "${TFENV_ROOT}/lib/helpers.sh"; then
41-
log 'debug' 'Helpers sourced successfully';
42-
else
43-
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
44-
fi;
45-
fi;
3+
# Source common test setup
4+
source "$(dirname "${0}")/test_common.sh";
465

476
#####################
487
# Begin Script Body #

test/test_symlink.sh

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,7 @@
11
#!/usr/bin/env bash
22

3-
set -uo pipefail;
4-
5-
####################################
6-
# Ensure we can execute standalone #
7-
####################################
8-
9-
function early_death() {
10-
echo "[FATAL] ${0}: ${1}" >&2;
11-
exit 1;
12-
};
13-
14-
if [ -z "${TFENV_ROOT:-""}" ]; then
15-
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
16-
readlink_f() {
17-
local target_file="${1}";
18-
local file_name;
19-
20-
while [ "${target_file}" != "" ]; do
21-
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
22-
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
23-
target_file="$(readlink "${file_name}")";
24-
done;
25-
26-
echo "$(pwd -P)/${file_name}";
27-
};
28-
29-
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
30-
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
31-
else
32-
TFENV_ROOT="${TFENV_ROOT%/}";
33-
fi;
34-
export TFENV_ROOT;
35-
36-
if [ -n "${TFENV_HELPERS:-""}" ]; then
37-
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
38-
else
39-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
40-
if source "${TFENV_ROOT}/lib/helpers.sh"; then
41-
log 'debug' 'Helpers sourced successfully';
42-
else
43-
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
44-
fi;
45-
fi;
3+
# Source common test setup
4+
source "$(dirname "${0}")/test_common.sh";
465

476
#####################
487
# Begin Script Body #
@@ -52,11 +11,11 @@ declare -a errors=();
5211

5312
log 'info' '### Testing symlink functionality';
5413

55-
TFENV_BIN_DIR='/tmp/tfenv-test';
56-
log 'info' "## Creating/clearing ${TFENV_BIN_DIR}"
57-
rm -rf "${TFENV_BIN_DIR}" && mkdir "${TFENV_BIN_DIR}";
58-
log 'info' "## Symlinking ${PWD}/bin/* into ${TFENV_BIN_DIR}";
59-
ln -s "${PWD}"/bin/* "${TFENV_BIN_DIR}";
14+
TFENV_BIN_DIR="$(mktemp -d)";
15+
log 'info' "## Using temporary directory ${TFENV_BIN_DIR}";
16+
trap 'rm -rf "${TFENV_BIN_DIR}"' EXIT;
17+
log 'info' "## Symlinking ${TFENV_ROOT}/bin/* into ${TFENV_BIN_DIR}";
18+
ln -s "${TFENV_ROOT}"/bin/* "${TFENV_BIN_DIR}";
6019

6120
cleanup || log 'error' 'Cleanup failed?!';
6221

test/test_uninstall.sh

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,7 @@
11
#!/usr/bin/env bash
22

3-
set -uo pipefail;
4-
5-
####################################
6-
# Ensure we can execute standalone #
7-
####################################
8-
9-
function early_death() {
10-
echo "[FATAL] ${0}: ${1}" >&2;
11-
exit 1;
12-
};
13-
14-
if [ -z "${TFENV_ROOT:-""}" ]; then
15-
# http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
16-
readlink_f() {
17-
local target_file="${1}";
18-
local file_name;
19-
20-
while [ "${target_file}" != "" ]; do
21-
cd "$(dirname ${target_file})" || early_death "Failed to 'cd \$(dirname ${target_file})' while trying to determine TFENV_ROOT";
22-
file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT";
23-
target_file="$(readlink "${file_name}")";
24-
done;
25-
26-
echo "$(pwd -P)/${file_name}";
27-
};
28-
29-
TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)";
30-
[ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT";
31-
else
32-
TFENV_ROOT="${TFENV_ROOT%/}";
33-
fi;
34-
export TFENV_ROOT;
35-
36-
if [ -n "${TFENV_HELPERS:-""}" ]; then
37-
log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again';
38-
else
39-
[ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh";
40-
if source "${TFENV_ROOT}/lib/helpers.sh"; then
41-
log 'debug' 'Helpers sourced successfully';
42-
else
43-
early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh";
44-
fi;
45-
fi;
3+
# Source common test setup
4+
source "$(dirname "${0}")/test_common.sh";
465

476
#####################
487
# Begin Script Body #
@@ -94,11 +53,11 @@ cleanup || error_and_die "Cleanup failed?!"
9453
(
9554
tfenv install 0.12.1 || exit 1
9655
tfenv install 0.12.2 || exit 1
97-
[ -d "./versions" ] || exit 1
56+
[ -d "${TFENV_CONFIG_DIR}/versions" ] || exit 1
9857
tfenv uninstall 0.12.1 || exit 1
99-
[ -d "./versions" ] || exit 1
58+
[ -d "${TFENV_CONFIG_DIR}/versions" ] || exit 1
10059
tfenv uninstall 0.12.2 || exit 1
101-
[ -d "./versions" ] && exit 1 || exit 0
60+
[ -d "${TFENV_CONFIG_DIR}/versions" ] && exit 1 || exit 0
10261
) || error_and_proceed "Removing last version deletes versions directory"
10362

10463
if [ "${#errors[@]}" -gt 0 ]; then

0 commit comments

Comments
 (0)