Blame tap-driver.sh

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