1079e7944Smrg#! /bin/sh
2079e7944Smrg# test-driver - basic testsuite driver script.
3079e7944Smrg
474901992Smrgscriptversion=2018-03-07.03; # UTC
5079e7944Smrg
674901992Smrg# Copyright (C) 2011-2021 Free Software Foundation, Inc.
7079e7944Smrg#
8079e7944Smrg# This program is free software; you can redistribute it and/or modify
9079e7944Smrg# it under the terms of the GNU General Public License as published by
10079e7944Smrg# the Free Software Foundation; either version 2, or (at your option)
11079e7944Smrg# any later version.
12079e7944Smrg#
13079e7944Smrg# This program is distributed in the hope that it will be useful,
14079e7944Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
15079e7944Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16079e7944Smrg# GNU General Public License for more details.
17079e7944Smrg#
18079e7944Smrg# You should have received a copy of the GNU General Public License
1974901992Smrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20079e7944Smrg
21079e7944Smrg# As a special exception to the GNU General Public License, if you
22079e7944Smrg# distribute this file as part of a program that contains a
23079e7944Smrg# configuration script generated by Autoconf, you may include it under
24079e7944Smrg# the same distribution terms that you use for the rest of that program.
25079e7944Smrg
26079e7944Smrg# This file is maintained in Automake, please report
27079e7944Smrg# bugs to <bug-automake@gnu.org> or send patches to
28079e7944Smrg# <automake-patches@gnu.org>.
29079e7944Smrg
30079e7944Smrg# Make unconditional expansion of undefined variables an error.  This
31079e7944Smrg# helps a lot in preventing typo-related bugs.
32079e7944Smrgset -u
33079e7944Smrg
34079e7944Smrgusage_error ()
35079e7944Smrg{
36079e7944Smrg  echo "$0: $*" >&2
37079e7944Smrg  print_usage >&2
38079e7944Smrg  exit 2
39079e7944Smrg}
40079e7944Smrg
41079e7944Smrgprint_usage ()
42079e7944Smrg{
43079e7944Smrg  cat <<END
44079e7944SmrgUsage:
4574901992Smrg  test-driver --test-name NAME --log-file PATH --trs-file PATH
4674901992Smrg              [--expect-failure {yes|no}] [--color-tests {yes|no}]
4774901992Smrg              [--enable-hard-errors {yes|no}] [--]
48b2f5b1dbSmrg              TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
4974901992Smrg
50079e7944SmrgThe '--test-name', '--log-file' and '--trs-file' options are mandatory.
5174901992SmrgSee the GNU Automake documentation for information.
52079e7944SmrgEND
53079e7944Smrg}
54079e7944Smrg
55079e7944Smrgtest_name= # Used for reporting.
56079e7944Smrglog_file=  # Where to save the output of the test script.
57079e7944Smrgtrs_file=  # Where to save the metadata of the test run.
58079e7944Smrgexpect_failure=no
59079e7944Smrgcolor_tests=no
60079e7944Smrgenable_hard_errors=yes
61079e7944Smrgwhile test $# -gt 0; do
62079e7944Smrg  case $1 in
63079e7944Smrg  --help) print_usage; exit $?;;
64079e7944Smrg  --version) echo "test-driver $scriptversion"; exit $?;;
65079e7944Smrg  --test-name) test_name=$2; shift;;
66079e7944Smrg  --log-file) log_file=$2; shift;;
67079e7944Smrg  --trs-file) trs_file=$2; shift;;
68079e7944Smrg  --color-tests) color_tests=$2; shift;;
69079e7944Smrg  --expect-failure) expect_failure=$2; shift;;
70079e7944Smrg  --enable-hard-errors) enable_hard_errors=$2; shift;;
71079e7944Smrg  --) shift; break;;
72079e7944Smrg  -*) usage_error "invalid option: '$1'";;
73b2f5b1dbSmrg   *) break;;
74079e7944Smrg  esac
75079e7944Smrg  shift
76079e7944Smrgdone
77079e7944Smrg
78b2f5b1dbSmrgmissing_opts=
79b2f5b1dbSmrgtest x"$test_name" = x && missing_opts="$missing_opts --test-name"
80b2f5b1dbSmrgtest x"$log_file"  = x && missing_opts="$missing_opts --log-file"
81b2f5b1dbSmrgtest x"$trs_file"  = x && missing_opts="$missing_opts --trs-file"
82b2f5b1dbSmrgif test x"$missing_opts" != x; then
83b2f5b1dbSmrg  usage_error "the following mandatory options are missing:$missing_opts"
84b2f5b1dbSmrgfi
85b2f5b1dbSmrg
86b2f5b1dbSmrgif test $# -eq 0; then
87b2f5b1dbSmrg  usage_error "missing argument"
88b2f5b1dbSmrgfi
89b2f5b1dbSmrg
90079e7944Smrgif test $color_tests = yes; then
91079e7944Smrg  # Keep this in sync with 'lib/am/check.am:$(am__tty_colors)'.
92079e7944Smrg  red='[0;31m' # Red.
93079e7944Smrg  grn='[0;32m' # Green.
94079e7944Smrg  lgn='[1;32m' # Light green.
95079e7944Smrg  blu='[1;34m' # Blue.
96079e7944Smrg  mgn='[0;35m' # Magenta.
97079e7944Smrg  std='[m'     # No color.
98079e7944Smrgelse
99079e7944Smrg  red= grn= lgn= blu= mgn= std=
100079e7944Smrgfi
101079e7944Smrg
102079e7944Smrgdo_exit='rm -f $log_file $trs_file; (exit $st); exit $st'
103079e7944Smrgtrap "st=129; $do_exit" 1
104079e7944Smrgtrap "st=130; $do_exit" 2
105079e7944Smrgtrap "st=141; $do_exit" 13
106079e7944Smrgtrap "st=143; $do_exit" 15
107079e7944Smrg
10874901992Smrg# Test script is run here. We create the file first, then append to it,
10974901992Smrg# to ameliorate tests themselves also writing to the log file. Our tests
11074901992Smrg# don't, but others can (automake bug#35762).
11174901992Smrg: >"$log_file"
11274901992Smrg"$@" >>"$log_file" 2>&1
113079e7944Smrgestatus=$?
114b2f5b1dbSmrg
115079e7944Smrgif test $enable_hard_errors = no && test $estatus -eq 99; then
116b2f5b1dbSmrg  tweaked_estatus=1
117b2f5b1dbSmrgelse
118b2f5b1dbSmrg  tweaked_estatus=$estatus
119079e7944Smrgfi
120079e7944Smrg
121b2f5b1dbSmrgcase $tweaked_estatus:$expect_failure in
122079e7944Smrg  0:yes) col=$red res=XPASS recheck=yes gcopy=yes;;
123079e7944Smrg  0:*)   col=$grn res=PASS  recheck=no  gcopy=no;;
124079e7944Smrg  77:*)  col=$blu res=SKIP  recheck=no  gcopy=yes;;
125079e7944Smrg  99:*)  col=$mgn res=ERROR recheck=yes gcopy=yes;;
126079e7944Smrg  *:yes) col=$lgn res=XFAIL recheck=no  gcopy=yes;;
127079e7944Smrg  *:*)   col=$red res=FAIL  recheck=yes gcopy=yes;;
128079e7944Smrgesac
129079e7944Smrg
130b2f5b1dbSmrg# Report the test outcome and exit status in the logs, so that one can
131b2f5b1dbSmrg# know whether the test passed or failed simply by looking at the '.log'
132b2f5b1dbSmrg# file, without the need of also peaking into the corresponding '.trs'
133b2f5b1dbSmrg# file (automake bug#11814).
13474901992Smrgecho "$res $test_name (exit status: $estatus)" >>"$log_file"
135b2f5b1dbSmrg
136079e7944Smrg# Report outcome to console.
137079e7944Smrgecho "${col}${res}${std}: $test_name"
138079e7944Smrg
139079e7944Smrg# Register the test result, and other relevant metadata.
140079e7944Smrgecho ":test-result: $res" > $trs_file
141079e7944Smrgecho ":global-test-result: $res" >> $trs_file
142079e7944Smrgecho ":recheck: $recheck" >> $trs_file
143079e7944Smrgecho ":copy-in-global-log: $gcopy" >> $trs_file
144079e7944Smrg
145079e7944Smrg# Local Variables:
146079e7944Smrg# mode: shell-script
147079e7944Smrg# sh-indentation: 2
14874901992Smrg# eval: (add-hook 'before-save-hook 'time-stamp)
149079e7944Smrg# time-stamp-start: "scriptversion="
150079e7944Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
15174901992Smrg# time-stamp-time-zone: "UTC0"
152079e7944Smrg# time-stamp-end: "; # UTC"
153079e7944Smrg# End:
154