run-tests 3.7 KB

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