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.

125 regels
2.8 KiB

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