Merge branch 'master' into test-protocols
[platform/upstream/automake.git] / lib / test-driver
1 #! /bin/sh
2 # test-driver - basic driver script for the `parallel-tests' mode.
3
4 scriptversion=2011-08-01.21; # UTC
5
6 # Copyright (C) 2011 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 <http://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.
32 set -u
33
34 fatal ()
35 {
36   echo "$0: fatal: $*" >&2
37   exit 1
38 }
39
40 usage_error ()
41 {
42   echo "$0: $*" >&2
43   print_usage >&2
44   exit 2
45 }
46
47 print_usage ()
48 {
49   cat <<END
50 Usage:
51   test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
52               [--expect-failure={yes|no}] [--color-tests={yes|no}]
53               [--enable-hard-errors={yes|no}] [--] TEST-SCRIPT
54 The \`--test-name' and \`--log-file' options are mandatory.
55 END
56 }
57
58 # Restructured Text section.
59 rst_section () { sed 'p;s/./=/g;p;g'; }
60
61 # TODO: better error handling in option parsing (in particular, ensure
62 # TODO: $logfile, $trsfile and $test_name are defined).
63 test_name= # Used for reporting.
64 logfile=   # Where to save the output of the test script.
65 trsfile=   # Where to save the result(s) the test script.
66 expect_failure=no
67 color_tests=no
68 enable_hard_errors=yes
69 while test $# -gt 0; do
70   case $1 in
71   --help) print_usage; exit $?;;
72   --version) echo "test-driver $scriptversion"; exit $?;;
73   --test-name) test_name=$2; shift;;
74   --log-file) logfile=$2; shift;;
75   --trs-file) trsfile=$2; shift;;
76   --color-tests) color_tests=$2; shift;;
77   --expect-failure) expect_failure=$2; shift;;
78   --enable-hard-errors) enable_hard_errors=$2; shift;;
79   --) shift; break;;
80   -*) usage_error "invalid option: '$1'";;
81   esac
82   shift
83 done
84
85 if test $color_tests = yes; then
86   red='\e[0;31m' # Red.
87   grn='\e[0;32m' # Green.
88   lgn='\e[1;32m' # Light green.
89   blu='\e[1;34m' # Blue.
90   mgn='\e[0;35m' # Magenta.
91   std='\e[m'     # No color.
92 else
93   red= grn= lgn= blu= mgn= std=
94 fi
95
96 tmpfile=$logfile-t
97 do_exit='rm -f $tmpfile; (exit $st); exit $st'
98 trap "st=129; $do_exit" 1
99 trap "st=130; $do_exit" 2
100 trap "st=141; $do_exit" 13
101 trap "st=143; $do_exit" 15
102 rm -f $tmpfile
103
104 # Test script is run here.
105 "$@" >$tmpfile 2>&1
106 estatus=$?
107 if test $enable_hard_errors = no && test $estatus -eq 99; then
108   estatus=1
109 fi
110
111 case $estatus:$expect_failure in
112   0:yes) col=$red res=XPASS recheck=yes gcopy=yes;;
113   0:*)   col=$grn res=PASS  recheck=no  gcopy=no;;
114   77:*)  col=$blu res=SKIP  recheck=no  gcopy=yes;;
115   99:*)  col=$mgn res=ERROR recheck=yes gcopy=yes;;
116   *:yes) col=$lgn res=XFAIL recheck=no  gcopy=yes;;
117   *:*)   col=$red res=FAIL  recheck=yes gcopy=yes;;
118 esac
119
120 # Report outcome to console.
121 echo "${col}${res}${std}: $test_name"
122
123 # Register the test result, and other relevant metadata.
124 echo ":test-result: $res (exit status: $estatus)" > $trsfile
125 echo ":recheck: $recheck" >> $trsfile
126 echo ":copy-in-global-log: $gcopy" >> $trsfile
127
128 # Now write log file.
129 {
130   echo "$res: $test_name (exit: $estatus)" | rst_section
131   echo
132   cat $tmpfile
133 } > $logfile
134 rm -f $tmpfile
135
136 # Local Variables:
137 # mode: shell-script
138 # sh-indentation: 2
139 # eval: (add-hook 'write-file-hooks 'time-stamp)
140 # time-stamp-start: "scriptversion="
141 # time-stamp-format: "%:y-%02m-%02d.%02H"
142 # time-stamp-time-zone: "UTC"
143 # time-stamp-end: "; # UTC"
144 # End: