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.

157 line
4.5 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 "doesn't include current directory in PATH search" {
  48. mkdir -p "$PYENV_TEST_DIR"
  49. cd "$PYENV_TEST_DIR"
  50. touch kill-all-humans
  51. chmod +x kill-all-humans
  52. PATH="$(path_without "kill-all-humans")" PYENV_VERSION=system run pyenv-which kill-all-humans
  53. assert_failure "pyenv: kill-all-humans: command not found"
  54. }
  55. @test "version not installed" {
  56. create_executable "3.4" "py.test"
  57. PYENV_VERSION=3.3 run pyenv-which py.test
  58. assert_failure "pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)"
  59. }
  60. @test "versions not installed" {
  61. create_executable "3.4" "py.test"
  62. PYENV_VERSION=2.7:3.3 run pyenv-which py.test
  63. assert_failure <<OUT
  64. pyenv: version \`2.7' is not installed (set by PYENV_VERSION environment variable)
  65. pyenv: version \`3.3' is not installed (set by PYENV_VERSION environment variable)
  66. OUT
  67. }
  68. @test "no executable found" {
  69. create_executable "2.7" "py.test"
  70. PYENV_VERSION=2.7 run pyenv-which fab
  71. assert_failure "pyenv: fab: command not found"
  72. }
  73. @test "no executable found for system version" {
  74. PATH="$(path_without "rake")" PYENV_VERSION=system run pyenv-which rake
  75. assert_failure "pyenv: rake: command not found"
  76. }
  77. @test "executable found in other versions" {
  78. create_executable "2.7" "python"
  79. create_executable "3.3" "py.test"
  80. create_executable "3.4" "py.test"
  81. PYENV_VERSION=2.7 run pyenv-which py.test
  82. assert_failure
  83. assert_output <<OUT
  84. pyenv: py.test: command not found
  85. The \`py.test' command exists in these Python versions:
  86. 3.3
  87. 3.4
  88. Note: See 'pyenv help global' for tips on allowing both
  89. python2 and python3 to be found.
  90. OUT
  91. }
  92. @test "carries original IFS within hooks" {
  93. create_hook which hello.bash <<SH
  94. hellos=(\$(printf "hello\\tugly world\\nagain"))
  95. echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
  96. exit
  97. SH
  98. IFS=$' \t\n' PYENV_VERSION=system run pyenv-which anything
  99. assert_success
  100. assert_output "HELLO=:hello:ugly:world:again"
  101. }
  102. @test "discovers version from pyenv-version-name" {
  103. mkdir -p "$PYENV_ROOT"
  104. cat > "${PYENV_ROOT}/version" <<<"3.4"
  105. create_executable "3.4" "python"
  106. mkdir -p "$PYENV_TEST_DIR"
  107. cd "$PYENV_TEST_DIR"
  108. PYENV_VERSION= run pyenv-which python
  109. assert_success "${PYENV_ROOT}/versions/3.4/bin/python"
  110. }
  111. @test "resolves pyenv-latest prefixes" {
  112. create_executable "3.4.2" "python"
  113. PYENV_VERSION=3.4 run pyenv-which python
  114. assert_success "${PYENV_ROOT}/versions/3.4.2/bin/python"
  115. }
  116. @test "hooks get resolved version name" {
  117. create_hook which echo.bash <<!
  118. echo version=\$version
  119. exit
  120. !
  121. create_executable "3.4.2" "python"
  122. PYENV_VERSION=3.4 run pyenv-which python
  123. assert_success "version=3.4.2"
  124. }