run-tests 3.1 KB

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