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