Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 řádky
3.4 KiB

  1. unset PYENV_VERSION
  2. unset PYENV_DIR
  3. # guard against executing this block twice due to bats internals
  4. if [ -z "$PYENV_TEST_DIR" ]; then
  5. PYENV_TEST_DIR="${BATS_TMPDIR}/pyenv"
  6. export PYENV_TEST_DIR="$(mktemp -d "${PYENV_TEST_DIR}.XXX" 2>/dev/null || echo "$PYENV_TEST_DIR")"
  7. if enable -f "${BATS_TEST_DIRNAME}"/../libexec/pyenv-realpath.dylib realpath 2>/dev/null; then
  8. export PYENV_TEST_DIR="$(realpath "$PYENV_TEST_DIR")"
  9. else
  10. if [ -n "$PYENV_NATIVE_EXT" ]; then
  11. echo "pyenv: failed to load \`realpath' builtin" >&2
  12. exit 1
  13. fi
  14. fi
  15. export PYENV_ROOT="${PYENV_TEST_DIR}/root"
  16. export HOME="${PYENV_TEST_DIR}/home"
  17. export PYENV_HOOK_PATH="${PYENV_ROOT}/pyenv.d"
  18. PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
  19. PATH="${PYENV_TEST_DIR}/bin:$PATH"
  20. PATH="${BATS_TEST_DIRNAME}/../libexec:$PATH"
  21. PATH="${BATS_TEST_DIRNAME}/libexec:$PATH"
  22. PATH="${PYENV_ROOT}/shims:$PATH"
  23. export PATH
  24. for xdg_var in `env 2>/dev/null | grep ^XDG_ | cut -d= -f1`; do unset "$xdg_var"; done
  25. unset xdg_var
  26. fi
  27. teardown() {
  28. rm -rf "$PYENV_TEST_DIR"
  29. }
  30. flunk() {
  31. { if [ "$#" -eq 0 ]; then cat -
  32. else echo "$@"
  33. fi
  34. } | sed "s:${PYENV_TEST_DIR}:TEST_DIR:g" >&2
  35. return 1
  36. }
  37. assert_success() {
  38. if [ "$status" -ne 0 ]; then
  39. flunk "command failed with exit status $status" $'\n'\
  40. "output: $output"
  41. elif [ "$#" -gt 0 ]; then
  42. assert_output "$1"
  43. fi
  44. }
  45. assert_failure() {
  46. if [ "$status" -eq 0 ]; then
  47. flunk "expected failed exit status" $'\n'\
  48. "output: $output"
  49. elif [ "$#" -gt 0 ]; then
  50. assert_output "$1"
  51. fi
  52. }
  53. assert_equal() {
  54. if [ "$1" != "$2" ]; then
  55. { echo "expected: $1"
  56. echo "actual: $2"
  57. } | flunk
  58. fi
  59. }
  60. assert_output() {
  61. local expected
  62. if [ $# -eq 0 ]; then expected="$(cat -)"
  63. else expected="$1"
  64. fi
  65. assert_equal "$expected" "$output"
  66. }
  67. assert_line() {
  68. if [ "$1" -ge 0 ] 2>/dev/null; then
  69. assert_equal "$2" "${lines[$1]}"
  70. else
  71. local line
  72. for line in "${lines[@]}"; do
  73. if [ "$line" = "$1" ]; then return 0; fi
  74. done
  75. flunk "expected line \`$1'" $'\n'\
  76. "output: $output"
  77. fi
  78. }
  79. refute_line() {
  80. if [ "$1" -ge 0 ] 2>/dev/null; then
  81. local num_lines="${#lines[@]}"
  82. if [ "$1" -lt "$num_lines" ]; then
  83. flunk "output has $num_lines lines"
  84. fi
  85. else
  86. local line
  87. for line in "${lines[@]}"; do
  88. if [ "$line" = "$1" ]; then
  89. flunk "expected to not find line \`$line'" $'\n'\
  90. "output: $output"
  91. fi
  92. done
  93. fi
  94. }
  95. assert() {
  96. if ! "$@"; then
  97. flunk "failed: $@"
  98. fi
  99. }
  100. # Output a modified PATH that ensures that the given executable is not present,
  101. # but in which system utils necessary for pyenv operation are still available.
  102. path_without() {
  103. local path=":${PATH}:"
  104. for exe; do
  105. local found alt util
  106. for found in $(PATH="$path" type -aP "$exe"); do
  107. found="${found%/*}"
  108. if [ "$found" != "${PYENV_ROOT}/shims" ]; then
  109. alt="${PYENV_TEST_DIR}/$(echo "${found#/}" | tr '/' '-')"
  110. mkdir -p "$alt"
  111. for util in bash head cut readlink greadlink; do
  112. if [ -x "${found}/$util" ]; then
  113. ln -s "${found}/$util" "${alt}/$util"
  114. fi
  115. done
  116. path="${path/:${found}:/:${alt}:}"
  117. fi
  118. done
  119. done
  120. path="${path#:}"
  121. path="${path%:}"
  122. echo "$path"
  123. }
  124. create_hook() {
  125. mkdir -p "${PYENV_HOOK_PATH}/$1"
  126. touch "${PYENV_HOOK_PATH}/$1/$2"
  127. if [ ! -t 0 ]; then
  128. cat > "${PYENV_HOOK_PATH}/$1/$2"
  129. fi
  130. }