You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run-tests 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. APT_TEST_SIGNINGHOME="$(mktemp --directory --tmpdir 'apt-key-signinghome.XXXXXXXXXX')"
  107. removesigninghome() {
  108. if [ -z "$APT_TEST_SIGNINGHOME" ]; then return; fi
  109. GNUPGHOME="${APT_TEST_SIGNINGHOME}" gpgconf --kill gpg-agent >/dev/null 2>&1 || true
  110. rm -rf -- "$APT_TEST_SIGNINGHOME"
  111. }
  112. trap "exit 1" 0 HUP INT ILL ABRT FPE SEGV PIPE TERM
  113. trap "removesigninghome" 0 QUIT
  114. export APT_TEST_SIGNINGHOME
  115. TOTAL="$(echo "$TESTLIST" | wc -l)"
  116. if [ "$MSGLEVEL" -le 1 ]; then
  117. printf "${CTEST}Running testcases${CRESET}: "
  118. fi
  119. for testcase in $TESTLIST; do
  120. if [ "$MSGLEVEL" -le 1 ]; then
  121. printf "${testcase##*/}"
  122. elif [ "$MSGLEVEL" -le 2 ]; then
  123. printf "($(($ALL+1))/${TOTAL}) ${CTEST}Testcase ${CHIGH}${testcase##*/}${CRESET}: "
  124. else
  125. printf "${CTEST}Run Testcase ($(($ALL+1))/${TOTAL}) ${CHIGH}${testcase##*/}${CRESET}\n"
  126. fi
  127. if ! ${testcase}; then
  128. FAIL=$((FAIL+1))
  129. FAILED_TESTS="$FAILED_TESTS ${testcase##*/}"
  130. if [ "$MSGLEVEL" -le 1 ]; then
  131. printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}\n"
  132. elif [ "$MSGLEVEL" -le 2 ]; then
  133. printf >&2 "\n${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  134. else
  135. echo >&2 "${CHIGH}Running ${testcase##*/} -> FAILED${CRESET}"
  136. fi
  137. else
  138. PASS=$((PASS+1))
  139. if [ "$MSGLEVEL" -le 1 ]; then
  140. printf " "
  141. fi
  142. fi
  143. ALL=$((ALL+1))
  144. if [ "$MSGLEVEL" -le 1 ]; then
  145. :
  146. elif [ "$MSGLEVEL" -le 2 ]; then
  147. echo
  148. fi
  149. done
  150. echo >&2 "Statistics: $ALL tests were run: $PASS successfully and $FAIL failed"
  151. if [ -n "$FAILED_TESTS" ]; then
  152. echo >&2 "Failed tests: $FAILED_TESTS"
  153. else
  154. echo >&2 'All tests seem to have been run successfully. What could possibly go wrong?'
  155. fi
  156. # ensure we don't overflow
  157. exit $((FAIL <= 255 ? FAIL : 255))