Blame tap-driver.sh

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