2 # This is a testing framework.
4 # Copyright (C) 1998-2016 Free Software Foundation, Inc.
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 3 of the License, or
9 # (at your option) any later version.
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.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 use vars
qw($VERSION @ISA @EXPORT);
23 use File::Compare qw(compare);
26 ($VERSION = '$Revision: 1.5 $ ') =~ tr/[0-9].//cd;
27 @EXPORT = qw
(run_tests triple_test getlimits
);
29 my $debug = $ENV{DEBUG
};
31 my @Types = qw
(IN IN_PIPE OUT ERR AUX CMP EXIT PRE POST OUT_SUBST
32 ERR_SUBST ENV ENV_DEL
);
33 my %Types = map {$_ => 1} @Types;
34 my %Zero_one_type = map {$_ => 1}
35 qw
(OUT ERR EXIT PRE POST OUT_SUBST ERR_SUBST ENV
);
36 my $srcdir = "$ENV{srcdir}";
39 # When running in a DJGPP environment, make $ENV{SHELL} point to bash.
40 # Otherwise, a bad shell might be used (e.g. command.com) and many
43 and $ENV{SHELL
} = "$ENV{DJDIR}/bin/bash.exe";
45 # A file spec: a scalar or a reference to a single-keyed hash
47 # 'contents' contents only (file name is derived from test name)
48 # {filename => 'contents'} filename and contents
49 # {filename => undef} filename only -- $(srcdir)/tests/filename must exist
51 # FIXME: If there is more than one input file, then you can't specify 'REDIR'.
54 # I/O spec: a hash ref with the following properties
56 # - one key/value pair
57 # - the key must be one of these strings: IN, OUT, ERR, AUX, CMP, EXIT
58 # - the value must be a file spec
59 # {OUT => 'data'} put data in a temp file and compare it to stdout from cmd
60 # {OUT => {'filename'=>undef}} compare contents of existing filename to
62 # {OUT => {'filename'=>[$CTOR, $DTOR]}} $CTOR and $DTOR are references to
63 # functions, each which is passed the single argument 'filename'.
64 # $CTOR must create 'filename'.
65 # DTOR may be omitted in which case 'sub{unlink @_[0]}' is used.
66 # FIXME: implement this
68 # Same as for OUT, but compare with stderr, not stdout.
69 # {OUT_SUBST => 's/variable_output/expected_output/'}
70 # Transform actual standard output before comparing it against expected.
71 # This is useful e.g. for programs like du that produce output that
72 # varies a lot from system. E.g., an empty file may consume zero file
73 # blocks, or more, depending on the OS and on the file system type.
74 # {ERR_SUBST => 's/variable_output/expected_output/'}
75 # Transform actual stderr output before comparing it against expected.
76 # This is useful when verifying that we get a meaningful diagnostic.
77 # For example, in rm/fail-2eperm, we have to account for three different
78 # diagnostics: Operation not permitted, Not owner, and Permission denied.
79 # {EXIT => N} expect exit status of cmd to be N
80 # {ENV => 'VAR=val ...'}
81 # Prepend 'VAR=val ...' to the command that we execute via 'system'.
83 # Remove VAR from the environment just before running the corresponding
84 # command, and restore any value just afterwards.
86 # There may be many input file specs. File names from the input specs
87 # are concatenated in order on the command line.
88 # There may be at most one of the OUT-, ERR-, and EXIT-keyed specs.
89 # If the OUT-(or ERR)-keyed hash ref is omitted, then expect no output
90 # on stdout (or stderr).
91 # If the EXIT-keyed one is omitted, then expect the exit status to be zero.
93 # FIXME: Make sure that no junkfile is also listed as a
94 # non-junkfile (i.e., with undef for contents)
99 $string =~ s/\'/\'\\\'\'/g;
103 sub _create_file
($$$$)
105 my ($program_name, $test_name, $file_name, $data) = @_;
107 if (defined $file_name)
113 $file = "$test_name.$Global_count";
117 warn "creating file '$file' with contents '$data'\n" if $debug;
119 # The test spec gave a string.
120 # Write it to a temp file and return tempfile name.
121 my $fh = new FileHandle
"> $file";
122 die "$program_name: $file: $!\n" if ! $fh;
124 $fh->close || die "$program_name: $file: $!\n";
129 sub _compare_files
($$$$$)
131 my ($program_name, $test_name, $in_or_out, $actual, $expected) = @_;
133 my $differ = compare
($actual, $expected);
136 my $info = (defined $in_or_out ?
"std$in_or_out " : '');
137 warn "$program_name: test $test_name: ${info}mismatch, comparing "
138 . "$expected (expected) and $actual (actual)\n";
139 # Ignore any failure, discard stderr.
140 system "diff -c $expected $actual 2>/dev/null";
146 sub _process_file_spec
($$$$$)
148 my ($program_name, $test_name, $file_spec, $type, $junk_files) = @_;
150 my ($file_name, $contents);
153 ($file_name, $contents) = (undef, $file_spec);
155 elsif (ref $file_spec eq 'HASH')
157 my $n = keys %$file_spec;
158 die "$program_name: $test_name: $type spec has $n elements --"
161 ($file_name, $contents) = each %$file_spec;
163 # This happens for the AUX hash in an io_spec like this:
164 # {CMP=> ['zy123utsrqponmlkji', {'@AUX@'=> undef}]},
166 or return $file_name;
170 die "$program_name: $test_name: invalid RHS in $type-spec\n"
173 my $is_junk_file = (! defined $file_name
174 || (($type eq 'IN' || $type eq 'AUX' || $type eq 'CMP')
175 && defined $contents));
176 my $file = _create_file
($program_name, $test_name,
177 $file_name, $contents);
181 push @
$junk_files, $file
185 # FIXME: put $srcdir in here somewhere
186 warn "$program_name: $test_name: specified file '$file' does"
188 if ! -f
"$srcdir/tests/$file";
197 foreach my $eo (qw
(AUX OUT ERR
))
202 and $s =~ s/\@$eo\@/$f/g;
210 open $NV, "getlimits |" or die "Error running getlimits\n";
211 my %limits = map {split /=|\n/} <$NV>;
215 # FIXME: cleanup on interrupt
216 # FIXME: extract 'do_1_test' function
218 # FIXME: having to include $program_name here is an expedient kludge.
219 # Library code doesn't 'die'.
220 sub run_tests
($$$$$)
222 my ($program_name, $prog, $t_spec, $save_temps, $verbose) = @_;
224 # To indicate that $prog is a shell built-in, you'd make it a string 'ref'.
225 # E.g., call run_tests ($prog, \$prog, \@Tests, $save_temps, $verbose);
226 # If it's a ref, invoke it via "env":
227 my $built_prog = ref $prog ?
$$prog : $prog;
228 my @prog = ref $prog ?
(qw(env --), $$prog) : $prog;
230 # Warn about empty t_spec.
233 # Remove all temp files upon interrupt.
236 # Verify that test names are distinct.
237 my $bad_test_name = 0;
241 foreach $t (@
$t_spec)
243 my $test_name = $t->[0];
244 if ($seen{$test_name})
246 warn "$program_name: $test_name: duplicate test name\n";
249 $seen{$test_name} = 1;
253 my $t8 = lc substr $test_name, 0, 8;
254 if ($seen_8dot3{$t8})
256 warn "$program_name: 8.3 test name conflict: "
257 . "$test_name, $seen_8dot3{$t8}\n";
260 $seen_8dot3{$t8} = $test_name;
263 # The test name may be no longer than 30 bytes.
264 # Yes, this is an arbitrary limit. If it causes trouble,
265 # consider removing it.
267 if ($max < length $test_name)
269 warn "$program_name: $test_name: test name is too long (> $max)\n";
273 return 1 if $bad_test_name;
275 $ENV{built_programs
} =~ /\b$built_prog\b/ ||
276 CuSkip
::skip
"required program(s) not built [$built_prog]\n";
278 # FIXME check exit status
279 system (@prog, '--version') if $verbose;
283 foreach my $tt (@
$t_spec)
288 my $test_name = shift @
$t;
292 # FIXME: maybe don't reset this.
300 foreach $io_spec (@
$t)
304 push @args, $io_spec;
308 if (ref $io_spec ne 'HASH')
310 eval 'use Data::Dumper';
311 die "$program_name: $test_name: invalid entry in test spec; "
312 . "expected HASH-ref,\nbut got this:\n"
313 . Data
::Dumper
->Dump ([\
$io_spec], ['$io_spec']) . "\n";
316 my $n = keys %$io_spec;
317 die "$program_name: $test_name: spec has $n elements --"
320 my ($type, $val) = each %$io_spec;
321 die "$program_name: $test_name: invalid key '$type' in test spec\n"
324 # Make sure there's no more than one of OUT, ERR, EXIT, etc.
325 die "$program_name: $test_name: more than one $type spec\n"
326 if $Zero_one_type{$type} and $seen_type{$type}++;
328 if ($type eq 'PRE' or $type eq 'POST')
330 $expect->{$type} = $val;
338 or die "$program_name: $test_name: invalid CMP spec\n";
340 or die "$program_name: $test_name: invalid CMP list; must have"
341 . " exactly 2 elements\n";
343 foreach my $e (@
$val)
347 and die "$program_name: $test_name: invalid element ($r)"
348 . " in CMP list; only scalars and hash references "
350 if ($r && $r eq 'HASH')
354 or die "$program_name: $test_name: CMP spec has $n "
355 . "elements -- expected 1\n";
357 # Replace any '@AUX@' in the key of %$e.
358 my ($ff, $val) = each %$e;
359 my $new_ff = _at_replace
$expect, $ff;
362 $e->{$new_ff} = $val;
366 my $cmp_file = _process_file_spec
($program_name, $test_name,
367 $e, $type, \
@junk_files);
368 push @cmp_files, $cmp_file;
370 push @post_compare, [@cmp_files];
372 $expect->{$type} = $val;
378 die "$program_name: $test_name: invalid EXIT code\n"
380 # FIXME: make sure $data is numeric
381 $expect->{EXIT
} = $val;
385 if ($type =~ /^(OUT|ERR)_SUBST$/)
387 $expect->{RESULT_SUBST
} ||= {};
388 $expect->{RESULT_SUBST
}->{$1} = $val;
394 $env_prefix = "$val ";
398 if ($type eq 'ENV_DEL')
400 push @env_delete, $val;
404 my $file = _process_file_spec
($program_name, $test_name, $val,
405 $type, \
@junk_files);
407 if ($type eq 'IN' || $type eq 'IN_PIPE')
409 my $quoted_file = _shell_quote
$file;
410 if ($type eq 'IN_PIPE')
412 defined $input_pipe_cmd
413 and die "$program_name: $test_name: only one input"
414 . " may be specified with IN_PIPE\n";
415 $input_pipe_cmd = "cat $quoted_file |";
419 push @args, $quoted_file;
422 elsif ($type eq 'AUX' || $type eq 'OUT' || $type eq 'ERR')
424 $expect->{$type} = $file;
428 die "$program_name: $test_name: invalid type: $type\n"
432 # Expect an exit status of zero if it's not specified.
433 $expect->{EXIT
} ||= 0;
435 # Allow ERR to be omitted -- in that case, expect no error output.
436 foreach my $eo (qw
(OUT ERR
))
438 if (!exists $expect->{$eo})
440 $expect->{$eo} = _create_file
($program_name, $test_name,
442 push @junk_files, $expect->{$eo};
446 # FIXME: Does it ever make sense to specify a filename *and* contents
447 # in OUT or ERR spec?
449 # FIXME: this is really suboptimal...
451 foreach my $a (@args)
453 $a = _at_replace
$expect, $a;
458 warn "$test_name...\n" if $verbose;
459 &{$expect->{PRE
}} if $expect->{PRE
};
461 $actual{OUT
} = "$test_name.O";
462 $actual{ERR
} = "$test_name.E";
463 push @junk_files, $actual{OUT
}, $actual{ERR
};
464 my @cmd = (@prog, @args, "> $actual{OUT}", "2> $actual{ERR}");
466 and unshift @cmd, $env_prefix;
467 defined $input_pipe_cmd
468 and unshift @cmd, $input_pipe_cmd;
469 my $cmd_str = join (' ', @cmd);
471 # Delete from the environment any symbols specified by syntax
472 # like this: {ENV_DEL => 'TZ'}.
474 foreach my $env_sym (@env_delete)
476 my $val = delete $ENV{$env_sym};
478 and $pushed_env{$env_sym} = $val;
481 warn "Running command: '$cmd_str'\n" if $debug;
482 my $rc = 0xffff & system $cmd_str;
484 # Restore any environment setting we changed via a deletion.
485 foreach my $env_sym (keys %pushed_env)
487 $ENV{$env_sym} = $pushed_env{$env_sym};
492 warn "$program_name: test $test_name failed: command failed:\n"
493 . " '$cmd_str': $!\n";
497 $rc >>= 8 if $rc > 0x80;
498 if ($expect->{EXIT
} != $rc)
500 warn "$program_name: test $test_name failed: exit status mismatch:"
501 . " expected $expect->{EXIT}, got $rc\n";
507 # Record actual stdout and stderr contents, if POST may need them.
510 foreach my $eo (qw
(OUT ERR
))
512 my $out_file = $actual{$eo};
515 "$program_name: cannot open $out_file for reading: $!\n"),
517 $actual_data{$eo} = <IN
>;
519 or (warn "$program_name: failed to read $out_file: $!\n"),
524 foreach my $eo (qw
(OUT ERR
))
526 my $subst_expr = $expect->{RESULT_SUBST
}->{$eo};
527 if (defined $subst_expr)
529 my $out = $actual{$eo};
530 my $orig = "$out.orig";
532 # Move $out aside (to $orig), then recreate $out
533 # by transforming each line of $orig via $subst_expr.
535 or (warn "$program_name: cannot rename $out to $orig: $!\n"),
538 or (warn "$program_name: cannot open $orig for reading: $!\n"),
539 $fail = 1, (unlink $orig), next;
541 or (warn "$program_name: cannot unlink $orig: $!\n"),
544 or (warn "$program_name: cannot open $out for writing: $!\n"),
546 while (defined (my $line = <IN
>))
548 eval "\$_ = \$line; $subst_expr; \$line = \$_";
553 or (warn "$program_name: failed to write $out: $!\n"),
557 my $eo_lower = lc $eo;
558 _compare_files
($program_name, $test_name, $eo_lower,
559 $actual{$eo}, $expect->{$eo})
563 foreach my $pair (@post_compare)
565 my ($expected, $actual) = @
$pair;
566 _compare_files
$program_name, $test_name, undef, $actual, $expected
572 and &{$expect->{POST
}} ($actual_data{OUT
}, $actual_data{ERR
});
576 # FIXME: maybe unlink files inside the big foreach loop?
577 unlink @junk_files if ! $save_temps;
582 # For each test in @$TESTS, generate two additional tests,
583 # one using stdin, the other using a pipe. I.e., given this one
584 # ['idem-0', {IN=>''}, {OUT=>''}],
586 # ['idem-0.r', '<', {IN=>''}, {OUT=>''}],
587 # ['idem-0.p', {IN_PIPE=>''}, {OUT=>''}],
588 # Generate new tests only if there is exactly one input spec.
589 # The returned list of tests contains each input test, followed
590 # by zero or two derived tests.
595 foreach my $t (@
$tests)
605 and push (@args, $e), next;
607 ref $e && ref $e eq 'HASH'
608 or (warn "$0: $t->[0]: unexpected entry type\n"), next;
610 and (push @in, $e->{IN
}), next;
611 push @list_of_hash, $e;
613 # Add variants IFF there is exactly one input file.
616 shift @args; # discard test name
617 push @new, ["$t->[0].r", @args, '<', {IN
=> $in[0]}, @list_of_hash];
618 push @new, ["$t->[0].p", @args, {IN_PIPE
=> $in[0]}, @list_of_hash];