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.

113 line
2.4 KiB

  1. #!/usr/bin/env bats
  2. load test_helper
  3. create_executable() {
  4. name="${1?}"
  5. shift 1
  6. bin="${PYENV_ROOT}/versions/${PYENV_VERSION}/bin"
  7. mkdir -p "$bin"
  8. { if [ $# -eq 0 ]; then cat -
  9. else echo "$@"
  10. fi
  11. } | sed -Ee '1s/^ +//' > "${bin}/$name"
  12. chmod +x "${bin}/$name"
  13. }
  14. @test "fails with invalid version" {
  15. export PYENV_VERSION="3.4"
  16. run pyenv-exec python -v
  17. assert_failure "pyenv: version \`3.4' is not installed"
  18. }
  19. @test "completes with names of executables" {
  20. export PYENV_VERSION="3.4"
  21. create_executable "fab" "#!/bin/sh"
  22. create_executable "python" "#!/bin/sh"
  23. pyenv-rehash
  24. run pyenv-completions exec
  25. assert_success
  26. assert_output <<OUT
  27. fab
  28. python
  29. OUT
  30. }
  31. @test "supports hook path with spaces" {
  32. hook_path="${PYENV_TEST_DIR}/custom stuff/pyenv hooks"
  33. mkdir -p "${hook_path}/exec"
  34. echo "export HELLO='from hook'" > "${hook_path}/exec/hello.bash"
  35. export PYENV_VERSION=system
  36. PYENV_HOOK_PATH="$hook_path" run pyenv-exec env
  37. assert_success
  38. assert_line "HELLO=from hook"
  39. }
  40. @test "carries original IFS within hooks" {
  41. hook_path="${PYENV_TEST_DIR}/pyenv.d"
  42. mkdir -p "${hook_path}/exec"
  43. cat > "${hook_path}/exec/hello.bash" <<SH
  44. hellos=(\$(printf "hello\\tugly world\\nagain"))
  45. echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
  46. SH
  47. export PYENV_VERSION=system
  48. PYENV_HOOK_PATH="$hook_path" IFS=$' \t\n' run pyenv-exec env
  49. assert_success
  50. assert_line "HELLO=:hello:ugly:world:again"
  51. }
  52. @test "forwards all arguments" {
  53. export PYENV_VERSION="3.4"
  54. create_executable "python" <<SH
  55. #!$BASH
  56. echo \$0
  57. for arg; do
  58. # hack to avoid bash builtin echo which can't output '-e'
  59. printf " %s\\n" "\$arg"
  60. done
  61. SH
  62. run pyenv-exec python -w "/path to/python script.rb" -- extra args
  63. assert_success
  64. assert_output <<OUT
  65. ${PYENV_ROOT}/versions/3.4/bin/python
  66. -w
  67. /path to/python script.rb
  68. --
  69. extra
  70. args
  71. OUT
  72. }
  73. @test "supports python -S <cmd>" {
  74. export PYENV_VERSION="3.4"
  75. # emulate `python -S' behavior
  76. create_executable "python" <<SH
  77. #!$BASH
  78. if [[ \$1 == "-S"* ]]; then
  79. found="\$(PATH="\${PYTHONPATH:-\$PATH}" which \$2)"
  80. # assert that the found executable has python for shebang
  81. if head -1 "\$found" | grep python >/dev/null; then
  82. \$BASH "\$found"
  83. else
  84. echo "python: no Python script found in input (LoadError)" >&2
  85. exit 1
  86. fi
  87. else
  88. echo 'python 3.4 (pyenv test)'
  89. fi
  90. SH
  91. create_executable "fab" <<SH
  92. #!/usr/bin/env python
  93. echo hello fab
  94. SH
  95. pyenv-rehash
  96. run python -S fab
  97. assert_success "hello fab"
  98. }