From 0965577b931b1e83b4e264440321248ce66e4d0e Mon Sep 17 00:00:00 2001 From: Yamashita Yuu Date: Fri, 3 Jan 2014 01:48:22 +0900 Subject: [PATCH] Import tests from rbenv with `sed -e s/rbenv/pyenv/g` --- .travis.yml | 4 ++ test/--version.bats | 46 ++++++++++++++ test/commands.bats | 39 ++++++++++++ test/completions.bats | 46 ++++++++++++++ test/exec.bats | 113 ++++++++++++++++++++++++++++++++++ test/global.bats | 31 ++++++++++ test/help.bats | 115 +++++++++++++++++++++++++++++++++++ test/hooks.bats | 65 ++++++++++++++++++++ test/init.bats | 79 ++++++++++++++++++++++++ test/libexec/rbenv-echo | 2 + test/local.bats | 103 +++++++++++++++++++++++++++++++ test/prefix.bats | 39 ++++++++++++ test/rbenv.bats | 47 ++++++++++++++ test/rehash.bats | 114 ++++++++++++++++++++++++++++++++++ test/shell.bats | 52 ++++++++++++++++ test/shims.bats | 29 +++++++++ test/test_helper.bash | 95 +++++++++++++++++++++++++++++ test/version-file-read.bats | 66 ++++++++++++++++++++ test/version-file-write.bats | 30 +++++++++ test/version-file.bats | 99 ++++++++++++++++++++++++++++++ test/version-name.bats | 65 ++++++++++++++++++++ test/version-origin.bats | 38 ++++++++++++ test/version.bats | 38 ++++++++++++ test/versions.bats | 115 +++++++++++++++++++++++++++++++++++ test/whence.bats | 30 +++++++++ test/which.bats | 74 ++++++++++++++++++++++ 26 files changed, 1574 insertions(+) create mode 100644 .travis.yml create mode 100644 test/--version.bats create mode 100644 test/commands.bats create mode 100644 test/completions.bats create mode 100644 test/exec.bats create mode 100644 test/global.bats create mode 100644 test/help.bats create mode 100644 test/hooks.bats create mode 100644 test/init.bats create mode 100755 test/libexec/rbenv-echo create mode 100644 test/local.bats create mode 100644 test/prefix.bats create mode 100644 test/rbenv.bats create mode 100755 test/rehash.bats create mode 100644 test/shell.bats create mode 100644 test/shims.bats create mode 100644 test/test_helper.bash create mode 100644 test/version-file-read.bats create mode 100644 test/version-file-write.bats create mode 100644 test/version-file.bats create mode 100644 test/version-name.bats create mode 100644 test/version-origin.bats create mode 100644 test/version.bats create mode 100644 test/versions.bats create mode 100644 test/whence.bats create mode 100644 test/which.bats diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..ef43ae71 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/test/--version.bats b/test/--version.bats new file mode 100644 index 00000000..e035211a --- /dev/null +++ b/test/--version.bats @@ -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."* ]] +} diff --git a/test/commands.bats b/test/commands.bats new file mode 100644 index 00000000..b0664a6f --- /dev/null +++ b/test/commands.bats @@ -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" +} diff --git a/test/completions.bats b/test/completions.bats new file mode 100644 index 00000000..8012a98d --- /dev/null +++ b/test/completions.bats @@ -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 < "${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 < "${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" <" { + export PYENV_VERSION="2.0" + + # emulate `python -S' behavior + create_executable "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" < "$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" +} diff --git a/test/help.bats b/test/help.bats new file mode 100644 index 00000000..db178c2b --- /dev/null +++ b/test/help.bats @@ -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 []" + 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" < +# 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 < + +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" < +# Summary: Says "hello" to you, from pyenv +echo hello +SH + + run pyenv-help hello + assert_success + assert_output < + +Says "hello" to you, from pyenv +SH +} + +@test "extracts only usage" { + mkdir -p "${PYENV_TEST_DIR}/bin" + cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" < +# 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 " +} + +@test "multiline usage section" { + mkdir -p "${PYENV_TEST_DIR}/bin" + cat > "${PYENV_TEST_DIR}/bin/pyenv-hello" < +# 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 < + 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" < +# 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 < + +This is extended help text. +It can contain multiple lines. + +And paragraphs. +SH +} diff --git a/test/hooks.bats b/test/hooks.bats new file mode 100644 index 00000000..46583aee --- /dev/null +++ b/test/hooks.bats @@ -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 " +} + +@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 </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 ;' +} diff --git a/test/libexec/rbenv-echo b/test/libexec/rbenv-echo new file mode 100755 index 00000000..0a802df4 --- /dev/null +++ b/test/libexec/rbenv-echo @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +eval "echo \$$1" diff --git a/test/local.bats b/test/local.bats new file mode 100644 index 00000000..d9901822 --- /dev/null +++ b/test/local.bats @@ -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 < .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 ] +} diff --git a/test/prefix.bats b/test/prefix.bats new file mode 100644 index 00000000..1245ceeb --- /dev/null +++ b/test/prefix.bats @@ -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" +} diff --git a/test/rbenv.bats b/test/rbenv.bats new file mode 100644 index 00000000..e16b636d --- /dev/null +++ b/test/rbenv.bats @@ -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'" +} diff --git a/test/rehash.bats b/test/rehash.bats new file mode 100755 index 00000000..fbe70f83 --- /dev/null +++ b/test/rehash.bats @@ -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 < "${hook_path}/rehash/hello.bash" </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" ] +} diff --git a/test/shell.bats b/test/shell.bats new file mode 100644 index 00000000..30bf127b --- /dev/null +++ b/test/shell.bats @@ -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 <&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 +} diff --git a/test/version-file-read.bats b/test/version-file-read.bats new file mode 100644 index 00000000..c9d19e51 --- /dev/null +++ b/test/version-file-read.bats @@ -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 < my-version < my-version + run pyenv-version-file-read my-version + assert_success "1.8.7" +} diff --git a/test/version-file-write.bats b/test/version-file-write.bats new file mode 100644 index 00000000..be23bbb4 --- /dev/null +++ b/test/version-file-write.bats @@ -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 " + 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" ] +} diff --git a/test/version-file.bats b/test/version-file.bats new file mode 100644 index 00000000..8ecf5b8a --- /dev/null +++ b/test/version-file.bats @@ -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" +} diff --git a/test/version-name.bats b/test/version-name.bats new file mode 100644 index 00000000..efbaa347 --- /dev/null +++ b/test/version-name.bats @@ -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 < ".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)" +} diff --git a/test/versions.bats b/test/versions.bats new file mode 100644 index 00000000..44fad1de --- /dev/null +++ b/test/versions.bats @@ -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 < "${PYENV_ROOT}/version" <<<"1.9.3" + run pyenv-versions + assert_success + assert_output < ".python-version" <<<"1.9.3" + run pyenv-versions + assert_success + assert_output < "${hook_path}/which/hello.bash" <