Blame script/tap-driver

Packit Service 7770af
#! /bin/sh
Packit Service 7770af
# Copyright (C) 2011-2013 Free Software Foundation, Inc.
Packit Service 7770af
#
Packit Service 7770af
# This program is free software; you can redistribute it and/or modify
Packit Service 7770af
# it under the terms of the GNU General Public License as published by
Packit Service 7770af
# the Free Software Foundation; either version 2, or (at your option)
Packit Service 7770af
# any later version.
Packit Service 7770af
#
Packit Service 7770af
# This program is distributed in the hope that it will be useful,
Packit Service 7770af
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 7770af
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 7770af
# GNU General Public License for more details.
Packit Service 7770af
#
Packit Service 7770af
# You should have received a copy of the GNU General Public License
Packit Service 7770af
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service 7770af
Packit Service 7770af
# As a special exception to the GNU General Public License, if you
Packit Service 7770af
# distribute this file as part of a program that contains a
Packit Service 7770af
# configuration script generated by Autoconf, you may include it under
Packit Service 7770af
# the same distribution terms that you use for the rest of that program.
Packit Service 7770af
Packit Service 7770af
# This file is maintained in Automake, please report
Packit Service 7770af
# bugs to <bug-automake@gnu.org> or send patches to
Packit Service 7770af
# <automake-patches@gnu.org>.
Packit Service 7770af
Packit Service 7770af
scriptversion=2011-12-27.17; # UTC
Packit Service 7770af
Packit Service 7770af
# Make unconditional expansion of undefined variables an error.  This
Packit Service 7770af
# helps a lot in preventing typo-related bugs.
Packit Service 7770af
set -u
Packit Service 7770af
Packit Service 7770af
me=tap-driver.sh
Packit Service 7770af
Packit Service 7770af
fatal ()
Packit Service 7770af
{
Packit Service 7770af
  echo "$me: fatal: $*" >&2
Packit Service 7770af
  exit 1
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
usage_error ()
Packit Service 7770af
{
Packit Service 7770af
  echo "$me: $*" >&2
Packit Service 7770af
  print_usage >&2
Packit Service 7770af
  exit 2
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
print_usage ()
Packit Service 7770af
{
Packit Service 7770af
  cat <
Packit Service 7770af
Usage:
Packit Service 7770af
  tap-driver.sh --test-name=NAME --log-file=PATH --trs-file=PATH
Packit Service 7770af
                [--expect-failure={yes|no}] [--color-tests={yes|no}]
Packit Service 7770af
                [--enable-hard-errors={yes|no}] [--ignore-exit]
Packit Service 7770af
                [--diagnostic-string=STRING] [--merge|--no-merge]
Packit Service 7770af
                [--comments|--no-comments] [--] TEST-COMMAND
Packit Service 7770af
The \`--test-name', \`--log-file' and \`--trs-file' options are mandatory.
Packit Service 7770af
END
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# TODO: better error handling in option parsing (in particular, ensure
Packit Service 7770af
# TODO: $log_file, $trs_file and $test_name are defined).
Packit Service 7770af
test_name= # Used for reporting.
Packit Service 7770af
log_file=  # Where to save the result and output of the test script.
Packit Service 7770af
trs_file=  # Where to save the metadata of the test run.
Packit Service 7770af
expect_failure=0
Packit Service 7770af
color_tests=0
Packit Service 7770af
merge=0
Packit Service 7770af
ignore_exit=0
Packit Service 7770af
comments=0
Packit Service 7770af
diag_string='#'
Packit Service 7770af
while test $# -gt 0; do
Packit Service 7770af
  case $1 in
Packit Service 7770af
  --help) print_usage; exit $?;;
Packit Service 7770af
  --version) echo "$me $scriptversion"; exit $?;;
Packit Service 7770af
  --test-name) test_name=$2; shift;;
Packit Service 7770af
  --log-file) log_file=$2; shift;;
Packit Service 7770af
  --trs-file) trs_file=$2; shift;;
Packit Service 7770af
  --color-tests) color_tests=$2; shift;;
Packit Service 7770af
  --expect-failure) expect_failure=$2; shift;;
Packit Service 7770af
  --enable-hard-errors) shift;; # No-op.
Packit Service 7770af
  --merge) merge=1;;
Packit Service 7770af
  --no-merge) merge=0;;
Packit Service 7770af
  --ignore-exit) ignore_exit=1;;
Packit Service 7770af
  --comments) comments=1;;
Packit Service 7770af
  --no-comments) comments=0;;
Packit Service 7770af
  --diagnostic-string) diag_string=$2; shift;;
Packit Service 7770af
  --) shift; break;;
Packit Service 7770af
  -*) usage_error "invalid option: '$1'";;
Packit Service 7770af
  esac
Packit Service 7770af
  shift
Packit Service 7770af
done
Packit Service 7770af
Packit Service 7770af
test $# -gt 0 || usage_error "missing test command"
Packit Service 7770af
Packit Service 7770af
case $expect_failure in
Packit Service 7770af
  yes) expect_failure=1;;
Packit Service 7770af
    *) expect_failure=0;;
Packit Service 7770af
esac
Packit Service 7770af
Packit Service 7770af
if test $color_tests = yes; then
Packit Service 7770af
  init_colors='
Packit Service 7770af
    color_map["red"]="?[0;31m" # Red.
Packit Service 7770af
    color_map["grn"]="?[0;32m" # Green.
Packit Service 7770af
    color_map["lgn"]="?[1;32m" # Light green.
Packit Service 7770af
    color_map["blu"]="?[1;34m" # Blue.
Packit Service 7770af
    color_map["mgn"]="?[0;35m" # Magenta.
Packit Service 7770af
    color_map["std"]="?[m"     # No color.
Packit Service 7770af
    color_for_result["ERROR"] = "mgn"
Packit Service 7770af
    color_for_result["PASS"]  = "grn"
Packit Service 7770af
    color_for_result["XPASS"] = "red"
Packit Service 7770af
    color_for_result["FAIL"]  = "red"
Packit Service 7770af
    color_for_result["XFAIL"] = "lgn"
Packit Service 7770af
    color_for_result["SKIP"]  = "blu"'
Packit Service 7770af
else
Packit Service 7770af
  init_colors=''
Packit Service 7770af
fi
Packit Service 7770af
Packit Service 7770af
# :; is there to work around a bug in bash 3.2 (and earlier) which
Packit Service 7770af
# does not always set '$?' properly on redirection failure.
Packit Service 7770af
# See the Autoconf manual for more details.
Packit Service 7770af
:;{
Packit Service 7770af
  (
Packit Service 7770af
    # Ignore common signals (in this subshell only!), to avoid potential
Packit Service 7770af
    # problems with Korn shells.  Some Korn shells are known to propagate
Packit Service 7770af
    # to themselves signals that have killed a child process they were
Packit Service 7770af
    # waiting for; this is done at least for SIGINT (and usually only for
Packit Service 7770af
    # it, in truth).  Without the `trap' below, such a behaviour could
Packit Service 7770af
    # cause a premature exit in the current subshell, e.g., in case the
Packit Service 7770af
    # test command it runs gets terminated by a SIGINT.  Thus, the awk
Packit Service 7770af
    # script we are piping into would never seen the exit status it
Packit Service 7770af
    # expects on its last input line (which is displayed below by the
Packit Service 7770af
    # last `echo $?' statement), and would thus die reporting an internal
Packit Service 7770af
    # error.
Packit Service 7770af
    # For more information, see the Autoconf manual and the threads:
Packit Service 7770af
    # <http://lists.gnu.org/archive/html/bug-autoconf/2011-09/msg00004.html>
Packit Service 7770af
    # <http://mail.opensolaris.org/pipermail/ksh93-integration-discuss/2009-February/004121.html>
Packit Service 7770af
    trap : 1 3 2 13 15
Packit Service 7770af
    if test $merge -gt 0; then
Packit Service 7770af
      exec 2>&1
Packit Service 7770af
    else
Packit Service 7770af
      exec 2>&3
Packit Service 7770af
    fi
Packit Service 7770af
    "$@"
Packit Service 7770af
    echo $?
Packit Service 7770af
  ) | LC_ALL=C ${AM_TAP_AWK-awk} \
Packit Service 7770af
        -v me="$me" \
Packit Service 7770af
        -v test_script_name="$test_name" \
Packit Service 7770af
        -v log_file="$log_file" \
Packit Service 7770af
        -v trs_file="$trs_file" \
Packit Service 7770af
        -v expect_failure="$expect_failure" \
Packit Service 7770af
        -v merge="$merge" \
Packit Service 7770af
        -v ignore_exit="$ignore_exit" \
Packit Service 7770af
        -v comments="$comments" \
Packit Service 7770af
        -v diag_string="$diag_string" \
Packit Service 7770af
'
Packit Service 7770af
# FIXME: the usages of "cat >&3" below could be optimized when using
Packit Service 7770af
# FIXME: GNU awk, and/on on systems that supports /dev/fd/.
Packit Service 7770af
Packit Service 7770af
# Implementation note: in what follows, `result_obj` will be an
Packit Service 7770af
# associative array that (partly) simulates a TAP result object
Packit Service 7770af
# from the `TAP::Parser` perl module.
Packit Service 7770af
Packit Service 7770af
## ----------- ##
Packit Service 7770af
##  FUNCTIONS  ##
Packit Service 7770af
## ----------- ##
Packit Service 7770af
Packit Service 7770af
function fatal(msg)
Packit Service 7770af
{
Packit Service 7770af
  print me ": " msg | "cat >&2"
Packit Service 7770af
  exit 1
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function abort(where)
Packit Service 7770af
{
Packit Service 7770af
  fatal("internal error " where)
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# Convert a boolean to a "yes"/"no" string.
Packit Service 7770af
function yn(bool)
Packit Service 7770af
{
Packit Service 7770af
  return bool ? "yes" : "no";
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function add_test_result(result)
Packit Service 7770af
{
Packit Service 7770af
  if (!test_results_index)
Packit Service 7770af
    test_results_index = 0
Packit Service 7770af
  test_results_list[test_results_index] = result
Packit Service 7770af
  test_results_index += 1
Packit Service 7770af
  test_results_seen[result] = 1;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# Whether the test script should be re-run by "make recheck".
Packit Service 7770af
function must_recheck()
Packit Service 7770af
{
Packit Service 7770af
  for (k in test_results_seen)
Packit Service 7770af
    if (k != "XFAIL" && k != "PASS" && k != "SKIP")
Packit Service 7770af
      return 1
Packit Service 7770af
  return 0
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# Whether the content of the log file associated to this test should
Packit Service 7770af
# be copied into the "global" test-suite.log.
Packit Service 7770af
function copy_in_global_log()
Packit Service 7770af
{
Packit Service 7770af
  for (k in test_results_seen)
Packit Service 7770af
    if (k != "PASS")
Packit Service 7770af
      return 1
Packit Service 7770af
  return 0
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# FIXME: this can certainly be improved ...
Packit Service 7770af
function get_global_test_result()
Packit Service 7770af
{
Packit Service 7770af
    if ("ERROR" in test_results_seen)
Packit Service 7770af
      return "ERROR"
Packit Service 7770af
    if ("FAIL" in test_results_seen || "XPASS" in test_results_seen)
Packit Service 7770af
      return "FAIL"
Packit Service 7770af
    all_skipped = 1
Packit Service 7770af
    for (k in test_results_seen)
Packit Service 7770af
      if (k != "SKIP")
Packit Service 7770af
        all_skipped = 0
Packit Service 7770af
    if (all_skipped)
Packit Service 7770af
      return "SKIP"
Packit Service 7770af
    return "PASS";
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function stringify_result_obj(result_obj)
Packit Service 7770af
{
Packit Service 7770af
  if (result_obj["is_unplanned"] || result_obj["number"] != testno)
Packit Service 7770af
    return "ERROR"
Packit Service 7770af
Packit Service 7770af
  if (plan_seen == LATE_PLAN)
Packit Service 7770af
    return "ERROR"
Packit Service 7770af
Packit Service 7770af
  if (result_obj["directive"] == "TODO")
Packit Service 7770af
    return result_obj["is_ok"] ? "XPASS" : "XFAIL"
Packit Service 7770af
Packit Service 7770af
  if (result_obj["directive"] == "SKIP")
Packit Service 7770af
    return result_obj["is_ok"] ? "SKIP" : COOKED_FAIL;
Packit Service 7770af
Packit Service 7770af
  if (length(result_obj["directive"]))
Packit Service 7770af
      abort("in function stringify_result_obj()")
Packit Service 7770af
Packit Service 7770af
  return result_obj["is_ok"] ? COOKED_PASS : COOKED_FAIL
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function decorate_result(result)
Packit Service 7770af
{
Packit Service 7770af
  color_name = color_for_result[result]
Packit Service 7770af
  if (color_name)
Packit Service 7770af
    return color_map[color_name] "" result "" color_map["std"]
Packit Service 7770af
  # If we are not using colorized output, or if we do not know how
Packit Service 7770af
  # to colorize the given result, we should return it unchanged.
Packit Service 7770af
  return result
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function report(result, details)
Packit Service 7770af
{
Packit Service 7770af
  if (result ~ /^(X?(PASS|FAIL)|SKIP|ERROR)/)
Packit Service 7770af
    {
Packit Service 7770af
      msg = ": " test_script_name
Packit Service 7770af
      add_test_result(result)
Packit Service 7770af
    }
Packit Service 7770af
  else if (result == "#")
Packit Service 7770af
    {
Packit Service 7770af
      msg = " " test_script_name ":"
Packit Service 7770af
    }
Packit Service 7770af
  else
Packit Service 7770af
    {
Packit Service 7770af
      abort("in function report()")
Packit Service 7770af
    }
Packit Service 7770af
  if (length(details))
Packit Service 7770af
    msg = msg " " details
Packit Service 7770af
  # Output on console might be colorized.
Packit Service 7770af
  print decorate_result(result) msg
Packit Service 7770af
  # Log the result in the log file too, to help debugging (this is
Packit Service 7770af
  # especially true when said result is a TAP error or "Bail out!").
Packit Service 7770af
  print result msg | "cat >&3;;
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function testsuite_error(error_message)
Packit Service 7770af
{
Packit Service 7770af
  report("ERROR", "- " error_message)
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function handle_tap_result()
Packit Service 7770af
{
Packit Service 7770af
  details = result_obj["number"];
Packit Service 7770af
  if (length(result_obj["description"]))
Packit Service 7770af
    details = details " " result_obj["description"]
Packit Service 7770af
Packit Service 7770af
  if (plan_seen == LATE_PLAN)
Packit Service 7770af
    {
Packit Service 7770af
      details = details " # AFTER LATE PLAN";
Packit Service 7770af
    }
Packit Service 7770af
  else if (result_obj["is_unplanned"])
Packit Service 7770af
    {
Packit Service 7770af
       details = details " # UNPLANNED";
Packit Service 7770af
    }
Packit Service 7770af
  else if (result_obj["number"] != testno)
Packit Service 7770af
    {
Packit Service 7770af
       details = sprintf("%s # OUT-OF-ORDER (expecting %d)",
Packit Service 7770af
                         details, testno);
Packit Service 7770af
    }
Packit Service 7770af
  else if (result_obj["directive"])
Packit Service 7770af
    {
Packit Service 7770af
      details = details " # " result_obj["directive"];
Packit Service 7770af
      if (length(result_obj["explanation"]))
Packit Service 7770af
        details = details " " result_obj["explanation"]
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
  report(stringify_result_obj(result_obj), details)
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# `skip_reason` should be empty whenever planned > 0.
Packit Service 7770af
function handle_tap_plan(planned, skip_reason)
Packit Service 7770af
{
Packit Service 7770af
  planned += 0 # Avoid getting confused if, say, `planned` is "00"
Packit Service 7770af
  if (length(skip_reason) && planned > 0)
Packit Service 7770af
    abort("in function handle_tap_plan()")
Packit Service 7770af
  if (plan_seen)
Packit Service 7770af
    {
Packit Service 7770af
      # Error, only one plan per stream is acceptable.
Packit Service 7770af
      testsuite_error("multiple test plans")
Packit Service 7770af
      return;
Packit Service 7770af
    }
Packit Service 7770af
  planned_tests = planned
Packit Service 7770af
  # The TAP plan can come before or after *all* the TAP results; we speak
Packit Service 7770af
  # respectively of an "early" or a "late" plan.  If we see the plan line
Packit Service 7770af
  # after at least one TAP result has been seen, assume we have a late
Packit Service 7770af
  # plan; in this case, any further test result seen after the plan will
Packit Service 7770af
  # be flagged as an error.
Packit Service 7770af
  plan_seen = (testno >= 1 ? LATE_PLAN : EARLY_PLAN)
Packit Service 7770af
  # If testno > 0, we have an error ("too many tests run") that will be
Packit Service 7770af
  # automatically dealt with later, so do not worry about it here.  If
Packit Service 7770af
  # $plan_seen is true, we have an error due to a repeated plan, and that
Packit Service 7770af
  # has already been dealt with above.  Otherwise, we have a valid "plan
Packit Service 7770af
  # with SKIP" specification, and should report it as a particular kind
Packit Service 7770af
  # of SKIP result.
Packit Service 7770af
  if (planned == 0 && testno == 0)
Packit Service 7770af
    {
Packit Service 7770af
      if (length(skip_reason))
Packit Service 7770af
        skip_reason = "- "  skip_reason;
Packit Service 7770af
      report("SKIP", skip_reason);
Packit Service 7770af
    }
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function extract_tap_comment(line)
Packit Service 7770af
{
Packit Service 7770af
  if (index(line, diag_string) == 1)
Packit Service 7770af
    {
Packit Service 7770af
      # Strip leading `diag_string` from `line`.
Packit Service 7770af
      line = substr(line, length(diag_string) + 1)
Packit Service 7770af
      # And strip any leading and trailing whitespace left.
Packit Service 7770af
      sub("^[ \t]*", "", line)
Packit Service 7770af
      sub("[ \t]*$", "", line)
Packit Service 7770af
      # Return what is left (if any).
Packit Service 7770af
      return line;
Packit Service 7770af
    }
Packit Service 7770af
  return "";
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
# When this function is called, we know that line is a TAP result line,
Packit Service 7770af
# so that it matches the (perl) RE "^(not )?ok\b".
Packit Service 7770af
function setup_result_obj(line)
Packit Service 7770af
{
Packit Service 7770af
  # Get the result, and remove it from the line.
Packit Service 7770af
  result_obj["is_ok"] = (substr(line, 1, 2) == "ok" ? 1 : 0)
Packit Service 7770af
  sub("^(not )?ok[ \t]*", "", line)
Packit Service 7770af
Packit Service 7770af
  # If the result has an explicit number, get it and strip it; otherwise,
Packit Service 7770af
  # automatically assing the next progresive number to it.
Packit Service 7770af
  if (line ~ /^[0-9]+$/ || line ~ /^[0-9]+[^a-zA-Z0-9_]/)
Packit Service 7770af
    {
Packit Service 7770af
      match(line, "^[0-9]+")
Packit Service 7770af
      # The final `+ 0` is to normalize numbers with leading zeros.
Packit Service 7770af
      result_obj["number"] = substr(line, 1, RLENGTH) + 0
Packit Service 7770af
      line = substr(line, RLENGTH + 1)
Packit Service 7770af
    }
Packit Service 7770af
  else
Packit Service 7770af
    {
Packit Service 7770af
      result_obj["number"] = testno
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
  if (plan_seen == LATE_PLAN)
Packit Service 7770af
    # No further test results are acceptable after a "late" TAP plan
Packit Service 7770af
    # has been seen.
Packit Service 7770af
    result_obj["is_unplanned"] = 1
Packit Service 7770af
  else if (plan_seen && testno > planned_tests)
Packit Service 7770af
    result_obj["is_unplanned"] = 1
Packit Service 7770af
  else
Packit Service 7770af
    result_obj["is_unplanned"] = 0
Packit Service 7770af
Packit Service 7770af
  # Strip trailing and leading whitespace.
Packit Service 7770af
  sub("^[ \t]*", "", line)
Packit Service 7770af
  sub("[ \t]*$", "", line)
Packit Service 7770af
Packit Service 7770af
  # This will have to be corrected if we have a "TODO"/"SKIP" directive.
Packit Service 7770af
  result_obj["description"] = line
Packit Service 7770af
  result_obj["directive"] = ""
Packit Service 7770af
  result_obj["explanation"] = ""
Packit Service 7770af
Packit Service 7770af
  if (index(line, "#") == 0)
Packit Service 7770af
    return # No possible directive, nothing more to do.
Packit Service 7770af
Packit Service 7770af
  # Directives are case-insensitive.
Packit Service 7770af
  rx = "[ \t]*#[ \t]*([tT][oO][dD][oO]|[sS][kK][iI][pP])[ \t]*"
Packit Service 7770af
Packit Service 7770af
  # See whether we have the directive, and if yes, where.
Packit Service 7770af
  pos = match(line, rx "$")
Packit Service 7770af
  if (!pos)
Packit Service 7770af
    pos = match(line, rx "[^a-zA-Z0-9_]")
Packit Service 7770af
Packit Service 7770af
  # If there was no TAP directive, we have nothing more to do.
Packit Service 7770af
  if (!pos)
Packit Service 7770af
    return
Packit Service 7770af
Packit Service 7770af
  # Let`s now see if the TAP directive has been escaped.  For example:
Packit Service 7770af
  #  escaped:     ok \# SKIP
Packit Service 7770af
  #  not escaped: ok \\# SKIP
Packit Service 7770af
  #  escaped:     ok \\\\\# SKIP
Packit Service 7770af
  #  not escaped: ok \ # SKIP
Packit Service 7770af
  if (substr(line, pos, 1) == "#")
Packit Service 7770af
    {
Packit Service 7770af
      bslash_count = 0
Packit Service 7770af
      for (i = pos; i > 1 && substr(line, i - 1, 1) == "\\"; i--)
Packit Service 7770af
        bslash_count += 1
Packit Service 7770af
      if (bslash_count % 2)
Packit Service 7770af
        return # Directive was escaped.
Packit Service 7770af
    }
Packit Service 7770af
Packit Service 7770af
  # Strip the directive and its explanation (if any) from the test
Packit Service 7770af
  # description.
Packit Service 7770af
  result_obj["description"] = substr(line, 1, pos - 1)
Packit Service 7770af
  # Now remove the test description from the line, that has been dealt
Packit Service 7770af
  # with already.
Packit Service 7770af
  line = substr(line, pos)
Packit Service 7770af
  # Strip the directive, and save its value (normalized to upper case).
Packit Service 7770af
  sub("^[ \t]*#[ \t]*", "", line)
Packit Service 7770af
  result_obj["directive"] = toupper(substr(line, 1, 4))
Packit Service 7770af
  line = substr(line, 5)
Packit Service 7770af
  # Now get the explanation for the directive (if any), with leading
Packit Service 7770af
  # and trailing whitespace removed.
Packit Service 7770af
  sub("^[ \t]*", "", line)
Packit Service 7770af
  sub("[ \t]*$", "", line)
Packit Service 7770af
  result_obj["explanation"] = line
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function get_test_exit_message(status)
Packit Service 7770af
{
Packit Service 7770af
  if (status == 0)
Packit Service 7770af
    return ""
Packit Service 7770af
  if (status !~ /^[1-9][0-9]*$/)
Packit Service 7770af
    abort("getting exit status")
Packit Service 7770af
  if (status < 127)
Packit Service 7770af
    exit_details = ""
Packit Service 7770af
  else if (status == 127)
Packit Service 7770af
    exit_details = " (command not found?)"
Packit Service 7770af
  else if (status >= 128 && status <= 255)
Packit Service 7770af
    exit_details = sprintf(" (terminated by signal %d?)", status - 128)
Packit Service 7770af
  else if (status > 256 && status <= 384)
Packit Service 7770af
    # We used to report an "abnormal termination" here, but some Korn
Packit Service 7770af
    # shells, when a child process die due to signal number n, can leave
Packit Service 7770af
    # in $? an exit status of 256+n instead of the more standard 128+n.
Packit Service 7770af
    # Apparently, both behaviours are allowed by POSIX (2008), so be
Packit Service 7770af
    # prepared to handle them both.  See also Austing Group report ID
Packit Service 7770af
    # 0000051 <http://www.austingroupbugs.net/view.php?id=51>
Packit Service 7770af
    exit_details = sprintf(" (terminated by signal %d?)", status - 256)
Packit Service 7770af
  else
Packit Service 7770af
    # Never seen in practice.
Packit Service 7770af
    exit_details = " (abnormal termination)"
Packit Service 7770af
  return sprintf("exited with status %d%s", status, exit_details)
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
function write_test_results()
Packit Service 7770af
{
Packit Service 7770af
  print ":global-test-result: " get_global_test_result() > trs_file
Packit Service 7770af
  print ":recheck: "  yn(must_recheck()) > trs_file
Packit Service 7770af
  print ":copy-in-global-log: " yn(copy_in_global_log()) > trs_file
Packit Service 7770af
  for (i = 0; i < test_results_index; i += 1)
Packit Service 7770af
    print ":test-result: " test_results_list[i] > trs_file
Packit Service 7770af
  close(trs_file);
Packit Service 7770af
}
Packit Service 7770af
Packit Service 7770af
BEGIN {
Packit Service 7770af
Packit Service 7770af
## ------- ##
Packit Service 7770af
##  SETUP  ##
Packit Service 7770af
## ------- ##
Packit Service 7770af
Packit Service 7770af
'"$init_colors"'
Packit Service 7770af
Packit Service 7770af
# Properly initialized once the TAP plan is seen.
Packit Service 7770af
planned_tests = 0
Packit Service 7770af
Packit Service 7770af
COOKED_PASS = expect_failure ? "XPASS": "PASS";
Packit Service 7770af
COOKED_FAIL = expect_failure ? "XFAIL": "FAIL";
Packit Service 7770af
Packit Service 7770af
# Enumeration-like constants to remember which kind of plan (if any)
Packit Service 7770af
# has been seen.  It is important that NO_PLAN evaluates "false" as
Packit Service 7770af
# a boolean.
Packit Service 7770af
NO_PLAN = 0
Packit Service 7770af
EARLY_PLAN = 1
Packit Service 7770af
LATE_PLAN = 2
Packit Service 7770af
Packit Service 7770af
testno = 0     # Number of test results seen so far.
Packit Service 7770af
bailed_out = 0 # Whether a "Bail out!" directive has been seen.
Packit Service 7770af
Packit Service 7770af
# Whether the TAP plan has been seen or not, and if yes, which kind
Packit Service 7770af
# it is ("early" is seen before any test result, "late" otherwise).
Packit Service 7770af
plan_seen = NO_PLAN
Packit Service 7770af
Packit Service 7770af
## --------- ##
Packit Service 7770af
##  PARSING  ##
Packit Service 7770af
## --------- ##
Packit Service 7770af
Packit Service 7770af
is_first_read = 1
Packit Service 7770af
Packit Service 7770af
while (1)
Packit Service 7770af
  {
Packit Service 7770af
    # Involutions required so that we are able to read the exit status
Packit Service 7770af
    # from the last input line.
Packit Service 7770af
    st = getline
Packit Service 7770af
    if (st < 0) # I/O error.
Packit Service 7770af
      fatal("I/O error while reading from input stream")
Packit Service 7770af
    else if (st == 0) # End-of-input
Packit Service 7770af
      {
Packit Service 7770af
        if (is_first_read)
Packit Service 7770af
          abort("in input loop: only one input line")
Packit Service 7770af
        break
Packit Service 7770af
      }
Packit Service 7770af
    if (is_first_read)
Packit Service 7770af
      {
Packit Service 7770af
        is_first_read = 0
Packit Service 7770af
        nextline = $0
Packit Service 7770af
        continue
Packit Service 7770af
      }
Packit Service 7770af
    else
Packit Service 7770af
      {
Packit Service 7770af
        curline = nextline
Packit Service 7770af
        nextline = $0
Packit Service 7770af
        $0 = curline
Packit Service 7770af
      }
Packit Service 7770af
    # Copy any input line verbatim into the log file.
Packit Service 7770af
    print | "cat >&3"
Packit Service 7770af
    # Parsing of TAP input should stop after a "Bail out!" directive.
Packit Service 7770af
    if (bailed_out)
Packit Service 7770af
      continue
Packit Service 7770af
Packit Service 7770af
    # TAP test result.
Packit Service 7770af
    if ($0 ~ /^(not )?ok$/ || $0 ~ /^(not )?ok[^a-zA-Z0-9_]/)
Packit Service 7770af
      {
Packit Service 7770af
        testno += 1
Packit Service 7770af
        setup_result_obj($0)
Packit Service 7770af
        handle_tap_result()
Packit Service 7770af
      }
Packit Service 7770af
    # TAP plan (normal or "SKIP" without explanation).
Packit Service 7770af
    else if ($0 ~ /^1\.\.[0-9]+[ \t]*$/)
Packit Service 7770af
      {
Packit Service 7770af
        # The next two lines will put the number of planned tests in $0.
Packit Service 7770af
        sub("^1\\.\\.", "")
Packit Service 7770af
        sub("[^0-9]*$", "")
Packit Service 7770af
        handle_tap_plan($0, "")
Packit Service 7770af
        continue
Packit Service 7770af
      }
Packit Service 7770af
    # TAP "SKIP" plan, with an explanation.
Packit Service 7770af
    else if ($0 ~ /^1\.\.0+[ \t]*#/)
Packit Service 7770af
      {
Packit Service 7770af
        # The next lines will put the skip explanation in $0, stripping
Packit Service 7770af
        # any leading and trailing whitespace.  This is a little more
Packit Service 7770af
        # tricky in truth, since we want to also strip a potential leading
Packit Service 7770af
        # "SKIP" string from the message.
Packit Service 7770af
        sub("^[^#]*#[ \t]*(SKIP[: \t][ \t]*)?", "")
Packit Service 7770af
        sub("[ \t]*$", "");
Packit Service 7770af
        handle_tap_plan(0, $0)
Packit Service 7770af
      }
Packit Service 7770af
    # "Bail out!" magic.
Packit Service 7770af
    # Older versions of prove and TAP::Harness (e.g., 3.17) did not
Packit Service 7770af
    # recognize a "Bail out!" directive when preceded by leading
Packit Service 7770af
    # whitespace, but more modern versions (e.g., 3.23) do.  So we
Packit Service 7770af
    # emulate the latter, "more modern" behaviour.
Packit Service 7770af
    else if ($0 ~ /^[ \t]*Bail out!/)
Packit Service 7770af
      {
Packit Service 7770af
        bailed_out = 1
Packit Service 7770af
        # Get the bailout message (if any), with leading and trailing
Packit Service 7770af
        # whitespace stripped.  The message remains stored in `$0`.
Packit Service 7770af
        sub("^[ \t]*Bail out![ \t]*", "");
Packit Service 7770af
        sub("[ \t]*$", "");
Packit Service 7770af
        # Format the error message for the
Packit Service 7770af
        bailout_message = "Bail out!"
Packit Service 7770af
        if (length($0))
Packit Service 7770af
          bailout_message = bailout_message " " $0
Packit Service 7770af
        testsuite_error(bailout_message)
Packit Service 7770af
      }
Packit Service 7770af
    # Maybe we have too look for dianogtic comments too.
Packit Service 7770af
    else if (comments != 0)
Packit Service 7770af
      {
Packit Service 7770af
        comment = extract_tap_comment($0);
Packit Service 7770af
        if (length(comment))
Packit Service 7770af
          report("#", comment);
Packit Service 7770af
      }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
## -------- ##
Packit Service 7770af
##  FINISH  ##
Packit Service 7770af
## -------- ##
Packit Service 7770af
Packit Service 7770af
# A "Bail out!" directive should cause us to ignore any following TAP
Packit Service 7770af
# error, as well as a non-zero exit status from the TAP producer.
Packit Service 7770af
if (!bailed_out)
Packit Service 7770af
  {
Packit Service 7770af
    if (!plan_seen)
Packit Service 7770af
      {
Packit Service 7770af
        testsuite_error("missing test plan")
Packit Service 7770af
      }
Packit Service 7770af
    else if (planned_tests != testno)
Packit Service 7770af
      {
Packit Service 7770af
        bad_amount = testno > planned_tests ? "many" : "few"
Packit Service 7770af
        testsuite_error(sprintf("too %s tests run (expected %d, got %d)",
Packit Service 7770af
                                bad_amount, planned_tests, testno))
Packit Service 7770af
      }
Packit Service 7770af
    if (!ignore_exit)
Packit Service 7770af
      {
Packit Service 7770af
        # Fetch exit status from the last line.
Packit Service 7770af
        exit_message = get_test_exit_message(nextline)
Packit Service 7770af
        if (exit_message)
Packit Service 7770af
          testsuite_error(exit_message)
Packit Service 7770af
      }
Packit Service 7770af
  }
Packit Service 7770af
Packit Service 7770af
write_test_results()
Packit Service 7770af
Packit Service 7770af
exit 0
Packit Service 7770af
Packit Service 7770af
} # End of "BEGIN" block.
Packit Service 7770af
'
Packit Service 7770af
Packit Service 7770af
# TODO: document that we consume the file descriptor 3 :-(
Packit Service 7770af
} 3>"$log_file"
Packit Service 7770af
Packit Service 7770af
test $? -eq 0 || fatal "I/O or internal error"
Packit Service 7770af
Packit Service 7770af
# Local Variables:
Packit Service 7770af
# mode: shell-script
Packit Service 7770af
# sh-indentation: 2
Packit Service 7770af
# eval: (add-hook 'write-file-hooks 'time-stamp)
Packit Service 7770af
# time-stamp-start: "scriptversion="
Packit Service 7770af
# time-stamp-format: "%:y-%02m-%02d.%02H"
Packit Service 7770af
# time-stamp-time-zone: "UTC"
Packit Service 7770af
# time-stamp-end: "; # UTC"
Packit Service 7770af
# End: