1
0
mirror of https://github.com/pyenv/pyenv.git synced 2025-11-10 04:23:47 -05:00

Auto-resolve prefixes to the latest version (#2487)

This commit is contained in:
native-api
2022-10-30 03:38:40 +03:00
committed by GitHub
parent 0b5e16add3
commit a12f947cc3
13 changed files with 258 additions and 13 deletions

View File

@@ -145,6 +145,10 @@ IFS=$'\n' scripts=(`pyenv-hooks install`)
IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done
# Try to resolve a prefix if user indeed gave a prefix.
# We install the version under the resolved name
# and hooks also see the resolved name
DEFINITION="$(pyenv-latest -q -k "$DEFINITION" || echo "$DEFINITION")"
# Set VERSION_NAME from $DEFINITION, if it is not already set. Then
# compute the installation prefix.

View File

@@ -2067,12 +2067,25 @@ DEFINITION_PATH="${ARGUMENTS[0]}"
if [ -z "$DEFINITION_PATH" ]; then
usage 1 >&2
elif [ ! -f "$DEFINITION_PATH" ]; then
for DEFINITION_DIR in "${PYTHON_BUILD_DEFINITIONS[@]}"; do
if [ -f "${DEFINITION_DIR}/${DEFINITION_PATH}" ]; then
DEFINITION_PATH="${DEFINITION_DIR}/${DEFINITION_PATH}"
break
search_definitions() {
for DEFINITION_DIR in "${PYTHON_BUILD_DEFINITIONS[@]}"; do
if [ -f "${DEFINITION_DIR}/${DEFINITION_PATH}" ]; then
DEFINITION_PATH="${DEFINITION_DIR}/${DEFINITION_PATH}"
break
fi
done
}
search_definitions
if [ ! -f "$DEFINITION_PATH" ]; then
if RESOLVED_DEFINITION_PATH="$(command -v pyenv-latest >/dev/null && pyenv-latest -k -q "$DEFINITION_PATH")"; then
DEFINITION_PATH="$RESOLVED_DEFINITION_PATH"
unset RESOLVED_DEFINITION_PATH
search_definitions
fi
done
fi
unset search_definitions
if [ ! -f "$DEFINITION_PATH" ]; then
echo "python-build: definition not found: ${DEFINITION_PATH}" >&2

View File

@@ -60,11 +60,30 @@ NUM_DEFINITIONS="$(find "$BATS_TEST_DIRNAME"/../share/python-build -maxdepth 1 -
}
@test "installing nonexistent definition" {
stub pyenv-latest false
run python-build "nonexistent" "${TMP}/install"
assert [ "$status" -eq 2 ]
assert_output "python-build: definition not found: nonexistent"
}
@test "resolves prefixes via pyenv-latest" {
stub pyenv-latest "echo 2.7.8"
export PYTHON_BUILD_ROOT="$TMP"
mkdir -p "${PYTHON_BUILD_ROOT}/share/python-build"
echo 'echo 2.7.8' > "${PYTHON_BUILD_ROOT}/share/python-build/2.7.8"
run python-build "2.7" "${TMP}/install"
assert_success "2.7.8"
}
@test "doesn't resolve prefixes if pyenv-latest is unavailable" {
export PATH="$(path_without pyenv-latest)"
export PYTHON_BUILD_ROOT="$TMP"
mkdir -p "${PYTHON_BUILD_ROOT}/share/python-build"
echo 'echo 2.7.8' > "${PYTHON_BUILD_ROOT}/share/python-build/2.7.8"
run python-build "2.7" "${TMP}/install"
assert_failure "python-build: definition not found: 2.7"
}
@test "sorting Python versions" {
export PYTHON_BUILD_ROOT="$TMP"
mkdir -p "${PYTHON_BUILD_ROOT}/share/python-build"

View File

@@ -15,6 +15,7 @@ after_install 'echo after: \$STATUS'
OUT
stub pyenv-hooks "install : echo '$HOOK_PATH'/install.bash"
stub pyenv-rehash "echo rehashed"
stub pyenv-latest false
definition="${TMP}/3.6.2"
cat > "$definition" <<<"echo python-build"

View File

@@ -10,6 +10,7 @@ setup() {
stub_python_build() {
stub python-build "--lib : $BATS_TEST_DIRNAME/../bin/python-build --lib" "$@"
stub pyenv-latest " : false"
}
@test "install proper" {
@@ -23,6 +24,19 @@ stub_python_build() {
unstub pyenv-rehash
}
@test "install resolves a prefix" {
stub_python_build 'echo python-build "$@"'
stub pyenv-latest '-q -k 3.4 : echo 3.4.2'
pyenv-latest || true # pass through the stub entry added by stub_python_build
run pyenv-install 3.4
assert_success "python-build 3.4.2 ${PYENV_ROOT}/versions/3.4.2"
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'

View File

@@ -139,3 +139,29 @@ assert_output_contains() {
} | flunk
}
}
# Output a modified PATH that ensures that the given executable is not present,
# but in which system utils necessary for pyenv operation are still available.
path_without() {
local path=":${PATH}:"
for exe; do
local found alt util
for found in $(PATH="$path" type -aP "$exe"); do
found="${found%/*}"
if [ "$found" != "${PYENV_ROOT}/shims" ]; then
alt="${PYENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')"
mkdir -p "$alt"
for util in bash head cut readlink greadlink; do
if [ -x "${found}/$util" ]; then
ln -s "${found}/$util" "${alt}/$util"
fi
done
path="${path/:${found}:/:${alt}:}"
fi
done
done
path="${path#:}"
path="${path%:}"
echo "$path"
}