1#! /bin/sh
2# test-driver - basic testsuite driver script.
3
4scriptversion=2020-05-12.10; # UTC
5
6# Copyright (C) 2011-2020 Free Software Foundation, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2, or (at your option)
11# any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21# As a special exception to the GNU General Public License, if you
22# distribute this file as part of a program that contains a
23# configuration script generated by Autoconf, you may include it under
24# the same distribution terms that you use for the rest of that program.
25
26# This file is maintained in Automake, please report
27# bugs to <bug-automake@gnu.org> or send patches to
28# <automake-patches@gnu.org>.
29
30# Make unconditional expansion of undefined variables an error.  This
31# helps a lot in preventing typo-related bugs.
32set -u
33
34usage_error ()
35{
36  echo "$0: $*" >&2
37  print_usage >&2
38  exit 2
39}
40
41print_usage ()
42{
43  cat <<END
44Usage:
45  test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
46              [--expect-failure={yes|no}] [--color-tests={yes|no}]
47              [--enable-hard-errors={yes|no}] [--]
48              TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
49The '--test-name', '--log-file' and '--trs-file' options are mandatory.
50END
51}
52
53test_name= # Used for reporting.
54log_file=  # Where to save the output of the test script.
55trs_file=  # Where to save the metadata of the test run.
56status_file= # Where to save the status of the test run.
57expect_failure=no
58color_tests=no
59enable_hard_errors=yes
60verbose=no
61while test $# -gt 0; do
62  case $1 in
63  --help) print_usage; exit $?;;
64  --version) echo "test-driver $scriptversion"; exit $?;;
65  --test-name) test_name=$2; shift;;
66  --log-file) log_file=$2; shift;;
67  --trs-file) trs_file=$2; shift;;
68  --status-file) status_file=$2; shift;;
69  --color-tests) color_tests=$2; shift;;
70  --expect-failure) expect_failure=$2; shift;;
71  --enable-hard-errors) enable_hard_errors=$2; shift;;
72  --verbose) verbose=$2; shift;;
73  --) shift; break;;
74  -*) usage_error "invalid option: '$1'";;
75   *) break;;
76  esac
77  shift
78done
79
80missing_opts=
81test x"$test_name" = x && missing_opts="$missing_opts --test-name"
82test x"$log_file"  = x && missing_opts="$missing_opts --log-file"
83test x"$trs_file"  = x && missing_opts="$missing_opts --trs-file"
84if test x"$missing_opts" != x; then
85  usage_error "the following mandatory options are missing:$missing_opts"
86fi
87
88if test $# -eq 0; then
89  usage_error "missing argument"
90fi
91
92if test $color_tests = yes; then
93  # Keep this in sync with 'lib/am/check.am:$(am__tty_colors)'.
94  red='' # Red.
95  grn='' # Green.
96  lgn='' # Light green.
97  blu='' # Blue.
98  mgn='' # Magenta.
99  std=''     # No color.
100else
101  red= grn= lgn= blu= mgn= std=
102fi
103
104do_exit='rm -f $log_file $trs_file $status_file; (exit $st); exit $st'
105trap "st=129; $do_exit" 1
106trap "st=130; $do_exit" 2
107trap "st=141; $do_exit" 13
108trap "st=143; $do_exit" 15
109
110# Set default
111test x"$status_file" = x && status_file=$(mktemp ./custom-test-runner.XXXXXX)
112# Test script is run here.
113if test $verbose = yes; then
114  ("$@" 2>&1; echo $? > "$status_file") | tee $log_file
115else
116  "$@" >$log_file 2>&1; echo $? > "$status_file"
117fi
118read -r estatus < "$status_file"
119rm "$status_file"
120
121if test $enable_hard_errors = no && test $estatus -eq 99; then
122  tweaked_estatus=1
123else
124  tweaked_estatus=$estatus
125fi
126
127case $tweaked_estatus:$expect_failure in
128  0:yes) col=$red res=XPASS recheck=yes gcopy=yes;;
129  0:*)   col=$grn res=PASS  recheck=no  gcopy=no;;
130  77:*)  col=$blu res=SKIP  recheck=no  gcopy=yes;;
131  99:*)  col=$mgn res=ERROR recheck=yes gcopy=yes;;
132  *:yes) col=$lgn res=XFAIL recheck=no  gcopy=yes;;
133  *:*)   col=$red res=FAIL  recheck=yes gcopy=yes;;
134esac
135
136# Report the test outcome and exit status in the logs, so that one can
137# know whether the test passed or failed simply by looking at the '.log'
138# file, without the need of also peaking into the corresponding '.trs'
139# file (automake bug#11814).
140echo "$res $test_name (exit status: $estatus)" >>$log_file
141
142# Report outcome to console.
143echo "${col}${res}${std}: $test_name"
144
145# Register the test result, and other relevant metadata.
146echo ":test-result: $res" > $trs_file
147echo ":global-test-result: $res" >> $trs_file
148echo ":recheck: $recheck" >> $trs_file
149echo ":copy-in-global-log: $gcopy" >> $trs_file
150
151# Local Variables:
152# mode: shell-script
153# sh-indentation: 2
154# eval: (add-hook 'before-save-hook 'time-stamp)
155# time-stamp-start: "scriptversion="
156# time-stamp-format: "%:y-%02m-%02d.%02H"
157# time-stamp-time-zone: "UTC0"
158# time-stamp-end: "; # UTC"
159# End:
160