diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index a70cffda..0a7cdda7 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,3 +1,3 @@ ## PRs will automatically tag the following users for Review -* @syndbg @ChronosMasterOfAllTime +* @syndbg @ChronosMasterOfAllTime @reneleonhardt diff --git a/libexec/goenv-init b/libexec/goenv-init index 391af31d..3e4d0912 100755 --- a/libexec/goenv-init +++ b/libexec/goenv-init @@ -18,8 +18,7 @@ fi print="" no_rehash="" -for args in "$@" -do +for args in "$@"; do if [ "$args" = "-" ]; then print=1 shift @@ -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="" ;; esac @@ -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 @@ -86,7 +85,7 @@ fi mkdir -p "${GOENV_ROOT}/"{shims,versions} case "$shell" in -fish ) +fish) cat </dev/null' fi -commands=(`goenv-commands --sh`) +commands=($(goenv-commands --sh)) case "$shell" in -fish ) +fish) cat < 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() { @@ -54,7 +54,6 @@ regex() { echo "${1//\./\\.}" } - if [ -n "$1" ]; then version="$1" else diff --git a/libexec/goenv-prefix b/libexec/goenv-prefix index 9db5ba2b..476013f6 100755 --- a/libexec/goenv-prefix +++ b/libexec/goenv-prefix @@ -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 } diff --git a/libexec/goenv-version-file-write b/libexec/goenv-version-file-write index 00d16f91..22a2b02b 100755 --- a/libexec/goenv-version-file-write +++ b/libexec/goenv-version-file-write @@ -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 @@ -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 diff --git a/libexec/goenv-version-name b/libexec/goenv-version-name index fb5cf516..f930371b 100755 --- a/libexec/goenv-version-name +++ b/libexec/goenv-version-name @@ -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 diff --git a/test/goenv-global.bats b/test/goenv-global.bats index 7dd589fc..e4825b0c 100644 --- a/test/goenv-global.bats +++ b/test/goenv-global.bats @@ -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" { @@ -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/'" { + 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" { @@ -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/'" { + 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" @@ -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" @@ -147,4 +159,5 @@ OUT rm "$GOENV_ROOT/global" run goenv-global assert_success "1.2.5" + unset USE_FAKE_DEFINITIONS } diff --git a/test/goenv-init.bats b/test/goenv-init.bats index ef5a9667..5b52d59f 100644 --- a/test/goenv-init.bats +++ b/test/goenv-init.bats @@ -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' @@ -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' @@ -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' @@ -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' diff --git a/test/goenv-local.bats b/test/goenv-local.bats index f6d43695..41dbd1f9 100644 --- a/test/goenv-local.bats +++ b/test/goenv-local.bats @@ -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/'" { + 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/'" { diff --git a/test/goenv-version-file-write.bats b/test/goenv-version-file-write.bats index e83f9498..ff874dcc 100644 --- a/test/goenv-version-file-write.bats +++ b/test/goenv-version-file-write.bats @@ -23,12 +23,14 @@ OUT assert_failure "Usage: goenv version-file-write ..." } -@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" ] } @@ -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" @@ -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 @@ -82,7 +83,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" > ${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" @@ -90,4 +91,3 @@ OUT run goenv-global assert_success "system" } - diff --git a/test/goenv.bats b/test/goenv.bats index 1cd0e035..496f4f01 100644 --- a/test/goenv.bats +++ b/test/goenv.bats @@ -230,8 +230,10 @@ OUT } @test "goenv fails setting latest local version when major.minor number is given and does not match at 'GOENV_ROOT/versions/'" { + 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 }