Merge branch 'unittest' (#21)

This commit is contained in:
Yamashita Yuu 2014-01-03 04:43:05 +09:00
commit 12d4fdb307
34 changed files with 1630 additions and 163 deletions

4
.travis.yml Normal file
View file

@ -0,0 +1,4 @@
install: git clone https://github.com/sstephenson/bats.git
script: bats/bin/bats --tap test
# skips unnecessary Python-specific setup
language: c

View file

@ -38,13 +38,14 @@ versions=($@)
if [ "$versions" = "--unset" ]; then
rm -f .python-version .pyenv-version
elif [ -n "$versions" ]; then
if [ "$(PYENV_VERSION= pyenv-version-origin)" -ef .pyenv-version ]; then
previous_file="$(PYENV_VERSION= pyenv-version-origin || true)"
pyenv-version-file-write .python-version "${versions[@]}"
if [ "$previous_file" -ef .pyenv-version ]; then
rm -f .pyenv-version
{ echo "pyenv: removed existing \`.pyenv-version' file and migrated"
echo " local version specification to \`.python-version' file"
} >&2
fi
pyenv-version-file-write .python-version "${versions[@]}"
else
OLDIFS="$IFS"
IFS=: versions=($(

View file

@ -16,32 +16,38 @@ if [ "$1" = "--complete" ]; then
fi
if [ -n "$1" ]; then
versions=($@)
OLDIFS="$IFS"
IFS=: PYENV_VERSION="${versions[*]}"
IFS="$OLDIFS"
export PYENV_VERSION
else
OLDIFS="$IFS"
IFS=: versions=($(pyenv-version-name))
{ IFS=:
export PYENV_VERSION="$*"
}
IFS="$OLDIFS"
elif [ -z "$PYENV_VERSION" ]; then
PYENV_VERSION="$(pyenv-version-name)"
fi
PYENV_PREFIX_PATHS=()
for version in "${versions[@]}"; do
if [ "$version" = "system" ]; then
PYTHON_PATH="$(PYENV_VERSION="${version}" pyenv-which python || true)"
PYENV_PREFIX_PATH="${PYTHON_PATH%/bin/*}"
else
PYENV_PREFIX_PATH="${PYENV_ROOT}/versions/${version}"
fi
if [ -d "$PYENV_PREFIX_PATH" ]; then
PYENV_PREFIX_PATHS=("${PYENV_PREFIX_PATHS[@]}" "$PYENV_PREFIX_PATH")
else
echo "pyenv: version \`${version}' not installed" >&2
exit 1
fi
done
OLDIFS="$IFS"
{ IFS=:
for version in ${PYENV_VERSION}; do
if [ "$version" = "system" ]; then
if PYTHON_PATH="$(pyenv-which python 2>/dev/null)"; then
PYENV_PREFIX_PATH="${PYTHON_PATH%/bin/*}"
else
echo "pyenv: system version not found in PATH" >&2
exit 1
fi
else
PYENV_PREFIX_PATH="${PYENV_ROOT}/versions/${version}"
fi
if [ -d "$PYENV_PREFIX_PATH" ]; then
PYENV_PREFIX_PATHS=("${PYENV_PREFIX_PATHS[@]}" "$PYENV_PREFIX_PATH")
else
echo "pyenv: version \`${version}' not installed" >&2
exit 1
fi
done
}
IFS="$OLDIFS"
OLDIFS="$IFS"
{ IFS=:

View file

@ -5,7 +5,7 @@ set -e
[ -n "$PYENV_DEBUG" ] && set -x
PYENV_VERSION_FILE="$1"
shift
shift || true
versions=("$@")
if [ -z "$versions" ] || [ -z "$PYENV_VERSION_FILE" ]; then

View file

@ -5,18 +5,10 @@ set -e
if [ -z "$PYENV_VERSION" ]; then
PYENV_VERSION_FILE="$(pyenv-version-file)"
OLDIFS="$IFS"
IFS=: versions=($(pyenv-version-file-read "$PYENV_VERSION_FILE" || true))
IFS=: PYENV_VERSION="${versions[*]}"
IFS="$OLDIFS"
export PYENV_VERSION
else
OLDIFS="$IFS"
IFS=: versions=($(echo "${PYENV_VERSION}"))
IFS="$OLDIFS"
PYENV_VERSION="$(pyenv-version-file-read "$PYENV_VERSION_FILE" || true)"
fi
if [ -z "$versions" ] || [ "$versions" = "system" ]; then
if [ -z "$PYENV_VERSION" ] || [ "$PYENV_VERSION" = "system" ]; then
echo "system"
exit
fi
@ -26,11 +18,27 @@ version_exists() {
[ -d "${PYENV_ROOT}/versions/${version}" ]
}
for version in "${versions[@]}"; do
if [ "$version" != "system" ] && ! version_exists "$version"; then
echo "pyenv: version \`$version' is not installed" >&2
exit 1
fi
done
versions=()
OLDIFS="$IFS"
{ IFS=:
for version in ${PYENV_VERSION}; do
if version_exists "$version" || [ "$version" = "system" ]; then
versions=("${versions[@]}" "${version}")
elif version_exists "${version#python-}"; then
{ echo "warning: ignoring extraneous \`python-' prefix in version \`${version}'"
echo " (set by $(pyenv-version-origin))"
} >&2
versions=("${versions[@]}" "${version#python-}")
else
echo "pyenv: version \`$version' is not installed" >&2
exit 1
fi
done
}
IFS="$OLDIFS"
echo "${PYENV_VERSION}"
OLDIFS="$IFS"
{ IFS=:
echo "${versions[*]}"
}
IFS="$OLDIFS"

View file

@ -1,67 +0,0 @@
# pyenv-version-ext
pyenv-version-ext is a [pyenv](https://github.com/yyuu/pyenv) plugin
that provides a `pyenv push` and `pyenv pop` commands to manage Python
versions.
(NOTICE:
This project has been moved out as external plugin of pyenv.
The version-ext-compat has left for backward compatibility, but will be removed from future release of pyenv.
See also [pyenv-version-ext](https://github.com/yyuu/pyenv-version-ext).)
## Installation
### Installing as an pyenv plugin
Installing pyenv-version-ext as a pyenv plugin will give you access to the
`pyenv push` and `pyenv pop` commands.
$ git clone git://github.com/yyuu/pyenv-version-ext.git ~/.pyenv/plugins/pyenv-version-ext
This will install the latest development version of pyenv-version-ext into
the `~/.pyenv/plugins/pyenv-version-ext` directory. From that directory, you
can check out a specific release tag. To update pyenv-version-ext, run `git
pull` to download the latest changes.
## Usage
You can manage your version stack by `pyenv push` and `pyenv pop`.
$ pyenv global
2.7.5
3.2.5
$ pyenv push 3.3.2
$ pyenv global
2.7.5
3.2.5
3.3.2
$ pyenv pop
2.7.5
3.2.5
The push/pop operation is also efective for local and shell versions.
### License
(The MIT License)
* Copyright (c) 2013 Yamashita, Yuu
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,21 +0,0 @@
#!/usr/bin/env bash
# Usage: pyenv pop <version>
set -e
[ -n "$PYENV_DEBUG" ] && set -x
IFS=: versions=($(pyenv-version-name))
echo "WARNING: The \`pop' command will be removed from pyenv. Please install https://github.com/yyuu/pyenv-version-ext." 1>&2
length="${#versions[@]}"
PYENV_VERSION_NAMES=()
for ((i=0; i<length-1; i++)); do
PYENV_VERSION_NAMES=("${PYENV_VERSION_NAMES[@]}" "${versions[$i]}")
done
if [ -n "$PYENV_VERSION" ]; then
IFS=: PYENV_VERSION="${PYENV_VERSION_NAMES[*]}"
echo "export PYENV_VERSION=\"${PYENV_VERSION}\""
else
pyenv-version-file-write "$(pyenv-version-file)" "${PYENV_VERSION_NAMES[@]}"
fi

View file

@ -1,17 +0,0 @@
#!/usr/bin/env bash
# Usage: pyenv push <version>
set -e
[ -n "$PYENV_DEBUG" ] && set -x
IFS=: PYENV_VERSION_NAMES=($(pyenv-version-name))
versions=("$@")
PYENV_VERSION_NAMES=("${PYENV_VERSION_NAMES[@]}" "${versions[@]}")
echo "WARNING: The \`push' command will be removed from pyenv. Please install https://github.com/yyuu/pyenv-version-ext." 1>&2
if [ -n "$PYENV_VERSION" ]; then
IFS=: PYENV_VERSION="${PYENV_VERSION_NAMES[*]}"
echo "export PYENV_VERSION=\"${PYENV_VERSION}\""
else
pyenv-version-file-write "$(pyenv-version-file)" "${PYENV_VERSION_NAMES[@]}"
fi

View file

@ -1,17 +0,0 @@
#!/bin/sh
set -e
if [ -z "${PREFIX}" ]; then
PREFIX="/usr/local"
fi
BIN_PATH="${PREFIX}/bin"
mkdir -p "${BIN_PATH}"
for file in bin/*; do
cp "${file}" "${BIN_PATH}"
done
echo "Installed pyenv-version-ext at ${PREFIX}"

46
test/--version.bats Normal file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "$HOME"
git config --global user.name "Tester"
git config --global user.email "tester@test.local"
}
git_commit() {
git commit --quiet --allow-empty -m "empty"
}
@test "default version" {
assert [ ! -e "$PYENV_ROOT" ]
run pyenv---version
assert_success
[[ $output == "pyenv 0."* ]]
}
@test "reads version from git repo" {
mkdir -p "$PYENV_ROOT"
cd "$PYENV_ROOT"
git init
git_commit
git tag v0.4.1
git_commit
git_commit
cd "$PYENV_TEST_DIR"
run pyenv---version
assert_success
[[ $output == "pyenv 0.4.1-2-g"* ]]
}
@test "prints default version if no tags in git repo" {
mkdir -p "$PYENV_ROOT"
cd "$PYENV_ROOT"
git init
git_commit
cd "$PYENV_TEST_DIR"
run pyenv---version
[[ $output == "pyenv 0."* ]]
}

39
test/commands.bats Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env bats
load test_helper
@test "commands" {
run pyenv-commands
assert_success
assert_line "init"
assert_line "rehash"
assert_line "shell"
refute_line "sh-shell"
assert_line "echo"
}
@test "commands --sh" {
run pyenv-commands --sh
assert_success
refute_line "init"
assert_line "shell"
}
@test "commands in path with spaces" {
path="${PYENV_TEST_DIR}/my commands"
cmd="${path}/pyenv-sh-hello"
mkdir -p "$path"
touch "$cmd"
chmod +x "$cmd"
PATH="${path}:$PATH" run pyenv-commands --sh
assert_success
assert_line "hello"
}
@test "commands --no-sh" {
run pyenv-commands --no-sh
assert_success
assert_line "init"
refute_line "shell"
}

46
test/completions.bats Normal file
View file

@ -0,0 +1,46 @@
#!/usr/bin/env bats
load test_helper
create_command() {
bin="${PYENV_TEST_DIR}/bin"
mkdir -p "$bin"
echo "$2" > "${bin}/$1"
chmod +x "${bin}/$1"
}
@test "command with no completion support" {
create_command "pyenv-hello" "#!$BASH
echo hello"
run pyenv-completions hello
assert_success ""
}
@test "command with completion support" {
create_command "pyenv-hello" "#!$BASH
# provide pyenv completions
if [[ \$1 = --complete ]]; then
echo hello
else
exit 1
fi"
run pyenv-completions hello
assert_success "hello"
}
@test "forwards extra arguments" {
create_command "pyenv-hello" "#!$BASH
# provide pyenv completions
if [[ \$1 = --complete ]]; then
shift 1
for arg; do echo \$arg; done
else
exit 1
fi"
run pyenv-completions hello happy world
assert_success
assert_output <<OUT
happy
world
OUT
}

113
test/exec.bats Normal file
View file

@ -0,0 +1,113 @@
#!/usr/bin/env bats
load test_helper
create_executable() {
name="${1?}"
shift 1
bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin"
mkdir -p "$bin"
{ if [ $# -eq 0 ]; then cat -
else echo "$@"
fi
} | sed -Ee '1s/^ +//' > "${bin}/$name"
chmod +x "${bin}/$name"
}
@test "fails with invalid version" {
export PYENV_VERSION="3.4"
run pyenv-exec python -v
assert_failure "pyenv: version \`3.4' is not installed"
}
@test "completes with names of executables" {
export PYENV_VERSION="3.4"
create_executable "fab" "#!/bin/sh"
create_executable "python" "#!/bin/sh"
pyenv-rehash
run pyenv-completions exec
assert_success
assert_output <<OUT
fab
python
OUT
}
@test "supports hook path with spaces" {
hook_path="${PYENV_TEST_DIR}/custom stuff/pyenv hooks"
mkdir -p "${hook_path}/exec"
echo "export HELLO='from hook'" > "${hook_path}/exec/hello.bash"
export PYENV_VERSION=system
PYENV_HOOK_PATH="$hook_path" run pyenv-exec env
assert_success
assert_line "HELLO=from hook"
}
@test "carries original IFS within hooks" {
hook_path="${PYENV_TEST_DIR}/pyenv.d"
mkdir -p "${hook_path}/exec"
cat > "${hook_path}/exec/hello.bash" <<SH
hellos=(\$(printf "hello\\tugly world\\nagain"))
echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
SH
export PYENV_VERSION=system
PYENV_HOOK_PATH="$hook_path" IFS=$' \t\n' run pyenv-exec env
assert_success
assert_line "HELLO=:hello:ugly:world:again"
}
@test "forwards all arguments" {
export PYENV_VERSION="3.4"
create_executable "python" <<SH
#!$BASH
echo \$0
for arg; do
# hack to avoid bash builtin echo which can't output '-e'
printf " %s\\n" "\$arg"
done
SH
run pyenv-exec python -w "/path to/python script.rb" -- extra args
assert_success
assert_output <<OUT
${PYENV_ROOT}/versions/3.4/bin/python
-w
/path to/python script.rb
--
extra
args
OUT
}
@test "supports python -S <cmd>" {
export PYENV_VERSION="3.4"
# emulate `python -S' behavior
create_executable "python" <<SH
#!$BASH
if [[ \$1 == "-S"* ]]; then
found="\$(PATH="\${PYTHONPATH:-\$PATH}" which \$2)"
# assert that the found executable has python for shebang
if head -1 "\$found" | grep python >/dev/null; then
\$BASH "\$found"
else
echo "python: no Python script found in input (LoadError)" >&2
exit 1
fi
else
echo 'python 3.4 (pyenv test)'
fi
SH
create_executable "fab" <<SH
#!/usr/bin/env python
echo hello fab
SH
pyenv-rehash
run python -S fab
assert_success "hello fab"
}

31
test/global.bats Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env bats
load test_helper
@test "default" {
run pyenv global
assert_success
assert_output "system"
}
@test "read PYENV_ROOT/version" {
mkdir -p "$PYENV_ROOT"
echo "1.2.3" > "$PYENV_ROOT/version"
run pyenv-global
assert_success
assert_output "1.2.3"
}
@test "set PYENV_ROOT/version" {
mkdir -p "$PYENV_ROOT/versions/1.2.3"
run pyenv-global "1.2.3"
assert_success
run pyenv global
assert_success "1.2.3"
}
@test "fail setting invalid PYENV_ROOT/version" {
mkdir -p "$PYENV_ROOT"
run pyenv-global "1.2.3"
assert_failure "pyenv: version \`1.2.3' not installed"
}

115
test/help.bats Normal file
View file

@ -0,0 +1,115 @@
#!/usr/bin/env bats
load test_helper
@test "without args shows summary of common commands" {
run pyenv-help
assert_success
assert_line "Usage: pyenv <command> [<args>]"
assert_line "Some useful pyenv commands are:"
}
@test "invalid command" {
run pyenv-help hello
assert_failure "pyenv: no such command \`hello'"
}
@test "shows help for a specific command" {
mkdir -p "${PYENV_TEST_DIR}/bin"
cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" <<SH
#!shebang
# Usage: pyenv hello <world>
# Summary: Says "hello" to you, from pyenv
# This command is useful for saying hello.
echo hello
SH
run pyenv-help hello
assert_success
assert_output <<SH
Usage: pyenv hello <world>
This command is useful for saying hello.
SH
}
@test "replaces missing extended help with summary text" {
mkdir -p "${PYENV_TEST_DIR}/bin"
cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" <<SH
#!shebang
# Usage: pyenv hello <world>
# Summary: Says "hello" to you, from pyenv
echo hello
SH
run pyenv-help hello
assert_success
assert_output <<SH
Usage: pyenv hello <world>
Says "hello" to you, from pyenv
SH
}
@test "extracts only usage" {
mkdir -p "${PYENV_TEST_DIR}/bin"
cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" <<SH
#!shebang
# Usage: pyenv hello <world>
# Summary: Says "hello" to you, from pyenv
# This extended help won't be shown.
echo hello
SH
run pyenv-help --usage hello
assert_success "Usage: pyenv hello <world>"
}
@test "multiline usage section" {
mkdir -p "${PYENV_TEST_DIR}/bin"
cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" <<SH
#!shebang
# Usage: pyenv hello <world>
# pyenv hi [everybody]
# pyenv hola --translate
# Summary: Says "hello" to you, from pyenv
# Help text.
echo hello
SH
run pyenv-help hello
assert_success
assert_output <<SH
Usage: pyenv hello <world>
pyenv hi [everybody]
pyenv hola --translate
Help text.
SH
}
@test "multiline extended help section" {
mkdir -p "${PYENV_TEST_DIR}/bin"
cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" <<SH
#!shebang
# Usage: pyenv hello <world>
# Summary: Says "hello" to you, from pyenv
# This is extended help text.
# It can contain multiple lines.
#
# And paragraphs.
echo hello
SH
run pyenv-help hello
assert_success
assert_output <<SH
Usage: pyenv hello <world>
This is extended help text.
It can contain multiple lines.
And paragraphs.
SH
}

65
test/hooks.bats Normal file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env bats
load test_helper
create_hook() {
mkdir -p "$1/$2"
touch "$1/$2/$3"
}
@test "prints usage help given no argument" {
run pyenv-hooks
assert_failure "Usage: pyenv hooks <command>"
}
@test "prints list of hooks" {
path1="${PYENV_TEST_DIR}/pyenv.d"
path2="${PYENV_TEST_DIR}/etc/pyenv_hooks"
create_hook "$path1" exec "hello.bash"
create_hook "$path1" exec "ahoy.bash"
create_hook "$path1" exec "invalid.sh"
create_hook "$path1" which "boom.bash"
create_hook "$path2" exec "bueno.bash"
PYENV_HOOK_PATH="$path1:$path2" run pyenv-hooks exec
assert_success
assert_output <<OUT
${PYENV_TEST_DIR}/pyenv.d/exec/ahoy.bash
${PYENV_TEST_DIR}/pyenv.d/exec/hello.bash
${PYENV_TEST_DIR}/etc/pyenv_hooks/exec/bueno.bash
OUT
}
@test "supports hook paths with spaces" {
path1="${PYENV_TEST_DIR}/my hooks/pyenv.d"
path2="${PYENV_TEST_DIR}/etc/pyenv hooks"
create_hook "$path1" exec "hello.bash"
create_hook "$path2" exec "ahoy.bash"
PYENV_HOOK_PATH="$path1:$path2" run pyenv-hooks exec
assert_success
assert_output <<OUT
${PYENV_TEST_DIR}/my hooks/pyenv.d/exec/hello.bash
${PYENV_TEST_DIR}/etc/pyenv hooks/exec/ahoy.bash
OUT
}
@test "resolves relative paths" {
path="${PYENV_TEST_DIR}/pyenv.d"
create_hook "$path" exec "hello.bash"
mkdir -p "$HOME"
PYENV_HOOK_PATH="${HOME}/../pyenv.d" run pyenv-hooks exec
assert_success "${PYENV_TEST_DIR}/pyenv.d/exec/hello.bash"
}
@test "resolves symlinks" {
path="${PYENV_TEST_DIR}/pyenv.d"
mkdir -p "${path}/exec"
mkdir -p "$HOME"
touch "${HOME}/hola.bash"
ln -s "../../home/hola.bash" "${path}/exec/hello.bash"
PYENV_HOOK_PATH="$path" run pyenv-hooks exec
assert_success "${HOME}/hola.bash"
}

79
test/init.bats Normal file
View file

@ -0,0 +1,79 @@
#!/usr/bin/env bats
load test_helper
@test "creates shims and versions directories" {
assert [ ! -d "${PYENV_ROOT}/shims" ]
assert [ ! -d "${PYENV_ROOT}/versions" ]
run pyenv-init -
assert_success
assert [ -d "${PYENV_ROOT}/shims" ]
assert [ -d "${PYENV_ROOT}/versions" ]
}
@test "auto rehash" {
run pyenv-init -
assert_success
assert_line "pyenv rehash 2>/dev/null"
}
@test "setup shell completions" {
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
run pyenv-init - bash
assert_success
assert_line "source '${root}/libexec/../completions/pyenv.bash'"
}
@test "detect parent shell" {
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
SHELL=/bin/false run pyenv-init -
assert_success
assert_line "export PYENV_SHELL=bash"
}
@test "setup shell completions (fish)" {
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
run pyenv-init - fish
assert_success
assert_line ". '${root}/libexec/../completions/pyenv.fish'"
}
@test "fish instructions" {
run pyenv-init fish
assert [ "$status" -eq 1 ]
assert_line 'status --is-interactive; and . (pyenv init -|psub)'
}
@test "option to skip rehash" {
run pyenv-init - --no-rehash
assert_success
refute_line "pyenv rehash 2>/dev/null"
}
@test "adds shims to PATH" {
export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin"
run pyenv-init - bash
assert_success
assert_line 0 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"'
}
@test "adds shims to PATH (fish)" {
export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin"
run pyenv-init - fish
assert_success
assert_line 0 "setenv PATH '${PYENV_ROOT}/shims' \$PATH"
}
@test "doesn't add shims to PATH more than once" {
export PATH="${PYENV_ROOT}/shims:$PATH"
run pyenv-init - bash
assert_success
refute_line 'export PATH="'${PYENV_ROOT}'/shims:${PATH}"'
}
@test "doesn't add shims to PATH more than once (fish)" {
export PATH="${PYENV_ROOT}/shims:$PATH"
run pyenv-init - fish
assert_success
refute_line 'setenv PATH "'${PYENV_ROOT}'/shims" $PATH ;'
}

2
test/libexec/pyenv-echo Executable file
View file

@ -0,0 +1,2 @@
#!/usr/bin/env bash
eval "echo \$$1"

103
test/local.bats Normal file
View file

@ -0,0 +1,103 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "${PYENV_TEST_DIR}/myproject"
cd "${PYENV_TEST_DIR}/myproject"
}
@test "no version" {
assert [ ! -e "${PWD}/.python-version" ]
run pyenv-local
assert_failure "pyenv: no local version configured for this directory"
}
@test "local version" {
echo "1.2.3" > .python-version
run pyenv-local
assert_success "1.2.3"
}
@test "supports legacy .pyenv-version file" {
echo "1.2.3" > .pyenv-version
run pyenv-local
assert_success "1.2.3"
}
@test "local .python-version has precedence over .pyenv-version" {
echo "2.7" > .pyenv-version
echo "3.4" > .python-version
run pyenv-local
assert_success "3.4"
}
@test "ignores version in parent directory" {
echo "1.2.3" > .python-version
mkdir -p "subdir" && cd "subdir"
run pyenv-local
assert_failure
}
@test "ignores PYENV_DIR" {
echo "1.2.3" > .python-version
mkdir -p "$HOME"
echo "3.4-home" > "${HOME}/.python-version"
PYENV_DIR="$HOME" run pyenv-local
assert_success "1.2.3"
}
@test "sets local version" {
mkdir -p "${PYENV_ROOT}/versions/1.2.3"
run pyenv-local 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"
run pyenv-local
assert_success "1.0-pre"
run pyenv-local 1.2.3
assert_success ""
assert [ "$(cat .python-version)" = "1.2.3" ]
}
@test "renames .pyenv-version to .python-version" {
echo "2.7.6" > .pyenv-version
mkdir -p "${PYENV_ROOT}/versions/3.3.3"
run pyenv-local
assert_success "2.7.6"
run pyenv-local "3.3.3"
assert_success
assert_output <<OUT
pyenv: removed existing \`.pyenv-version' file and migrated
local version specification to \`.python-version' file
OUT
assert [ ! -e .pyenv-version ]
assert [ "$(cat .python-version)" = "3.3.3" ]
}
@test "doesn't rename .pyenv-version if changing the version failed" {
echo "2.7.6" > .pyenv-version
assert [ ! -e "${PYENV_ROOT}/versions/3.3.3" ]
run pyenv-local "3.3.3"
assert_failure "pyenv: version \`3.3.3' not installed"
assert [ ! -e .python-version ]
assert [ "$(cat .pyenv-version)" = "2.7.6" ]
}
@test "unsets local version" {
touch .python-version
run pyenv-local --unset
assert_success ""
assert [ ! -e .pyenv-version ]
}
@test "unsets alternate version file" {
touch .pyenv-version
run pyenv-local --unset
assert_success ""
assert [ ! -e .pyenv-version ]
}

39
test/prefix.bats Normal file
View file

@ -0,0 +1,39 @@
#!/usr/bin/env bats
load test_helper
@test "prefix" {
mkdir -p "${PYENV_TEST_DIR}/myproject"
cd "${PYENV_TEST_DIR}/myproject"
echo "1.2.3" > .python-version
mkdir -p "${PYENV_ROOT}/versions/1.2.3"
run pyenv-prefix
assert_success "${PYENV_ROOT}/versions/1.2.3"
}
@test "prefix for invalid version" {
PYENV_VERSION="1.2.3" run pyenv-prefix
assert_failure "pyenv: version \`1.2.3' not installed"
}
@test "prefix for system" {
mkdir -p "${PYENV_TEST_DIR}/bin"
touch "${PYENV_TEST_DIR}/bin/python"
chmod +x "${PYENV_TEST_DIR}/bin/python"
PYENV_VERSION="system" run pyenv-prefix
assert_success "$PYENV_TEST_DIR"
}
@test "prefix for invalid system" {
USRBIN_ALT="${PYENV_TEST_DIR}/usr-bin-alt"
mkdir -p "$USRBIN_ALT"
for util in head readlink greadlink; do
if [ -x "/usr/bin/$util" ]; then
ln -s "/usr/bin/$util" "${USRBIN_ALT}/$util"
fi
done
PATH_WITHOUT_PYTHON="${PATH/\/usr\/bin:/$USRBIN_ALT:}"
PATH="$PATH_WITHOUT_PYTHON" run pyenv-prefix system
assert_failure "pyenv: system version not found in PATH"
}

47
test/pyenv.bats Normal file
View file

@ -0,0 +1,47 @@
#!/usr/bin/env bats
load test_helper
@test "blank invocation" {
run pyenv
assert_success
assert [ "${lines[0]}" == "pyenv 0.4.0-20131217" ]
}
@test "invalid command" {
run pyenv does-not-exist
assert_failure
assert_output "pyenv: no such command \`does-not-exist'"
}
@test "default PYENV_ROOT" {
PYENV_ROOT="" HOME=/home/mislav run pyenv root
assert_success
assert_output "/home/mislav/.pyenv"
}
@test "inherited PYENV_ROOT" {
PYENV_ROOT=/opt/pyenv run pyenv root
assert_success
assert_output "/opt/pyenv"
}
@test "default PYENV_DIR" {
run pyenv echo PYENV_DIR
assert_output "$(pwd)"
}
@test "inherited PYENV_DIR" {
dir="${BATS_TMPDIR}/myproject"
mkdir -p "$dir"
PYENV_DIR="$dir" run pyenv echo PYENV_DIR
assert_output "$dir"
}
@test "invalid PYENV_DIR" {
dir="${BATS_TMPDIR}/does-not-exist"
assert [ ! -d "$dir" ]
PYENV_DIR="$dir" run pyenv echo PYENV_DIR
assert_failure
assert_output "pyenv: cannot change working directory to \`$dir'"
}

114
test/rehash.bats Executable file
View file

@ -0,0 +1,114 @@
#!/usr/bin/env bats
load test_helper
create_executable() {
local bin="${PYENV_ROOT}/versions/${1}/bin"
mkdir -p "$bin"
touch "${bin}/$2"
chmod +x "${bin}/$2"
}
@test "empty rehash" {
assert [ ! -d "${PYENV_ROOT}/shims" ]
run pyenv-rehash
assert_success ""
assert [ -d "${PYENV_ROOT}/shims" ]
rmdir "${PYENV_ROOT}/shims"
}
@test "non-writable shims directory" {
mkdir -p "${PYENV_ROOT}/shims"
chmod -w "${PYENV_ROOT}/shims"
run pyenv-rehash
assert_failure "pyenv: cannot rehash: ${PYENV_ROOT}/shims isn't writable"
}
@test "rehash in progress" {
mkdir -p "${PYENV_ROOT}/shims"
touch "${PYENV_ROOT}/shims/.pyenv-shim"
run pyenv-rehash
assert_failure "pyenv: cannot rehash: ${PYENV_ROOT}/shims/.pyenv-shim exists"
}
@test "creates shims" {
create_executable "2.7" "python"
create_executable "2.7" "fab"
create_executable "3.4" "python"
create_executable "3.4" "py.test"
assert [ ! -e "${PYENV_ROOT}/shims/fab" ]
assert [ ! -e "${PYENV_ROOT}/shims/python" ]
assert [ ! -e "${PYENV_ROOT}/shims/py.test" ]
run pyenv-rehash
assert_success ""
run ls "${PYENV_ROOT}/shims"
assert_success
assert_output <<OUT
fab
py.test
python
OUT
}
@test "removes stale shims" {
mkdir -p "${PYENV_ROOT}/shims"
touch "${PYENV_ROOT}/shims/oldshim1"
chmod +x "${PYENV_ROOT}/shims/oldshim1"
create_executable "3.4" "fab"
create_executable "3.4" "python"
run pyenv-rehash
assert_success ""
assert [ ! -e "${PYENV_ROOT}/shims/oldshim1" ]
}
@test "binary install locations containing spaces" {
create_executable "dirname1 p247" "python"
create_executable "dirname2 preview1" "py.test"
assert [ ! -e "${PYENV_ROOT}/shims/python" ]
assert [ ! -e "${PYENV_ROOT}/shims/py.test" ]
run pyenv-rehash
assert_success ""
run ls "${PYENV_ROOT}/shims"
assert_success
assert_output <<OUT
py.test
python
OUT
}
@test "carries original IFS within hooks" {
hook_path="${PYENV_TEST_DIR}/pyenv.d"
mkdir -p "${hook_path}/rehash"
cat > "${hook_path}/rehash/hello.bash" <<SH
hellos=(\$(printf "hello\\tugly world\\nagain"))
echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
exit
SH
PYENV_HOOK_PATH="$hook_path" IFS=$' \t\n' run pyenv-rehash
assert_success
assert_output "HELLO=:hello:ugly:world:again"
}
@test "sh-rehash in bash" {
create_executable "3.4" "python"
PYENV_SHELL=bash run pyenv-sh-rehash
assert_success "hash -r 2>/dev/null || true"
assert [ -x "${PYENV_ROOT}/shims/python" ]
}
@test "sh-rehash in fish" {
create_executable "3.4" "python"
PYENV_SHELL=fish run pyenv-sh-rehash
assert_success ""
assert [ -x "${PYENV_ROOT}/shims/python" ]
}

52
test/shell.bats Normal file
View file

@ -0,0 +1,52 @@
#!/usr/bin/env bats
load test_helper
@test "no shell version" {
mkdir -p "${PYENV_TEST_DIR}/myproject"
cd "${PYENV_TEST_DIR}/myproject"
echo "1.2.3" > .python-version
PYENV_VERSION="" run pyenv-sh-shell
assert_failure "pyenv: no shell-specific version configured"
}
@test "shell version" {
PYENV_SHELL=bash PYENV_VERSION="1.2.3" run pyenv-sh-shell
assert_success 'echo "$PYENV_VERSION"'
}
@test "shell version (fish)" {
PYENV_SHELL=fish PYENV_VERSION="1.2.3" run pyenv-sh-shell
assert_success 'echo "$PYENV_VERSION"'
}
@test "shell unset" {
PYENV_SHELL=bash run pyenv-sh-shell --unset
assert_success "unset PYENV_VERSION"
}
@test "shell unset (fish)" {
PYENV_SHELL=fish run pyenv-sh-shell --unset
assert_success "set -e PYENV_VERSION"
}
@test "shell change invalid version" {
run pyenv-sh-shell 1.2.3
assert_failure
assert_output <<SH
pyenv: version \`1.2.3' not installed
false
SH
}
@test "shell change version" {
mkdir -p "${PYENV_ROOT}/versions/1.2.3"
PYENV_SHELL=bash run pyenv-sh-shell 1.2.3
assert_success 'export PYENV_VERSION="1.2.3"'
}
@test "shell change version (fish)" {
mkdir -p "${PYENV_ROOT}/versions/1.2.3"
PYENV_SHELL=fish run pyenv-sh-shell 1.2.3
assert_success 'setenv PYENV_VERSION "1.2.3"'
}

29
test/shims.bats Normal file
View file

@ -0,0 +1,29 @@
#!/usr/bin/env bats
load test_helper
@test "no shims" {
run pyenv-shims
assert_success
assert [ -z "$output" ]
}
@test "shims" {
mkdir -p "${PYENV_ROOT}/shims"
touch "${PYENV_ROOT}/shims/python"
touch "${PYENV_ROOT}/shims/irb"
run pyenv-shims
assert_success
assert_line "${PYENV_ROOT}/shims/python"
assert_line "${PYENV_ROOT}/shims/irb"
}
@test "shims --short" {
mkdir -p "${PYENV_ROOT}/shims"
touch "${PYENV_ROOT}/shims/python"
touch "${PYENV_ROOT}/shims/irb"
run pyenv-shims --short
assert_success
assert_line "irb"
assert_line "python"
}

95
test/test_helper.bash Normal file
View file

@ -0,0 +1,95 @@
unset PYENV_VERSION
unset PYENV_DIR
PYENV_TEST_DIR="${BATS_TMPDIR}/pyenv"
# guard against executing this block twice due to bats internals
if [ "$PYENV_ROOT" != "${PYENV_TEST_DIR}/root" ]; then
export PYENV_ROOT="${PYENV_TEST_DIR}/root"
export HOME="${PYENV_TEST_DIR}/home"
PATH=/usr/bin:/bin:/usr/sbin:/sbin
PATH="${PYENV_TEST_DIR}/bin:$PATH"
PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH"
PATH="${BATS_TEST_DIRNAME}/libexec:$PATH"
PATH="${PYENV_ROOT}/shims:$PATH"
export PATH
fi
teardown() {
rm -rf "$PYENV_TEST_DIR"
}
flunk() {
{ if [ "$#" -eq 0 ]; then cat -
else echo "$@"
fi
} | sed "s:${PYENV_TEST_DIR}:TEST_DIR:g" >&2
return 1
}
assert_success() {
if [ "$status" -ne 0 ]; then
flunk "command failed with exit status $status"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_failure() {
if [ "$status" -eq 0 ]; then
flunk "expected failed exit status"
elif [ "$#" -gt 0 ]; then
assert_output "$1"
fi
}
assert_equal() {
if [ "$1" != "$2" ]; then
{ echo "expected: $1"
echo "actual: $2"
} | flunk
fi
}
assert_output() {
local expected
if [ $# -eq 0 ]; then expected="$(cat -)"
else expected="$1"
fi
assert_equal "$expected" "$output"
}
assert_line() {
if [ "$1" -ge 0 ] 2>/dev/null; then
assert_equal "$2" "${lines[$1]}"
else
local line
for line in "${lines[@]}"; do
if [ "$line" = "$1" ]; then return 0; fi
done
flunk "expected line \`$1'"
fi
}
refute_line() {
if [ "$1" -ge 0 ] 2>/dev/null; then
local num_lines="${#lines[@]}"
if [ "$1" -lt "$num_lines" ]; then
flunk "output has $num_lines lines"
fi
else
local line
for line in "${lines[@]}"; do
if [ "$line" = "$1" ]; then
flunk "expected to not find line \`$line'"
fi
done
fi
}
assert() {
if ! "$@"; then
flunk "failed: $@"
fi
}

View file

@ -0,0 +1,66 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "${PYENV_TEST_DIR}/myproject"
cd "${PYENV_TEST_DIR}/myproject"
}
@test "fails without arguments" {
run pyenv-version-file-read
assert_failure ""
}
@test "fails for invalid file" {
run pyenv-version-file-read "non-existent"
assert_failure ""
}
@test "fails for blank file" {
echo > my-version
run pyenv-version-file-read my-version
assert_failure ""
}
@test "reads simple version file" {
cat > my-version <<<"3.3.3"
run pyenv-version-file-read my-version
assert_success "3.3.3"
}
@test "ignores leading spaces" {
cat > my-version <<<" 3.3.3"
run pyenv-version-file-read my-version
assert_success "3.3.3"
}
@test "reads only the first word from file" {
cat > my-version <<<"3.3.3-p194@tag 2.7.6 hi"
run pyenv-version-file-read my-version
assert_success "3.3.3-p194@tag:2.7.6:hi"
}
@test "loads only the first line in file" {
cat > my-version <<IN
2.7.6 one
3.3.3 two
IN
run pyenv-version-file-read my-version
assert_success "2.7.6:one:3.3.3:two"
}
@test "ignores leading blank lines" {
cat > my-version <<IN
3.3.3
IN
run pyenv-version-file-read my-version
assert_success "3.3.3"
}
@test "handles the file with no trailing newline" {
echo -n "2.7.6" > my-version
run pyenv-version-file-read my-version
assert_success "2.7.6"
}

View file

@ -0,0 +1,30 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}
@test "invocation without 2 arguments prints usage" {
run pyenv-version-file-write
assert_failure "Usage: pyenv version-file-write <file> <version>"
run pyenv-version-file-write "one" ""
assert_failure
}
@test "setting nonexistent version fails" {
assert [ ! -e ".python-version" ]
run pyenv-version-file-write ".python-version" "2.7.6"
assert_failure "pyenv: version \`2.7.6' not installed"
assert [ ! -e ".python-version" ]
}
@test "writes value to arbitrary file" {
mkdir -p "${PYENV_ROOT}/versions/2.7.6"
assert [ ! -e "my-version" ]
run pyenv-version-file-write "${PWD}/my-version" "2.7.6"
assert_success ""
assert [ "$(cat my-version)" = "2.7.6" ]
}

99
test/version-file.bats Normal file
View file

@ -0,0 +1,99 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}
create_file() {
mkdir -p "$(dirname "$1")"
touch "$1"
}
@test "prints global file if no version files exist" {
assert [ ! -e "${PYENV_ROOT}/version" ]
assert [ ! -e ".python-version" ]
run pyenv-version-file
assert_success "${PYENV_ROOT}/version"
}
@test "detects 'global' file" {
create_file "${PYENV_ROOT}/global"
run pyenv-version-file
assert_success "${PYENV_ROOT}/global"
}
@test "detects 'default' file" {
create_file "${PYENV_ROOT}/default"
run pyenv-version-file
assert_success "${PYENV_ROOT}/default"
}
@test "'version' has precedence over 'global' and 'default'" {
create_file "${PYENV_ROOT}/version"
create_file "${PYENV_ROOT}/global"
create_file "${PYENV_ROOT}/default"
run pyenv-version-file
assert_success "${PYENV_ROOT}/version"
}
@test "in current directory" {
create_file ".python-version"
run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/.python-version"
}
@test "legacy file in current directory" {
create_file ".pyenv-version"
run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/.pyenv-version"
}
@test ".python-version has precedence over legacy file" {
create_file ".python-version"
create_file ".pyenv-version"
run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/.python-version"
}
@test "in parent directory" {
create_file ".python-version"
mkdir -p project
cd project
run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/.python-version"
}
@test "topmost file has precedence" {
create_file ".python-version"
create_file "project/.python-version"
cd project
run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/project/.python-version"
}
@test "legacy file has precedence if higher" {
create_file ".python-version"
create_file "project/.pyenv-version"
cd project
run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/project/.pyenv-version"
}
@test "PYENV_DIR has precedence over PWD" {
create_file "widget/.python-version"
create_file "project/.python-version"
cd project
PYENV_DIR="${PYENV_TEST_DIR}/widget" run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/widget/.python-version"
}
@test "PWD is searched if PYENV_DIR yields no results" {
mkdir -p "widget/blank"
create_file "project/.python-version"
cd project
PYENV_DIR="${PYENV_TEST_DIR}/widget/blank" run pyenv-version-file
assert_success "${PYENV_TEST_DIR}/project/.python-version"
}

65
test/version-name.bats Normal file
View file

@ -0,0 +1,65 @@
#!/usr/bin/env bats
load test_helper
create_version() {
mkdir -p "${PYENV_ROOT}/versions/$1"
}
setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}
@test "no version selected" {
assert [ ! -d "${PYENV_ROOT}/versions" ]
run pyenv-version-name
assert_success "system"
}
@test "system version is not checked for existance" {
PYENV_VERSION=system run pyenv-version-name
assert_success "system"
}
@test "PYENV_VERSION has precedence over local" {
create_version "2.7.6"
create_version "3.3.3"
cat > ".python-version" <<<"2.7.6"
run pyenv-version-name
assert_success "2.7.6"
PYENV_VERSION=3.3.3 run pyenv-version-name
assert_success "3.3.3"
}
@test "local file has precedence over global" {
create_version "2.7.6"
create_version "3.3.3"
cat > "${PYENV_ROOT}/version" <<<"2.7.6"
run pyenv-version-name
assert_success "2.7.6"
cat > ".python-version" <<<"3.3.3"
run pyenv-version-name
assert_success "3.3.3"
}
@test "missing version" {
PYENV_VERSION=1.2 run pyenv-version-name
assert_failure "pyenv: version \`1.2' is not installed"
}
@test "version with prefix in name" {
create_version "2.7.6"
cat > ".python-version" <<<"python-2.7.6"
run pyenv-version-name
assert_success
assert_output <<OUT
warning: ignoring extraneous \`python-' prefix in version \`python-2.7.6'
(set by ${PWD}/.python-version)
2.7.6
OUT
}

38
test/version-origin.bats Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}
@test "reports global file even if it doesn't exist" {
assert [ ! -e "${PYENV_ROOT}/version" ]
run pyenv-version-origin
assert_success "${PYENV_ROOT}/version"
}
@test "detects global file" {
mkdir -p "$PYENV_ROOT"
touch "${PYENV_ROOT}/version"
run pyenv-version-origin
assert_success "${PYENV_ROOT}/version"
}
@test "detects PYENV_VERSION" {
PYENV_VERSION=1 run pyenv-version-origin
assert_success "PYENV_VERSION environment variable"
}
@test "detects local file" {
touch .python-version
run pyenv-version-origin
assert_success "${PWD}/.python-version"
}
@test "detects alternate version file" {
touch .pyenv-version
run pyenv-version-origin
assert_success "${PWD}/.pyenv-version"
}

38
test/version.bats Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env bats
load test_helper
create_version() {
mkdir -p "${PYENV_ROOT}/versions/$1"
}
setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}
@test "no version selected" {
assert [ ! -d "${PYENV_ROOT}/versions" ]
run pyenv-version
assert_success "system (set by ${PYENV_ROOT}/version)"
}
@test "set by PYENV_VERSION" {
create_version "3.3.3"
PYENV_VERSION=3.3.3 run pyenv-version
assert_success "3.3.3 (set by PYENV_VERSION environment variable)"
}
@test "set by local file" {
create_version "3.3.3"
cat > ".python-version" <<<"3.3.3"
run pyenv-version
assert_success "3.3.3 (set by ${PWD}/.python-version)"
}
@test "set by global file" {
create_version "3.3.3"
cat > "${PYENV_ROOT}/version" <<<"3.3.3"
run pyenv-version
assert_success "3.3.3 (set by ${PYENV_ROOT}/version)"
}

115
test/versions.bats Normal file
View file

@ -0,0 +1,115 @@
#!/usr/bin/env bats
load test_helper
create_version() {
mkdir -p "${PYENV_ROOT}/versions/$1"
}
setup() {
mkdir -p "$PYENV_TEST_DIR"
cd "$PYENV_TEST_DIR"
}
stub_system_python() {
local stub="${PYENV_TEST_DIR}/bin/python"
mkdir -p "$(dirname "$stub")"
touch "$stub" && chmod +x "$stub"
}
@test "no versions installed" {
stub_system_python
assert [ ! -d "${PYENV_ROOT}/versions" ]
run pyenv-versions
assert_success "* system (set by ${PYENV_ROOT}/version)"
}
@test "bare output no versions installed" {
assert [ ! -d "${PYENV_ROOT}/versions" ]
run pyenv-versions --bare
assert_success ""
}
@test "single version installed" {
stub_system_python
create_version "3.3"
run pyenv-versions
assert_success
assert_output <<OUT
* system (set by ${PYENV_ROOT}/version)
3.3
OUT
}
@test "single version bare" {
create_version "3.3"
run pyenv-versions --bare
assert_success "3.3"
}
@test "multiple versions" {
stub_system_python
create_version "2.7.6"
create_version "3.3.3"
create_version "3.4.0"
run pyenv-versions
assert_success
assert_output <<OUT
* system (set by ${PYENV_ROOT}/version)
2.7.6
3.3.3
3.4.0
OUT
}
@test "indicates current version" {
stub_system_python
create_version "3.3.3"
create_version "3.4.0"
PYENV_VERSION=3.3.3 run pyenv-versions
assert_success
assert_output <<OUT
system
* 3.3.3 (set by PYENV_VERSION environment variable)
3.4.0
OUT
}
@test "bare doesn't indicate current version" {
create_version "3.3.3"
create_version "3.4.0"
PYENV_VERSION=3.3.3 run pyenv-versions --bare
assert_success
assert_output <<OUT
3.3.3
3.4.0
OUT
}
@test "globally selected version" {
stub_system_python
create_version "3.3.3"
create_version "3.4.0"
cat > "${PYENV_ROOT}/version" <<<"3.3.3"
run pyenv-versions
assert_success
assert_output <<OUT
system
* 3.3.3 (set by ${PYENV_ROOT}/version)
3.4.0
OUT
}
@test "per-project version" {
stub_system_python
create_version "3.3.3"
create_version "3.4.0"
cat > ".python-version" <<<"3.3.3"
run pyenv-versions
assert_success
assert_output <<OUT
system
* 3.3.3 (set by ${PYENV_TEST_DIR}/.python-version)
3.4.0
OUT
}

30
test/whence.bats Normal file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env bats
load test_helper
create_executable() {
local bin="${PYENV_ROOT}/versions/${1}/bin"
mkdir -p "$bin"
touch "${bin}/$2"
chmod +x "${bin}/$2"
}
@test "finds versions where present" {
create_executable "2.7" "python"
create_executable "2.7" "fab"
create_executable "3.4" "python"
create_executable "3.4" "py.test"
run pyenv-whence python
assert_success
assert_output <<OUT
2.7
3.4
OUT
run pyenv-whence fab
assert_success "2.7"
run pyenv-whence py.test
assert_success "3.4"
}

74
test/which.bats Normal file
View file

@ -0,0 +1,74 @@
#!/usr/bin/env bats
load test_helper
create_executable() {
local bin
if [[ $1 == */* ]]; then bin="$1"
else bin="${PYENV_ROOT}/versions/${1}/bin"
fi
mkdir -p "$bin"
touch "${bin}/$2"
chmod +x "${bin}/$2"
}
@test "outputs path to executable" {
create_executable "2.7" "python"
create_executable "3.4" "py.test"
PYENV_VERSION=2.7 run pyenv-which python
assert_success "${PYENV_ROOT}/versions/2.7/bin/python"
PYENV_VERSION=3.4 run pyenv-which py.test
assert_success "${PYENV_ROOT}/versions/3.4/bin/py.test"
}
@test "searches PATH for system version" {
create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
PYENV_VERSION=system run pyenv-which kill-all-humans
assert_success "${PYENV_TEST_DIR}/bin/kill-all-humans"
}
@test "version not installed" {
create_executable "3.4" "py.test"
PYENV_VERSION=3.3 run pyenv-which py.test
assert_failure "pyenv: version \`3.3' is not installed"
}
@test "no executable found" {
create_executable "2.7" "py.test"
PYENV_VERSION=2.7 run pyenv-which fab
assert_failure "pyenv: fab: command not found"
}
@test "executable found in other versions" {
create_executable "2.7" "python"
create_executable "3.3" "py.test"
create_executable "3.4" "py.test"
PYENV_VERSION=2.7 run pyenv-which py.test
assert_failure
assert_output <<OUT
pyenv: py.test: command not found
The \`py.test' command exists in these Python versions:
3.3
3.4
OUT
}
@test "carries original IFS within hooks" {
hook_path="${PYENV_TEST_DIR}/pyenv.d"
mkdir -p "${hook_path}/which"
cat > "${hook_path}/which/hello.bash" <<SH
hellos=(\$(printf "hello\\tugly world\\nagain"))
echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
exit
SH
PYENV_HOOK_PATH="$hook_path" IFS=$' \t\n' run pyenv-which anything
assert_success
assert_output "HELLO=:hello:ugly:world:again"
}