Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix Issues From Previous Merges #419

Merged
merged 4 commits into from
Dec 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## PRs will automatically tag the following users for Review

* @syndbg @ChronosMasterOfAllTime
* @syndbg @ChronosMasterOfAllTime @reneleonhardt
41 changes: 20 additions & 21 deletions libexec/goenv-init
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ fi

print=""
no_rehash=""
for args in "$@"
do
for args in "$@"; do
if [ "$args" = "-" ]; then
print=1
shift
Expand All @@ -44,23 +43,23 @@ root="${0%/*}/.."

if [ -z "$print" ]; then
case "$shell" in
bash )
bash)
if [ -f "${HOME}/.bashrc" ] && [ ! -f "${HOME}/.bash_profile" ]; then
profile='~/.bashrc'
else
profile='~/.bash_profile'
fi
;;
zsh )
zsh)
profile='~/.zshrc'
;;
ksh )
ksh)
profile='~/.profile'
;;
fish )
fish)
profile='~/.config/fish/config.fish'
;;
* )
*)
profile="<unknown shell: $shell, replace with your profile path>"
;;
esac
Expand All @@ -70,10 +69,10 @@ if [ -z "$print" ]; then
echo "# the following to ${profile}:"
echo
case "$shell" in
fish )
fish)
echo 'status --is-interactive; and source (goenv init -|psub)'
;;
* )
*)
echo 'eval "$(goenv init -)"'
;;
esac
Expand All @@ -86,7 +85,7 @@ fi
mkdir -p "${GOENV_ROOT}/"{shims,versions}

case "$shell" in
fish )
fish)
cat <<EOL
set -gx GOENV_SHELL ${shell}
set -gx GOENV_ROOT ${GOENV_ROOT}
Expand All @@ -105,15 +104,15 @@ if not contains \$GOENV_ROOT/shims \$PATH
end
EOL
;;
* )
*)
cat <<EOL
export GOENV_SHELL=${shell}
export GOENV_ROOT=${GOENV_ROOT}
if [ -z \${GOENV_RC_FILE} ]; then
GOENV_RC_FILE=\${HOME}/.goenvrc
if [ -z "\${GOENV_RC_FILE}" ]; then
GOENV_RC_FILE="\${HOME}/.goenvrc"
fi
if [ -e \${GOENV_RC_FILE} ]; then
source \${GOENV_RC_FILE}
if [ -e "\${GOENV_RC_FILE}" ]; then
source "\${GOENV_RC_FILE}"
fi
if [ "\${PATH#*\$GOENV_ROOT/shims}" = "\${PATH}" ]; then
if [ "\${GOENV_PATH_ORDER}" = "front" ] ; then
Expand All @@ -135,9 +134,9 @@ if [ -z "$no_rehash" ]; then
echo 'command goenv rehash 2>/dev/null'
fi

commands=(`goenv-commands --sh`)
commands=($(goenv-commands --sh))
case "$shell" in
fish )
fish)
cat <<EOS
function goenv
set command \$argv[1]
Expand All @@ -152,13 +151,13 @@ function goenv
end
EOS
;;
ksh )
ksh)
cat <<EOS
function goenv {
typeset command
EOS
;;
* )
*)
cat <<EOS
goenv() {
local command
Expand All @@ -167,8 +166,8 @@ EOS
esac

if [ "$shell" != "fish" ]; then
IFS="|"
cat <<EOS
IFS="|"
cat <<EOS
command="\$1"
if [ "\$#" -gt 0 ]; then
shift
Expand Down
5 changes: 2 additions & 3 deletions libexec/goenv-installed
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# If no version can be found or no versions are installed, an error message will be displayed.
# Run `goenv versions` for a list of available Go versions.


set -e
[ -n "$GOENV_DEBUG" ] && set -x

Expand All @@ -26,6 +25,7 @@ majors=({1,}) # Supported Go versions: 1 (latest first)

versions() {
# Sort correctly (1.20.9 comes before 1.20.10)
local query="$1"
goenv versions --bare | sort -V | $(type -p ggrep grep | head -1) -F "$query" || true
}

Expand All @@ -42,7 +42,7 @@ latest_minor() {
[[ "$1" =~ ^([0-9]+\.[0-9]+rc)[0-9]+ ]] && minor="${BASH_REMATCH[1]}" || minor="$1"
# 1.23rc1 -> latest release candidate 1.23rc2
# 1.23 -> latest minor 1.23.4
versions | grep -oE "^$(regex "$minor")\\.([0-9]+)$" | tail -1
versions | grep -oE "^$(regex "$minor")\\.?([0-9]+)?$" | tail -1
}

installed() {
Expand All @@ -54,7 +54,6 @@ regex() {
echo "${1//\./\\.}"
}


if [ -n "$1" ]; then
version="$1"
else
Expand Down
1 change: 1 addition & 0 deletions libexec/goenv-prefix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fi

versions() {
# Sort correctly (1.20.9 comes before 1.20.10)
local query="$1"
goenv versions --bare | sort -V | $(type -p ggrep grep | head -1) -F "$query" || true
}

Expand Down
20 changes: 16 additions & 4 deletions libexec/goenv-version-file-write
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,20 @@ OLDIFS="$IFS"
IFS=:
for version in ${GOENV_VERSION}; do
if ! INSTALLED="$(goenv-installed "$version" 2>&1)"; then
echo "$INSTALLED" >&2
exit 1
if [[ $version != "system" ]]; then
set +e
goenv install -q -f $version &>/dev/null
set -e
if [[ $? -ne 0 ]]; then
echo "$INSTALLED" >&2
exit 1
fi
fi
# check again
if ! INSTALLED="$(goenv-installed "$version" 2>&1)"; then
echo "$INSTALLED" >&2
exit 1
fi
fi
GOENV_VERSIONS=("${GOENV_VERSIONS[@]}" "$INSTALLED")
done
Expand All @@ -62,8 +74,8 @@ if [ "$1" = "system" ]; then
else
# Write the version out to disk.
# Create an empty file. Using "rm" might cause a permission error.
> "$GOENV_VERSION_FILE"
>"$GOENV_VERSION_FILE"
for version in "${GOENV_VERSIONS[@]}"; do
echo "$version" >> "$GOENV_VERSION_FILE"
echo "$version" >>"$GOENV_VERSION_FILE"
done
fi
10 changes: 8 additions & 2 deletions libexec/goenv-version-name
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ 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
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/\\./\\\\./)
local versions=($(/bin/ls ${GOENV_ROOT}/versions | grep -E "^$version_regex(\\.[0-9]+)?$" | sort -V))

# check if input_version is major/minor/patch instead of just major/minor
if ! echo "${input_version}" | grep -q -E "^[0-9]+\.[0-9]+\.[0-9]+(\s*)$"; then
version_regex="${version_regex}(\\.[0-9]+)?"
fi

local versions=($(/bin/ls ${GOENV_ROOT}/versions | grep -E "^$version_regex$" | sort -V))
IFS="$OLDIFS"
if [[ ${#versions[@]} -eq 0 ]]; then
return 1
Expand Down
13 changes: 13 additions & 0 deletions test/goenv-global.bats
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ OUT
assert_success ""
run goenv-global
assert_success "1.2.3"

mkdir -p "$GOENV_ROOT/versions/1.20rc2"
run goenv-global 1.20rc1
assert_success ""
run goenv-global
assert_success "1.20rc2"
}

@test "sets properly sorted latest global version when 'latest' version is given and any version is installed" {
Expand All @@ -81,10 +87,12 @@ OUT
}

@test "fails setting latest global version when major or minor single number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
export USE_FAKE_DEFINITIONS=true
mkdir -p "${GOENV_ROOT}/versions/1.2.9"
mkdir -p "${GOENV_ROOT}/versions/4.5.10"
run goenv-global 9
assert_failure "goenv: version '9' not installed"
unset USE_FAKE_DEFINITIONS
}

@test "sets latest global version when minor version is given as single number and any matching major.minor version is installed" {
Expand All @@ -111,12 +119,15 @@ OUT
}

@test "fails setting latest global version when major.minor number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
export USE_FAKE_DEFINITIONS=true
mkdir -p "${GOENV_ROOT}/versions/1.1.9"
run goenv-global 1.9
assert_failure "goenv: version '1.9' not installed"
unset USE_FAKE_DEFINITIONS
}

@test "fails writing specified version to GOENV_ROOT/version if version is not installed" {
export USE_FAKE_DEFINITIONS=true
mkdir -p "${GOENV_ROOT}/versions/4.5.6"
run goenv-global system
assert_failure "goenv: system version not found in PATH"
Expand All @@ -132,6 +143,7 @@ OUT
}

@test "reads version from GOENV_ROOT/{version,global,default} in the order they're specified" {
export USE_FAKE_DEFINITIONS=true
mkdir -p "$GOENV_ROOT"
echo "1.2.3" > "$GOENV_ROOT/version"
echo "1.2.4" > "$GOENV_ROOT/global"
Expand All @@ -147,4 +159,5 @@ OUT
rm "$GOENV_ROOT/global"
run goenv-global
assert_success "1.2.5"
unset USE_FAKE_DEFINITIONS
}
32 changes: 16 additions & 16 deletions test/goenv-init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ OUT

assert_line 0 'export GOENV_SHELL=bash'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z ${GOENV_RC_FILE} ]; then'
assert_line 3 ' GOENV_RC_FILE=${HOME}/.goenvrc'
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e ${GOENV_RC_FILE} ]; then'
assert_line 6 ' source ${GOENV_RC_FILE}'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
Expand Down Expand Up @@ -173,11 +173,11 @@ OUT

assert_line 0 'export GOENV_SHELL=zsh'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z ${GOENV_RC_FILE} ]; then'
assert_line 3 ' GOENV_RC_FILE=${HOME}/.goenvrc'
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e ${GOENV_RC_FILE} ]; then'
assert_line 6 ' source ${GOENV_RC_FILE}'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
Expand Down Expand Up @@ -244,11 +244,11 @@ OUT

assert_line 0 'export GOENV_SHELL=ksh'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z ${GOENV_RC_FILE} ]; then'
assert_line 3 ' GOENV_RC_FILE=${HOME}/.goenvrc'
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e ${GOENV_RC_FILE} ]; then'
assert_line 6 ' source ${GOENV_RC_FILE}'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
Expand Down Expand Up @@ -281,11 +281,11 @@ OUT
# NOTE: This is very likely to be invalid for your specific shell
assert_line 0 'export GOENV_SHELL=magicshell'
assert_line 1 "export GOENV_ROOT=$GOENV_ROOT"
assert_line 2 'if [ -z ${GOENV_RC_FILE} ]; then'
assert_line 3 ' GOENV_RC_FILE=${HOME}/.goenvrc'
assert_line 2 'if [ -z "${GOENV_RC_FILE}" ]; then'
assert_line 3 ' GOENV_RC_FILE="${HOME}/.goenvrc"'
assert_line 4 'fi'
assert_line 5 'if [ -e ${GOENV_RC_FILE} ]; then'
assert_line 6 ' source ${GOENV_RC_FILE}'
assert_line 5 'if [ -e "${GOENV_RC_FILE}" ]; then'
assert_line 6 ' source "${GOENV_RC_FILE}"'
assert_line 7 'fi'
assert_line 8 'if [ "${PATH#*$GOENV_ROOT/shims}" = "${PATH}" ]; then'
assert_line 9 ' if [ "${GOENV_PATH_ORDER}" = "front" ] ; then'
Expand Down
2 changes: 2 additions & 0 deletions test/goenv-local.bats
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ OUT
}

@test "goenv local fails setting latest version when major.minor number is given and does not match at 'GOENV_ROOT/versions/<version>'" {
export USE_FAKE_DEFINITIONS=true
mkdir -p "${GOENV_ROOT}/versions/1.1.9"
run goenv-local 1.9
assert_failure "goenv: version '1.9' not installed"
unset USE_FAKE_DEFINITIONS
}

@test "fails setting local version when version argument is given and does not match at 'GOENV_ROOT/versions/<version>'" {
Expand Down
10 changes: 5 additions & 5 deletions test/goenv-version-file-write.bats
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ OUT
assert_failure "Usage: goenv version-file-write <file> <version>..."
}

@test "fails when 2 arguments are specified, but version is non-existent" {
@test "succeeds when 2 arguments are specified, but version is non-existent" {
assert [ ! -e ".go-version" ]

export USE_FAKE_DEFINITIONS=true
run goenv-version-file-write ".go-version" "1.11.1"
assert_failure "goenv: version '1.11.1' not installed"

unset USE_FAKE_DEFINITIONS
assert [ ! -e ".go-version" ]
}

Expand Down Expand Up @@ -57,7 +59,7 @@ OUT
touch "${GOENV_TEST_DIR}/bin/go"
chmod +x "${GOENV_TEST_DIR}/bin/go"
# Make test harder by referencing not installed version
echo "4.5.6" > .go-version
echo "4.5.6" >.go-version

run goenv-local system
assert_success "goenv: using system version instead of 4.5.6 now"
Expand All @@ -66,7 +68,6 @@ OUT
assert_failure "goenv: no local version configured for this directory"
}


@test "remove global version when 'system' version is given and any global version is installed" {
mkdir -p "${GOENV_ROOT}/versions/1.2.3"
run goenv-global latest
Expand All @@ -82,12 +83,11 @@ OUT
touch "${GOENV_TEST_DIR}/bin/go"
chmod +x "${GOENV_TEST_DIR}/bin/go"
# Make test harder by referencing not installed version
echo "4.5.6" > ${GOENV_ROOT}/version
echo "4.5.6" >${GOENV_ROOT}/version

run goenv-global system
assert_success "goenv: using system version instead of 4.5.6 now"
assert [ ! -f "${GOENV_ROOT}/version" ]
run goenv-global
assert_success "system"
}

Loading
Loading