Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libexec/goenv-version-name
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ version_exists() {
IFS=$'\n'
local version_regex=$(echo ${input_version} | sed s/\\./\\\\./)

# check if input_version is major/minor/patch instead of just major/minor
# check if input_version is major.minor only (not major.minor.patch) to add optional patch pattern
if ! echo "${input_version}" | grep -q -E "^[0-9]+\.[0-9]+\.[0-9]+(\s*)$"; then
version_regex="${version_regex}(\\.[0-9]+)?"
fi
Expand Down Expand Up @@ -61,6 +61,9 @@ OLDIFS="$IFS"
if [[ -z $GOENV_GOMOD_VERSION_ENABLE ]] || ! version_exists "$version" "$GOENV_GOMOD_VERSION_ENABLE"; then
echo "goenv: version '$version' is not installed (set by $(goenv-version-origin))" >&2
any_not_installed=1
else
# version_exists with GOENV_GOMOD_VERSION_ENABLE succeeded and resolved the version
versions=("${versions[@]}" "${version}")
fi
fi
done
Expand Down
64 changes: 44 additions & 20 deletions libexec/goenv-which
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,49 @@ OLDIFS="$IFS"
IFS=: versions=(${GOENV_VERSION:-$(goenv-version-name)})
IFS="$OLDIFS"

version_exists() {
local input_version="$1"
local use_go_mod="$2"

if [[ -n $use_go_mod ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+\.?[0-9]*(\s*)$" <<<${input_version}; then
OLDIFS="$IFS"
IFS=$'\n'
local version_regex=$(echo ${input_version} | sed s/\\./\\\\./)

# check if input_version is major.minor only (not major.minor.patch) to add optional patch pattern
if ! echo "${input_version}" | grep -q -E "^[0-9]+\.[0-9]+\.[0-9]+(\s*)$"; then
version_regex="${version_regex}(\\.[0-9]+)?"
fi

local versions=($(ls ${GOENV_ROOT}/versions | grep -E "^$version_regex$" | sort -V))
IFS="$OLDIFS"
if [[ ${#versions[@]} -eq 0 ]]; then
return 1
fi
version=${versions[${#versions[@]} - 1]}
fi

[ -d "${GOENV_ROOT}/versions/${version}" ]
}

# Resolve major.minor versions to full versions when GOENV_GOMOD_VERSION_ENABLE is set
if [[ -n "$GOENV_GOMOD_VERSION_ENABLE" ]]; then
resolved_versions=()
for ver in "${versions[@]}"; do
if [ "$ver" != "system" ]; then
version="$ver"
if version_exists "$version" "$GOENV_GOMOD_VERSION_ENABLE"; then
resolved_versions=("${resolved_versions[@]}" "${version}")
else
resolved_versions=("${resolved_versions[@]}" "$ver")
fi
else
resolved_versions=("${resolved_versions[@]}" "$ver")
fi
done
versions=("${resolved_versions[@]}")
fi

remove_from_path() {
local path_to_remove="$1"
local path_before
Expand Down Expand Up @@ -59,25 +102,6 @@ for version in "${versions[@]}"; do
fi
done

version_exists() {
local input_version="$1"
local use_go_mod="$2"

if [[ -n $use_go_mod ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+(\s*)$" <<<${input_version}; then
OLDIFS="$IFS"
IFS=$'\n'
local version_regex=$(echo ${input_version} | sed s/\\./\\\\./)
local versions=($(ls ${GOENV_ROOT}/versions | grep -E "^$version_regex(\\.[0-9]+)?$" | sort -V))
IFS="$OLDIFS"
if [[ ${#versions[@]} -eq 0 ]]; then
return 1
fi
version=${versions[${#versions[@]} - 1]}
fi

[ -d "${GOENV_ROOT}/versions/${version}" ]
}

OLDIFS="$IFS"
IFS=$'\n'
scripts=($(goenv-hooks which))
Expand All @@ -96,7 +120,7 @@ for version in "${versions[@]}"; do
if [ "$version" = "system" ]; then
continue
fi
if ! version_exists "$version" "$GOENV_GO_MOD_ENABLE"; then
if ! version_exists "$version" "$GOENV_GOMOD_VERSION_ENABLE"; then
echo "goenv: version '$version' is not installed (set by $(goenv-version-origin))" >&2
any_not_installed=1
fi
Expand Down
33 changes: 33 additions & 0 deletions test/goenv-exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,36 @@ SH
LC_ALL is unset
OUT
}

@test "resolves major.minor version from go.mod and executes command when GOENV_GOMOD_VERSION_ENABLE=1" {
create_executable "1.20.14" "go" <<SH
#!/bin/sh
echo "go version 1.20.14"
SH

create_executable "1.25.3" "go" <<SH
#!/bin/sh
echo "go version 1.25.3"
SH

create_executable "1.25.4" "go" <<SH
#!/bin/sh
echo "go version 1.25.4"
SH

mkdir -p "$GOENV_TEST_DIR"
cd "$GOENV_TEST_DIR"

cat > go.mod <<EOF
module testmodule

go 1.25
EOF

export GOENV_GOMOD_VERSION_ENABLE=1
run goenv-exec go

assert_success_out <<OUT
go version 1.25.4
OUT
}
28 changes: 28 additions & 0 deletions test/goenv-version-name.bats
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,31 @@ OUT

assert_success "1.10.3"
}

@test "resolves major.minor version from go.mod to latest patch version when GOENV_GOMOD_VERSION_ENABLE=1" {
create_version "1.20.14"
create_version "1.25.3"
create_version "1.25.4"

cat > go.mod <<EOF
module testmodule

go 1.25
EOF

export GOENV_GOMOD_VERSION_ENABLE=1
run goenv-version-name

assert_success "1.25.4"
}

@test "resolves major.minor version to latest patch version when GOENV_GOMOD_VERSION_ENABLE=1 and version specified by GOENV_VERSION" {
create_version "1.20.14"
create_version "1.25.3"
create_version "1.25.4"

export GOENV_GOMOD_VERSION_ENABLE=1
GOENV_VERSION=1.25 run goenv-version-name

assert_success "1.25.4"
}
36 changes: 36 additions & 0 deletions test/goenv-which.bats
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,39 @@ SH
assert_failure "goenv: 'kill-all-humans' command not found"
}

@test "resolves major.minor version to latest patch and finds executable when GOENV_GOMOD_VERSION_ENABLE=1" {
create_executable "1.20.14" "go"
create_executable "1.25.3" "go"
create_executable "1.25.4" "go"

export GOENV_GOMOD_VERSION_ENABLE=1
GOENV_VERSION=1.25 run goenv-which go
assert_success "${GOENV_ROOT}/versions/1.25.4/bin/go"
}

@test "resolves major.minor version from go.mod and finds executable when GOENV_GOMOD_VERSION_ENABLE=1" {
create_executable "1.20.14" "go"
create_executable "1.25.3" "go"
create_executable "1.25.4" "go"

cat > "${GOENV_TEST_DIR}/go.mod" <<EOF
module testmodule

go 1.25
EOF

export GOENV_GOMOD_VERSION_ENABLE=1
cd "${GOENV_TEST_DIR}"
run goenv-which go
assert_success "${GOENV_ROOT}/versions/1.25.4/bin/go"
}

@test "does not re-resolve full major.minor.patch version when GOENV_GOMOD_VERSION_ENABLE=1" {
create_executable "1.25.3" "go"
create_executable "1.25.4" "go"

export GOENV_GOMOD_VERSION_ENABLE=1
GOENV_VERSION=1.25.3 run goenv-which go
assert_success "${GOENV_ROOT}/versions/1.25.3/bin/go"
}

Loading