From 0296e22786b768bcf8a90bcb253299c1bf0083e1 Mon Sep 17 00:00:00 2001 From: Rodrigue Koffi Date: Thu, 20 Oct 2022 10:27:36 +0200 Subject: [PATCH 01/30] Update README.md --- README.md | 56 +++++++++++++++++++++++++++---------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 9155dbe..d1b134c 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,13 @@ Currently tfenv supports the following OSes Install via Homebrew ```console -$ brew install tfenv +brew install tfenv ``` Install via Arch User Repository (AUR) ```console -$ yay --sync tfenv +yay --sync tfenv ``` Install via puppet @@ -45,34 +45,34 @@ include ::tfenv 1. Check out tfenv into any path (here is `${HOME}/.tfenv`) ```console -$ git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv +git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv ``` 2. Add `~/.tfenv/bin` to your `$PATH` any way you like ```console -$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile +echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile ``` For WSL users ```bash -$ echo 'export PATH=$PATH:$HOME/.tfenv/bin' >> ~/.bashrc +echo 'export PATH=$PATH:$HOME/.tfenv/bin' >> ~/.bashrc ``` OR you can make symlinks for `tfenv/bin/*` scripts into a path that is already added to your `$PATH` (e.g. `/usr/local/bin`) `OSX/Linux Only!` ```console -$ ln -s ~/.tfenv/bin/* /usr/local/bin +ln -s ~/.tfenv/bin/* /usr/local/bin ``` On Ubuntu/Debian touching `/usr/local/bin` might require sudo access, but you can create `${HOME}/bin` or `${HOME}/.local/bin` and on next login it will get added to the session `$PATH` or by running `. ${HOME}/.profile` it will get added to the current shell session's `$PATH`. ```console -$ mkdir -p ~/.local/bin/ -$ . ~/.profile -$ ln -s ~/.tfenv/bin/* ~/.local/bin -$ which tfenv +mkdir -p ~/.local/bin/ +. ~/.profile +ln -s ~/.tfenv/bin/* ~/.local/bin +which tfenv ``` ## Usage @@ -110,8 +110,8 @@ You can opt-in to using GnuPG tools for PGP signature verification if keybase is Where `TFENV_INSTALL_DIR` is for example, `~/.tfenv` or `/usr/local/Cellar/tfenv/` ```console -$ echo 'trust-tfenv: yes' > ${TFENV_INSTALL_DIR}/use-gpgv -$ tfenv install +echo 'trust-tfenv: yes' > ${TFENV_INSTALL_DIR}/use-gpgv +tfenv install ``` The `trust-tfenv` directive means that verification uses a copy of the @@ -157,7 +157,7 @@ Specify architecture. Architecture other than the default amd64 can be specified Note: Default changes to `arm64` for versions that have arm64 builds available when `$(uname -m)` matches `aarch64* | arm64*` ```console -$ TFENV_ARCH=arm64 tfenv install 0.7.9 +TFENV_ARCH=arm64 tfenv install 0.7.9 ``` ##### `TFENV_AUTO_INSTALL` @@ -167,11 +167,11 @@ String (Default: true) Should tfenv automatically install terraform if the version specified by defaults or a .terraform-version file is not currently installed. ```console -$ TFENV_AUTO_INSTALL=false terraform plan +TFENV_AUTO_INSTALL=false terraform plan ``` ```console -$ terraform use +terraform use ``` ##### `TFENV_CURL_OUTPUT` @@ -202,7 +202,7 @@ String (Default: https://releases.hashicorp.com) To install from a remote other than the default ```console -$ TFENV_REMOTE=https://example.jfrog.io/artifactory/hashicorp +TFENV_REMOTE=https://example.jfrog.io/artifactory/hashicorp ``` ##### `TFENV_REVERSE_REMOTE` @@ -217,7 +217,7 @@ is instead providing a list that is oldes-first, set `TFENV_REVERSE_REMOTE=1` an functionality will be restored. ```console -$ TFENV_REVERSE_REMOTE=1 tfenv list-remote +TFENV_REVERSE_REMOTE=1 tfenv list-remote ``` ##### `TFENV_CONFIG_DIR` @@ -241,7 +241,7 @@ If not empty string, this variable overrides Terraform version, specified in [.t e.g. ```console -$ TFENV_TERRAFORM_VERSION=latest:^0.11. terraform --version +TFENV_TERRAFORM_VERSION=latest:^0.11. terraform --version ``` ##### `TFENV_NETRC_PATH` @@ -253,7 +253,7 @@ If not empty string, this variable specifies the credentials file used to access e.g. ```console -$ TFENV_NETRC_PATH="$PWD/.netrc.tfenv" +TFENV_NETRC_PATH="$PWD/.netrc.tfenv" ``` #### Bashlog Logging Library @@ -294,7 +294,7 @@ Each executable logs to its own file. e.g. ```console -$ BASHLOG_FILE=1 tfenv use latest +BASHLOG_FILE=1 tfenv use latest ``` will log to `/tmp/tfenv-use.log` @@ -316,7 +316,7 @@ This variable allows you to pass a string containing a command that will be exec e.g. ```console -$ BASHLOG_I_PROMISE_TO_BE_CAREFUL_CUSTOM_EVAL_PREFIX='echo "${$$} "' +BASHLOG_I_PROMISE_TO_BE_CAREFUL_CUSTOM_EVAL_PREFIX='echo "${$$} "' ``` will prefix every log line with the calling process' PID. @@ -332,7 +332,7 @@ Each executable logs to its own file. e.g. ```console -$ BASHLOG_JSON=1 tfenv use latest +BASHLOG_JSON=1 tfenv use latest ``` will log in JSON format to `/tmp/tfenv-use.log.json` @@ -356,10 +356,10 @@ To log to syslog using the `logger` binary, set this to 1. The basic functionality is thus: ```console -$ local tag="${BASHLOG_SYSLOG_TAG:-$(basename "${0}")}"; -$ local facility="${BASHLOG_SYSLOG_FACILITY:-local0}"; -$ local pid="${$}"; -$ logger --id="${pid}" -t "${tag}" -p "${facility}.${severity}" "${syslog_line}" +local tag="${BASHLOG_SYSLOG_TAG:-$(basename "${0}")}"; +local facility="${BASHLOG_SYSLOG_FACILITY:-local0}"; +local pid="${$}"; +logger --id="${pid}" -t "${tag}" -p "${facility}.${severity}" "${syslog_line}" ``` ##### `BASHLOG_SYSLOG_FACILITY` @@ -489,13 +489,13 @@ Terraform v0.7.3 ## Upgrading ```console -$ git --git-dir=~/.tfenv/.git pull +git --git-dir=~/.tfenv/.git pull ``` ## Uninstalling ```console -$ rm -rf /some/path/to/tfenv +rm -rf /some/path/to/tfenv ``` ## LICENSE From e87532a7db08ab721f8a67865da621b6a57d571c Mon Sep 17 00:00:00 2001 From: David Beitey Date: Tue, 6 Dec 2022 11:00:20 +1000 Subject: [PATCH 02/30] Update CI badge in README This updates the CI badge according to the updated GitHub Action --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9155dbe..8307709 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![CI](https://github.com/tfutils/tfenv/workflows/CI/badge.svg) +[![CI Test](https://github.com/tfutils/tfenv/actions/workflows/test.yml/badge.svg)](https://github.com/tfutils/tfenv/actions/workflows/test.yml) # tfenv From fb2086cb4e60852455bdc9d87ed912e2a30bdbb1 Mon Sep 17 00:00:00 2001 From: Bob Idle <102661087+bobidle@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:30:21 +0100 Subject: [PATCH 03/30] Update link to terraform docs --- README.md | 2 +- libexec/tfenv-min-required | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9155dbe..4ef1adf 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ If a parameter is passed, available options: - `latest-allowed` is a syntax to scan your Terraform files to detect which version is maximally allowed. - `min-required` is a syntax to scan your Terraform files to detect which version is minimally required. -See [required_version](https://www.terraform.io/docs/configuration/terraform.html) docs. Also [see min-required & latest-allowed](#min-required) section below. +See [required_version](https://developer.hashicorp.com/terraform/language/settings) docs. Also [see min-required & latest-allowed](#min-required) section below. ```console $ tfenv install diff --git a/libexec/tfenv-min-required b/libexec/tfenv-min-required index 80d91b1..ee1642c 100755 --- a/libexec/tfenv-min-required +++ b/libexec/tfenv-min-required @@ -68,7 +68,7 @@ terraform { required_version = ">= 0.0.0" } -see https://www.terraform.io/docs/configuration/terraform.html for details'; +see https://developer.hashicorp.com/terraform/language/settings for details'; }; declare min_required="$(tfenv-min-required "${TFENV_DIR:-$(pwd)}")"; From 8d515e8c06612b16d3157af9f472ba0f7449da6f Mon Sep 17 00:00:00 2001 From: John Losito Date: Sun, 2 Apr 2023 13:24:51 -0400 Subject: [PATCH 04/30] Check for github action updates monthly --- .github/dependabot.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..64284b9 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +--- +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "monthly" From 0e7c19e686487eec490178ef0e3f32c66699120c Mon Sep 17 00:00:00 2001 From: Don Luchini Date: Tue, 30 May 2023 09:11:12 -0400 Subject: [PATCH 05/30] Do not check the release list if a Terraform is already installed (fixes #395) Today, tfenv checks the Terraform release list when running `tfenv install`, even if the requested Terraform is already installed. This fixes an issue where a script containing `tfenv install` becomes inoperable even if Terraform is installed, but releases.hashicorp.com is down. See #395 for more information. --- libexec/tfenv-install | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libexec/tfenv-install b/libexec/tfenv-install index d13b384..d298118 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -73,15 +73,15 @@ declare regex="${resolved##*\:}"; log 'debug' "Processing install for version ${version}, using regex ${regex}"; -remote_version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"; -[ -n "${remote_version}" ] && version="${remote_version}" || log 'error' "No versions matching '${requested:-$version}' found in remote"; - dst_path="${TFENV_CONFIG_DIR}/versions/${version}"; if [ -f "${dst_path}/terraform" ]; then echo "Terraform v${version} is already installed"; exit 0; fi; +remote_version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"; +[ -n "${remote_version}" ] && version="${remote_version}" || log 'error' "No versions matching '${requested:-$version}' found in remote"; + case "$(uname -s)" in Darwin*) kernel="darwin"; From 594cd00b14c2142bc2a47f0003ae1744a9587a7e Mon Sep 17 00:00:00 2001 From: Kasper Christensen Date: Tue, 22 Aug 2023 23:38:52 +0200 Subject: [PATCH 06/30] Added export PATH zprofile example in README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9155dbe..cc63013 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,16 @@ $ git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv 2. Add `~/.tfenv/bin` to your `$PATH` any way you like +bash: ```console $ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile ``` +zsh: +```console +$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile +``` - For WSL users +For WSL users: ```bash $ echo 'export PATH=$PATH:$HOME/.tfenv/bin' >> ~/.bashrc ``` From e70ae89ac1e8cca875eb735b0f6e9c2b51e8784b Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:37:36 +0000 Subject: [PATCH 07/30] Bump docker/build-push-action from 3 to 5 Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 3 to 5. - [Release notes](https://github.com/docker/build-push-action/releases) - [Commits](https://github.com/docker/build-push-action/compare/v3...v5) --- updated-dependencies: - dependency-name: docker/build-push-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8a711cd..23d6421 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,7 +34,7 @@ jobs: if: contains(matrix.os, 'ubuntu') with: install: true - - uses: 'docker/build-push-action@v3' + - uses: 'docker/build-push-action@v5' if: contains(matrix.os, 'ubuntu') with: context: . @@ -73,7 +73,7 @@ jobs: if: contains(matrix.os, 'ubuntu') with: install: true - - uses: 'docker/build-push-action@v3' + - uses: 'docker/build-push-action@v5' if: contains(matrix.os, 'ubuntu') with: context: . From 0d393891fbc61594077f1623169b0f8994b23272 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:37:39 +0000 Subject: [PATCH 08/30] Bump actions/checkout from 2 to 4 Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 4. - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v2...v4) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8a711cd..1af056f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,7 +21,7 @@ jobs: - 'macos-latest' - 'ubuntu-latest' steps: - - uses: 'actions/checkout@v2' + - uses: 'actions/checkout@v4' with: fetch-depth: 1 - name: 'Install Dependencies' @@ -60,7 +60,7 @@ jobs: - 'ubuntu-18.04' - 'windows-2019' steps: - - uses: 'actions/checkout@v2' + - uses: 'actions/checkout@v4' with: fetch-depth: 1 - name: 'Install Dependencies' From 266c7648e44d9aac29ca1c89da10ddded71c1481 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 19 Dec 2023 11:47:53 +0000 Subject: [PATCH 09/30] Bump docker/setup-buildx-action from 2 to 3 Bumps [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) from 2 to 3. - [Release notes](https://github.com/docker/setup-buildx-action/releases) - [Commits](https://github.com/docker/setup-buildx-action/compare/v2...v3) --- updated-dependencies: - dependency-name: docker/setup-buildx-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c599295..c8c26ee 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,7 +30,7 @@ jobs: run: './test/run.sh' shell: 'bash' - - uses: 'docker/setup-buildx-action@v2' + - uses: 'docker/setup-buildx-action@v3' if: contains(matrix.os, 'ubuntu') with: install: true @@ -69,7 +69,7 @@ jobs: run: './test/run.sh' shell: 'bash' - - uses: 'docker/setup-buildx-action@v2' + - uses: 'docker/setup-buildx-action@v3' if: contains(matrix.os, 'ubuntu') with: install: true From c14ab32c2bd7c7b2dc473751cdf742398d1cec78 Mon Sep 17 00:00:00 2001 From: Zordrak Date: Tue, 19 Dec 2023 12:55:00 +0000 Subject: [PATCH 10/30] Revert "Do not check the release list if a Terraform is already installed (fixes #395)" --- libexec/tfenv-install | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libexec/tfenv-install b/libexec/tfenv-install index d298118..d13b384 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -73,15 +73,15 @@ declare regex="${resolved##*\:}"; log 'debug' "Processing install for version ${version}, using regex ${regex}"; +remote_version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"; +[ -n "${remote_version}" ] && version="${remote_version}" || log 'error' "No versions matching '${requested:-$version}' found in remote"; + dst_path="${TFENV_CONFIG_DIR}/versions/${version}"; if [ -f "${dst_path}/terraform" ]; then echo "Terraform v${version} is already installed"; exit 0; fi; -remote_version="$(tfenv-list-remote | grep -e "${regex}" | head -n 1)"; -[ -n "${remote_version}" ] && version="${remote_version}" || log 'error' "No versions matching '${requested:-$version}' found in remote"; - case "$(uname -s)" in Darwin*) kernel="darwin"; From 6f3d636f31470790ba1efaa6f040b653fbaee582 Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Tue, 19 Dec 2023 13:35:06 +0000 Subject: [PATCH 11/30] Replace use of rev for Windows compatibility --- libexec/tfenv-resolve-version | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/tfenv-resolve-version b/libexec/tfenv-resolve-version index d0b66bc..69468e5 100755 --- a/libexec/tfenv-resolve-version +++ b/libexec/tfenv-resolve-version @@ -129,7 +129,7 @@ fi; if [[ "${version_requested}" =~ ^latest-allowed$ ]]; then log 'debug' 'Detecting latest allowable version...'; - version_spec="$(grep -h required_version "${TFENV_DIR:-$(pwd)}"/{*.tf,*.tf.json} 2>/dev/null | rev | cut -d'"' -f2 | rev | cut -d, -f1)"; + version_spec="$(grep -h required_version "${TFENV_DIR:-$(pwd)}"/{*.tf,*.tf.json} 2>/dev/null | { IFS='"' read -r _ ver _; echo "${ver%%,*}"; })"; version_num="$(echo "${version_spec}" | sed -E 's/[^0-9.]+//')"; log 'debug' "Using ${version_num} from version spec: ${version_spec}"; @@ -141,7 +141,7 @@ if [[ "${version_requested}" =~ ^latest-allowed$ ]]; then version_requested="${version_num}"; ;; '~>'*) - version_without_rightmost="$(echo "${version_num}" | rev | cut -d. -f2- | rev)"; + version_without_rightmost="${version_num%.*}"; version_requested="latest:^${version_without_rightmost}"; ;; *) From b9042b0f75012e8a0f55bff883656c8da6eddb66 Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Tue, 19 Dec 2023 14:20:58 +0000 Subject: [PATCH 12/30] Just suppress darwin tests for unsupported darwin builds --- test/test_install_and_use.sh | 21 +++++++++++++++++++-- test/test_symlink.sh | 12 ++++++------ test/test_use_minrequired.sh | 8 ++++---- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index fd2b6f0..879b62d 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -105,11 +105,28 @@ tests__kv=( "$(tfenv list-remote | grep 'rc' | head -n 1),latest:rc" "$(tfenv list-remote | grep '^0\.11\.' | head -n 1),latest:^0.11." '0.11.15-oci,0.11.15-oci' - '0.8.8,latest:^0.8' - '0.7.13,0.7.13' + '1.3.10,latest:^1\.3' + '1.6.3,1.6.3' '0.14.6,v0.14.6' ); +log 'info' "Kernel under test: $(uname -s)"; + +if [[ "$(uname -s)" != Darwin* ]]; then + log 'info' "We're not Darwin! Adding legacy tests."; + tests__desc+=( + 'legacy latest version matching regex' + 'legacy specific version' + ); + + tests__kv+=( + '0.8.8,latest:^0.8' + '0.7.13,0.7.13' + ); +else + log 'warn' "We're Darwin! Skipping legacy tests."; +fi; + tests_count=${#tests__desc[@]}; declare desc kv k v test_num; diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 3c99bfd..5529c9e 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -60,14 +60,14 @@ ln -s "${PWD}"/bin/* "${TFENV_BIN_DIR}"; cleanup || log 'error' 'Cleanup failed?!'; -log 'info' '## Installing 0.8.2'; -${TFENV_BIN_DIR}/tfenv install 0.8.2 || error_and_proceed 'Install failed'; +log 'info' '## Installing 1.6.1'; +${TFENV_BIN_DIR}/tfenv install 1.6.1 || error_and_proceed 'Install failed'; -log 'info' '## Using 0.8.2'; -${TFENV_BIN_DIR}/tfenv use 0.8.2 || error_and_proceed 'Use failed'; +log 'info' '## Using 1.6.1'; +${TFENV_BIN_DIR}/tfenv use 1.6.1 || error_and_proceed 'Use failed'; -log 'info' '## Check-Version for 0.8.2'; -check_active_version 0.8.2 || error_and_proceed 'Version check failed'; +log 'info' '## Check-Version for 1.6.1'; +check_active_version 1.6.1 || error_and_proceed 'Version check failed'; if [ "${#errors[@]}" -gt 0 ]; then log 'warn' '===== The following symlink tests failed ====='; diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index b53b2fa..b915d78 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -55,7 +55,7 @@ cleanup || log 'error' 'Cleanup failed?!'; log 'info' '### Install min-required normal version (#.#.#)'; -minv='0.8.0'; +minv='1.6.0'; echo "terraform { required_version = \">=${minv}\" @@ -72,7 +72,7 @@ cleanup || log 'error' 'Cleanup failed?!'; log 'info' '### Install min-required tagged version (#.#.#-tag#)' -minv='0.13.0-rc1' +minv='1.5.0-rc1' echo "terraform { required_version = \">=${minv}\" @@ -89,7 +89,7 @@ cleanup || log 'error' 'Cleanup failed?!'; log 'info' '### Install min-required incomplete version (#.#.)' -minv='0.12'; +minv='1.3'; echo "terraform { required_version = \">=${minv}\" @@ -106,7 +106,7 @@ cleanup || log 'error' 'Cleanup failed?!'; log 'info' '### Install min-required with TFENV_AUTO_INSTALL'; -minv='1.0.0'; +minv='1.2.0'; echo "terraform { required_version = \">=${minv}\" From c12a0ad11f280b3789bf157349f34b2ef15f4db2 Mon Sep 17 00:00:00 2001 From: Bryan Hiestand Date: Sat, 24 Jun 2023 00:54:13 -0700 Subject: [PATCH 13/30] Fix bug in regex for darwin arm64->amd64 workaround The previous regex matched `0..` instead of `0.1.2` --- libexec/tfenv-install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/tfenv-install b/libexec/tfenv-install index d13b384..4d331ae 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -124,7 +124,7 @@ case "$(uname -m)" in ;; "darwin") # No Apple Silicon builds before 1.0.2 - if [[ "${version}" =~ 0\..+$ || "${version}" =~ 1\.0\.0|1$ + if [[ "${version}" =~ ^0\.[0-9]+\.[0-9]+$ || "${version}" =~ ^1\.0\.[0-1]$ ]]; then TFENV_ARCH="${TFENV_ARCH:-amd64}"; else From 9d2bd7709dae0fb7d9f9edfd4defd73365ffe816 Mon Sep 17 00:00:00 2001 From: Bryan Hiestand Date: Sat, 24 Jun 2023 01:56:19 -0700 Subject: [PATCH 14/30] allow match for 0. versions that end with extra characters like 0.11.0-beta1 --- libexec/tfenv-install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/tfenv-install b/libexec/tfenv-install index 4d331ae..4aa7607 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -124,7 +124,7 @@ case "$(uname -m)" in ;; "darwin") # No Apple Silicon builds before 1.0.2 - if [[ "${version}" =~ ^0\.[0-9]+\.[0-9]+$ || "${version}" =~ ^1\.0\.[0-1]$ + if [[ "${version}" =~ ^0\.[0-9]+\.[0-9]+ || "${version}" =~ ^1\.0\.[0-1]$ ]]; then TFENV_ARCH="${TFENV_ARCH:-amd64}"; else From e00d30a109d426db83da1073388c88a9ca179665 Mon Sep 17 00:00:00 2001 From: Zordrak Date: Tue, 19 Dec 2023 14:57:44 +0000 Subject: [PATCH 15/30] Update test.yml --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c8c26ee..b9f94f7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -39,12 +39,12 @@ jobs: with: context: . load: true - tags: "tfenv-terraform:${{ github.head_ref }}" + tags: "tfenv-terraform:${{ github.sha }}" - name: 'Check Dockerfile' if: contains(matrix.os, 'ubuntu') run: | expect=1.2.3; - got="$(docker run -e "TFENV_TERRAFORM_VERSION=${expect}" "tfenv-terraform:${{ github.head_ref }}" version)"; + got="$(docker run -e "TFENV_TERRAFORM_VERSION=${expect}" "tfenv-terraform:${{ github.sha }}" version)"; echo "${got}" | tee /dev/stderr | grep -e 'Terraform v1.2.3' # When we push to master, test everything in order to guarantee releases From bcfaed267521ac255a6b2473e1e89ed33002d048 Mon Sep 17 00:00:00 2001 From: Jack Blower Date: Tue, 21 Feb 2023 13:02:36 +0000 Subject: [PATCH 16/30] fix: Underscores are needed for Cygwin-like tools --- libexec/tfenv-install | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/tfenv-install b/libexec/tfenv-install index 4aa7607..15ab531 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -89,10 +89,10 @@ case "$(uname -s)" in MINGW64*) kernel="windows"; ;; - MSYSNT*) + MSYS_NT*) kernel="windows"; ;; - CYGWINNT*) + CYGWIN_NT*) kernel="windows"; ;; FreeBSD*) From 521e2c523c1838b2fef95b3c659290361f9e9f4e Mon Sep 17 00:00:00 2001 From: Zordrak Date: Tue, 19 Dec 2023 14:49:40 +0000 Subject: [PATCH 17/30] Update tfenv-install Modify to allow for no underscore also --- libexec/tfenv-install | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libexec/tfenv-install b/libexec/tfenv-install index 15ab531..9b7d69a 100755 --- a/libexec/tfenv-install +++ b/libexec/tfenv-install @@ -89,10 +89,10 @@ case "$(uname -s)" in MINGW64*) kernel="windows"; ;; - MSYS_NT*) + MSYS*NT*) kernel="windows"; ;; - CYGWIN_NT*) + CYGWIN*NT*) kernel="windows"; ;; FreeBSD*) From 28de4b0a063f4dbe53709a758de2603783223e3c Mon Sep 17 00:00:00 2001 From: sato-s Date: Tue, 26 Nov 2024 21:39:55 +0900 Subject: [PATCH 18/30] Added fish way to add PATH to README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 1887e0a..c17b62f 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,10 @@ zsh: ```console $ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile ``` +fish: +```console +$ echo 'set -x PATH $HOME/.tfenv/bin $PATH' >> ~/.config/fish/config.fish +``` For WSL users: ```bash From 51cce09b373650c50de62172f47f89c501982ca1 Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Wed, 2 Jul 2025 13:27:32 +0100 Subject: [PATCH 19/30] Fix latest-allowed version matching --- libexec/tfenv-resolve-version | 2 +- test/test_use_latestallowed.sh | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/libexec/tfenv-resolve-version b/libexec/tfenv-resolve-version index 69468e5..b099643 100755 --- a/libexec/tfenv-resolve-version +++ b/libexec/tfenv-resolve-version @@ -142,7 +142,7 @@ if [[ "${version_requested}" =~ ^latest-allowed$ ]]; then ;; '~>'*) version_without_rightmost="${version_num%.*}"; - version_requested="latest:^${version_without_rightmost}"; + version_requested="latest:^${version_without_rightmost}\."; ;; *) log 'error' "Unsupported version spec: '${version_spec}', only >, >=, <=, and ~> are supported."; diff --git a/test/test_use_latestallowed.sh b/test/test_use_latestallowed.sh index 10af0d5..42f8d4f 100755 --- a/test/test_use_latestallowed.sh +++ b/test/test_use_latestallowed.sh @@ -63,7 +63,7 @@ echo "terraform { tfenv install latest-allowed; tfenv use latest-allowed; check_active_version 1.1.9; -) || error_and_proceed 'Latest allowed version does not match'; +) || error_and_proceed 'Latest allowed version does not match. Requested: "~> 1.1.0", Expected: 1.1.9'; cleanup || log 'error' 'Cleanup failed?!'; @@ -78,7 +78,7 @@ echo "terraform { tfenv install latest-allowed; tfenv use latest-allowed; check_active_version 0.13.0-rc1; -) || error_and_proceed 'Latest allowed tagged-version does not match'; +) || error_and_proceed 'Latest allowed tagged-version does not match. Requested: "<=0.13.0-rc1", Expected: 0.13.0-rc1'; cleanup || log 'error' 'Cleanup failed?!'; @@ -93,7 +93,7 @@ echo "terraform { tfenv install latest-allowed; tfenv use latest-allowed; check_active_version 0.15.5; -) || error_and_proceed 'Latest allowed incomplete-version does not match'; +) || error_and_proceed 'Latest allowed incomplete-version does not match. Requested: "~> 0.12", Expected: 0.15.5'; cleanup || log 'error' 'Cleanup failed?!'; @@ -108,7 +108,7 @@ echo 'latest-allowed' > .terraform-version; ( TFENV_AUTO_INSTALL=true terraform version; check_active_version 1.0.11; -) || error_and_proceed 'Latest allowed auto-installed version does not match'; +) || error_and_proceed 'Latest allowed auto-installed version does not match. Requested: "~> 1.0.0", Expected: 1.0.11'; cleanup || log 'error' 'Cleanup failed?!'; @@ -124,7 +124,7 @@ echo 'latest-allowed' > chdir-dir/.terraform-version ( TFENV_AUTO_INSTALL=true terraform -chdir=chdir-dir version; check_active_version 0.14.11 chdir-dir; -) || error_and_proceed 'Latest allowed version from -chdir does not match'; +) || error_and_proceed 'Latest allowed version from -chdir does not match. Requested: "~> 0.14.3", Expected: 0.14.11'; cleanup || log 'error' 'Cleanup failed?!'; From 8d1c2413afec29c0f088b63821effbc3ec674c98 Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Fri, 4 Jul 2025 11:30:30 +0100 Subject: [PATCH 20/30] Fix testing to be source path agnostic --- lib/helpers.sh | 8 ++++---- test/test_install_and_use.sh | 2 ++ test/test_list.sh | 2 ++ test/test_symlink.sh | 6 ++++-- test/test_uninstall.sh | 8 +++++--- test/test_use_latestallowed.sh | 2 ++ test/test_use_minrequired.sh | 2 ++ 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/helpers.sh b/lib/helpers.sh index 535232d..2b7c3bf 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -118,10 +118,10 @@ export -f check_default_version; function cleanup() { log 'info' 'Performing cleanup'; local pwd="$(pwd)"; - log 'debug' "Deleting ${pwd}/version"; - rm -rf ./version; - log 'debug' "Deleting ${pwd}/versions"; - rm -rf ./versions; + log 'debug' "Deleting ${TFENV_CONFIG_DIR}/version"; + rm -rf "${TFENV_CONFIG_DIR}/version"; + log 'debug' "Deleting ${TFENV_CONFIG_DIR}/versions"; + rm -rf "${TFENV_CONFIG_DIR}/versions"; log 'debug' "Deleting ${pwd}/.terraform-version"; rm -rf ./.terraform-version; log 'debug' "Deleting ${pwd}/latest_allowed.tf"; diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 879b62d..9e1428d 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -47,6 +47,8 @@ fi; # Begin Script Body # ##################### +export PATH="${TFENV_ROOT}/bin:${PATH}"; + test_install_and_use() { # Takes a static version and the optional keyword to install it with local k="${2-""}"; diff --git a/test/test_list.sh b/test/test_list.sh index bcb8d57..ea5576a 100755 --- a/test/test_list.sh +++ b/test/test_list.sh @@ -48,6 +48,8 @@ fi; # Begin Script Body # ##################### +export PATH="${TFENV_ROOT}/bin:${PATH}"; + declare -a errors=(); log 'info' '### List local versions'; diff --git a/test/test_symlink.sh b/test/test_symlink.sh index 5529c9e..c2f1ca6 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -48,6 +48,8 @@ fi; # Begin Script Body # ##################### +export PATH="${TFENV_ROOT}/bin:${PATH}"; + declare -a errors=(); log 'info' '### Testing symlink functionality'; @@ -55,8 +57,8 @@ log 'info' '### Testing symlink functionality'; TFENV_BIN_DIR='/tmp/tfenv-test'; log 'info' "## Creating/clearing ${TFENV_BIN_DIR}" rm -rf "${TFENV_BIN_DIR}" && mkdir "${TFENV_BIN_DIR}"; -log 'info' "## Symlinking ${PWD}/bin/* into ${TFENV_BIN_DIR}"; -ln -s "${PWD}"/bin/* "${TFENV_BIN_DIR}"; +log 'info' "## Symlinking ${TFENV_ROOT}/bin/* into ${TFENV_BIN_DIR}"; +ln -s "${TFENV_ROOT}"/bin/* "${TFENV_BIN_DIR}"; cleanup || log 'error' 'Cleanup failed?!'; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index 70fe803..4957e1c 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -48,6 +48,8 @@ fi; # Begin Script Body # ##################### +export PATH="${TFENV_ROOT}/bin:${PATH}"; + declare -a errors=(); function test_uninstall() { @@ -94,11 +96,11 @@ cleanup || error_and_die "Cleanup failed?!" ( tfenv install 0.12.1 || exit 1 tfenv install 0.12.2 || exit 1 - [ -d "./versions" ] || exit 1 + [ -d "${TFENV_CONFIG_DIR}/versions" ] || exit 1 tfenv uninstall 0.12.1 || exit 1 - [ -d "./versions" ] || exit 1 + [ -d "${TFENV_CONFIG_DIR}/versions" ] || exit 1 tfenv uninstall 0.12.2 || exit 1 - [ -d "./versions" ] && exit 1 || exit 0 + [ -d "${TFENV_CONFIG_DIR}/versions" ] && exit 1 || exit 0 ) || error_and_proceed "Removing last version deletes versions directory" if [ "${#errors[@]}" -gt 0 ]; then diff --git a/test/test_use_latestallowed.sh b/test/test_use_latestallowed.sh index 42f8d4f..8c88fe4 100755 --- a/test/test_use_latestallowed.sh +++ b/test/test_use_latestallowed.sh @@ -48,6 +48,8 @@ fi; # Begin Script Body # ##################### +export PATH="${TFENV_ROOT}/bin:${PATH}"; + declare -a errors=(); cleanup || log 'error' 'Cleanup failed?!'; diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index b915d78..e1650c8 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -48,6 +48,8 @@ fi; # Begin Script Body # ##################### +export PATH="${TFENV_ROOT}/bin:${PATH}"; + declare -a errors=(); cleanup || log 'error' 'Cleanup failed?!'; From 535407486d735a6380d76a06f9dd28b7086fd3a2 Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Fri, 4 Jul 2025 12:38:22 +0100 Subject: [PATCH 21/30] Reduce duplication, and add safety --- lib/helpers.sh | 7 +++++ test/run.sh | 46 ++--------------------------- test/test_common.sh | 51 ++++++++++++++++++++++++++++++++ test/test_install_and_use.sh | 46 ++--------------------------- test/test_list.sh | 47 ++---------------------------- test/test_symlink.sh | 53 ++++------------------------------ test/test_uninstall.sh | 47 ++---------------------------- test/test_use_latestallowed.sh | 47 ++---------------------------- test/test_use_minrequired.sh | 47 ++---------------------------- 9 files changed, 75 insertions(+), 316 deletions(-) create mode 100644 test/test_common.sh diff --git a/lib/helpers.sh b/lib/helpers.sh index 2b7c3bf..0f0de24 100755 --- a/lib/helpers.sh +++ b/lib/helpers.sh @@ -118,6 +118,13 @@ export -f check_default_version; function cleanup() { log 'info' 'Performing cleanup'; local pwd="$(pwd)"; + + # Safety check to ensure TFENV_CONFIG_DIR is set and not empty + if [ -z "${TFENV_CONFIG_DIR:-""}" ]; then + log 'error' 'TFENV_CONFIG_DIR is not set, cannot perform cleanup safely'; + return 1; + fi; + log 'debug' "Deleting ${TFENV_CONFIG_DIR}/version"; rm -rf "${TFENV_CONFIG_DIR}/version"; log 'debug' "Deleting ${TFENV_CONFIG_DIR}/versions"; diff --git a/test/run.sh b/test/run.sh index 0e1d362..cfec2fc 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1,54 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - errors=(); if [ "${#}" -ne 0 ]; then targets="$@"; diff --git a/test/test_common.sh b/test/test_common.sh new file mode 100644 index 0000000..2797137 --- /dev/null +++ b/test/test_common.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +# Common test setup header +# Source this file at the beginning of test scripts to set up the test environment + +set -uo pipefail; + +#################################### +# Ensure we can execute standalone # +#################################### + +function early_death() { + echo "[FATAL] ${0}: ${1}" >&2; + exit 1; +}; + +if [ -z "${TFENV_ROOT:-""}" ]; then + # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; + file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; + target_file="$(readlink "${file_name}")"; + done; + + echo "$(pwd -P)/${file_name}"; + }; + + TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; + [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; +else + TFENV_ROOT="${TFENV_ROOT%/}"; +fi; +export TFENV_ROOT; + +if [ -n "${TFENV_HELPERS:-""}" ]; then + log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; +else + [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; + if source "${TFENV_ROOT}/lib/helpers.sh"; then + log 'debug' 'Helpers sourced successfully'; + else + early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; + fi; +fi; + +# Ensure local tfenv binaries take precedence +export PATH="${TFENV_ROOT}/bin:${PATH}"; diff --git a/test/test_install_and_use.sh b/test/test_install_and_use.sh index 9e1428d..e5d13d8 100755 --- a/test/test_install_and_use.sh +++ b/test/test_install_and_use.sh @@ -1,54 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - test_install_and_use() { # Takes a static version and the optional keyword to install it with local k="${2-""}"; diff --git a/test/test_list.sh b/test/test_list.sh index ea5576a..a9a171b 100755 --- a/test/test_list.sh +++ b/test/test_list.sh @@ -1,55 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - declare -a errors=(); log 'info' '### List local versions'; diff --git a/test/test_symlink.sh b/test/test_symlink.sh index c2f1ca6..ac47eac 100755 --- a/test/test_symlink.sh +++ b/test/test_symlink.sh @@ -1,62 +1,19 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - declare -a errors=(); log 'info' '### Testing symlink functionality'; -TFENV_BIN_DIR='/tmp/tfenv-test'; -log 'info' "## Creating/clearing ${TFENV_BIN_DIR}" -rm -rf "${TFENV_BIN_DIR}" && mkdir "${TFENV_BIN_DIR}"; +TFENV_BIN_DIR="$(mktemp -d)"; +log 'info' "## Using temporary directory ${TFENV_BIN_DIR}"; +trap 'rm -rf "${TFENV_BIN_DIR}"' EXIT; log 'info' "## Symlinking ${TFENV_ROOT}/bin/* into ${TFENV_BIN_DIR}"; ln -s "${TFENV_ROOT}"/bin/* "${TFENV_BIN_DIR}"; diff --git a/test/test_uninstall.sh b/test/test_uninstall.sh index 4957e1c..f75e4ae 100755 --- a/test/test_uninstall.sh +++ b/test/test_uninstall.sh @@ -1,55 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - declare -a errors=(); function test_uninstall() { diff --git a/test/test_use_latestallowed.sh b/test/test_use_latestallowed.sh index 8c88fe4..bc4a07d 100755 --- a/test/test_use_latestallowed.sh +++ b/test/test_use_latestallowed.sh @@ -1,55 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - declare -a errors=(); cleanup || log 'error' 'Cleanup failed?!'; diff --git a/test/test_use_minrequired.sh b/test/test_use_minrequired.sh index e1650c8..8e03a3e 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -1,55 +1,12 @@ #!/usr/bin/env bash -set -uo pipefail; - -#################################### -# Ensure we can execute standalone # -#################################### - -function early_death() { - echo "[FATAL] ${0}: ${1}" >&2; - exit 1; -}; - -if [ -z "${TFENV_ROOT:-""}" ]; then - # 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 \$(dirname ${target_file})' while trying to determine TFENV_ROOT"; - file_name="$(basename "${target_file}")" || early_death "Failed to 'basename \"${target_file}\"' while trying to determine TFENV_ROOT"; - target_file="$(readlink "${file_name}")"; - done; - - echo "$(pwd -P)/${file_name}"; - }; - - TFENV_ROOT="$(cd "$(dirname "$(readlink_f "${0}")")/.." && pwd)"; - [ -n ${TFENV_ROOT} ] || early_death "Failed to 'cd \"\$(dirname \"\$(readlink_f \"${0}\")\")/..\" && pwd' while trying to determine TFENV_ROOT"; -else - TFENV_ROOT="${TFENV_ROOT%/}"; -fi; -export TFENV_ROOT; - -if [ -n "${TFENV_HELPERS:-""}" ]; then - log 'debug' 'TFENV_HELPERS is set, not sourcing helpers again'; -else - [ "${TFENV_DEBUG:-0}" -gt 0 ] && echo "[DEBUG] Sourcing helpers from ${TFENV_ROOT}/lib/helpers.sh"; - if source "${TFENV_ROOT}/lib/helpers.sh"; then - log 'debug' 'Helpers sourced successfully'; - else - early_death "Failed to source helpers from ${TFENV_ROOT}/lib/helpers.sh"; - fi; -fi; +# Source common test setup +source "$(dirname "${0}")/test_common.sh"; ##################### # Begin Script Body # ##################### -export PATH="${TFENV_ROOT}/bin:${PATH}"; - declare -a errors=(); cleanup || log 'error' 'Cleanup failed?!'; From 08d328e4ef27779c6e26f58ddfc45fc28ce4f3f8 Mon Sep 17 00:00:00 2001 From: Adam Christie Date: Thu, 11 Jul 2024 11:05:36 +0100 Subject: [PATCH 22/30] Cope with different line endings in .terraform-version Use `tr` to strip carriage returns from this file as it can cause failures in repositories with CRLF. (remove log message changes) --- lib/tfenv-version-name.sh | 2 +- libexec/tfenv-resolve-version | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tfenv-version-name.sh b/lib/tfenv-version-name.sh index 3975769..c457875 100644 --- a/lib/tfenv-version-name.sh +++ b/lib/tfenv-version-name.sh @@ -10,7 +10,7 @@ function tfenv-version-name() { && log 'debug' "TFENV_VERSION_FILE retrieved from tfenv-version-file: ${TFENV_VERSION_FILE}" \ || log 'error' 'Failed to retrieve TFENV_VERSION_FILE from tfenv-version-file'; - TFENV_VERSION="$(cat "${TFENV_VERSION_FILE}" || true)" \ + TFENV_VERSION="$(cat "${TFENV_VERSION_FILE}" || true | tr -d '\r')" \ && log 'debug' "TFENV_VERSION specified in TFENV_VERSION_FILE: ${TFENV_VERSION}"; TFENV_VERSION_SOURCE="${TFENV_VERSION_FILE}"; diff --git a/libexec/tfenv-resolve-version b/libexec/tfenv-resolve-version index b099643..46d0704 100755 --- a/libexec/tfenv-resolve-version +++ b/libexec/tfenv-resolve-version @@ -77,12 +77,12 @@ if [ -z "${arg}" -a -z "${TFENV_TERRAFORM_VERSION:-""}" ]; then if [ "${version_file}" != "${TFENV_CONFIG_DIR}/version" ]; then log 'debug' "Version File (${version_file}) is not the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)"; - version_requested="$(cat "${version_file}")" \ + version_requested="$(cat "${version_file}" | tr -d '\r')" \ || log 'error' "Failed to open ${version_file}"; elif [ -f "${version_file}" ]; then log 'debug' "Version File is the default \${TFENV_CONFIG_DIR}/version (${TFENV_CONFIG_DIR}/version)"; - version_requested="$(cat "${version_file}")" \ + version_requested="$(cat "${version_file}" | tr -d '\r')" \ || log 'error' "Failed to open ${version_file}"; # Absolute fallback From 01ef43cca653bbfc5c0f3861d61078c78fc8e831 Mon Sep 17 00:00:00 2001 From: TAKANO Mitsuhiro Date: Mon, 19 Feb 2024 18:53:43 +0900 Subject: [PATCH 23/30] GitHub Actions: fix `matrix.os` strategy see also. https://github.com/actions/runner-images --- .github/workflows/test.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b9f94f7..3b7867d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,10 +54,13 @@ jobs: strategy: matrix: os: - - 'macos-11' - - 'macos-10.15' + - 'ubuntu-22.04' - 'ubuntu-20.04' - - 'ubuntu-18.04' + - 'macos-14' + - 'macos-13' + - 'macos-12' + - 'macos-11' + - 'windows-2022' - 'windows-2019' steps: - uses: 'actions/checkout@v4' From 37bac16003eba2f8bd3e65ec1e9d84a4e2a3399a Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Fri, 4 Jul 2025 14:00:21 +0100 Subject: [PATCH 24/30] fix README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c17b62f..f9049b0 100644 --- a/README.md +++ b/README.md @@ -54,13 +54,15 @@ bash: ```console echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile ``` + zsh: ```console $ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile ``` + fish: ```console -$ echo 'set -x PATH $HOME/.tfenv/bin $PATH' >> ~/.config/fish/config.fish +echo 'set -x PATH $HOME/.tfenv/bin $PATH' >> ~/.config/fish/config.fish ``` For WSL users: From 79a3b2fac9dc974d4abac7fc104e75a584dad327 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Fri, 16 Feb 2024 00:04:32 +0900 Subject: [PATCH 25/30] docs: fix README to enable copy and use as is --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f9049b0..6b40a1f 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile zsh: ```console -$ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile +echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.zprofile ``` fish: From 461d5c7bd74d17aed4bc9e742c3a9622b3be1a25 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 4 Jul 2025 13:04:35 +0000 Subject: [PATCH 26/30] Bump docker/build-push-action from 5 to 6 Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 5 to 6. - [Release notes](https://github.com/docker/build-push-action/releases) - [Commits](https://github.com/docker/build-push-action/compare/v5...v6) --- updated-dependencies: - dependency-name: docker/build-push-action dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3b7867d..ec54776 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,7 +34,7 @@ jobs: if: contains(matrix.os, 'ubuntu') with: install: true - - uses: 'docker/build-push-action@v5' + - uses: 'docker/build-push-action@v6' if: contains(matrix.os, 'ubuntu') with: context: . @@ -76,7 +76,7 @@ jobs: if: contains(matrix.os, 'ubuntu') with: install: true - - uses: 'docker/build-push-action@v5' + - uses: 'docker/build-push-action@v6' if: contains(matrix.os, 'ubuntu') with: context: . From b80e3cea00fd92091e5137f421a405e4e0ee3873 Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 27 Jul 2022 19:22:26 +0100 Subject: [PATCH 27/30] 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 8e03a3e..de25949 100755 --- a/test/test_use_minrequired.sh +++ b/test/test_use_minrequired.sh @@ -80,7 +80,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'; @@ -97,6 +97,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 12be2ac6096f7716a4e93cec9f7753189eb9bef2 Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 27 Jul 2022 19:35:04 +0100 Subject: [PATCH 28/30] 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 9acfdf84447c68fcbb893b7ee8670846bf9b833c Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Fri, 4 Nov 2022 22:05:45 +0000 Subject: [PATCH 29/30] 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; From c8eb402135bf6fbcd2809f6cdbf7ea1113de6e54 Mon Sep 17 00:00:00 2001 From: Mike Peachey Date: Fri, 11 Jul 2025 13:42:52 +0100 Subject: [PATCH 30/30] Update test os matrix --- .github/workflows/test.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ec54776..4b285fb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -54,14 +54,12 @@ jobs: strategy: matrix: os: + - 'ubuntu-24.04' - 'ubuntu-22.04' - - 'ubuntu-20.04' - 'macos-14' - 'macos-13' - - 'macos-12' - - 'macos-11' + - 'windows-2025' - 'windows-2022' - - 'windows-2019' steps: - uses: 'actions/checkout@v4' with: