The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

651 lines
19 KiB

2 months ago
  1. #! /bin/sh
  2. # Copyright (C) 2011-2013 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=2013-12-23.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. # TODO: the usages of "cat >&3" below could be optimized when using
  144. # 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. function get_global_test_result()
  191. {
  192. if ("ERROR" in test_results_seen)
  193. return "ERROR"
  194. if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
  195. return "FAIL"
  196. all_skipped = 1
  197. for (k in test_results_seen)
  198. if (k != "SKIP")
  199. all_skipped = 0
  200. if (all_skipped)
  201. return "SKIP"
  202. return "PASS";
  203. }
  204. function stringify_result_obj(result_obj)
  205. {
  206. if (result_obj["is_unplanned"] || result_obj["number"] != testno)
  207. return "ERROR"
  208. if (plan_seen == LATE_PLAN)
  209. return "ERROR"
  210. if (result_obj["directive"] == "TODO")
  211. return result_obj["is_ok"] ? "XPASS" : "XFAIL"
  212. if (result_obj["directive"] == "SKIP")
  213. return result_obj["is_ok"] ? "SKIP" : COOKED_FAIL;
  214. if (length(result_obj["directive"]))
  215. abort("in function stringify_result_obj()")
  216. return result_obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
  217. }
  218. function decorate_result(result)
  219. {
  220. color_name = color_for_result[result]
  221. if (color_name)
  222. return color_map[color_name] "" result "" color_map["std"]
  223. # If we are not using colorized output, or if we do not know how
  224. # to colorize the given result, we should return it unchanged.
  225. return result
  226. }
  227. function report(result, details)
  228. {
  229. if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
  230. {
  231. msg = ": " test_script_name
  232. add_test_result(result)
  233. }
  234. else if (result == "#")
  235. {
  236. msg = " " test_script_name ":"
  237. }
  238. else
  239. {
  240. abort("in function report()")
  241. }
  242. if (length(details))
  243. msg = msg " " details
  244. # Output on console might be colorized.
  245. print decorate_result(result) msg
  246. # Log the result in the log file too, to help debugging (this is
  247. # especially true when said result is a TAP error or "Bail out!").
  248. print result msg | "cat >&3";
  249. }
  250. function testsuite_error(error_message)
  251. {
  252. report("ERROR", "- " error_message)
  253. }
  254. function handle_tap_result()
  255. {
  256. details = result_obj["number"];
  257. if (length(result_obj["description"]))
  258. details = details " " result_obj["description"]
  259. if (plan_seen == LATE_PLAN)
  260. {
  261. details = details " # AFTER LATE PLAN";
  262. }
  263. else if (result_obj["is_unplanned"])
  264. {
  265. details = details " # UNPLANNED";
  266. }
  267. else if (result_obj["number"] != testno)
  268. {
  269. details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
  270. details, testno);
  271. }
  272. else if (result_obj["directive"])
  273. {
  274. details = details " # " result_obj["directive"];
  275. if (length(result_obj["explanation"]))
  276. details = details " " result_obj["explanation"]
  277. }
  278. report(stringify_result_obj(result_obj), details)
  279. }
  280. # `skip_reason` should be empty whenever planned > 0.
  281. function handle_tap_plan(planned, skip_reason)
  282. {
  283. planned += 0 # Avoid getting confused if, say, `planned` is "00"
  284. if (length(skip_reason) && planned > 0)
  285. abort("in function handle_tap_plan()")
  286. if (plan_seen)
  287. {
  288. # Error, only one plan per stream is acceptable.
  289. testsuite_error("multiple test plans")
  290. return;
  291. }
  292. planned_tests = planned
  293. # The TAP plan can come before or after *all* the TAP results; we speak
  294. # respectively of an "early" or a "late" plan. If we see the plan line
  295. # after at least one TAP result has been seen, assume we have a late
  296. # plan; in this case, any further test result seen after the plan will
  297. # be flagged as an error.
  298. plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
  299. # If testno > 0, we have an error ("too many tests run") that will be
  300. # automatically dealt with later, so do not worry about it here. If
  301. # $plan_seen is true, we have an error due to a repeated plan, and that
  302. # has already been dealt with above. Otherwise, we have a valid "plan
  303. # with SKIP" specification, and should report it as a particular kind
  304. # of SKIP result.
  305. if (planned == 0 && testno == 0)
  306. {
  307. if (length(skip_reason))
  308. skip_reason = "- " skip_reason;
  309. report("SKIP", skip_reason);
  310. }
  311. }
  312. function extract_tap_comment(line)
  313. {
  314. if (index(line, diag_string) == 1)
  315. {
  316. # Strip leading `diag_string` from `line`.
  317. line = substr(line, length(diag_string) + 1)
  318. # And strip any leading and trailing whitespace left.
  319. sub("^[ \t]*", "", line)
  320. sub("[ \t]*$", "", line)
  321. # Return what is left (if any).
  322. return line;
  323. }
  324. return "";
  325. }
  326. # When this function is called, we know that line is a TAP result line,
  327. # so that it matches the (perl) RE "^(not )?ok\b".
  328. function setup_result_obj(line)
  329. {
  330. # Get the result, and remove it from the line.
  331. result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
  332. sub("^(not )?ok[ \t]*", "", line)
  333. # If the result has an explicit number, get it and strip it; otherwise,
  334. # automatically assing the next progresive number to it.
  335. if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
  336. {
  337. match(line, "^[0-9]+")
  338. # The final `+ 0` is to normalize numbers with leading zeros.
  339. result_obj["number"] = substr(line, 1, RLENGTH) + 0
  340. line = substr(line, RLENGTH + 1)
  341. }
  342. else
  343. {
  344. result_obj["number"] = testno
  345. }
  346. if (plan_seen == LATE_PLAN)
  347. # No further test results are acceptable after a "late" TAP plan
  348. # has been seen.
  349. result_obj["is_unplanned"] = 1
  350. else if (plan_seen && testno > planned_tests)
  351. result_obj["is_unplanned"] = 1
  352. else
  353. result_obj["is_unplanned"] = 0
  354. # Strip trailing and leading whitespace.
  355. sub("^[ \t]*", "", line)
  356. sub("[ \t]*$", "", line)
  357. # This will have to be corrected if we have a "TODO"/"SKIP" directive.
  358. result_obj["description"] = line
  359. result_obj["directive"] = ""
  360. result_obj["explanation"] = ""
  361. if (index(line, "#") == 0)
  362. return # No possible directive, nothing more to do.
  363. # Directives are case-insensitive.
  364. rx = "[ \t]*#[ \t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \t]*"
  365. # See whether we have the directive, and if yes, where.
  366. pos = match(line, rx "$")
  367. if (!pos)
  368. pos = match(line, rx "[^a-zA-Z0-9_]")
  369. # If there was no TAP directive, we have nothing more to do.
  370. if (!pos)
  371. return
  372. # Let`s now see if the TAP directive has been escaped. For example:
  373. # escaped: ok \# SKIP
  374. # not escaped: ok \\# SKIP
  375. # escaped: ok \\\\\# SKIP
  376. # not escaped: ok \ # SKIP
  377. if (substr(line, pos, 1) == "#")
  378. {
  379. bslash_count = 0
  380. for (i = pos; i > 1 && substr(line, i - 1, 1) == "\\"; i--)
  381. bslash_count += 1
  382. if (bslash_count % 2)
  383. return # Directive was escaped.
  384. }
  385. # Strip the directive and its explanation (if any) from the test
  386. # description.
  387. result_obj["description"] = substr(line, 1, pos - 1)
  388. # Now remove the test description from the line, that has been dealt
  389. # with already.
  390. line = substr(line, pos)
  391. # Strip the directive, and save its value (normalized to upper case).
  392. sub("^[ \t]*#[ \t]*", "", line)
  393. result_obj["directive"] = toupper(substr(line, 1, 4))
  394. line = substr(line, 5)
  395. # Now get the explanation for the directive (if any), with leading
  396. # and trailing whitespace removed.
  397. sub("^[ \t]*", "", line)
  398. sub("[ \t]*$", "", line)
  399. result_obj["explanation"] = line
  400. }
  401. function get_test_exit_message(status)
  402. {
  403. if (status == 0)
  404. return ""
  405. if (status !~ /^[1-9][0-9]*$/)
  406. abort("getting exit status")
  407. if (status < 127)
  408. exit_details = ""
  409. else if (status == 127)
  410. exit_details = " (command not found?)"
  411. else if (status >= 128 && status <= 255)
  412. exit_details = sprintf(" (terminated by signal %d?)", status - 128)
  413. else if (status > 256 && status <= 384)
  414. # We used to report an "abnormal termination" here, but some Korn
  415. # shells, when a child process die due to signal number n, can leave
  416. # in $? an exit status of 256+n instead of the more standard 128+n.
  417. # Apparently, both behaviours are allowed by POSIX (2008), so be
  418. # prepared to handle them both. See also Austing Group report ID
  419. # 0000051 <http://www.austingroupbugs.net/view.php?id=51>
  420. exit_details = sprintf(" (terminated by signal %d?)", status - 256)
  421. else
  422. # Never seen in practice.
  423. exit_details = " (abnormal termination)"
  424. return sprintf("exited with status %d%s", status, exit_details)
  425. }
  426. function write_test_results()
  427. {
  428. print ":global-test-result: " get_global_test_result() > trs_file
  429. print ":recheck: " yn(must_recheck()) > trs_file
  430. print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
  431. for (i = 0; i < test_results_index; i += 1)
  432. print ":test-result: " test_results_list[i] > trs_file
  433. close(trs_file);
  434. }
  435. BEGIN {
  436. ## ------- ##
  437. ## SETUP ##
  438. ## ------- ##
  439. '"$init_colors"'
  440. # Properly initialized once the TAP plan is seen.
  441. planned_tests = 0
  442. COOKED_PASS = expect_failure ? "XPASS": "PASS";
  443. COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
  444. # Enumeration-like constants to remember which kind of plan (if any)
  445. # has been seen. It is important that NO_PLAN evaluates "false" as
  446. # a boolean.
  447. NO_PLAN = 0
  448. EARLY_PLAN = 1
  449. LATE_PLAN = 2
  450. testno = 0 # Number of test results seen so far.
  451. bailed_out = 0 # Whether a "Bail out!" directive has been seen.
  452. # Whether the TAP plan has been seen or not, and if yes, which kind
  453. # it is ("early" is seen before any test result, "late" otherwise).
  454. plan_seen = NO_PLAN
  455. ## --------- ##
  456. ## PARSING ##
  457. ## --------- ##
  458. is_first_read = 1
  459. while (1)
  460. {
  461. # Involutions required so that we are able to read the exit status
  462. # from the last input line.
  463. st = getline
  464. if (st < 0) # I/O error.
  465. fatal("I/O error while reading from input stream")
  466. else if (st == 0) # End-of-input
  467. {
  468. if (is_first_read)
  469. abort("in input loop: only one input line")
  470. break
  471. }
  472. if (is_first_read)
  473. {
  474. is_first_read = 0
  475. nextline = $0
  476. continue
  477. }
  478. else
  479. {
  480. curline = nextline
  481. nextline = $0
  482. $0 = curline
  483. }
  484. # Copy any input line verbatim into the log file.
  485. print | "cat >&3"
  486. # Parsing of TAP input should stop after a "Bail out!" directive.
  487. if (bailed_out)
  488. continue
  489. # TAP test result.
  490. if ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/)
  491. {
  492. testno += 1
  493. setup_result_obj($0)
  494. handle_tap_result()
  495. }
  496. # TAP plan (normal or "SKIP" without explanation).
  497. else if ($0 ~ /^1\.\.[0-9]+[ \t]*$/)
  498. {
  499. # The next two lines will put the number of planned tests in $0.
  500. sub("^1\\.\\.", "")
  501. sub("[^0-9]*$", "")
  502. handle_tap_plan($0, "")
  503. continue
  504. }
  505. # TAP "SKIP" plan, with an explanation.
  506. else if ($0 ~ /^1\.\.0+[ \t]*#/)
  507. {
  508. # The next lines will put the skip explanation in $0, stripping
  509. # any leading and trailing whitespace. This is a little more
  510. # tricky in truth, since we want to also strip a potential leading
  511. # "SKIP" string from the message.
  512. sub("^[^#]*#[ \t]*(SKIP[: \t][ \t]*)?", "")
  513. sub("[ \t]*$", "");
  514. handle_tap_plan(0, $0)
  515. }
  516. # "Bail out!" magic.
  517. # Older versions of prove and TAP::Harness (e.g., 3.17) did not
  518. # recognize a "Bail out!" directive when preceded by leading
  519. # whitespace, but more modern versions (e.g., 3.23) do. So we
  520. # emulate the latter, "more modern" behaviour.
  521. else if ($0 ~ /^[ \t]*Bail out!/)
  522. {
  523. bailed_out = 1
  524. # Get the bailout message (if any), with leading and trailing
  525. # whitespace stripped. The message remains stored in `$0`.
  526. sub("^[ \t]*Bail out![ \t]*", "");
  527. sub("[ \t]*$", "");
  528. # Format the error message for the
  529. bailout_message = "Bail out!"
  530. if (length($0))
  531. bailout_message = bailout_message " " $0
  532. testsuite_error(bailout_message)
  533. }
  534. # Maybe we have too look for dianogtic comments too.
  535. else if (comments != 0)
  536. {
  537. comment = extract_tap_comment($0);
  538. if (length(comment))
  539. report("#", comment);
  540. }
  541. }
  542. ## -------- ##
  543. ## FINISH ##
  544. ## -------- ##
  545. # A "Bail out!" directive should cause us to ignore any following TAP
  546. # error, as well as a non-zero exit status from the TAP producer.
  547. if (!bailed_out)
  548. {
  549. if (!plan_seen)
  550. {
  551. testsuite_error("missing test plan")
  552. }
  553. else if (planned_tests != testno)
  554. {
  555. bad_amount = testno > planned_tests ? "many" : "few"
  556. testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
  557. bad_amount, planned_tests, testno))
  558. }
  559. if (!ignore_exit)
  560. {
  561. # Fetch exit status from the last line.
  562. exit_message = get_test_exit_message(nextline)
  563. if (exit_message)
  564. testsuite_error(exit_message)
  565. }
  566. }
  567. write_test_results()
  568. exit 0
  569. } # End of "BEGIN" block.
  570. '
  571. # TODO: document that we consume the file descriptor 3 :-(
  572. } 3>"$log_file"
  573. test $? -eq 0 || fatal "I/O or internal error"
  574. # Local Variables:
  575. # mode: shell-script
  576. # sh-indentation: 2
  577. # eval: (add-hook 'write-file-hooks 'time-stamp)
  578. # time-stamp-start: "scriptversion="
  579. # time-stamp-format: "%:y-%02m-%02d.%02H"
  580. # time-stamp-time-zone: "UTC"
  581. # time-stamp-end: "; # UTC"
  582. # End: