You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

  1. #!/usr/bin/env bats
  2. load test_helper
  3. create_executable() {
  4. local bin
  5. if [[ $1 == */* ]]; then bin="$1"
  6. else bin="${PYENV_ROOT}/versions/${1}/bin"
  7. fi
  8. mkdir -p "$bin"
  9. touch "${bin}/$2"
  10. chmod +x "${bin}/$2"
  11. }
  12. @test "outputs path to executable" {
  13. create_executable "2.7" "python"
  14. create_executable "3.4" "py.test"
  15. PYENV_VERSION=2.7 run pyenv-which python
  16. assert_success "${PYENV_ROOT}/versions/2.7/bin/python"
  17. PYENV_VERSION=3.4 run pyenv-which py.test
  18. assert_success "${PYENV_ROOT}/versions/3.4/bin/py.test"
  19. PYENV_VERSION=3.4:2.7 run pyenv-which py.test
  20. assert_success "${PYENV_ROOT}/versions/3.4/bin/py.test"
  21. }
  22. @test "searches PATH for system version" {
  23. create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
  24. create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
  25. PYENV_VERSION=system run pyenv-which kill-all-humans
  26. assert_success "${PYENV_TEST_DIR}/bin/kill-all-humans"
  27. }
  28. @test "searches PATH for system version (shims prepended)" {
  29. create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
  30. create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
  31. PATH="${PYENV_ROOT}/shims:$PATH" PYENV_VERSION=system run pyenv-which kill-all-humans
  32. assert_success "${PYENV_TEST_DIR}/bin/kill-all-humans"
  33. }
  34. @test "searches PATH for system version (shims appended)" {
  35. create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
  36. create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
  37. PATH="$PATH:${PYENV_ROOT}/shims" PYENV_VERSION=system run pyenv-which kill-all-humans
  38. assert_success "${PYENV_TEST_DIR}/bin/kill-all-humans"
  39. }
  40. @test "searches PATH for system version (shims spread)" {
  41. create_executable "${PYENV_TEST_DIR}/bin" "kill-all-humans"
  42. create_executable "${PYENV_ROOT}/shims" "kill-all-humans"
  43. PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/shims:/tmp/non-existent:$PATH:${PYENV_ROOT}/shims" \
  44. PYENV_VERSION=system run pyenv-which kill-all-humans
  45. assert_success "${PYENV_TEST_DIR}/bin/kill-all-humans"
  46. }
  47. @test "version not installed" {
  48. create_executable "3.4" "py.test"
  49. PYENV_VERSION=3.3 run pyenv-which py.test
  50. assert_failure "pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)"
  51. }
  52. @test "versions not installed" {
  53. create_executable "3.4" "py.test"
  54. PYENV_VERSION=2.7:3.3 run pyenv-which py.test
  55. assert_failure <<OUT
  56. pyenv: version \`2.7' is not installed (set by PYENV_VERSION environment variable)
  57. pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)
  58. OUT
  59. }
  60. @test "no executable found" {
  61. create_executable "2.7" "py.test"
  62. PYENV_VERSION=2.7 run pyenv-which fab
  63. assert_failure "pyenv: fab: command not found"
  64. }
  65. @test "no executable found for system version" {
  66. export PATH="$(path_without "rake")"
  67. PYENV_VERSION=system run pyenv-which rake
  68. assert_failure "pyenv: rake: command not found"
  69. }
  70. @test "executable found in other versions" {
  71. create_executable "2.7" "python"
  72. create_executable "3.3" "py.test"
  73. create_executable "3.4" "py.test"
  74. PYENV_VERSION=2.7 run pyenv-which py.test
  75. assert_failure
  76. assert_output <<OUT
  77. pyenv: py.test: command not found
  78. The \`py.test' command exists in these Python versions:
  79. 3.3
  80. 3.4
  81. OUT
  82. }
  83. @test "carries original IFS within hooks" {
  84. hook_path="${PYENV_TEST_DIR}/pyenv.d"
  85. mkdir -p "${hook_path}/which"
  86. cat > "${hook_path}/which/hello.bash" <<SH
  87. hellos=(\$(printf "hello\\tugly world\\nagain"))
  88. echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
  89. exit
  90. SH
  91. PYENV_HOOK_PATH="$hook_path" IFS=$' \t\n' PYENV_VERSION=system run pyenv-which anything
  92. assert_success
  93. assert_output "HELLO=:hello:ugly:world:again"
  94. }
  95. @test "discovers version from pyenv-version-name" {
  96. mkdir -p "$PYENV_ROOT"
  97. cat > "${PYENV_ROOT}/version" <<<"3.4"
  98. create_executable "3.4" "python"
  99. mkdir -p "$PYENV_TEST_DIR"
  100. cd "$PYENV_TEST_DIR"
  101. PYENV_VERSION= run pyenv-which python
  102. assert_success "${PYENV_ROOT}/versions/3.4/bin/python"
  103. }