From f4c6311b085223ac7822a9250b5f7263d08a6097 Mon Sep 17 00:00:00 2001 From: Luke Bakken Date: Sat, 29 Mar 2025 08:06:43 -0700 Subject: [PATCH] chore: Export `BASE_DIR` environment variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New users to `asdf` who wish to contribute will run into the following errors when running `./scripts/test.bash`: ``` ✗ help should show dummy plugin help specific to version when version is present [7] (from function `setup_asdf_dir' in file test/test_helpers.bash, line 16, from function `setup' in test file test/help_command.bats, line 6) `setup_asdf_dir' failed mkdir: cannot create directory ‘/w spacetest_help_should_show_dummy_plugin_help_specific_to_version_when_version_is_present’: Permission denied ``` This is due to paths within the various test suites relying on the `BASE_DIR` environment variable, which is not set by default. This pull request modifies `scripts/test.bash` to correctly export `BASE_DIR` Note: I am a long-time asdf user, who would like to contribute to the project and noticed this while setting up my dev environment on an up-to-date Arch Linux system. My intention is to work on this issue: https://github.com/asdf-vm/asdf/issues/2042 Add `PATH` to variables exported to `bats`, because the use of `Env` requires providing *all* necessary env vars. Otherwise, this is the result of running `make test`: $ make test go test -coverprofile=/tmp/coverage.out -bench= -race ./... --- FAIL: TestBatsTests (0.48s) --- FAIL: TestBatsTests/current_command (0.01s) main_test.go:23: stdout: main_test.go:23: stderr: error loading config: exec: "getent": executable file not found in $PATH --- cmd/asdf/main_test.go | 3 ++- scripts/test.bash | 28 +++++++++++++++------------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cmd/asdf/main_test.go b/cmd/asdf/main_test.go index 38c39c7da..834ea5d35 100644 --- a/cmd/asdf/main_test.go +++ b/cmd/asdf/main_test.go @@ -126,7 +126,8 @@ func runBatsFile(t *testing.T, dir, filename string) { // Add dir to asdf test variables asdfTestHome := fmt.Sprintf("BASE_DIR=%s", dir) asdfBinPath := fmt.Sprintf("ASDF_BIN=%s", dir) - cmd.Env = []string{asdfBinPath, asdfTestHome} + path := "PATH=/bin:/usr/bin:/usr/local/bin" + cmd.Env = []string{asdfBinPath, asdfTestHome, path} err := cmd.Run() if err != nil { diff --git a/scripts/test.bash b/scripts/test.bash index 67c605f3a..f2eed57c7 100755 --- a/scripts/test.bash +++ b/scripts/test.bash @@ -1,7 +1,6 @@ #!/usr/bin/env bash set -euo pipefail -IFS=$'\n\t' print.info() { printf '[INFO] %s\n' "$1" @@ -11,18 +10,21 @@ print.error() { printf '[ERROR] %s\n' "$1" >&2 } -{ - repo_dir=$(git rev-parse --show-toplevel) - current_dir=$(pwd -P) - if [ "$repo_dir" != "$current_dir" ]; then - print.error "This scripts requires execution from the repository root directory." - printf "\t%s\t%s\n" "Repo root dir:" "$repo_dir" - printf "\t%s\t%s\n\n" "Current dir:" "$current_dir" - exit 1 - fi -} +BASE_DIR="$(git rev-parse --show-toplevel)" +readonly BASE_DIR +export BASE_DIR + +current_dir="$(pwd -P)" +readonly current_dir + +if [[ $BASE_DIR != "$current_dir" ]]; then + print.error "This script requires execution from the repository root directory." + printf "\t%s\t%s\n" "asdf repo root dir:" "$BASE_DIR" >&2 + printf "\t%s\t%s\n\n" "current dir:" "$current_dir" >&2 + exit 1 +fi -test_directory="./test" +readonly test_directory="$BASE_DIR/test" bats_options=(--timing --print-output-on-failure) if command -v parallel >/dev/null; then @@ -35,5 +37,5 @@ else print.info "For faster test execution, install GNU parallel." fi -print.info "Running Bats in directory '${test_directory}' with options:" "${bats_options[@]}" +print.info "Running Bats in directory '${test_directory}' with options: '${bats_options[*]}'" bats "${bats_options[@]}" "${test_directory}"