Add support for multiple versions in pyenv uninstall (#2432)

This commit is contained in:
hardikpnsp 2022-08-15 22:55:46 +05:30 committed by GitHub
parent 965421d5d4
commit afeb971fa2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 99 additions and 38 deletions

View file

@ -238,9 +238,9 @@ To install the latest major release for Python 3 try:
## `pyenv uninstall` ## `pyenv uninstall`
Uninstall a specific Python version. Uninstall Python versions.
Usage: pyenv uninstall [-f|--force] <version> Usage: pyenv uninstall [-f|--force] <version> ...
-f Attempt to remove the specified version without prompting -f Attempt to remove the specified version without prompting
for confirmation. If the version does not exist, do not for confirmation. If the version does not exist, do not

View file

@ -432,7 +432,7 @@ for more details on how the selection works and more information on its usage.
As time goes on, you will accumulate Python versions in your As time goes on, you will accumulate Python versions in your
`$(pyenv root)/versions` directory. `$(pyenv root)/versions` directory.
To remove old Python versions, use [`pyenv uninstall <version>`](COMMANDS.md#pyenv-uninstall). To remove old Python versions, use [`pyenv uninstall <versions>`](COMMANDS.md#pyenv-uninstall).
Alternatively, you can simply `rm -rf` the directory of the version you want Alternatively, you can simply `rm -rf` the directory of the version you want
to remove. You can find the directory of a particular Python version to remove. You can find the directory of a particular Python version

View file

@ -106,7 +106,7 @@ Set or show the shell\-specific Python version
List existing pyenv shims List existing pyenv shims
.TP .TP
.B uninstall .B uninstall
Uninstall a specific Python version Uninstall Python versions
.TP .TP
.B version .B version
Show the current Python version(s) and its origin Show the current Python version(s) and its origin
@ -452,10 +452,10 @@ $ pyenv versions
.fi .fi
.IP "" 0 .IP "" 0
.SS "pyenv uninstall" .SS "pyenv uninstall"
Uninstall a specific Python version\. Uninstall Python versions\.
.IP "" 4 .IP "" 4
.nf .nf
Usage: pyenv uninstall [\-f|\-\-force] <version> Usage: pyenv uninstall [\-f|\-\-force] <version> ...
\-f Attempt to remove the specified version without prompting \-f Attempt to remove the specified version without prompting
for confirmation\. If the version does not exist, do not for confirmation\. If the version does not exist, do not

View file

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# #
# Summary: Uninstall a specific Python version # Summary: Uninstall Python versions
# #
# Usage: pyenv uninstall [-f|--force] <version> # Usage: pyenv uninstall [-f|--force] <version> ...
# #
# -f Attempt to remove the specified version without prompting # -f Attempt to remove the specified version without prompting
# for confirmation. If the version does not exist, do not # for confirmation. If the version does not exist, do not
@ -33,14 +33,17 @@ if [ "$1" = "-f" ] || [ "$1" = "--force" ]; then
shift shift
fi fi
[ "$#" -eq 1 ] || usage 1 >&2 [ "$#" -gt 0 ] || usage 1 >&2
DEFINITION="$1" versions=("$@")
case "$DEFINITION" in
"" | -* ) for version in "${versions[@]}"; do
usage 1 >&2 case "$version" in
;; "" | -* )
esac usage 1 >&2
;;
esac
done
declare -a before_hooks after_hooks declare -a before_hooks after_hooks
@ -59,29 +62,36 @@ IFS=$'\n' scripts=(`pyenv-hooks uninstall`)
IFS="$OLDIFS" IFS="$OLDIFS"
for script in "${scripts[@]}"; do source "$script"; done for script in "${scripts[@]}"; do source "$script"; done
uninstall-python() {
local DEFINITION="$1"
VERSION_NAME="${DEFINITION##*/}" local VERSION_NAME="${DEFINITION##*/}"
PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}" local PREFIX="${PYENV_ROOT}/versions/${VERSION_NAME}"
if [ -z "$FORCE" ]; then if [ -z "$FORCE" ]; then
if [ ! -d "$PREFIX" ]; then if [ ! -d "$PREFIX" ]; then
echo "pyenv: version \`$VERSION_NAME' not installed" >&2 echo "pyenv: version \`$VERSION_NAME' not installed" >&2
exit 1 exit 1
fi
read -p "pyenv: remove $PREFIX? [y|N] "
case "$REPLY" in
y | Y | yes | YES ) ;;
* ) exit 1 ;;
esac
fi fi
read -p "pyenv: remove $PREFIX? [y|N] " for hook in "${before_hooks[@]}"; do eval "$hook"; done
case "$REPLY" in
y | Y | yes | YES ) ;;
* ) exit 1 ;;
esac
fi
for hook in "${before_hooks[@]}"; do eval "$hook"; done if [ -d "$PREFIX" ]; then
rm -rf "$PREFIX"
pyenv-rehash
echo "pyenv: $VERSION_NAME uninstalled"
fi
if [ -d "$PREFIX" ]; then for hook in "${after_hooks[@]}"; do eval "$hook"; done
rm -rf "$PREFIX" }
pyenv-rehash
echo "pyenv: $VERSION_NAME uninstalled"
fi
for hook in "${after_hooks[@]}"; do eval "$hook"; done for version in "${versions[@]}"; do
uninstall-python "$version"
done

View file

@ -55,3 +55,38 @@ OUT
refute [ -d "${PYENV_ROOT}/versions/3.6.2" ] refute [ -d "${PYENV_ROOT}/versions/3.6.2" ]
} }
@test "pyenv-uninstall hooks with multiple versions" {
cat > "${HOOK_PATH}/uninstall.bash" <<OUT
before_uninstall 'echo before: \$PREFIX'
after_uninstall 'echo after.'
rm() {
echo "rm \$@"
command rm "\$@"
}
OUT
stub pyenv-hooks "uninstall : echo '$HOOK_PATH'/uninstall.bash"
stub pyenv-rehash "echo rehashed"
stub pyenv-rehash "echo rehashed"
mkdir -p "${PYENV_ROOT}/versions/3.6.2"
mkdir -p "${PYENV_ROOT}/versions/3.6.3"
run pyenv-uninstall -f 3.6.2 3.6.3
assert_success
assert_output <<-OUT
before: ${PYENV_ROOT}/versions/3.6.2
rm -rf ${PYENV_ROOT}/versions/3.6.2
rehashed
pyenv: 3.6.2 uninstalled
after.
before: ${PYENV_ROOT}/versions/3.6.3
rm -rf ${PYENV_ROOT}/versions/3.6.3
rehashed
pyenv: 3.6.3 uninstalled
after.
OUT
refute [ -d "${PYENV_ROOT}/versions/3.6.2" ]
refute [ -d "${PYENV_ROOT}/versions/3.6.3" ]
}

View file

@ -195,12 +195,28 @@ OUT
unstub pyenv-help unstub pyenv-help
} }
@test "too many arguments for pyenv-uninstall" { @test "more than one argument for pyenv-uninstall" {
stub pyenv-help 'uninstall : true' mkdir -p "${PYENV_ROOT}/versions/3.4.1"
mkdir -p "${PYENV_ROOT}/versions/3.4.2"
run pyenv-uninstall -f 3.4.1 3.4.2
run pyenv-uninstall 3.4.1 3.4.2 assert_success
refute [ -d "${PYENV_ROOT}/versions/3.4.1" ]
refute [ -d "${PYENV_ROOT}/versions/3.4.2" ]
}
@test "invalid arguments for pyenv-uninstall" {
mkdir -p "${PYENV_ROOT}/versions/3.10.3"
mkdir -p "${PYENV_ROOT}/versions/3.10.4"
run pyenv-uninstall -f 3.10.3 --invalid-option 3.10.4
assert_failure assert_failure
unstub pyenv-help
assert [ -d "${PYENV_ROOT}/versions/3.10.3" ]
assert [ -d "${PYENV_ROOT}/versions/3.10.4" ]
rmdir "${PYENV_ROOT}/versions/3.10.3"
rmdir "${PYENV_ROOT}/versions/3.10.4"
} }
@test "show help for pyenv-uninstall" { @test "show help for pyenv-uninstall" {