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.

123 lines
2.5 KiB

пре 10 година
  1. export TMP="$BATS_TEST_DIRNAME/tmp"
  2. if [ "$FIXTURE_ROOT" != "$BATS_TEST_DIRNAME/fixtures" ]; then
  3. export FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures"
  4. export INSTALL_ROOT="$TMP/install"
  5. PATH=/usr/bin:/usr/sbin:/bin/:/sbin
  6. PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
  7. PATH="$TMP/bin:$PATH"
  8. export PATH
  9. fi
  10. teardown() {
  11. rm -fr "$TMP"/*
  12. }
  13. stub() {
  14. local program="$1"
  15. local prefix="$(echo "$program" | tr a-z- A-Z_)"
  16. shift
  17. export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
  18. export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
  19. export "${prefix}_STUB_END"=
  20. mkdir -p "${TMP}/bin"
  21. ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}"
  22. touch "${TMP}/${program}-stub-plan"
  23. for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
  24. }
  25. unstub() {
  26. local program="$1"
  27. local prefix="$(echo "$program" | tr a-z- A-Z_)"
  28. local path="${TMP}/bin/${program}"
  29. export "${prefix}_STUB_END"=1
  30. local STATUS=0
  31. "$path" || STATUS="$?"
  32. rm -f "$path"
  33. rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
  34. return "$STATUS"
  35. }
  36. run_inline_definition() {
  37. local definition="${TMP}/build-definition"
  38. cat > "$definition"
  39. run python-build "$definition" "${1:-$INSTALL_ROOT}"
  40. }
  41. install_fixture() {
  42. local args
  43. while [ "${1#-}" != "$1" ]; do
  44. args="$args $1"
  45. shift 1
  46. done
  47. local name="$1"
  48. local destination="$2"
  49. [ -n "$destination" ] || destination="$INSTALL_ROOT"
  50. run python-build $args "$FIXTURE_ROOT/$name" "$destination"
  51. }
  52. assert() {
  53. if ! "$@"; then
  54. flunk "failed: $@"
  55. fi
  56. }
  57. flunk() {
  58. { if [ "$#" -eq 0 ]; then cat -
  59. else echo "$@"
  60. fi
  61. } | sed "s:${TMP}:\${TMP}:g" >&2
  62. return 1
  63. }
  64. assert_success() {
  65. if [ "$status" -ne 0 ]; then
  66. { echo "command failed with exit status $status"
  67. echo "output: $output"
  68. } | flunk
  69. elif [ "$#" -gt 0 ]; then
  70. assert_output "$1"
  71. fi
  72. }
  73. assert_failure() {
  74. if [ "$status" -eq 0 ]; then
  75. flunk "expected failed exit status"
  76. elif [ "$#" -gt 0 ]; then
  77. assert_output "$1"
  78. fi
  79. }
  80. assert_equal() {
  81. if [ "$1" != "$2" ]; then
  82. { echo "expected: $1"
  83. echo "actual: $2"
  84. } | flunk
  85. fi
  86. }
  87. assert_output() {
  88. local expected
  89. if [ $# -eq 0 ]; then expected="$(cat -)"
  90. else expected="$1"
  91. fi
  92. assert_equal "$expected" "$output"
  93. }
  94. assert_output_contains() {
  95. local expected="$1"
  96. echo "$output" | grep -F "$expected" >/dev/null || {
  97. { echo "expected output to contain $expected"
  98. echo "actual: $output"
  99. } | flunk
  100. }
  101. }