Blame tap-driver.sh

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