run-tests 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. {
  48. if [ "$MSGLEVEL" -le 2 ]; then
  49. printf "${CTEST}Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}: "
  50. else
  51. printf "${CTEST}Run Testcase ${CHIGH}${TESTTORUN##*/}${CRESET}\n"
  52. fi
  53. if ! "$TESTTORUN"; then
  54. FAIL='yes'
  55. if [ "$MSGLEVEL" -le 2 ]; then
  56. printf >&2 "\n${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}"
  57. else
  58. echo >&2 "${CHIGH}Running ${TESTTORUN##*/} -> FAILED${CRESET}"
  59. fi
  60. fi
  61. if [ "$MSGLEVEL" -le 2 ]; then
  62. echo
  63. fi
  64. } >"$OUTPUT" 2>&1
  65. # without we end up getting stepped output 'randomly'
  66. stty sane
  67. cat >&2 "$OUTPUT"
  68. stty sane
  69. if [ "$FAIL" = 'yes' ]; then
  70. exit 1
  71. else
  72. exit 0
  73. fi
  74. fi
  75. FAIL=0
  76. PASS=0
  77. ALL=0
  78. FAILED_TESTS=""
  79. DIR="$(readlink -f "$(dirname "$0")")"
  80. TESTLIST="$(run-parts --list "$DIR" --regex '^test-.*$')"
  81. if [ -n "$APT_TEST_JOBS" ]; then
  82. if [ "$MSGCOLOR" != 'NO' ]; then
  83. export MSGCOLOR='ALWAYS'
  84. fi
  85. exec parallel -j "$APT_TEST_JOBS" "$0" -- $(echo "$TESTLIST")
  86. fi
  87. TOTAL="$(echo "$TESTLIST" | wc -l)"
  88. for testcase in $TESTLIST; do
  89. if [ "$MSGLEVEL" -le 2 ]; then
  90. printf "($(($ALL+1))/${TOTAL}) ${CTEST}Testcase ${CHIGH}${testcase##*/}${CRESET}: "
  91. else
  92. printf "${CTEST}Run Testcase ($(($ALL+1))/${TOTAL}) ${CHIGH}${testcase##*/}${CRESET}\n"
  93. fi
  94. if ! ${testcase}; then
  95. FAIL=$((FAIL+1))
  96. FAILED_TESTS="$FAILED_TESTS ${testcase##*/}"
  97. if [ "$MSGLEVEL" -le 2 ]; then
  98. printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  99. else
  100. echo >&2 "${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  101. fi
  102. else
  103. PASS=$((PASS+1))
  104. fi
  105. ALL=$((ALL+1))
  106. if [ "$MSGLEVEL" -le 2 ]; then
  107. echo
  108. fi
  109. done
  110. echo >&2 "Statistics: $ALL tests were run: $PASS successfully and $FAIL failed"
  111. if [ -n "$FAILED_TESTS" ]; then
  112. echo >&2 "Failed tests: $FAILED_TESTS"
  113. else
  114. echo >&2 'All tests seem to have been run successfully. What could possibly go wrong?'
  115. fi
  116. # ensure we don't overflow
  117. exit $((FAIL <= 255 ? FAIL : 255))