diff --git a/libexec/pyenv-local b/libexec/pyenv-local index c0389281..a44576a6 100755 --- a/libexec/pyenv-local +++ b/libexec/pyenv-local @@ -2,9 +2,11 @@ # # Summary: Set or show the local application-specific Python version(s) # -# Usage: pyenv local <..> +# Usage: pyenv local [-f|--force] [ [...]] # 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 # version name to a file named `.python-version'. # @@ -36,12 +38,25 @@ if [ "$1" = "--complete" ]; then exec pyenv-versions --bare fi +while [[ $# -gt 0 ]] +do + case "$1" in + -f|--force) + FORCE=1 + shift + ;; + *) + break + ;; + esac +done + versions=("$@") if [ "$versions" = "--unset" ]; then rm -f .python-version elif [ -n "$versions" ]; then - pyenv-version-file-write .python-version "${versions[@]}" + pyenv-version-file-write ${FORCE:+-f }.python-version "${versions[@]}" else if version_file="$(pyenv-version-file "$PWD")"; then IFS=: versions=($(pyenv-version-file-read "$version_file")) diff --git a/test/local.bats b/test/local.bats index 7003c99c..9d97f144 100644 --- a/test/local.bats +++ b/test/local.bats @@ -41,6 +41,18 @@ setup() { 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" { echo "1.0-pre" > .python-version mkdir -p "${PYENV_ROOT}/versions/1.2.3"