Fix :latest after #2568 (#2599)

* Cleanup mocks logic
* Add test for `:latest`
This commit is contained in:
native-api 2023-01-22 05:49:40 +03:00 committed by GitHub
parent b64b0ab5e2
commit f1a1f59c87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 84 deletions

View file

@ -120,8 +120,8 @@ unset VERSION_NAME
# version is specified by pyenv. Show usage instructions if a local # version is specified by pyenv. Show usage instructions if a local
# version is not specified. # version is not specified.
DEFINITIONS=("${ARGUMENTS[@]}") DEFINITIONS=("${ARGUMENTS[@]}")
[ -n "${DEFINITIONS[*]}" ] || DEFINITIONS=($(pyenv-local 2>/dev/null || true)) [[ "${#DEFINITIONS[*]}" -eq 0 ]] && DEFINITIONS=($(pyenv-local 2>/dev/null || true))
[ -n "${DEFINITIONS[*]}" ] || usage 1 >&2 [[ "${#DEFINITIONS[*]}" -eq 0 ]] && usage 1 >&2
# Define `before_install` and `after_install` functions that allow # Define `before_install` and `after_install` functions that allow
# plugin hooks to register a string of code for execution before or # plugin hooks to register a string of code for execution before or
@ -151,7 +151,10 @@ IFS=$'\n' scripts=(`pyenv-hooks install`)
IFS="$OLDIFS" IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done for script in "${scripts[@]}"; do source "$script"; done
for DEFINITION in "${DEFINITIONS[@]}";do COMBINED_STATUS=0
for DEFINITION in "${DEFINITIONS[@]}"; do
STATUS=0
# Try to resolve a prefix if user indeed gave a prefix. # Try to resolve a prefix if user indeed gave a prefix.
# We install the version under the resolved name # We install the version under the resolved name
# and hooks also see the resolved name # and hooks also see the resolved name
@ -173,7 +176,7 @@ for DEFINITION in "${DEFINITIONS[@]}";do
case "$REPLY" in case "$REPLY" in
y | Y | yes | YES ) ;; y | Y | yes | YES ) ;;
* ) exit 1 ;; * ) { STATUS=1; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS=$STATUS; }; continue ;;
esac esac
elif [ -n "$SKIP_EXISTING" ]; then elif [ -n "$SKIP_EXISTING" ]; then
# Since we know the python version is already installed, and are opting to # Since we know the python version is already installed, and are opting to
@ -247,8 +250,8 @@ for DEFINITION in "${DEFINITIONS[@]}";do
for hook in "${before_hooks[@]}"; do eval "$hook"; done for hook in "${before_hooks[@]}"; do eval "$hook"; done
# Invoke `python-build` and record the exit status in $STATUS. # Invoke `python-build` and record the exit status in $STATUS.
STATUS=0 python-build $KEEP $VERBOSE $HAS_PATCH $DEBUG "$DEFINITION" "$PREFIX" || \
python-build $KEEP $VERBOSE $HAS_PATCH $DEBUG "$DEFINITION" "$PREFIX" || STATUS="$?" { STATUS=$?; [[ $STATUS -gt $COMBINED_STATUS ]] && COMBINED_STATUS=$STATUS; }
# Display a more helpful message if the definition wasn't found. # Display a more helpful message if the definition wasn't found.
if [ "$STATUS" == "2" ]; then if [ "$STATUS" == "2" ]; then
@ -279,13 +282,14 @@ for DEFINITION in "${DEFINITIONS[@]}";do
for hook in "${after_hooks[@]}"; do eval "$hook"; done for hook in "${after_hooks[@]}"; do eval "$hook"; done
# Run `pyenv-rehash` after a successful installation. # Run `pyenv-rehash` after a successful installation.
if [ "$STATUS" == "0" ]; then if [[ $STATUS -eq 0 ]]; then
pyenv-rehash pyenv-rehash
else else
break
cleanup cleanup
break
fi fi
done done
exit "${STATUS:-0}"
exit "${COMBINED_STATUS}"

View file

@ -5,31 +5,36 @@ export PYENV_ROOT="${TMP}/pyenv"
setup() { setup() {
stub pyenv-hooks 'install : true' stub pyenv-hooks 'install : true'
stub pyenv-rehash 'true' stub pyenv-rehash true
} }
stub_python_build() { stub_python_build_lib() {
stub python-build "--lib : $BATS_TEST_DIRNAME/../bin/python-build --lib" "$@" stub python-build "--lib : $BATS_TEST_DIRNAME/../bin/python-build --lib" "$@"
stub pyenv-latest ": false"
} }
@test "install proper" { stub_python_build_no_latest() {
stub_python_build 'echo python-build "$@"' stub python-build "${@:-echo python-build \"\$@\"}"
}
stub_python_build() {
stub_python_build_no_latest "$@"
stub pyenv-latest false
}
@test "install a single version" {
stub_python_build_lib
stub_python_build
run pyenv-install 3.4.2 run pyenv-install 3.4.2
assert_success "python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2" assert_success "python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2"
unstub python-build unstub python-build
unstub pyenv-hooks
unstub pyenv-rehash
} }
@test "install proper multi versions" { @test "install multiple versions" {
#stub python-build "--lib : $BATS_TEST_DIRNAME/../bin/python-build --lib" 'echo python-build "$@"' 'echo python-build "$@"' stub_python_build_lib
#stub pyenv-latest ": false" ": false" stub_python_build
stub_python_build 'echo python-build "$@"' 'echo python-build "$@"' stub_python_build
stub pyenv-latest ": false"
stub pyenv-rehash 'true'
run pyenv-install 3.4.1 3.4.2 run pyenv-install 3.4.1 3.4.2
assert_success assert_success
@ -40,61 +45,72 @@ OUT
unstub python-build unstub python-build
unstub pyenv-latest unstub pyenv-latest
unstub pyenv-hooks
unstub pyenv-rehash
} }
@test "install resolves a prefix" { @test "install multiple versions, some fail" {
stub_python_build 'echo python-build "$@"' stub_python_build_lib
stub pyenv-latest '-q -k 3.4 : echo 3.4.2' stub_python_build 'echo "fail: python-build" "$@"; false'
pyenv-latest || true # pass through the stub entry added by stub_python_build
run pyenv-install 3.4 run pyenv-install 3.4.1 3.4.2
assert_success "python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2" assert_failure
assert_output <<OUT
fail: python-build 3.4.1 ${TMP}/pyenv/versions/3.4.1
OUT
unstub python-build unstub python-build
unstub pyenv-hooks
unstub pyenv-rehash
} }
@test "install resolves a prefix with multi versions" {
stub_python_build 'echo python-build "$@"' 'echo python-build "$@"'
stub pyenv-latest '-q -k 3.4 : echo 3.4.2' '-q -k 3.5 : echo 3.5.2'
stub pyenv-rehash 'true'
pyenv-latest || true # pass through the stub entry added by stub_python_build
run pyenv-install 3.4 3.5 @test "install resolves a prefix" {
stub_python_build_lib
for i in {1..3}; do stub_python_build_no_latest; done
stub pyenv-latest \
'-q -k 3.4 : echo 3.4.2' \
'-q -k 3.5.1 : false' \
'-q -k 3.5 : echo 3.5.2'
run pyenv-install 3.4 3.5.1 3.5
assert_success <<OUT assert_success <<OUT
python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2 python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2
python-build 3.5.1 ${PYENV_ROOT}/versions/3.5.1
python-build 3.5.2 ${PYENV_ROOT}/versions/3.5.2 python-build 3.5.2 ${PYENV_ROOT}/versions/3.5.2
OUT OUT
unstub python-build unstub python-build
unstub pyenv-hooks
unstub pyenv-rehash
} }
@test "install pyenv local version by default" {
stub_python_build 'echo python-build "$1"'
stub pyenv-local 'echo 3.4.2'
run pyenv-install @test "install resolves :latest" {
assert_success "python-build 3.4.2" stub_python_build_lib
for i in {1..2}; do stub_python_build '--definitions : echo -e 3.4.2\\n3.5.1\\n3.5.2'; done
for i in {1..2}; do stub_python_build; done
pyenv-hooks install; unstub pyenv-hooks
PYENV_INSTALL_ROOT="$BATS_TEST_DIRNAME/../../.."
export PYENV_HOOK_PATH="$PYENV_INSTALL_ROOT/pyenv.d"
[[ -d "$PYENV_INSTALL_ROOT/libexec" ]] || skip "python-build is installed separately from pyenv"
export PATH="$PATH:$PYENV_INSTALL_ROOT/libexec"
run pyenv-install 3.4:latest 3:latest
assert_success <<!
python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2
python-build 3.5.2 ${PYENV_ROOT}/versions/3.5.2
!
unstub python-build unstub python-build
unstub pyenv-local
} }
@test "install pyenv local multi versions by default" { @test "install installs local versions by default" {
stub_python_build 'echo python-build "$1"' 'echo python-build "$1"' stub_python_build_lib
stub pyenv-local 'printf "%s\n%s\n" 3.4.2 3.4.1' stub_python_build
stub pyenv-latest ": false" stub_python_build
stub pyenv-local 'echo 3.4.2; echo 3.4.1'
run pyenv-install run pyenv-install
assert_success <<OUT assert_success <<OUT
python-build 3.4.2" python-build 3.4.2
python-build 3.4.1" python-build 3.4.1
OUT OUT
unstub python-build unstub python-build
@ -102,8 +118,8 @@ OUT
} }
@test "list available versions" { @test "list available versions" {
stub_python_build \ stub_python_build_lib
"--definitions : echo 2.6.9 2.7.9-rc1 2.7.9-rc2 3.4.2 | tr ' ' $'\\n'" stub_python_build "--definitions : echo 2.6.9 2.7.9-rc1 2.7.9-rc2 3.4.2 | tr ' ' $'\\n'"
run pyenv-install --list run pyenv-install --list
assert_success assert_success
@ -118,10 +134,11 @@ OUT
unstub python-build unstub python-build
} }
@test "nonexistent version" { @test "upgrade instructions given for a nonexistent version" {
stub brew false stub brew false
stub_python_build 'echo ERROR >&2 && exit 2' \ stub_python_build_lib
"--definitions : echo 2.6.9 2.7.9-rc1 2.7.9-rc2 3.4.2 | tr ' ' $'\\n'" stub_python_build 'echo ERROR >&2 && exit 2'
stub_python_build "--definitions : echo 2.6.9 2.7.9-rc1 2.7.9-rc2 3.4.2 | tr ' ' $'\\n'"
run pyenv-install 2.7.9 run pyenv-install 2.7.9
assert_failure assert_failure
@ -142,8 +159,9 @@ OUT
unstub python-build unstub python-build
} }
@test "Homebrew upgrade instructions" { @test "homebrew upgrade instructions given when pyenv is homebrew-installed" {
stub brew "--prefix : echo '${BATS_TEST_DIRNAME%/*}'" stub brew "--prefix : echo '${BATS_TEST_DIRNAME%/*}'"
stub_python_build_lib
stub_python_build 'echo ERROR >&2 && exit 2' \ stub_python_build 'echo ERROR >&2 && exit 2' \
"--definitions : true" "--definitions : true"
@ -165,15 +183,19 @@ OUT
@test "no build definitions from plugins" { @test "no build definitions from plugins" {
assert [ ! -e "${PYENV_ROOT}/plugins" ] assert [ ! -e "${PYENV_ROOT}/plugins" ]
stub_python_build_lib
stub_python_build 'echo $PYTHON_BUILD_DEFINITIONS' stub_python_build 'echo $PYTHON_BUILD_DEFINITIONS'
run pyenv-install 3.4.2 run pyenv-install 3.4.2
assert_success "" assert_success ""
unstub python-build
} }
@test "some build definitions from plugins" { @test "some build definitions from plugins" {
mkdir -p "${PYENV_ROOT}/plugins/foo/share/python-build" mkdir -p "${PYENV_ROOT}/plugins/foo/share/python-build"
mkdir -p "${PYENV_ROOT}/plugins/bar/share/python-build" mkdir -p "${PYENV_ROOT}/plugins/bar/share/python-build"
stub_python_build_lib
stub_python_build "echo \$PYTHON_BUILD_DEFINITIONS | tr ':' $'\\n'" stub_python_build "echo \$PYTHON_BUILD_DEFINITIONS | tr ':' $'\\n'"
run pyenv-install 3.4.2 run pyenv-install 3.4.2
@ -183,11 +205,14 @@ OUT
${PYENV_ROOT}/plugins/bar/share/python-build ${PYENV_ROOT}/plugins/bar/share/python-build
${PYENV_ROOT}/plugins/foo/share/python-build ${PYENV_ROOT}/plugins/foo/share/python-build
OUT OUT
unstub python-build
} }
@test "list build definitions from plugins" { @test "list build definitions from plugins" {
mkdir -p "${PYENV_ROOT}/plugins/foo/share/python-build" mkdir -p "${PYENV_ROOT}/plugins/foo/share/python-build"
mkdir -p "${PYENV_ROOT}/plugins/bar/share/python-build" mkdir -p "${PYENV_ROOT}/plugins/bar/share/python-build"
stub_python_build_lib
stub_python_build "--definitions : echo \$PYTHON_BUILD_DEFINITIONS | tr ':' $'\\n'" stub_python_build "--definitions : echo \$PYTHON_BUILD_DEFINITIONS | tr ':' $'\\n'"
run pyenv-install --list run pyenv-install --list
@ -198,12 +223,14 @@ Available versions:
${PYENV_ROOT}/plugins/bar/share/python-build ${PYENV_ROOT}/plugins/bar/share/python-build
${PYENV_ROOT}/plugins/foo/share/python-build ${PYENV_ROOT}/plugins/foo/share/python-build
OUT OUT
unstub python-build
} }
@test "completion results include build definitions from plugins" { @test "completion results include build definitions from plugins" {
mkdir -p "${PYENV_ROOT}/plugins/foo/share/python-build" mkdir -p "${PYENV_ROOT}/plugins/foo/share/python-build"
mkdir -p "${PYENV_ROOT}/plugins/bar/share/python-build" mkdir -p "${PYENV_ROOT}/plugins/bar/share/python-build"
stub python-build "--definitions : echo \$PYTHON_BUILD_DEFINITIONS | tr ':' $'\\n'" stub_python_build "--definitions : echo \$PYTHON_BUILD_DEFINITIONS | tr ':' $'\\n'"
run pyenv-install --complete run pyenv-install --complete
assert_success assert_success
@ -220,10 +247,12 @@ OUT
${PYENV_ROOT}/plugins/bar/share/python-build ${PYENV_ROOT}/plugins/bar/share/python-build
${PYENV_ROOT}/plugins/foo/share/python-build ${PYENV_ROOT}/plugins/foo/share/python-build
OUT OUT
unstub python-build
} }
@test "not enough arguments for pyenv-install if no local version" { @test "not enough arguments for pyenv-install if no local version" {
stub_python_build stub_python_build_lib
stub pyenv-help 'install : true' stub pyenv-help 'install : true'
run pyenv-install run pyenv-install
@ -232,17 +261,7 @@ OUT
assert_output "" assert_output ""
} }
@test "multi arguments for pyenv-install" {
stub_python_build
stub pyenv-help 'install : true'
run pyenv-install 3.4.1 3.4.2
assert_success
assert_output ""
}
@test "show help for pyenv-install" { @test "show help for pyenv-install" {
stub_python_build
stub pyenv-help 'install : true' stub pyenv-help 'install : true'
run pyenv-install -h run pyenv-install -h
@ -263,7 +282,7 @@ OUT
unstub pyenv-help unstub pyenv-help
} }
@test "more than one argument for pyenv-uninstall" { @test "multiple arguments for pyenv-uninstall" {
mkdir -p "${PYENV_ROOT}/versions/3.4.1" mkdir -p "${PYENV_ROOT}/versions/3.4.1"
mkdir -p "${PYENV_ROOT}/versions/3.4.2" mkdir -p "${PYENV_ROOT}/versions/3.4.2"
run pyenv-uninstall -f 3.4.1 3.4.2 run pyenv-uninstall -f 3.4.1 3.4.2

View file

@ -1,13 +1,25 @@
DEFINITION_PREFIX="${DEFINITION%%:*}" pyenv_install_resolve_latest() {
DEFINITION_TYPE="${DEFINITION_PREFIX%%-*}" # TODO: support non-CPython versions local DEFINITION_PREFIX DEFINITION_TYPE
if [[ "${DEFINITION}" != "${DEFINITION_PREFIX}" ]]; then local -a DEFINITION_CANDIDATES
DEFINITION_CANDIDATES=(\ local DEFINITION="$1"
$(python-build --definitions | \
grep -F "${DEFINITION_PREFIX}" | \ DEFINITION_PREFIX="${DEFINITION%%:*}"
grep "^${DEFINITION_TYPE}" | \ DEFINITION_TYPE="${DEFINITION_PREFIX%%-*}" # TODO: support non-CPython versions
sed -E -e '/-dev$/d' -e '/-src$/d' -e '/(b|rc)[0-9]+$/d' | \ if [[ "${DEFINITION}" != "${DEFINITION_PREFIX}" ]]; then
sort -t. -k1,1r -k 2,2nr -k 3,3nr \ DEFINITION_CANDIDATES=(\
|| true)) $(python-build --definitions | \
DEFINITION="${DEFINITION_CANDIDATES}" grep -F "${DEFINITION_PREFIX}" | \
VERSION_NAME="${DEFINITION##*/}" grep "^${DEFINITION_TYPE}" | \
fi sed -E -e '/-dev$/d' -e '/-src$/d' -e '/(b|rc)[0-9]+$/d' | \
sort -t. -k1,1r -k 2,2nr -k 3,3nr \
|| true))
DEFINITION="${DEFINITION_CANDIDATES}"
fi
echo "$DEFINITION"
}
for i in ${!DEFINITIONS[*]}; do
DEFINITIONS[$i]="$(pyenv_install_resolve_latest "${DEFINITIONS[$i]}")"
done
unset pyenv_install_resolve_latest