run_tests.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env bash
  2. line='........................................'
  3. ucode='ucode -S -T, -L./tests/lib -L./root/usr/share/ucode'
  4. extract_sections() {
  5. local file=$1
  6. local dir=$2
  7. local count=0
  8. local tag line outfile
  9. while IFS= read -r line; do
  10. case "$line" in
  11. "-- Testcase --")
  12. tag="test"
  13. count=$((count + 1))
  14. outfile=$(printf "%s/%03d.in" "$dir" $count)
  15. printf "" > "$outfile"
  16. ;;
  17. "-- Environment --")
  18. tag="env"
  19. count=$((count + 1))
  20. outfile=$(printf "%s/%03d.env" "$dir" $count)
  21. printf "" > "$outfile"
  22. ;;
  23. "-- Expect stdout --"|"-- Expect stderr --"|"-- Expect exitcode --")
  24. tag="${line#-- Expect }"
  25. tag="${tag% --}"
  26. count=$((count + 1))
  27. outfile=$(printf "%s/%03d.%s" "$dir" $count "$tag")
  28. printf "" > "$outfile"
  29. ;;
  30. "-- File "*" --")
  31. tag="file"
  32. outfile="${line#-- File }"
  33. outfile="$(echo "${outfile% --}" | xargs)"
  34. outfile="$dir/files$(readlink -m "/${outfile:-file}")"
  35. mkdir -p "$(dirname "$outfile")"
  36. printf "" > "$outfile"
  37. ;;
  38. "-- End --")
  39. tag=""
  40. outfile=""
  41. ;;
  42. *)
  43. if [ -n "$tag" ]; then
  44. printf "%s\\n" "$line" >> "$outfile"
  45. fi
  46. ;;
  47. esac
  48. done < "$file"
  49. return $(ls -l "$dir/"*.in 2>/dev/null | wc -l)
  50. }
  51. run_testcase() {
  52. local num=$1
  53. local dir=$2
  54. local in=$3
  55. local env=$4
  56. local out=$5
  57. local err=$6
  58. local code=$7
  59. local fail=0
  60. $ucode \
  61. -D MOCK_SEARCH_PATH='["'"$dir"'/files", "./tests/mocks"]' \
  62. ${env:+-F "$env"} \
  63. -l mocklib -l fw4 \
  64. - <"$in" >"$dir/res.out" 2>"$dir/res.err"
  65. printf "%d\n" $? > "$dir/res.code"
  66. touch "$dir/empty"
  67. if ! cmp -s "$dir/res.err" "${err:-$dir/empty}"; then
  68. [ $fail = 0 ] && printf "!\n"
  69. printf "Testcase #%d: Expected stderr did not match:\n" $num
  70. diff -u --color=always --label="Expected stderr" --label="Resulting stderr" "${err:-$dir/empty}" "$dir/res.err"
  71. printf -- "---\n"
  72. fail=1
  73. fi
  74. if ! cmp -s "$dir/res.out" "${out:-$dir/empty}"; then
  75. [ $fail = 0 ] && printf "!\n"
  76. printf "Testcase #%d: Expected stdout did not match:\n" $num
  77. diff -u --color=always --label="Expected stdout" --label="Resulting stdout" "${out:-$dir/empty}" "$dir/res.out"
  78. printf -- "---\n"
  79. fail=1
  80. fi
  81. if [ -n "$code" ] && ! cmp -s "$dir/res.code" "$code"; then
  82. [ $fail = 0 ] && printf "!\n"
  83. printf "Testcase #%d: Expected exit code did not match:\n" $num
  84. diff -u --color=always --label="Expected code" --label="Resulting code" "$code" "$dir/res.code"
  85. printf -- "---\n"
  86. fail=1
  87. fi
  88. return $fail
  89. }
  90. run_test() {
  91. local file=$1
  92. local name=${file##*/}
  93. local res ecode eout eerr ein eenv tests
  94. local testcase_first=0 failed=0 count=0
  95. printf "%s %s " "$name" "${line:${#name}}"
  96. mkdir "/tmp/test.$$"
  97. extract_sections "$file" "/tmp/test.$$"
  98. tests=$?
  99. [ -f "/tmp/test.$$/001.in" ] && testcase_first=1
  100. for res in "/tmp/test.$$/"[0-9]*; do
  101. case "$res" in
  102. *.in)
  103. count=$((count + 1))
  104. if [ $testcase_first = 1 ]; then
  105. # Flush previous test
  106. if [ -n "$ein" ]; then
  107. run_testcase $count "/tmp/test.$$" "$ein" "$eenv" "$eout" "$eerr" "$ecode" || failed=$((failed + 1))
  108. eout=""
  109. eerr=""
  110. ecode=""
  111. eenv=""
  112. fi
  113. ein=$res
  114. else
  115. run_testcase $count "/tmp/test.$$" "$res" "$eenv" "$eout" "$eerr" "$ecode" || failed=$((failed + 1))
  116. eout=""
  117. eerr=""
  118. ecode=""
  119. eenv=""
  120. fi
  121. ;;
  122. *.env) eenv=$res ;;
  123. *.stdout) eout=$res ;;
  124. *.stderr) eerr=$res ;;
  125. *.exitcode) ecode=$res ;;
  126. esac
  127. done
  128. # Flush last test
  129. if [ $testcase_first = 1 ] && [ -n "$eout$eerr$ecode" ]; then
  130. run_testcase $count "/tmp/test.$$" "$ein" "$eenv" "$eout" "$eerr" "$ecode" || failed=$((failed + 1))
  131. fi
  132. rm -r "/tmp/test.$$"
  133. if [ $failed = 0 ]; then
  134. printf "OK\n"
  135. else
  136. printf "%s %s FAILED (%d/%d)\n" "$name" "${line:${#name}}" $failed $tests
  137. fi
  138. return $failed
  139. }
  140. n_tests=0
  141. n_fails=0
  142. select_tests="$@"
  143. use_test() {
  144. local input="$(readlink -f "$1")"
  145. local test
  146. [ -f "$input" ] || return 1
  147. [ -n "$select_tests" ] || return 0
  148. for test in "$select_tests"; do
  149. test="$(readlink -f "$test")"
  150. [ "$test" != "$input" ] || return 0
  151. done
  152. return 1
  153. }
  154. for catdir in tests/[0-9][0-9]_*; do
  155. [ -d "$catdir" ] || continue
  156. printf "\n##\n## Running %s tests\n##\n\n" "${catdir##*/[0-9][0-9]_}"
  157. for testfile in "$catdir/"[0-9][0-9]_*; do
  158. use_test "$testfile" || continue
  159. n_tests=$((n_tests + 1))
  160. run_test "$testfile" || n_fails=$((n_fails + 1))
  161. done
  162. done
  163. printf "\nRan %d tests, %d okay, %d failures\n" $n_tests $((n_tests - n_fails)) $n_fails
  164. exit $n_fails