Debian Codegraph @

tests/atgeneral.m4

raw
1 include(m4sh.m4) -*- Autoconf -*-
2 # M4 macros used in building test suites.
3 # Copyright 2022,2023 Thomas E. Dickey
4 # Copyright 2000, 2001 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 # 02111-1307, USA.
20
21 # This script is part of Autotest. Unlimited permission to copy,
22 # distribute and modify the testing scripts that are the output of
23 # that Autotest script is given. You need not follow the terms of the
24 # GNU General Public License when using or distributing such scripts,
25 # even though portions of the text of Autotest appear in them. The
26 # GNU General Public License (GPL) does govern all other use of the
27 # material that constitutes the Autotest.
28 #
29 # Certain portions of the Autotest source text are designed to be
30 # copied (in certain cases, depending on the input) into the output of
31 # Autotest. We call these the "data" portions. The rest of the
32 # Autotest source text consists of comments plus executable code that
33 # decides which of the data portions to output in any given case. We
34 # call these comments and executable code the "non-data" portions.
35 # Autotest never copies any of the non-data portions into its output.
36 #
37 # This special exception to the GPL applies to versions of Autotest
38 # released by the Free Software Foundation. When you make and
39 # distribute a modified version of Autotest, you may extend this
40 # special exception to the GPL to apply to your modified version as
41 # well, *unless* your modified version has the potential to copy into
42 # its output some of the text that was the non-data portion of the
43 # version that you started with. (In other words, unless your change
44 # moves or copies text from the non-data portions to the data
45 # portions.) If your modification has such potential, you must delete
46 # any notice of this special exception to the GPL from your modified
47 # version.
48
49
50 # Use of diversions:
51 #
52 # - DEFAULT
53 # Overall initialization, value of $at_tests_all.
54 # - OPTIONS
55 # Option processing
56 # - HELP
57 # Help message. Of course it is useless, you could just push into
58 # OPTIONS, but that's much clearer this way.
59 # - SETUP
60 # Be ready to run the tests.
61 # - TESTS
62 # The core of the test suite, the ``normal'' diversion.
63 # - TAIL
64 # tail of the core for;case, overall wrap up, generation of debugging
65 # scripts and statistics.
66
67 m4_define([_m4_divert(DEFAULT)], 0)
68 m4_define([_m4_divert(OPTIONS)], 10)
69 m4_define([_m4_divert(HELP)], 20)
70 m4_define([_m4_divert(SETUP)], 30)
71 m4_define([_m4_divert(TESTS)], 50)
72 m4_define([_m4_divert(TAIL)], 60)
73
74 m4_divert_push([TESTS])
75 m4_divert_push([KILL])
76
77
78 # AT_LINE
79 # -------
80 # Return the current file sans directory, a colon, and the current line.
81 m4_define([AT_LINE],
82 [m4_patsubst(__file__, ^.*/\(.*\), \1):__line__])
83
84
85 # AT_INIT(PROGRAM)
86 # ----------------
87 # Begin test suite, using PROGRAM to check version. The search path
88 # should be already preset so the proper executable will be selected.
89 m4_define([AT_INIT],
90 [m4_define([AT_ordinal], 0)
91 m4_define([AT_banner_ordinal], 0)
92 m4_define([AT_data_files],
93 [stdout expout at-setup-line at-check-line at-stdout stderr experr
94 at-stder1 at-stderr ])
95 m4_divert_push([DEFAULT])dnl
96 #! /bin/sh
97
98 AS_SHELL_SANITIZE
99 SHELL=${CONFIG_SHELL-/bin/sh}
100
101 . ./atconfig
102 # Use absolute file notations, as the test might change directories.
103 at_srcdir=`cd "$srcdir" && pwd`
104 at_top_srcdir=`cd "$top_srcdir" && pwd`
105
106 # Don't take risks: use absolute path names.
107 at_path=`pwd`
108 at_IFS_save=$IFS
109 IFS=$PATH_SEPARATOR
110 for at_dir in $AUTOTEST_PATH $PATH; do
111 # There might be directories that don't exist, but don't redirect
112 # builtins' (eg., cd) stderr directly: Ultrix's sh hates that.
113 at_dir=`(cd "$at_dir" && pwd) 2>/dev/null`
114 test -n "$at_dir" && at_path="$at_path$PATH_SEPARATOR$at_dir"
115 done
116 IFS=$at_IFS_save
117 PATH=$at_path
118 export PATH
119
120 test -f atlocal && . ./atlocal
121
122 # -e sets to true
123 at_stop_on_error=false
124 # Shall we be verbose?
125 at_verbose=:
126 at_quiet=echo
127 # Shall we keep the debug scripts? Must be `:' when testsuite is
128 # run by a debug script, so that the script doesn't remove itself.
129 at_debug=false
130 # Display help message?
131 at_help=false
132 # Tests to run
133 at_tests=
134 dnl Other vars inserted here (DEFAULT).
135 m4_divert([OPTIONS])
136
137 while test $[#] -gt 0; do
138 case $[1] in
139 --help | -h) at_help=: ;;
140 --version) echo "$[0] ($at_package) $at_version"; exit 0 ;;
141
142 -d) at_debug=:;;
143 -e) at_stop_on_error=:;;
144 -v) at_verbose=echo; at_quiet=:;;
145 -x) at_traceon='set -vx'; at_traceoff='set +vx';;
146
147 [[0-9] | [0-9][0-9] | [0-9][0-9][0-9] | [0-9][0-9][0-9][0-9]])
148 at_tests="$at_tests$[1] ";;
149
150 *) echo "$as_me: invalid option: $[1]" >&2
151 echo "Try \`$[0] --help' for more information." >&2
152 exit 1 ;;
153 esac
154 shift
155 done
156
157 # Help message.
158 if $at_help; then
159 # If tests were specified, display only their title.
160 if test -z "$at_tests"; then
161 cat <<EOF
162 Usage: $[0] [[OPTION]]... [[TESTS]]
163
164 Run all the tests, or the selected TESTS.
165
166 Options:
167 -h Display this help message and the description of TESTS
168 -e Abort the full suite and inhibit normal clean up if a test fails
169 -v Force more detailed output, default for debugging scripts
170 -d Inhibit clean up and debug script creation, default for debugging scripts
171 -x Have the shell to trace command execution
172
173 Tests:
174 EOF
175 else
176 # " 1 42 45 " => " (1|42|45): "
177 at_tests_pattern=`echo "$at_tests" | sed 's/^ *//;s/ *$//;s/ */|/g'`
178 at_tests_pattern=" (${at_tests_pattern}): "
179 fi
180 $EGREP -e "$at_tests_pattern" <<EOF
181 m4_divert([HELP])dnl Help message inserted here.
182 m4_divert([SETUP])dnl
183 EOF
184 exit 0
185 fi
186
187 # Tests to run.
188 test -z "$at_tests" && at_tests=$at_tests_all
189
190 # Can we diff with `/dev/null'? DU 5.0 refuses.
191 if diff /dev/null /dev/null >/dev/null 2>&1; then
192 at_devnull=/dev/null
193 else
194 at_devnull=at-devnull
195 cp /dev/null $at_devnull
196 fi
197
198 # Use `diff -u' when possible.
199 if diff -u $at_devnull $at_devnull >/dev/null 2>&1; then
200 at_diff='diff -u'
201 else
202 at_diff=diff
203 fi
204
205 # Tester and tested.
206 if $1 --version | grep "$at_package.*$at_version" >/dev/null; then
207 AS_BOX([Test suite for $at_package $at_version])
208 else
209 AS_BOX([ERROR: Not using the proper version, no tests performed])
210 exit 1
211 fi
212
213 # Setting up the FDs.
214 # 5 is stdout conditioned by verbosity.
215 if test $at_verbose = echo; then
216 exec 5>&1
217 else
218 exec 5>/dev/null
219 fi
220
221 at_fail_list=
222 at_skip_list=
223 at_test_count=0
224 m4_divert([TESTS])dnl
225
226 for at_test in $at_tests
227 do
228 at_status=0
229 rm -rf $at_data_files
230 # Clearly separate the tests when verbose.
231 test $at_test_count != 0 && $at_verbose
232 case $at_test in
233 dnl Tests inserted here (TESTS).
234 m4_divert([TAIL])[]dnl
235
236 * )
237 echo $as_me: no such test: $at_test
238 continue
239 ;;
240 esac
241 case $at_test in
242 banner-*) ;;
243 *)
244 if test ! -f at-check-line; then
245 echo "$as_me: warning: no at-check-line which means a failure happened"
246 echo "$as_me: warning: in a [AT_SETUP/AT_CLEANUP] pair before any"
247 echo "$as_me: warning: [AT_CHECK] could be run. This test suite is"
248 echo "$as_me: warning: improperly designed, please report to"
249 echo "$as_me: warning: <$at_bugreport>."
250 cp at-setup-line at-check-line
251 fi
252 at_test_count=`expr 1 + $at_test_count`
253 $at_verbose $at_n "$at_test. $srcdir/`cat at-setup-line`: $at_c"
254 case $at_status in
255 0) echo ok
256 ;;
257 77) echo "ok (skipped near \``cat at-check-line`')"
258 at_skip_list="$at_skip_list $at_test"
259 ;;
260 *) echo "FAILED near \``cat at-check-line`'"
261 at_fail_list="$at_fail_list $at_test"
262 $at_stop_on_error && break
263 ;;
264 esac
265 $at_debug || rm -rf $at_data_files
266 ;;
267 esac
268 done
269
270 # Wrap up the test suite with summary statistics.
271
272 rm -f at-check-line at-setup-line
273 at_skip_count=`set dummy $at_skip_list; shift; echo $[#]`
274 at_fail_count=`set dummy $at_fail_list; shift; echo $[#]`
275 if test $at_fail_count = 0; then
276 if test $at_skip_count = 0; then
277 AS_BOX([All $at_test_count tests were successful])
278 else
279 AS_BOX([All $at_test_count tests were successful ($at_skip_count skipped)])
280 fi
281 elif test $at_debug = false; then
282 if $at_stop_on_error; then
283 AS_BOX([ERROR: One of the tests failed, inhibiting subsequent tests])
284 else
285 AS_BOX([ERROR: Suite unsuccessful, $at_fail_count of $at_test_count tests failed])
286 fi
287
288 # Remove any debugging script resulting from a previous run.
289 rm -f debug-*.sh $[0].log
290 echo
291 echo $at_n "Writing \`debug-NN.sh' scripts, NN =$at_c"
292 for at_group in $at_fail_list; do
293 echo $at_n " $at_group$at_c"
294 ( echo "#! /bin/sh"
295 echo 'exec ${CONFIG_SHELL-'"$SHELL"'} '"$[0]"' -v -d '"$at_group"' ${1+"$[@]"}'
296 echo 'exit 1'
297 ) >debug-$at_group.sh
298 chmod +x debug-$at_group.sh
299 done
300 echo ', done'
301 echo
302 echo 'You may investigate any problem if you feel able to do so, in which'
303 echo 'case the testsuite provide a good starting point.'
304 echo
305 echo 'Now, failed tests will be executed again, verbosely, and logged'
306 echo 'in the file '$[0]'.log.'
307
308 {
309 AS_BOX([Test suite log for $at_package $at_version])
310 echo
311
312 # Try to find a few ChangeLogs in case it might help determining the
313 # exact version.
314 find "$at_top_srcdir" -name ChangeLog \
315 -exec echo {} : ';' \
316 -exec sed 's/^/| /;10q' {} ';' \
317 -exec echo ';'
318
319 # Summary of failed and skipped tests.
320 if test $at_fail_count != 0; then
321 echo "Failed tests:"
322 $SHELL $[0] $at_fail_list --help
323 echo
324 fi
325 if test $at_skip_count != 0; then
326 echo "Skipped tests:"
327 $SHELL $[0] $at_skip_list --help
328 echo
329 fi
330
331 AS_UNAME
332 } >>$[0].log
333
334 $SHELL $[0] -v -d $at_fail_list 2>&1 | tee -a $[0].log
335 AS_BOX([$[0].log is created])
336
337 echo
338 echo "Please send \`$[0].log' to <$at_bugreport> together with all"
339 echo "the information you think might help."
340 exit 1
341 fi
342
343 exit 0
344 m4_divert_pop()dnl
345 m4_wrap([m4_divert_text([DEFAULT],
346 [# List of the tests.
347 at_tests_all="AT_TESTS_ALL "])])dnl
348 m4_wrap([m4_divert_text([SETUP],
349 [# List of the output files.
350 at_data_files="AT_data_files "])])dnl
351 ])# AT_INIT
352
353
354
355 # AT_SETUP(DESCRIPTION)
356 # ---------------------
357 # Start a group of related tests, all to be executed in the same subshell.
358 # The group is testing what DESCRIPTION says.
359 m4_define([AT_SETUP],
360 [m4_define([AT_ordinal], m4_incr(AT_ordinal))
361 m4_append([AT_TESTS_ALL], [ ]m4_defn([AT_ordinal]))
362 m4_divert_text([HELP],
363 [m4_format([ %3d: %-15s %s], AT_ordinal, AT_LINE, [$1])])
364 m4_divert_push([TESTS])dnl
365 AT_ordinal ) [#] AT_ordinal. AT_LINE: $1
366 echo AT_LINE >at-setup-line
367 $at_verbose "AT_ordinal. $srcdir/AT_LINE: testing $1..."
368 $at_quiet $at_n "m4_format([%3d: %-18s], AT_ordinal, AT_LINE)[]$at_c"
369 (
370 $at_traceon
371 ])
372
373
374 # AT_CLEANUP_FILE_IFELSE(FILE, IF-REGISTERED, IF-NOT-REGISTERED)
375 # --------------------------------------------------------------
376 # We try to build a regular expression matching `[', `]', `*', and
377 # `.', i.e., the regexp active characters.
378 #
379 # Novices would write, `[[]*.]', which sure fails since the character
380 # class ends with the first closing braquet.
381 # M4 gurus will sure write `[\[\]*.]', but it will fail too because
382 # regexp does not support this and understands `\' per se.
383 # Regexp gurus will write `[][*.]' which is indeed what Regexp expects,
384 # but it will fail for M4 reasons: it's the same as `[*.]'.
385 #
386 # So the question is:
387 #
388 # Can you write a regexp that matches those four characters,
389 # and respects the M4 quotation constraints?
390 #
391 # The answer is: (rot13) tvira va gur ertrkc orybj, lbh vqvbg!
392 m4_define([AT_CLEANUP_FILE_IFELSE],
393 [m4_if(m4_regexp(AT_data_files, m4_patsubst([ $1 ], [[[]\|[]]\|[*.]], [\\\&])),
394 -1,
395 [$3], [$2])])
396
397
398 # AT_CLEANUP_FILE(FILE)
399 # ---------------------
400 # Register FILE for AT_CLEANUP.
401 m4_define([AT_CLEANUP_FILE],
402 [AT_CLEANUP_FILE_IFELSE([$1], [],
403 [m4_append([AT_data_files], [$1 ])])])
404
405
406 # AT_CLEANUP_FILES(FILES)
407 # -----------------------
408 # Declare a list of FILES to clean.
409 m4_define([AT_CLEANUP_FILES],
410 [m4_foreach([AT_File], m4_quote(m4_patsubst([$1], [ *], [,])),
411 [AT_CLEANUP_FILE(AT_File)])])
412
413
414 # AT_CLEANUP(FILES)
415 # -----------------
416 # Complete a group of related tests, recursively remove those FILES
417 # created within the test. There is no need to list files created with
418 # AT_DATA.
419 m4_define([AT_CLEANUP],
420 [AT_CLEANUP_FILES([$1])dnl
421 )
422 at_status=$?
423 ;;
424
425 m4_divert([TESTS])[]dnl
426 m4_divert_pop()dnl
427 ])# AT_CLEANUP
428
429
430 # AT_BANNER(TEXT)
431 # ---------------
432 # Output TEXT without any shell expansion.
433 m4_define([AT_BANNER],
434 [m4_define([AT_banner_ordinal], m4_incr(AT_banner_ordinal))
435 m4_append([AT_TESTS_ALL], [ banner-]m4_defn([AT_banner_ordinal]))
436 m4_divert_push([TESTS])dnl
437 banner-AT_banner_ordinal ) [#] Banner AT_banner_ordinal. AT_LINE
438 cat <<\_ATEOF
439
440 $1
441
442 _ATEOF
443 ;;
444
445 m4_divert_pop()dnl
446 ])# AT_BANNER
447
448
449 # AT_DATA(FILE, CONTENTS)
450 # -----------------------
451 # Initialize an input data FILE with given CONTENTS, which should end with
452 # an end of line.
453 # This macro is not robust to active symbols in CONTENTS *on purpose*.
454 # If you don't want CONTENT to be evaluated, quote it twice.
455 m4_define([AT_DATA],
456 [AT_CLEANUP_FILES([$1])dnl
457 cat >$1 <<'_ATEOF'
458 $2[]_ATEOF
459 ])
460
461
462 # AT_CHECK(COMMANDS, [STATUS = 0], STDOUT, STDERR)
463 # ------------------------------------------------
464 # Execute a test by performing given shell COMMANDS. These commands
465 # should normally exit with STATUS, while producing expected STDOUT and
466 # STDERR contents.
467 #
468 # STATUS, STDOUT, and STDERR are not checked if equal to `ignore'.
469 #
470 # If STDOUT is `expout', then stdout is compared to the content of the file
471 # `expout'. Likewise for STDERR and `experr'.
472 #
473 # If STDOUT is `stdout', then the stdout is left in the file `stdout',
474 # likewise for STDERR and `stderr'. Don't do this:
475 #
476 # AT_CHECK([command >out])
477 # # Some checks on `out'
478 #
479 # do this instead:
480 #
481 # AT_CHECK([command], [], [stdout])
482 # # Some checks on `stdout'
483 #
484 # This is an unfortunate limitation inherited from Ultrix which will not
485 # let you redirect several times the same FD (see the Autoconf documentation).
486 # If you use the `AT_CHECK([command >out])' be sure to have the test
487 # suite introduces spurious failures.
488 #
489 # You might wander why not just use `ignore' and directly use stdout and
490 # stderr left by the test suite. Firstly because the names of these files
491 # is an internal detail, and secondly, because
492 #
493 # AT_CHECK([command], [], [ignore])
494 # AT_CHECK([check stdout])
495 #
496 # will use `stdout' both in input and output: undefined behavior would
497 # certainly result. That's why the test suite will save them in `at-stdout'
498 # and `at-stderr', and will provide you with `stdout' and `stderr'.
499 #
500 # Any line of stderr starting with leading blanks and a `+' are filtered
501 # out, since most shells when tracing include subshell traces in stderr.
502 # This may cause spurious failures when the test suite is run with `-x'.
503 #
504 #
505 # Implementation Details
506 # ----------------------
507 # Ideally, we would like to run
508 #
509 # ( $at_traceon; COMMANDS >at-stdout 2> at-stderr )
510 #
511 # but we must group COMMANDS as it is not limited to a single command, and
512 # then the shells will save the traces in at-stderr. So we have to filter
513 # them out when checking stderr, and we must send them into the test suite's
514 # stderr to honor -x properly.
515 #
516 # Limiting COMMANDS to a single command is not good either, since them
517 # the user herself would use {} or (), and then we face the same problem.
518 #
519 # But then, there is no point in running
520 #
521 # ( $at_traceon { $1 ; } >at-stdout 2>at-stder1 )
522 #
523 # instead of the simpler
524 #
525 # ( $at_traceon; $1 ) >at-stdout 2>at-stder1
526 #
527 m4_define([AT_CHECK],
528 [$at_traceoff
529 $at_verbose "$srcdir/AT_LINE: AS_ESCAPE([$1])"
530 echo AT_LINE >at-check-line
531 ( $at_traceon; $1 ) >at-stdout 2>at-stder1
532 at_status=$?
533 $EGREP '^ *\+' at-stder1 >&2
534 $EGREP -v '^ *\+' at-stder1 >at-stderr
535 at_failed=false
536 dnl Check stderr.
537 m4_case([$4],
538 stderr, [(echo stderr:; tee stderr <at-stderr) >&5],
539 ignore, [(echo stderr:; cat at-stderr) >&5],
540 experr, [$at_diff experr at-stderr >&5 || at_failed=:],
541 [], [$at_diff $at_devnull at-stderr >&5 || at_failed=:],
542 [echo >>at-stderr; echo "AS_ESCAPE([$4])" | $at_diff - at-stderr >&5 || at_failed=:])
543 dnl Check stdout.
544 m4_case([$3],
545 stdout, [(echo stdout:; tee stdout <at-stdout) >&5],
546 ignore, [(echo stdout:; cat at-stdout) >&5],
547 expout, [$at_diff expout at-stdout >&5 || at_failed=:],
548 [], [$at_diff $at_devnull at-stdout >&5 || at_failed=:],
549 [echo >>at-stdout; echo "AS_ESCAPE([$3])" | $at_diff - at-stdout >&5 || at_failed=:])
550 dnl Check exit val. Don't `skip' if we are precisely checking $? = 77.
551 case $at_status in
552 m4_case([$2],
553 [77],
554 [],
555 [ 77) exit 77;;
556 ])dnl
557 m4_case([$2],
558 [ignore],
559 [ *);;],
560 [ m4_default([$2], [0])) ;;
561 *) $at_verbose "$srcdir/AT_LINE: exit code was $at_status, expected m4_default([$2], [0])" >&2
562 at_failed=:;;])
563 esac
564 AS_IF($at_failed, [$5], [$6])
565 $at_failed && exit 1
566 $at_traceon
567 ])# AT_CHECK