run-tests 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #! /bin/bash
  2. set -e
  3. abspath () {
  4. pushd "$1" > /dev/null
  5. local path=$(pwd)
  6. popd > /dev/null
  7. echo "${path}"
  8. }
  9. declare -r srcdir=$(abspath "${0%/*}/../")
  10. declare -a tests=( )
  11. declare -a fails=( )
  12. declare -a okays=( )
  13. declare -i current=0
  14. for hi in "${srcdir}/test"/*.hi ; do
  15. name=${hi##*/}
  16. name=${name%.hi}
  17. tests=( "${tests[@]}" "${name}" )
  18. if [[ ${name} != fail-* ]] ; then
  19. tests=( "${tests[@]}" "roundtrip/${name}" )
  20. fi
  21. done
  22. declare -ir total=${#tests[@]}
  23. : ${TEST_TIMEOUT:=10}
  24. GOT_TIMEOUT=false
  25. if timeout --version 2> /dev/null | grep -qF 'GNU coreutils'
  26. then
  27. function with_timeout () {
  28. local code=0
  29. GOT_TIMEOUT=false
  30. timeout ${TEST_TIMEOUT} "$@"
  31. code=$?
  32. if [[ ${code} -eq 124 ]] ; then
  33. GOT_TIMEOUT=true
  34. fi
  35. return ${code}
  36. }
  37. else
  38. function with_timeout () {
  39. "$@"
  40. }
  41. fi
  42. LAST_CMD=''
  43. function run_cmd () {
  44. LAST_CMD="$*"
  45. with_timeout "$@"
  46. }
  47. if [[ -z ${DUMB_CONSOLE} || ${DUMB_CONSOLE} -eq 0 ]] ; then
  48. echo
  49. report_progress () {
  50. printf '[%%%3i|+%2i|-%2i] %s\n' \
  51. $(( 100 * ++current / total )) ${#okays[@]} ${#fails[@]} "$1"
  52. }
  53. else
  54. report_progress () {
  55. local status='ok'
  56. if ! $2 ; then
  57. if ${GOT_TIMEOUT} ; then
  58. status='timeout'
  59. else
  60. status='failed'
  61. fi
  62. fi
  63. printf '[%i/%i] %s (%s)\n' \ $(( ++current )) ${total} "$1" ${status}
  64. }
  65. fi
  66. for name in "${tests[@]}" ; do
  67. if [[ ${name} = fail-* ]] ; then
  68. expect_fail=true
  69. else
  70. expect_fail=false
  71. fi
  72. if [[ ${name} = roundtrip/* ]] ; then
  73. if run_cmd \
  74. "${srcdir}/tools/hipack-roundtrip" \
  75. "${srcdir}/test/${name#roundtrip/}.hi" && \
  76. "${srcdir}/tools/hipack-roundtrip" -c \
  77. "${srcdir}/test/${name#roundtrip/}.hi"
  78. then
  79. passed=true
  80. else
  81. passed=false
  82. fi &> "/tmp/hipack-$$-${name//\//-}.err"
  83. else
  84. if run_cmd \
  85. "${srcdir}/tools/hipack-parse" \
  86. "${srcdir}/test/${name}.hi" \
  87. &> "/tmp/hipack-$$-${name}.err"
  88. then
  89. if ${expect_fail} ; then passed=false ; else passed=true ; fi
  90. else
  91. if ${expect_fail} ; then passed=true ; else passed=false ; fi
  92. fi
  93. diff_text=''
  94. if ${passed} && [[ -r ${srcdir}/test/${name}.err ]] ; then
  95. # Show diff
  96. if ! diff -q \
  97. "${srcdir}/test/${name}.err" \
  98. "/tmp/hipack-$$-${name}.err" \
  99. &> /dev/null
  100. then
  101. # Diffs don't match, show 'em
  102. diff_text=$(diff -ud \
  103. "${srcdir}/test/${name}.err" \
  104. "/tmp/hipack-$$-${name}.err" \
  105. || true)
  106. passed=false
  107. fi
  108. fi
  109. fi
  110. report_progress "${name}" ${passed}
  111. if ${passed} ; then
  112. okays=( "${okays[@]}" "${name}" )
  113. else
  114. fails=( "${fails[@]}" "${name}" )
  115. if [[ -n ${diff_text} ]] ; then
  116. echo "${diff_text}"
  117. elif [[ -s /tmp/hipack-$$-${name//\//-}.err ]] ; then
  118. cat "/tmp/hipack-$$-${name//\//-}.err"
  119. fi
  120. echo "Command: ${LAST_CMD}"
  121. echo
  122. echo
  123. fi
  124. rm -f "/tmp/hipack-$$-${name//\//-}.err"
  125. done
  126. echo "Total: ${total}"
  127. echo "Passed: ${#okays[@]} ($(( 100 * ${#okays[@]} / total ))%)"
  128. if [[ ${#fails[@]} -gt 0 ]] ; then
  129. echo "Failed: ${#fails[@]} ($(( 100 * ${#fails[@]} / total ))%)"
  130. for name in "${fails[@]}" ; do
  131. echo " ${name}"
  132. done
  133. fi
  134. [[ ${#fails[@]} -eq 0 ]] || exit 1