summaryrefslogtreecommitdiff
path: root/tests/init.cfg
blob: bc62c321b542451bb6ada092a7f971fb06b306a2 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# This file is sourced by init.sh, *before* its initialization.

# This goes hand in hand with the "9>&2;" in tests/Makefile.am's
# TESTS_ENVIRONMENT definition.
stderr_fileno_=9

# Map settings of "none" to the empty string.
test _"$LOCALE_FR" = _none && LOCALE_FR=
test _"$LOCALE_FR_UTF8" = _none && LOCALE_FR_UTF8=

# Unset key environment variables.
if (FOO=FOO; unset FOO) >/dev/null 2>&1; then
  as_unset=unset
else
  as_unset=false
fi

# Derive this list by searching for string literals as the first
# argument to getenv:
# git grep getenv|perl -nle '/\bgetenv *\("(.+?)"\)/ and print $1'|sort -u grep
vars_='
GREP_COLOR
GREP_COLORS
GREP_OPTIONS
TERM
'
envvar_check_fail=0
for v_ in $vars_
do
  $as_unset $v_
  if eval test \"\${$v_+set}\" = set; then
    echo "$0: the $v_ environment variable is set --" \
      ' unset it and rerun this test' >&2
    envvar_check_fail=1
  fi
done

test "$envvar_check_fail" = 1 && fail_ "failed to unset the above envvars"

require_timeout_()
{
  ( timeout 10s true ) > /dev/null 2>&1 \
    || skip_ your system lacks the timeout program
  timeout 10s false; test $? = 1 \
    || skip_ your system has a non-GNU timeout program
}

require_pcre_()
{
  echo . | grep -P . 2>err || {
    test $? -eq 1 && fail_ PCRE available, but does not work.
    skip_ no PCRE support
  }
  compare /dev/null err || fail_ PCRE available, but stderr not empty.
}

# Some tests would fail without this particular locale.
# If the locale is not available, just skip the test.
require_en_utf8_locale_()
{
  path_prepend_ .
  case $(get-mb-cur-max en_US.UTF-8) in
    [3456]) ;;
    *) skip_ 'en_US.UTF-8 locale not found' ;;
  esac
}

require_tr_utf8_locale_()
{
  path_prepend_ .
  case $(get-mb-cur-max tr_TR.UTF-8) in
    [3456]) ;;
    *) skip_ 'tr_TR.UTF-8 locale not found' ;;
  esac
}

require_ru_RU_koi8_r()
{
  path_prepend_ .
  case $(get-mb-cur-max ru_RU.KOI8-R) in
    1) ;;
    *) skip_ 'ru_RU.KOI8-R locale not found' ;;
  esac
}

require_compiled_in_MB_support()
{
  require_en_utf8_locale_
  printf 'é' | LC_ALL=en_US.UTF-8 grep '[[:lower:]]' \
    || skip_ this test requires MBS support
}

require_unibyte_locale()
{
  path_prepend_ .
  for loc in C en_US; do
    for encoding in '' .iso88591 .iso885915 .ISO8859-1 .ISO8859-15; do
      locale=$loc$encoding
      MB_CUR_MAX=$(get-mb-cur-max $locale 2>/dev/null) &&
        test "$MB_CUR_MAX" -eq 1 &&
        LC_ALL=$locale &&
        export LC_ALL &&
        return
    done
  done
  skip_ 'no unibyte locale found'
}

# Define hi_res_time_ to a function that prints the current time
# as a floating point number with greater than 1-second resolution.
# Otherwise, skip the requiring test.
require_hi_res_time_()
{
  local cmd
  for cmd in 'date +%s.%N' \
          'perl -le "use Time::HiRes qw(time); print scalar time()"'; do
      case $($cmd) in
          *.[0-9]*) eval 'hi_res_time_() { '"$cmd"'; }'; break;;
      esac
  done
  type hi_res_time_ || skip_ no high-resolution timer support
}

require_JP_EUC_locale_()
{
  local locale=ja_JP.eucJP
  path_prepend_ .
  case $(get-mb-cur-max $locale) in
    [23])
        LC_ALL=$locale &&
        export LC_ALL &&
        return
        ;;
    *) ;;
  esac

  skip_ "$loc locale not found"
}

expensive_()
{
  if test "$RUN_EXPENSIVE_TESTS" != yes; then
    skip_ 'expensive: disabled by default
This test is relatively expensive, so it is disabled by default.
To run it anyway, rerun make check with the RUN_EXPENSIVE_TESTS
environment variable set to yes.  E.g.,

  env RUN_EXPENSIVE_TESTS=yes make check

or use the shortcut target of the toplevel Makefile,

  make check-expensive
'
  fi
}

# Like printf with a single argument, but that argument must be a
# sequence of four-byte strings \xHH where each H is a hexadecimal byte.
hex_printf_()
{
  local octal_fmt=$(printf '\\%o' \
    $(printf '%s\n' "$1" \
      | sed 's,\\x\([0-9abcdefABCDEF][0-9abcdefABCDEF]\), 0x\1,g'))
  printf "$octal_fmt"
}

# Wrap tr so that it always runs in the C locale.
# Otherwise, in a multibyte locale, GNU tr (which is not multibyte-aware
# as of 2014-11-08), would work differently than others.  For example,
# this command, which was written with unibyte GNU tr in mind,
#   LC_ALL=ja_JP.eucJP tr AB '\244\263'
# would act like this with the multibyte tr from HP-UX and Solaris:
#   LC_ALL=ja_JP.eucJP tr A  '\244\263'
tr() { LC_ALL=C env -- tr "$@"; }

# Usage: user_time_ EXPECTED_EXIT_STATUS CMD ...
# If CMD ... exits with the expected exit status, print the elapsed
# child "user" time (not "system" time) in milliseconds and return 0.
# Otherwise, diagnose the exit status mismatch and return nonzero.
user_time_()
{
  $PERL -le '
    my $expected_exit_status = $ARGV[0];
    shift @ARGV;

    system (@ARGV);
    my ($user, $system, $child_user, $child_system) = times;

    my $me = q('"$ME_"');
    $? == -1
      and die qq($me: failed to exec ") . join (" ", @ARGV) . qq(": $!\n);
    my $rc = $?;
    my $sig = ($rc & 127);
    $sig and die "$me: child died with signal $sig\n";
    $rc >>= 8;
    $rc == $expected_exit_status
      or die "$me: bad exit status: expected $expected_exit_status; got $rc\n";

    # Print milliseconds of child user time.
    $child_user *= 1000;
    print int ($child_user + 0.5)' "$@"
}

# yes is not portable, fake it with $AWK
yes() { line=${*-y} ${AWK-awk} 'BEGIN{for (;;) print ENVIRON["line"]}'; }