Import tests from rbenv with sed -e s/rbenv/pyenv/g

This commit is contained in:
Yamashita Yuu 2014-01-03 01:48:22 +09:00
parent f538169992
commit 0965577b93
26 changed files with 1574 additions and 0 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

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="2.0"
run pyenv-exec python -v
assert_failure "pyenv: version \`2.0' is not installed"
}
@test "completes with names of executables" {
export PYENV_VERSION="2.0"
create_executable "python" "#!/bin/sh"
create_executable "rake" "#!/bin/sh"
pyenv-rehash
run pyenv-completions exec
assert_success
assert_output <<OUT
rake
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="2.0"
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/2.0/bin/python
-w
/path to/python script.rb
--
extra
args
OUT
}
@test "supports python -S <cmd>" {
export PYENV_VERSION="2.0"
# 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 2.0 (pyenv test)'
fi
SH
create_executable "rake" <<SH
#!/usr/bin/env python
echo hello rake
SH
pyenv-rehash
run python -S rake
assert_success "hello rake"
}

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/rbenv-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 "1.8" > .pyenv-version
echo "2.0" > .python-version
run pyenv-local
assert_success "2.0"
}
@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 "2.0-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 "1.8.7" > .pyenv-version
mkdir -p "${PYENV_ROOT}/versions/1.9.3"
run pyenv-local
assert_success "1.8.7"
run pyenv-local "1.9.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)" = "1.9.3" ]
}
@test "doesn't rename .pyenv-version if changing the version failed" {
echo "1.8.7" > .pyenv-version
assert [ ! -e "${PYENV_ROOT}/versions/1.9.3" ]
run pyenv-local "1.9.3"
assert_failure "pyenv: version \`1.9.3' not installed"
assert [ ! -e .python-version ]
assert [ "$(cat .pyenv-version)" = "1.8.7" ]
}
@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/rbenv.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" ]
}
@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 "1.8" "python"
create_executable "1.8" "rake"
create_executable "2.0" "python"
create_executable "2.0" "rspec"
assert [ ! -e "${PYENV_ROOT}/shims/python" ]
assert [ ! -e "${PYENV_ROOT}/shims/rake" ]
assert [ ! -e "${PYENV_ROOT}/shims/rspec" ]
run pyenv-rehash
assert_success ""
run ls "${PYENV_ROOT}/shims"
assert_success
assert_output <<OUT
rake
rspec
python
OUT
}
@test "removes stale shims" {
mkdir -p "${PYENV_ROOT}/shims"
touch "${PYENV_ROOT}/shims/oldshim1"
chmod +x "${PYENV_ROOT}/shims/oldshim1"
create_executable "2.0" "rake"
create_executable "2.0" "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" "rspec"
assert [ ! -e "${PYENV_ROOT}/shims/python" ]
assert [ ! -e "${PYENV_ROOT}/shims/rspec" ]
run pyenv-rehash
assert_success ""
run ls "${PYENV_ROOT}/shims"
assert_success
assert_output <<OUT
rspec
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 "2.0" "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 "2.0" "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 <<<"1.9.3"
run pyenv-version-file-read my-version
assert_success "1.9.3"
}
@test "ignores leading spaces" {
cat > my-version <<<" 1.9.3"
run pyenv-version-file-read my-version
assert_success "1.9.3"
}
@test "reads only the first word from file" {
cat > my-version <<<"1.9.3-p194@tag 1.8.7 hi"
run pyenv-version-file-read my-version
assert_success "1.9.3-p194@tag"
}
@test "loads only the first line in file" {
cat > my-version <<IN
1.8.7 one
1.9.3 two
IN
run pyenv-version-file-read my-version
assert_success "1.8.7"
}
@test "ignores leading blank lines" {
cat > my-version <<IN
1.9.3
IN
run pyenv-version-file-read my-version
assert_success "1.9.3"
}
@test "handles the file with no trailing newline" {
echo -n "1.8.7" > my-version
run pyenv-version-file-read my-version
assert_success "1.8.7"
}

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" "1.8.7"
assert_failure "pyenv: version \`1.8.7' not installed"
assert [ ! -e ".python-version" ]
}
@test "writes value to arbitrary file" {
mkdir -p "${PYENV_ROOT}/versions/1.8.7"
assert [ ! -e "my-version" ]
run pyenv-version-file-write "${PWD}/my-version" "1.8.7"
assert_success ""
assert [ "$(cat my-version)" = "1.8.7" ]
}

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 "1.8.7"
create_version "1.9.3"
cat > ".python-version" <<<"1.8.7"
run pyenv-version-name
assert_success "1.8.7"
PYENV_VERSION=1.9.3 run pyenv-version-name
assert_success "1.9.3"
}
@test "local file has precedence over global" {
create_version "1.8.7"
create_version "1.9.3"
cat > "${PYENV_ROOT}/version" <<<"1.8.7"
run pyenv-version-name
assert_success "1.8.7"
cat > ".python-version" <<<"1.9.3"
run pyenv-version-name
assert_success "1.9.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 "1.8.7"
cat > ".python-version" <<<"python-1.8.7"
run pyenv-version-name
assert_success
assert_output <<OUT
warning: ignoring extraneous \`python-' prefix in version \`python-1.8.7'
(set by ${PWD}/.python-version)
1.8.7
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 "1.9.3"
PYENV_VERSION=1.9.3 run pyenv-version
assert_success "1.9.3 (set by PYENV_VERSION environment variable)"
}
@test "set by local file" {
create_version "1.9.3"
cat > ".python-version" <<<"1.9.3"
run pyenv-version
assert_success "1.9.3 (set by ${PWD}/.python-version)"
}
@test "set by global file" {
create_version "1.9.3"
cat > "${PYENV_ROOT}/version" <<<"1.9.3"
run pyenv-version
assert_success "1.9.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 "1.9"
run pyenv-versions
assert_success
assert_output <<OUT
* system (set by ${PYENV_ROOT}/version)
1.9
OUT
}
@test "single version bare" {
create_version "1.9"
run pyenv-versions --bare
assert_success "1.9"
}
@test "multiple versions" {
stub_system_python
create_version "1.8.7"
create_version "1.9.3"
create_version "2.0.0"
run pyenv-versions
assert_success
assert_output <<OUT
* system (set by ${PYENV_ROOT}/version)
1.8.7
1.9.3
2.0.0
OUT
}
@test "indicates current version" {
stub_system_python
create_version "1.9.3"
create_version "2.0.0"
PYENV_VERSION=1.9.3 run pyenv-versions
assert_success
assert_output <<OUT
system
* 1.9.3 (set by PYENV_VERSION environment variable)
2.0.0
OUT
}
@test "bare doesn't indicate current version" {
create_version "1.9.3"
create_version "2.0.0"
PYENV_VERSION=1.9.3 run pyenv-versions --bare
assert_success
assert_output <<OUT
1.9.3
2.0.0
OUT
}
@test "globally selected version" {
stub_system_python
create_version "1.9.3"
create_version "2.0.0"
cat > "${PYENV_ROOT}/version" <<<"1.9.3"
run pyenv-versions
assert_success
assert_output <<OUT
system
* 1.9.3 (set by ${PYENV_ROOT}/version)
2.0.0
OUT
}
@test "per-project version" {
stub_system_python
create_version "1.9.3"
create_version "2.0.0"
cat > ".python-version" <<<"1.9.3"
run pyenv-versions
assert_success
assert_output <<OUT
system
* 1.9.3 (set by ${PYENV_TEST_DIR}/.python-version)
2.0.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 "1.8" "python"
create_executable "1.8" "rake"
create_executable "2.0" "python"
create_executable "2.0" "rspec"
run pyenv-whence python
assert_success
assert_output <<OUT
1.8
2.0
OUT
run pyenv-whence rake
assert_success "1.8"
run pyenv-whence rspec
assert_success "2.0"
}

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 "1.8" "python"
create_executable "2.0" "rspec"
PYENV_VERSION=1.8 run pyenv-which python
assert_success "${PYENV_ROOT}/versions/1.8/bin/python"
PYENV_VERSION=2.0 run pyenv-which rspec
assert_success "${PYENV_ROOT}/versions/2.0/bin/rspec"
}
@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 "2.0" "rspec"
PYENV_VERSION=1.9 run pyenv-which rspec
assert_failure "pyenv: version \`1.9' is not installed"
}
@test "no executable found" {
create_executable "1.8" "rspec"
PYENV_VERSION=1.8 run pyenv-which rake
assert_failure "pyenv: rake: command not found"
}
@test "executable found in other versions" {
create_executable "1.8" "python"
create_executable "1.9" "rspec"
create_executable "2.0" "rspec"
PYENV_VERSION=1.8 run pyenv-which rspec
assert_failure
assert_output <<OUT
pyenv: rspec: command not found
The \`rspec' command exists in these Python versions:
1.9
2.0
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"
}