tap-driver.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. #! /bin/sh
  2. # Copyright (C) 2011-2012 Free Software Foundation, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2, or (at your option)
  7. # any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # As a special exception to the GNU General Public License, if you
  17. # distribute this file as part of a program that contains a
  18. # configuration script generated by Autoconf, you may include it under
  19. # the same distribution terms that you use for the rest of that program.
  20. # This file is maintained in Automake, please report
  21. # bugs to <bug-automake@gnu.org> or send patches to
  22. # <automake-patches@gnu.org>.
  23. scriptversion=2011-12-27.17; # UTC
  24. # Make unconditional expansion of undefined variables an error. This
  25. # helps a lot in preventing typo-related bugs.
  26. set -u
  27. me=tap-driver.sh
  28. fatal ()
  29. {
  30. echo "$me: fatal: $*" >&2
  31. exit 1
  32. }
  33. usage_error ()
  34. {
  35. echo "$me: $*" >&2
  36. print_usage >&2
  37. exit 2
  38. }
  39. print_usage ()
  40. {
  41. cat <<END
  42. Usage:
  43. tap-driver.sh --test-name=NAME --log-file=PATH --trs-file=PATH
  44. [--expect-failure={yes|no}] [--color-tests={yes|no}]
  45. [--enable-hard-errors={yes|no}] [--ignore-exit]
  46. [--diagnostic-string=STRING] [--merge|--no-merge]
  47. [--comments|--no-comments] [--] TEST-COMMAND
  48. The \`--test-name', \`--log-file' and \`--trs-file' options are mandatory.
  49. END
  50. }
  51. # TODO: better error handling in option parsing (in particular, ensure
  52. # TODO: $log_file, $trs_file and $test_name are defined).
  53. test_name= # Used for reporting.
  54. log_file= # Where to save the result and output of the test script.
  55. trs_file= # Where to save the metadata of the test run.
  56. expect_failure=0
  57. color_tests=0
  58. merge=0
  59. ignore_exit=0
  60. comments=0
  61. diag_string='#'
  62. while test $# -gt 0; do
  63. case $1 in
  64. --help) print_usage; exit $?;;
  65. --version) echo "$me $scriptversion"; exit $?;;
  66. --test-name) test_name=$2; shift;;
  67. --log-file) log_file=$2; shift;;
  68. --trs-file) trs_file=$2; shift;;
  69. --color-tests) color_tests=$2; shift;;
  70. --expect-failure) expect_failure=$2; shift;;
  71. --enable-hard-errors) shift;; # No-op.
  72. --merge) merge=1;;
  73. --no-merge) merge=0;;
  74. --ignore-exit) ignore_exit=1;;
  75. --comments) comments=1;;
  76. --no-comments) comments=0;;
  77. --diagnostic-string) diag_string=$2; shift;;
  78. --) shift; break;;
  79. -*) usage_error "invalid option: '$1'";;
  80. esac
  81. shift
  82. done
  83. test $# -gt 0 || usage_error "missing test command"
  84. case $expect_failure in
  85. yes) expect_failure=1;;
  86. *) expect_failure=0;;
  87. esac
  88. if test $color_tests = yes; then
  89. init_colors='
  90. color_map["red"]="" # Red.
  91. color_map["grn"]="" # Green.
  92. color_map["lgn"]="" # Light green.
  93. color_map["blu"]="" # Blue.
  94. color_map["mgn"]="" # Magenta.
  95. color_map["std"]="" # No color.
  96. color_for_result["ERROR"] = "mgn"
  97. color_for_result["PASS"] = "grn"
  98. color_for_result["XPASS"] = "red"
  99. color_for_result["FAIL"] = "red"
  100. color_for_result["XFAIL"] = "lgn"
  101. color_for_result["SKIP"] = "blu"'
  102. else
  103. init_colors=''
  104. fi
  105. # :; is there to work around a bug in bash 3.2 (and earlier) which
  106. # does not always set '$?' properly on redirection failure.
  107. # See the Autoconf manual for more details.
  108. :;{
  109. (
  110. # Ignore common signals (in this subshell only!), to avoid potential
  111. # problems with Korn shells. Some Korn shells are known to propagate
  112. # to themselves signals that have killed a child process they were
  113. # waiting for; this is done at least for SIGINT (and usually only for
  114. # it, in truth). Without the `trap' below, such a behaviour could
  115. # cause a premature exit in the current subshell, e.g., in case the
  116. # test command it runs gets terminated by a SIGINT. Thus, the awk
  117. # script we are piping into would never seen the exit status it
  118. # expects on its last input line (which is displayed below by the
  119. # last `echo $?' statement), and would thus die reporting an internal
  120. # error.
  121. # For more information, see the Autoconf manual and the threads:
  122. # <http://lists.gnu.org/archive/html/bug-autoconf/2011-09/msg00004.html>
  123. # <http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/004121.html>
  124. trap : 1 3 2 13 15
  125. if test $merge -gt 0; then
  126. exec 2>&1
  127. else
  128. exec 2>&3
  129. fi
  130. "$@"
  131. echo $?
  132. ) | LC_ALL=C ${AM_TAP_AWK-awk} \
  133. -v me="$me" \
  134. -v test_script_name="$test_name" \
  135. -v log_file="$log_file" \
  136. -v trs_file="$trs_file" \
  137. -v expect_failure="$expect_failure" \
  138. -v merge="$merge" \
  139. -v ignore_exit="$ignore_exit" \
  140. -v comments="$comments" \
  141. -v diag_string="$diag_string" \
  142. '
  143. # FIXME: the usages of "cat >&3" below could be optimized when using
  144. # FIXME: GNU awk, and/on on systems that supports /dev/fd/.
  145. # Implementation note: in what follows, `result_obj` will be an
  146. # associative array that (partly) simulates a TAP result object
  147. # from the `TAP::Parser` perl module.
  148. ## ----------- ##
  149. ## FUNCTIONS ##
  150. ## ----------- ##
  151. function fatal(msg)
  152. {
  153. print me ": " msg | "cat >&2"
  154. exit 1
  155. }
  156. function abort(where)
  157. {
  158. fatal("internal error " where)
  159. }
  160. # Convert a boolean to a "yes"/"no" string.
  161. function yn(bool)
  162. {
  163. return bool ? "yes" : "no";
  164. }
  165. function add_test_result(result)
  166. {
  167. if (!test_results_index)
  168. test_results_index = 0
  169. test_results_list[test_results_index] = result
  170. test_results_index += 1
  171. test_results_seen[result] = 1;
  172. }
  173. # Whether the test script should be re-run by "make recheck".
  174. function must_recheck()
  175. {
  176. for (k in test_results_seen)
  177. if (k != "XFAIL" && k != "PASS" && k != "SKIP")
  178. return 1
  179. return 0
  180. }
  181. # Whether the content of the log file associated to this test should
  182. # be copied into the "global" test-suite.log.
  183. function copy_in_global_log()
  184. {
  185. for (k in test_results_seen)
  186. if (k != "PASS")
  187. return 1
  188. return 0
  189. }
  190. # FIXME: this can certainly be improved ...
  191. function get_global_test_result()
  192. {
  193. if ("ERROR" in test_results_seen)
  194. return "ERROR"
  195. if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
  196. return "FAIL"
  197. all_skipped = 1
  198. for (k in test_results_seen)
  199. if (k != "SKIP")
  200. all_skipped = 0
  201. if (all_skipped)
  202. return "SKIP"
  203. return "PASS";
  204. }
  205. function stringify_result_obj(result_obj)
  206. {
  207. if (result_obj["is_unplanned"] || result_obj["number"] != testno)
  208. return "ERROR"
  209. if (plan_seen == LATE_PLAN)
  210. return "ERROR"
  211. if (result_obj["directive"] == "TODO")
  212. return result_obj["is_ok"] ? "XPASS" : "XFAIL"
  213. if (result_obj["directive"] == "SKIP")
  214. return result_obj["is_ok"] ? "SKIP" : COOKED_FAIL;
  215. if (length(result_obj["directive"]))
  216. abort("in function stringify_result_obj()")
  217. return result_obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
  218. }
  219. function decorate_result(result)
  220. {
  221. color_name = color_for_result[result]
  222. if (color_name)
  223. return color_map[color_name] "" result "" color_map["std"]
  224. # If we are not using colorized output, or if we do not know how
  225. # to colorize the given result, we should return it unchanged.
  226. return result
  227. }
  228. function report(result, details)
  229. {
  230. if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
  231. {
  232. msg = ": " test_script_name
  233. add_test_result(result)
  234. }
  235. else if (result == "#")
  236. {
  237. msg = " " test_script_name ":"
  238. }
  239. else
  240. {
  241. abort("in function report()")
  242. }
  243. if (length(details))
  244. msg = msg " " details
  245. # Output on console might be colorized.
  246. print decorate_result(result) msg
  247. # Log the result in the log file too, to help debugging (this is
  248. # especially true when said result is a TAP error or "Bail out!").
  249. print result msg | "cat >&3";
  250. }
  251. function testsuite_error(error_message)
  252. {
  253. report("ERROR", "- " error_message)
  254. }
  255. function handle_tap_result()
  256. {
  257. details = result_obj["number"];
  258. if (length(result_obj["description"]))
  259. details = details " " result_obj["description"]
  260. if (plan_seen == LATE_PLAN)
  261. {
  262. details = details " # AFTER LATE PLAN";
  263. }
  264. else if (result_obj["is_unplanned"])
  265. {
  266. details = details " # UNPLANNED";
  267. }
  268. else if (result_obj["number"] != testno)
  269. {
  270. details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
  271. details, testno);
  272. }
  273. else if (result_obj["directive"])
  274. {
  275. details = details " # " result_obj["directive"];
  276. if (length(result_obj["explanation"]))
  277. details = details " " result_obj["explanation"]
  278. }
  279. report(stringify_result_obj(result_obj), details)
  280. }
  281. # `skip_reason` should be empty whenever planned > 0.
  282. function handle_tap_plan(planned, skip_reason)
  283. {
  284. planned += 0 # Avoid getting confused if, say, `planned` is "00"
  285. if (length(skip_reason) && planned > 0)
  286. abort("in function handle_tap_plan()")
  287. if (plan_seen)
  288. {
  289. # Error, only one plan per stream is acceptable.
  290. testsuite_error("multiple test plans")
  291. return;
  292. }
  293. planned_tests = planned
  294. # The TAP plan can come before or after *all* the TAP results; we speak
  295. # respectively of an "early" or a "late" plan. If we see the plan line
  296. # after at least one TAP result has been seen, assume we have a late
  297. # plan; in this case, any further test result seen after the plan will
  298. # be flagged as an error.
  299. plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
  300. # If testno > 0, we have an error ("too many tests run") that will be
  301. # automatically dealt with later, so do not worry about it here. If
  302. # $plan_seen is true, we have an error due to a repeated plan, and that
  303. # has already been dealt with above. Otherwise, we have a valid "plan
  304. # with SKIP" specification, and should report it as a particular kind
  305. # of SKIP result.
  306. if (planned == 0 && testno == 0)
  307. {
  308. if (length(skip_reason))
  309. skip_reason = "- " skip_reason;
  310. report("SKIP", skip_reason);
  311. }
  312. }
  313. function extract_tap_comment(line)
  314. {
  315. if (index(line, diag_string) == 1)
  316. {
  317. # Strip leading `diag_string` from `line`.
  318. line = substr(line, length(diag_string) + 1)
  319. # And strip any leading and trailing whitespace left.
  320. sub("^[ \t]*", "", line)
  321. sub("[ \t]*$", "", line)
  322. # Return what is left (if any).
  323. return line;
  324. }
  325. return "";
  326. }
  327. # When this function is called, we know that line is a TAP result line,
  328. # so that it matches the (perl) RE "^(not )?ok\b".
  329. function setup_result_obj(line)
  330. {
  331. # Get the result, and remove it from the line.
  332. result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
  333. sub("^(not )?ok[ \t]*", "", line)
  334. # If the result has an explicit number, get it and strip it; otherwise,
  335. # automatically assing the next progresive number to it.
  336. if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
  337. {
  338. match(line, "^[0-9]+")
  339. # The final `+ 0` is to normalize numbers with leading zeros.
  340. result_obj["number"] = substr(line, 1, RLENGTH) + 0
  341. line = substr(line, RLENGTH + 1)
  342. }
  343. else
  344. {
  345. result_obj["number"] = testno
  346. }
  347. if (plan_seen == LATE_PLAN)
  348. # No further test results are acceptable after a "late" TAP plan
  349. # has been seen.
  350. result_obj["is_unplanned"] = 1
  351. else if (plan_seen && testno > planned_tests)
  352. result_obj["is_unplanned"] = 1
  353. else
  354. result_obj["is_unplanned"] = 0
  355. # Strip trailing and leading whitespace.
  356. sub("^[ \t]*", "", line)
  357. sub("[ \t]*$", "", line)
  358. # This will have to be corrected if we have a "TODO"/"SKIP" directive.
  359. result_obj["description"] = line
  360. result_obj["directive"] = ""
  361. result_obj["explanation"] = ""
  362. if (index(line, "#") == 0)
  363. return # No possible directive, nothing more to do.
  364. # Directives are case-insensitive.
  365. rx = "[ \t]*#[ \t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \t]*"
  366. # See whether we have the directive, and if yes, where.
  367. pos = match(line, rx "$")
  368. if (!pos)
  369. pos = match(line, rx "[^a-zA-Z0-9_]")
  370. # If there was no TAP directive, we have nothing more to do.
  371. if (!pos)
  372. return
  373. # Let`s now see if the TAP directive has been escaped. For example:
  374. # escaped: ok \# SKIP
  375. # not escaped: ok \\# SKIP
  376. # escaped: ok \\\\\# SKIP
  377. # not escaped: ok \ # SKIP
  378. if (substr(line, pos, 1) == "#")
  379. {
  380. bslash_count = 0
  381. for (i = pos; i > 1 && substr(line, i - 1, 1) == "\\"; i--)
  382. bslash_count += 1
  383. if (bslash_count % 2)
  384. return # Directive was escaped.
  385. }
  386. # Strip the directive and its explanation (if any) from the test
  387. # description.
  388. result_obj["description"] = substr(line, 1, pos - 1)
  389. # Now remove the test description from the line, that has been dealt
  390. # with already.
  391. line = substr(line, pos)
  392. # Strip the directive, and save its value (normalized to upper case).
  393. sub("^[ \t]*#[ \t]*", "", line)
  394. result_obj["directive"] = toupper(substr(line, 1, 4))
  395. line = substr(line, 5)
  396. # Now get the explanation for the directive (if any), with leading
  397. # and trailing whitespace removed.
  398. sub("^[ \t]*", "", line)
  399. sub("[ \t]*$", "", line)
  400. result_obj["explanation"] = line
  401. }
  402. function get_test_exit_message(status)
  403. {
  404. if (status == 0)
  405. return ""
  406. if (status !~ /^[1-9][0-9]*$/)
  407. abort("getting exit status")
  408. if (status < 127)
  409. exit_details = ""
  410. else if (status == 127)
  411. exit_details = " (command not found?)"
  412. else if (status >= 128 && status <= 255)
  413. exit_details = sprintf(" (terminated by signal %d?)", status - 128)
  414. else if (status > 256 && status <= 384)
  415. # We used to report an "abnormal termination" here, but some Korn
  416. # shells, when a child process die due to signal number n, can leave
  417. # in $? an exit status of 256+n instead of the more standard 128+n.
  418. # Apparently, both behaviours are allowed by POSIX (2008), so be
  419. # prepared to handle them both. See also Austing Group report ID
  420. # 0000051 <http://www.austingroupbugs.net/view.php?id=51>
  421. exit_details = sprintf(" (terminated by signal %d?)", status - 256)
  422. else
  423. # Never seen in practice.
  424. exit_details = " (abnormal termination)"
  425. return sprintf("exited with status %d%s", status, exit_details)
  426. }
  427. function write_test_results()
  428. {
  429. print ":global-test-result: " get_global_test_result() > trs_file
  430. print ":recheck: " yn(must_recheck()) > trs_file
  431. print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
  432. for (i = 0; i < test_results_index; i += 1)
  433. print ":test-result: " test_results_list[i] > trs_file
  434. close(trs_file);
  435. }
  436. BEGIN {
  437. ## ------- ##
  438. ## SETUP ##
  439. ## ------- ##
  440. '"$init_colors"'
  441. # Properly initialized once the TAP plan is seen.
  442. planned_tests = 0
  443. COOKED_PASS = expect_failure ? "XPASS": "PASS";
  444. COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
  445. # Enumeration-like constants to remember which kind of plan (if any)
  446. # has been seen. It is important that NO_PLAN evaluates "false" as
  447. # a boolean.
  448. NO_PLAN = 0
  449. EARLY_PLAN = 1
  450. LATE_PLAN = 2
  451. testno = 0 # Number of test results seen so far.
  452. bailed_out = 0 # Whether a "Bail out!" directive has been seen.
  453. # Whether the TAP plan has been seen or not, and if yes, which kind
  454. # it is ("early" is seen before any test result, "late" otherwise).
  455. plan_seen = NO_PLAN
  456. ## --------- ##
  457. ## PARSING ##
  458. ## --------- ##
  459. is_first_read = 1
  460. while (1)
  461. {
  462. # Involutions required so that we are able to read the exit status
  463. # from the last input line.
  464. st = getline
  465. if (st < 0) # I/O error.
  466. fatal("I/O error while reading from input stream")
  467. else if (st == 0) # End-of-input
  468. {
  469. if (is_first_read)
  470. abort("in input loop: only one input line")
  471. break
  472. }
  473. if (is_first_read)
  474. {
  475. is_first_read = 0
  476. nextline = $0
  477. continue
  478. }
  479. else
  480. {
  481. curline = nextline
  482. nextline = $0
  483. $0 = curline
  484. }
  485. # Copy any input line verbatim into the log file.
  486. print | "cat >&3"
  487. # Parsing of TAP input should stop after a "Bail out!" directive.
  488. if (bailed_out)
  489. continue
  490. # TAP test result.
  491. if ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/)
  492. {
  493. testno += 1
  494. setup_result_obj($0)
  495. handle_tap_result()
  496. }
  497. # TAP plan (normal or "SKIP" without explanation).
  498. else if ($0 ~ /^1\.\.[0-9]+[ \t]*$/)
  499. {
  500. # The next two lines will put the number of planned tests in $0.
  501. sub("^1\\.\\.", "")
  502. sub("[^0-9]*$", "")
  503. handle_tap_plan($0, "")
  504. continue
  505. }
  506. # TAP "SKIP" plan, with an explanation.
  507. else if ($0 ~ /^1\.\.0+[ \t]*#/)
  508. {
  509. # The next lines will put the skip explanation in $0, stripping
  510. # any leading and trailing whitespace. This is a little more
  511. # tricky in truth, since we want to also strip a potential leading
  512. # "SKIP" string from the message.
  513. sub("^[^#]*#[ \t]*(SKIP[: \t][ \t]*)?", "")
  514. sub("[ \t]*$", "");
  515. handle_tap_plan(0, $0)
  516. }
  517. # "Bail out!" magic.
  518. # Older versions of prove and TAP::Harness (e.g., 3.17) did not
  519. # recognize a "Bail out!" directive when preceded by leading
  520. # whitespace, but more modern versions (e.g., 3.23) do. So we
  521. # emulate the latter, "more modern" behaviour.
  522. else if ($0 ~ /^[ \t]*Bail out!/)
  523. {
  524. bailed_out = 1
  525. # Get the bailout message (if any), with leading and trailing
  526. # whitespace stripped. The message remains stored in `$0`.
  527. sub("^[ \t]*Bail out![ \t]*", "");
  528. sub("[ \t]*$", "");
  529. # Format the error message for the
  530. bailout_message = "Bail out!"
  531. if (length($0))
  532. bailout_message = bailout_message " " $0
  533. testsuite_error(bailout_message)
  534. }
  535. # Maybe we have too look for dianogtic comments too.
  536. else if (comments != 0)
  537. {
  538. comment = extract_tap_comment($0);
  539. if (length(comment))
  540. report("#", comment);
  541. }
  542. }
  543. ## -------- ##
  544. ## FINISH ##
  545. ## -------- ##
  546. # A "Bail out!" directive should cause us to ignore any following TAP
  547. # error, as well as a non-zero exit status from the TAP producer.
  548. if (!bailed_out)
  549. {
  550. if (!plan_seen)
  551. {
  552. testsuite_error("missing test plan")
  553. }
  554. else if (planned_tests != testno)
  555. {
  556. bad_amount = testno > planned_tests ? "many" : "few"
  557. testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
  558. bad_amount, planned_tests, testno))
  559. }
  560. if (!ignore_exit)
  561. {
  562. # Fetch exit status from the last line.
  563. exit_message = get_test_exit_message(nextline)
  564. if (exit_message)
  565. testsuite_error(exit_message)
  566. }
  567. }
  568. write_test_results()
  569. exit 0
  570. } # End of "BEGIN" block.
  571. '
  572. # TODO: document that we consume the file descriptor 3 :-(
  573. } 3>"$log_file"
  574. test $? -eq 0 || fatal "I/O or internal error"
  575. # Local Variables:
  576. # mode: shell-script
  577. # sh-indentation: 2
  578. # eval: (add-hook 'write-file-hooks 'time-stamp)
  579. # time-stamp-start: "scriptversion="
  580. # time-stamp-format: "%:y-%02m-%02d.%02H"
  581. # time-stamp-time-zone: "UTC"
  582. # time-stamp-end: "; # UTC"
  583. # End: