run-tests 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. TESTLIST="$(run-parts --list "$DIR" --regex '^test-.*$')"
  83. if [ -n "$APT_TEST_JOBS" ]; then
  84. if [ "$MSGCOLOR" != 'NO' ]; then
  85. export MSGCOLOR='ALWAYS'
  86. fi
  87. exec parallel -j "$APT_TEST_JOBS" "$0" -- $(echo "$TESTLIST")
  88. fi
  89. TOTAL="$(echo "$TESTLIST" | wc -l)"
  90. for testcase in $TESTLIST; do
  91. if [ "$MSGLEVEL" -le 2 ]; then
  92. printf "($(($ALL+1))/${TOTAL}) ${CTEST}Testcase ${CHIGH}${testcase##*/}${CRESET}: "
  93. else
  94. printf "${CTEST}Run Testcase ($(($ALL+1))/${TOTAL}) ${CHIGH}${testcase##*/}${CRESET}\n"
  95. fi
  96. if ! ${testcase}; then
  97. FAIL=$((FAIL+1))
  98. FAILED_TESTS="$FAILED_TESTS ${testcase##*/}"
  99. if [ "$MSGLEVEL" -le 2 ]; then
  100. printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  101. else
  102. echo >&2 "${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  103. fi
  104. else
  105. PASS=$((PASS+1))
  106. fi
  107. ALL=$((ALL+1))
  108. if [ "$MSGLEVEL" -le 2 ]; then
  109. echo
  110. fi
  111. done
  112. echo >&2 "Statistics: $ALL tests were run: $PASS successfully and $FAIL failed"
  113. if [ -n "$FAILED_TESTS" ]; then
  114. echo >&2 "Failed tests: $FAILED_TESTS"
  115. else
  116. echo >&2 'All tests seem to have been run successfully. What could possibly go wrong?'
  117. fi
  118. # ensure we don't overflow
  119. exit $((FAIL <= 255 ? FAIL : 255))