suite.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # HACK: get newline for use in strings given that "\n" and $'' do not work.
  2. NL="$(printf '\nE')"
  3. NL="${NL%E}"
  4. FAIL_SUMMARY=""
  5. # Test success marker. If END_MARKER file exists, we know that all tests
  6. # finished. If FAIL_SUMMARY_FILE exists we know that some tests failed, this
  7. # file will contain information about failed tests. Build is considered
  8. # successful if tests ended without any of them failing.
  9. END_MARKER="$BUILD_DIR/.tests_finished"
  10. FAIL_SUMMARY_FILE="$BUILD_DIR/.test_errors"
  11. ANSI_CLEAR="\033[0K"
  12. travis_fold() {
  13. local action="$1"
  14. local name="$2"
  15. name="$(echo -n "$name" | tr '\n\0' '--' | sed 's/[^A-Za-z0-9]\{1,\}/-/g')"
  16. name="$(echo -n "$name" | sed 's/-$//')"
  17. echo -en "travis_fold:${action}:${name}\r${ANSI_CLEAR}"
  18. }
  19. if test "$TRAVIS" != "true" ; then
  20. travis_fold() {
  21. return 0
  22. }
  23. fi
  24. enter_suite() {
  25. set +x
  26. FAILED=0
  27. rm -f "${END_MARKER}"
  28. local suite_name="$1"
  29. export NVIM_TEST_CURRENT_SUITE="${NVIM_TEST_CURRENT_SUITE}/$suite_name"
  30. travis_fold start "${NVIM_TEST_CURRENT_SUITE}"
  31. set -x
  32. }
  33. exit_suite() {
  34. set +x
  35. if test $FAILED -ne 0 ; then
  36. echo "Suite ${NVIM_TEST_CURRENT_SUITE} failed, summary:"
  37. echo "${FAIL_SUMMARY}"
  38. else
  39. travis_fold end "${NVIM_TEST_CURRENT_SUITE}"
  40. fi
  41. export NVIM_TEST_CURRENT_SUITE="${NVIM_TEST_CURRENT_SUITE%/*}"
  42. if test "$1" != "--continue" ; then
  43. exit $FAILED
  44. else
  45. local saved_failed=$FAILED
  46. FAILED=0
  47. return $saved_failed
  48. fi
  49. }
  50. fail() {
  51. local test_name="$1"
  52. local fail_char="$2"
  53. local message="$3"
  54. : ${fail_char:=F}
  55. : ${message:=Test $test_name failed}
  56. local full_msg="$fail_char $NVIM_TEST_CURRENT_SUITE|$test_name :: $message"
  57. FAIL_SUMMARY="${FAIL_SUMMARY}${NL}${full_msg}"
  58. echo "${full_msg}" >> "${FAIL_SUMMARY_FILE}"
  59. echo "Failed: $full_msg"
  60. FAILED=1
  61. }
  62. run_test() {
  63. local cmd="$1"
  64. test $# -gt 0 && shift
  65. local test_name="$1"
  66. : ${test_name:=$cmd}
  67. test $# -gt 0 && shift
  68. if ! eval "$cmd" ; then
  69. fail "${test_name}" "$@"
  70. fi
  71. }
  72. run_test_wd() {
  73. local hang_ok=
  74. if test "$1" = "--allow-hang" ; then
  75. hang_ok=1
  76. shift
  77. fi
  78. local timeout="$1"
  79. test $# -gt 0 && shift
  80. local cmd="$1"
  81. test $# -gt 0 && shift
  82. local restart_cmd="$1"
  83. : ${restart_cmd:=true}
  84. test $# -gt 0 && shift
  85. local test_name="$1"
  86. : ${test_name:=$cmd}
  87. test $# -gt 0 && shift
  88. local output_file="$(mktemp)"
  89. local status_file="$(mktemp)"
  90. local sid_file="$(mktemp)"
  91. local restarts=5
  92. local prev_tmpsize=-1
  93. while test $restarts -gt 0 ; do
  94. : > "$status_file"
  95. : > "$sid_file"
  96. setsid \
  97. env \
  98. output_file="$output_file" \
  99. status_file="$status_file" \
  100. sid_file="$sid_file" \
  101. cmd="$cmd" \
  102. CI_DIR="$CI_DIR" \
  103. sh -c '
  104. . "${CI_DIR}/common/test.sh"
  105. ps -o sid= > "$sid_file"
  106. (
  107. ret=0
  108. if ! eval "$cmd" 2>&1 ; then
  109. ret=1
  110. fi
  111. echo "$ret" > "$status_file"
  112. ) | tee -a "$output_file"
  113. '
  114. while test "$(stat -c "%s" "$status_file")" -eq 0 ; do
  115. prev_tmpsize=$tmpsize
  116. sleep $timeout
  117. tmpsize="$(stat -c "%s" "$output_file")"
  118. if test $tempsize -eq $prev_temsize ; then
  119. # no output, assuming either hang or exit
  120. break
  121. fi
  122. done
  123. restarts=$(( restarts - 1 ))
  124. if test "$(stat -c "%s" "$status_file")" -eq 0 ; then
  125. # Status file not updated, assuming hang
  126. # SID not known, this should not ever happen
  127. if test "$(stat -c "%s" "$sid_file")" -eq 0 ; then
  128. fail "$test_name" E "Shell did not run"
  129. break
  130. fi
  131. # Kill all processes which belong to one session: should get rid of test
  132. # processes as well as sh itself.
  133. pkill -KILL -s$(cat "$sid_file")
  134. if test $restarts -eq 0 ; then
  135. if test -z "$hang_ok" ; then
  136. fail "$test_name" E "Test hang up"
  137. fi
  138. else
  139. echo "Test ${test_name} hang up, restarting"
  140. eval "$restart_cmd"
  141. fi
  142. else
  143. local new_failed="$(cat "$status_file")"
  144. if test "$new_failed" != "0" ; then
  145. fail "$test_name" F "Test failed in run_test_wd"
  146. fi
  147. break
  148. fi
  149. done
  150. rm -f "$output_file"
  151. rm -f "$status_file"
  152. rm -f "$sid_file"
  153. }
  154. ended_successfully() {
  155. if test -f "${FAIL_SUMMARY_FILE}" ; then
  156. echo 'Test failed, complete summary:'
  157. cat "${FAIL_SUMMARY_FILE}"
  158. return 1
  159. fi
  160. if ! test -f "${END_MARKER}" ; then
  161. echo 'ended_successfully called before end marker was touched'
  162. return 1
  163. fi
  164. return 0
  165. }
  166. end_tests() {
  167. touch "${END_MARKER}"
  168. ended_successfully
  169. }