2 # This is a testing framework.
4 # Copyright (C) 1998, 2000, 2001, 2002, 2004, 2005, 2006 Free Software
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 use vars qw($VERSION @ISA @EXPORT);
27 use File::Compare qw(compare);
30 ($VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
31 @EXPORT = qw (run_tests);
33 my $debug = $ENV{DEBUG};
35 my @Types = qw (IN IN_PIPE OUT ERR AUX CMP EXIT PRE POST OUT_SUBST
36 ERR_SUBST ENV ENV_DEL);
37 my %Types = map {$_ => 1} @Types;
38 my %Zero_one_type = map {$_ => 1}
39 qw (OUT ERR EXIT PRE POST OUT_SUBST ERR_SUBST ENV);
40 my $srcdir = $ENV{srcdir};
43 # When running in a DJGPP environment, make $ENV{SHELL} point to bash.
44 # Otherwise, a bad shell might be used (e.g. command.com) and many
47 and $ENV{SHELL} = "$ENV{DJDIR}/bin/bash.exe";
49 # A file spec: a scalar or a reference to a single-keyed hash
51 # 'contents' contents only (file name is derived from test name)
52 # {filename => 'contents'} filename and contents
53 # {filename => undef} filename only -- $(srcdir)/filename must exist
55 # FIXME: If there is more than one input file, then you can't specify `REDIR'.
58 # I/O spec: a hash ref with the following properties
60 # - one key/value pair
61 # - the key must be one of these strings: IN, OUT, ERR, AUX, CMP, EXIT
62 # - the value must be a file spec
63 # {OUT => 'data'} put data in a temp file and compare it to stdout from cmd
64 # {OUT => {'filename'=>undef}} compare contents of existing filename to
66 # {OUT => {'filename'=>[$CTOR, $DTOR]}} $CTOR and $DTOR are references to
67 # functions, each which is passed the single argument `filename'.
68 # $CTOR must create `filename'.
69 # DTOR may be omitted in which case `sub{unlink @_[0]}' is used.
70 # FIXME: implement this
72 # Same as for OUT, but compare with stderr, not stdout.
73 # {OUT_SUBST => 's/variable_output/expected_output/'}
74 # Transform actual standard output before comparing it against expected output.
75 # This is useful e.g. for programs like du that produce output that
76 # varies a lot from system. E.g., an empty file may consume zero file
77 # blocks, or more, depending on the OS and on the file system type.
78 # {ERR_SUBST => 's/variable_output/expected_output/'}
79 # Transform actual stderr output before comparing it against expected.
80 # This is useful when verifying that we get a meaningful diagnostic.
81 # For example, in rm/fail-2eperm, we have to account for three different
82 # diagnostics: Operation not permitted, Not owner, and Permission denied.
83 # {EXIT => N} expect exit status of cmd to be N
84 # {ENV => 'VAR=val ...'}
85 # Prepend 'VAR=val ...' to the command that we execute via `system'.
87 # Remove VAR from the environment just before running the corresponding
88 # command, and restore any value just afterwards.
90 # There may be many input file specs. File names from the input specs
91 # are concatenated in order on the command line.
92 # There may be at most one of the OUT-, ERR-, and EXIT-keyed specs.
93 # If the OUT-(or ERR)-keyed hash ref is omitted, then expect no output
94 # on stdout (or stderr).
95 # If the EXIT-keyed one is omitted, then expect the exit status to be zero.
97 # FIXME: Make sure that no junkfile is also listed as a
98 # non-junkfile (i.e. with undef for contents)
103 $string =~ s/\'/\'\\\'\'/g;
107 sub _create_file ($$$$)
109 my ($program_name, $test_name, $file_name, $data) = @_;
111 if (defined $file_name)
117 $file = "$test_name.$Global_count";
121 warn "creating file `$file' with contents `$data'\n" if $debug;
123 # The test spec gave a string.
124 # Write it to a temp file and return tempfile name.
125 my $fh = new FileHandle "> $file";
126 die "$program_name: $file: $!\n" if ! $fh;
128 $fh->close || die "$program_name: $file: $!\n";
133 sub _compare_files ($$$$$)
135 my ($program_name, $test_name, $in_or_out, $actual, $expected) = @_;
137 my $differ = compare ($expected, $actual);
140 my $info = (defined $in_or_out ? "std$in_or_out " : '');
141 warn "$program_name: test $test_name: ${info}mismatch, comparing "
142 . "$actual (actual) and $expected (expected)\n";
143 # Ignore any failure, discard stderr.
144 system "diff -c $actual $expected 2>/dev/null";
150 sub _process_file_spec ($$$$$)
152 my ($program_name, $test_name, $file_spec, $type, $junk_files) = @_;
154 my ($file_name, $contents);
157 ($file_name, $contents) = (undef, $file_spec);
159 elsif (ref $file_spec eq 'HASH')
161 my $n = keys %$file_spec;
162 die "$program_name: $test_name: $type spec has $n elements --"
165 ($file_name, $contents) = each %$file_spec;
167 # This happens for the AUX hash in an io_spec like this:
168 # {CMP=> ['zy123utsrqponmlkji', {'@AUX@'=> undef}]},
170 or return $file_name;
174 die "$program_name: $test_name: invalid RHS in $type-spec\n"
177 my $is_junk_file = (! defined $file_name
178 || (($type eq 'IN' || $type eq 'AUX' || $type eq 'CMP')
179 && defined $contents));
180 my $file = _create_file ($program_name, $test_name,
181 $file_name, $contents);
185 push @$junk_files, $file
189 # FIXME: put $srcdir in here somewhere
190 warn "$program_name: $test_name: specified file `$file' does"
192 if ! -f "$srcdir/$file";
201 foreach my $eo (qw (AUX OUT ERR))
206 and $s =~ s/\@$eo\@/$f/g;
211 # FIXME: cleanup on interrupt
212 # FIXME: extract `do_1_test' function
214 # FIXME: having to include $program_name here is an expedient kludge.
215 # Library code doesn't `die'.
216 sub run_tests ($$$$$)
218 my ($program_name, $prog, $t_spec, $save_temps, $verbose) = @_;
220 # Warn about empty t_spec.
223 # Remove all temp files upon interrupt.
226 # Verify that test names are distinct.
227 my $bad_test_name = 0;
231 foreach $t (@$t_spec)
233 my $test_name = $t->[0];
234 if ($seen{$test_name})
236 warn "$program_name: $test_name: duplicate test name\n";
239 $seen{$test_name} = 1;
243 my $t8 = lc substr $test_name, 0, 8;
244 if ($seen_8dot3{$t8})
246 warn "$program_name: 8.3 test name conflict: "
247 . "$test_name, $seen_8dot3{$t8}\n";
250 $seen_8dot3{$t8} = $test_name;
253 # The test name may be no longer than 12 bytes,
254 # so that we can add a two-byte suffix without exceeding
255 # the maximum of 14 imposed on some old file systems.
256 if (14 < (length $test_name) + 2)
258 warn "$program_name: $test_name: test name is too long (> 12)\n";
262 return 1 if $bad_test_name;
264 # FIXME check exit status
265 system ($prog, '--version') if $verbose;
269 foreach $t (@$t_spec)
272 my $test_name = shift @$t;
276 # FIXME: maybe don't reset this.
284 foreach $io_spec (@$t)
288 push @args, $io_spec;
292 die "$program_name: $test_name: invalid test spec\n"
293 if ref $io_spec ne 'HASH';
295 my $n = keys %$io_spec;
296 die "$program_name: $test_name: spec has $n elements --"
299 my ($type, $val) = each %$io_spec;
300 die "$program_name: $test_name: invalid key `$type' in test spec\n"
303 # Make sure there's no more than one of OUT, ERR, EXIT, etc.
304 die "$program_name: $test_name: more than one $type spec\n"
305 if $Zero_one_type{$type} and $seen_type{$type}++;
307 if ($type eq 'PRE' or $type eq 'POST')
309 $expect->{$type} = $val;
317 or die "$program_name: $test_name: invalid CMP spec\n";
319 or die "$program_name: $test_name: invalid CMP list; must have"
320 . " exactly 2 elements\n";
322 foreach my $e (@$val)
326 and die "$program_name: $test_name: invalid element ($r)"
327 . " in CMP list; only scalars and hash references "
329 if ($r && $r eq 'HASH')
333 or die "$program_name: $test_name: CMP spec has $n "
334 . "elements -- expected 1\n";
336 # Replace any `@AUX@' in the key of %$e.
337 my ($ff, $val) = each %$e;
338 my $new_ff = _at_replace $expect, $ff;
341 $e->{$new_ff} = $val;
345 my $cmp_file = _process_file_spec ($program_name, $test_name,
346 $e, $type, \@junk_files);
347 push @cmp_files, $cmp_file;
349 push @post_compare, [@cmp_files];
351 $expect->{$type} = $val;
357 die "$program_name: $test_name: invalid EXIT code\n"
359 # FIXME: make sure $data is numeric
360 $expect->{EXIT} = $val;
364 if ($type =~ /^(OUT|ERR)_SUBST$/)
366 $expect->{RESULT_SUBST} ||= {};
367 $expect->{RESULT_SUBST}->{$1} = $val;
373 $env_prefix = "$val ";
377 if ($type eq 'ENV_DEL')
379 push @env_delete, $val;
383 my $file = _process_file_spec ($program_name, $test_name, $val,
384 $type, \@junk_files);
386 if ($type eq 'IN' || $type eq 'IN_PIPE')
388 my $quoted_file = _shell_quote $file;
389 if ($type eq 'IN_PIPE')
391 defined $input_pipe_cmd
392 and die "$program_name: $test_name: only one input"
393 . " may be specified with IN_PIPE\n";
394 $input_pipe_cmd = "cat $quoted_file |";
398 push @args, $quoted_file;
401 elsif ($type eq 'AUX' || $type eq 'OUT' || $type eq 'ERR')
403 $expect->{$type} = $file;
407 die "$program_name: $test_name: invalid type: $type\n"
411 # Expect an exit status of zero if it's not specified.
412 $expect->{EXIT} ||= 0;
414 # Allow ERR to be omitted -- in that case, expect no error output.
415 foreach my $eo (qw (OUT ERR))
417 if (!exists $expect->{$eo})
419 $expect->{$eo} = _create_file ($program_name, $test_name,
421 push @junk_files, $expect->{$eo};
425 # FIXME: Does it ever make sense to specify a filename *and* contents
426 # in OUT or ERR spec?
428 # FIXME: this is really suboptimal...
430 foreach my $a (@args)
432 $a = _at_replace $expect, $a;
437 warn "$test_name...\n" if $verbose;
438 &{$expect->{PRE}} if $expect->{PRE};
440 $actual{OUT} = "$test_name.O";
441 $actual{ERR} = "$test_name.E";
442 push @junk_files, $actual{OUT}, $actual{ERR};
443 my @cmd = ($prog, @args, "> $actual{OUT}", "2> $actual{ERR}");
444 defined $input_pipe_cmd
445 and unshift @cmd, $input_pipe_cmd;
446 my $cmd_str = $env_prefix . join (' ', @cmd);
448 # Delete from the environment any symbols specified by syntax
449 # like this: {ENV_DEL => 'TZ'}.
451 foreach my $env_sym (@env_delete)
453 my $val = delete $ENV{$env_sym};
455 and $pushed_env{$env_sym} = $val;
458 warn "Running command: `$cmd_str'\n" if $debug;
459 my $rc = 0xffff & system $cmd_str;
461 # Restore any environment setting we changed via a deletion.
462 foreach my $env_sym (keys %pushed_env)
464 $ENV{$env_sym} = $pushed_env{$env_sym};
469 warn "$program_name: test $test_name failed: command failed:\n"
470 . " `$cmd_str': $!\n";
474 $rc >>= 8 if $rc > 0x80;
475 if ($expect->{EXIT} != $rc)
477 warn "$program_name: test $test_name failed: exit status mismatch:"
478 . " expected $expect->{EXIT}, got $rc\n";
483 foreach my $eo (qw (OUT ERR))
485 my $subst_expr = $expect->{RESULT_SUBST}->{$eo};
486 if (defined $subst_expr)
488 my $out = $actual{$eo};
489 my $orig = "$out.orig";
491 # Move $out aside (to $orig), then recreate $out
492 # by transforming each line of $orig via $subst_expr.
494 or (warn "$program_name: cannot rename $out to $orig: $!\n"),
497 or (warn "$program_name: cannot open $orig for reading: $!\n"),
498 $fail = 1, (unlink $orig), next;
500 or (warn "$program_name: cannot unlink $orig: $!\n"),
503 or (warn "$program_name: cannot open $out for writing: $!\n"),
505 while (defined (my $line = <IN>))
507 eval "\$_ = \$line; $subst_expr; \$line = \$_";
512 or (warn "$program_name: failed to write $out: $!\n"),
516 my $eo_lower = lc $eo;
517 _compare_files ($program_name, $test_name, $eo_lower,
518 $actual{$eo}, $expect->{$eo})
522 foreach my $pair (@post_compare)
524 my ($expected, $actual) = @$pair;
525 _compare_files $program_name, $test_name, undef, $actual, $expected
530 &{$expect->{POST}} if $expect->{POST};
534 # FIXME: maybe unlink files inside the big foreach loop?
535 unlink @junk_files if ! $save_temps;