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.

117 lines
2.5 KiB

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