Support pyenv local --force

This commit is contained in:
Ivan Pozdeev 2024-12-15 21:09:35 +03:00
parent 057d7ad445
commit a9c5e18235
2 changed files with 29 additions and 2 deletions

View File

@ -2,9 +2,11 @@
# #
# Summary: Set or show the local application-specific Python version(s) # Summary: Set or show the local application-specific Python version(s)
# #
# Usage: pyenv local <version> <version2> <..> # Usage: pyenv local [-f|--force] [<version> [...]]
# pyenv local --unset # pyenv local --unset
# #
# -f/--force Do not verify that the versions being set exist
#
# Sets the local application-specific Python version(s) by writing the # Sets the local application-specific Python version(s) by writing the
# version name to a file named `.python-version'. # version name to a file named `.python-version'.
# #
@ -36,12 +38,25 @@ if [ "$1" = "--complete" ]; then
exec pyenv-versions --bare exec pyenv-versions --bare
fi fi
while [[ $# -gt 0 ]]
do
case "$1" in
-f|--force)
FORCE=1
shift
;;
*)
break
;;
esac
done
versions=("$@") versions=("$@")
if [ "$versions" = "--unset" ]; then if [ "$versions" = "--unset" ]; then
rm -f .python-version rm -f .python-version
elif [ -n "$versions" ]; then elif [ -n "$versions" ]; then
pyenv-version-file-write .python-version "${versions[@]}" pyenv-version-file-write ${FORCE:+-f }.python-version "${versions[@]}"
else else
if version_file="$(pyenv-version-file "$PWD")"; then if version_file="$(pyenv-version-file "$PWD")"; then
IFS=: versions=($(pyenv-version-file-read "$version_file")) IFS=: versions=($(pyenv-version-file-read "$version_file"))

View File

@ -41,6 +41,18 @@ setup() {
assert [ "$(cat .python-version)" = "1.2.3" ] assert [ "$(cat .python-version)" = "1.2.3" ]
} }
@test "fails to set a nonexistent local version" {
run pyenv-local 1.2.3
assert_failure "pyenv: version \`1.2.3' not installed"
assert [ ! -e .python-version ]
}
@test "sets a nonexistent local version with --force" {
run pyenv-local -f 1.2.3
assert_success ""
assert [ "$(cat .python-version)" = "1.2.3" ]
}
@test "changes local version" { @test "changes local version" {
echo "1.0-pre" > .python-version echo "1.0-pre" > .python-version
mkdir -p "${PYENV_ROOT}/versions/1.2.3" mkdir -p "${PYENV_ROOT}/versions/1.2.3"