run-tests 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/sh
  2. set -e
  3. TESTTORUN=''
  4. while [ -n "$1" ]; do
  5. if [ "$1" = "-q" ]; then
  6. export MSGLEVEL=2
  7. elif [ "$1" = "-v" ]; then
  8. export MSGLEVEL=4
  9. elif [ "$1" = '--color=no' ]; then
  10. export MSGCOLOR='NO'
  11. elif [ "$1" = '--color=yes' ]; then
  12. export MSGCOLOR='YES'
  13. elif [ "$1" = '--color' ]; then
  14. export MSGCOLOR="$(echo "$2" | tr 'a-z' 'A-Z')"
  15. shift
  16. elif [ "$1" = '--level' ]; then
  17. export MSGLEVEL=$2
  18. shift
  19. elif [ "$1" = '-j' ]; then
  20. APT_TEST_JOBS=$2
  21. shift
  22. elif [ -x "$1" ]; then
  23. TESTTORUN="$1"
  24. else
  25. echo >&2 "WARNING: Unknown parameter »$1« will be ignored"
  26. fi
  27. shift
  28. done
  29. export MSGLEVEL="${MSGLEVEL:-3}"
  30. if [ "${MSGCOLOR:-YES}" = 'YES' ]; then
  31. if [ ! -t 1 ]; then # but check that we output to a terminal
  32. export MSGCOLOR='NO'
  33. fi
  34. fi
  35. if [ "$MSGCOLOR" != 'NO' ]; then
  36. CTEST='\033[1;32m'
  37. CHIGH='\033[1;35m'
  38. CRESET='\033[0m'
  39. else
  40. CTEST=''
  41. CHIGH=''
  42. CRESET=''
  43. fi
  44. if [ -n "$TESTTORUN" ]; then
  45. # collecting the output of one test to have it together
  46. OUTPUT="$(mktemp)"
  47. CURRENTTRAP="rm -f \"$OUTPUT\"; $CURRENTTRAP"
  48. trap "$CURRENTTRAP" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  49. {
  50. if [ "$MSGLEVEL" -le 2 ]; then
  51. printf "${CTEST}Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}: "
  52. else
  53. printf "${CTEST}Run Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}\n"
  54. fi
  55. if ! "$TESTTORUN"; then
  56. FAIL='yes'
  57. if [ "$MSGLEVEL" -le 2 ]; then
  58. printf >&2 "\n${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}"
  59. else
  60. echo >&2 "${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}"
  61. fi
  62. fi
  63. if [ "$MSGLEVEL" -le 2 ]; then
  64. echo
  65. fi
  66. } >"$OUTPUT" 2>&1
  67. # without we end up getting stepped output 'randomly'
  68. stty sane
  69. cat >&2 "$OUTPUT"
  70. stty sane
  71. if [ "$FAIL" = 'yes' ]; then
  72. exit 1
  73. else
  74. exit 0
  75. fi
  76. fi
  77. FAIL=0
  78. PASS=0
  79. ALL=0
  80. FAILED_TESTS=""
  81. DIR="$(readlink -f "$(dirname "$0")")"
  82. cd "$DIR"
  83. TESTLIST="$(find . -mindepth 1 -maxdepth 1 -regex '^\./test-[^/]*$' | sort)"
  84. if [ -n "$APT_TEST_JOBS" ]; then
  85. if [ "$MSGCOLOR" != 'NO' ]; then
  86. export MSGCOLOR='ALWAYS'
  87. fi
  88. exec parallel -j "$APT_TEST_JOBS" "./$(basename "$0")" -- $(echo "$TESTLIST")
  89. fi
  90. TOTAL="$(echo "$TESTLIST" | wc -l)"
  91. for testcase in $TESTLIST; do
  92. if [ "$MSGLEVEL" -le 2 ]; then
  93. printf "($(($ALL+1))/${TOTAL}) ${CTEST}Testcase ${CHIGH}${testcase##*/}${CRESET}: "
  94. else
  95. printf "${CTEST}Run Testcase ($(($ALL+1))/${TOTAL}) ${CHIGH}${testcase##*/}${CRESET}\n"
  96. fi
  97. if ! ${testcase}; then
  98. FAIL=$((FAIL+1))
  99. FAILED_TESTS="$FAILED_TESTS ${testcase##*/}"
  100. if [ "$MSGLEVEL" -le 2 ]; then
  101. printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  102. else
  103. echo >&2 "${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  104. fi
  105. else
  106. PASS=$((PASS+1))
  107. fi
  108. ALL=$((ALL+1))
  109. if [ "$MSGLEVEL" -le 2 ]; then
  110. echo
  111. fi
  112. done
  113. echo >&2 "Statistics: $ALL tests were run: $PASS successfully and $FAIL failed"
  114. if [ -n "$FAILED_TESTS" ]; then
  115. echo >&2 "Failed tests: $FAILED_TESTS"
  116. else
  117. echo >&2 'All tests seem to have been run successfully. What could possibly go wrong?'
  118. fi
  119. # ensure we don't overflow
  120. exit $((FAIL <= 255 ? FAIL : 255))