summaryrefslogtreecommitdiff
path: root/build-aux/run-test
blob: 38e0493ff367bf0da9708a33ac39af58066e7bc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/sh
#
# Copyright (C) 2009-2018 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

# This program is a test driver that supports running a test under valgrind.
# Usage: run-test CHECKER PROGRAM [ARGUMENT...]

progname=$0

# func_usage
# outputs to stdout the --help usage message.
func_usage ()
{
  echo "\
Usage: run-test [OPTION...] CHECKER PROGRAM [ARGUMENT...]

Runs PROGRAM under the control of CHECKER.

CHECKER may be empty or a valgrind command with some options, such as
'valgrind --tool=memcheck --num-callers=20 --leak-check=yes --leak-resolution=high --show-reachable=yes'.

When CHECKER is not empty, it is recommended that the package has been
configured with
  --disable-shared     so that tests are real executables and not libtool
                       wrapper scripts, and
  CFLAGS=\"-g\"          so that valgrind shows line numbers.

Report bugs to Bruno Haible."
}

# func_version
# outputs to stdout the --version message.
func_version ()
{
  echo "\
run-test (GNU libunistring)
Copyright (C) 2009-2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by" "Bruno Haible"
}

# func_fatal_error message
# outputs to stderr a fatal error message, and terminates the program.
# Input:
# - progname                 name of this program
func_fatal_error ()
{
  echo "$progname: *** $1" 1>&2
  echo "$progname: *** Stop." 1>&2
  func_exit 1
}

# Command-line option processing.
# Removes the OPTIONS from the arguments. Sets the variables:
# - checker        wrapper program for executables
{
  while test $# -gt 0; do
    case "$1" in
      --help | --hel | --he | --h )
        func_usage
        exit $? ;;
      --version | --versio | --versi | --vers | --ver | --ve | --v )
        func_version
        exit $? ;;
      -- )
        # Stop option processing
        shift
        break ;;
      -* )
        echo "run-test: unknown option $1" 1>&2
        echo "Try 'run-test --help' for more information." 1>&2
        exit 1 ;;
      * )
        break ;;
    esac
  done

  if test $# -lt 2; then
    echo "run-test: too few arguments" 1>&2
    echo "Try 'run-test --help' for more information." 1>&2
    exit 1
  fi

  checker="$1"
  shift
}

if test -z "$checker"; then
  # No checker. Run the test directly.
  exec "$@"
else
  # Using valgrind. We want to apply valgrind only to executables, not to
  # shell script, because
  # 1. we don't want to look for memory leaks in bash,
  # 2. on a bi-arch system, we would get an error message such as
  #    "valgrind: wrong executable class (eg. 32-bit instead of 64-bit)".
  case "$1" in
    *.sh)
      # A shell script. Ignore the checker.
      exec "$@"
      ;;
    *)
      # The 'file' command is not portable enough. So, look
      # at the first two bytes of the file. Are they '#!'?
      if { if od -A x < /dev/null >/dev/null 2>/dev/null; then
             # Use POSIX od.
             firstbytes=`od -A n -t o1 -N 2 < "$1" | tr -d ' '`
           else
             # Use BSD hexdump.
             firstbytes=`dd if="$1" bs=1 count=2 2>/dev/null | hexdump -e '1/1 "%03o"'`
           fi
           test "$firstbytes" = "043041"
         }; then
        # A shell script. Ignore the checker.
        exec "$@"
      else
        # An executable. Use the checker.
        exec $checker "$@"
      fi
      ;;
  esac
fi