Debian Codegraph @

tests/test_driver.pl

raw
1 #!/usr/bin/perl
2 # -*-perl-*-
3 #
4 # Modification history:
5 # Written 91-12-02 through 92-01-01 by Stephen McGee.
6 # Modified 92-02-11 through 92-02-22 by Chris Arthur to further generalize.
7 #
8 # Copyright (C) 1991-2020 Free Software Foundation, Inc.
9 # This file is part of GNU Make.
10 #
11 # GNU Make is free software; you can redistribute it and/or modify it under
12 # the terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 3 of the License, or (at your option) any later
14 # version.
15 #
16 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 # details.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # this program. If not, see <http://www.gnu.org/licenses/>.
23
24
25 # Test driver routines used by a number of test suites, including
26 # those for SCS, make, roll_dir, and scan_deps (?).
27 #
28 # this routine controls the whole mess; each test suite sets up a few
29 # variables and then calls &toplevel, which does all the real work.
30
31 # $Id$
32
33 use Config;
34 use Cwd;
35 use File::Spec;
36 use File::Basename 'dirname';
37
38 # The number of test categories we've run
39 $categories_run = 0;
40 # The number of test categroies that have passed
41 $categories_passed = 0;
42 # The total number of individual tests that have been run
43 $total_tests_run = 0;
44 # The total number of individual tests that have passed
45 $total_tests_passed = 0;
46 # The number of tests in this category that have been run
47 $tests_run = 0;
48 # The number of tests in this category that have passed
49 $tests_passed = 0;
50
51
52 # Yeesh. This whole test environment is such a hack!
53 $test_passed = 1;
54
55 # Timeout in seconds. If the test takes longer than this we'll fail it.
56 $test_timeout = 10;
57
58 # Path to Perl
59 $perl_name = $^X;
60 if ($^O ne 'VMS') {
61 $perl_name .= $Config{_exe} unless $perl_name =~ m/$Config{_exe}$/i;
62 }
63 # If it's a simple name, look it up on PATH
64 {
65 my ($v,$d,$f) = File::Spec->splitpath($perl_name);
66 if (!$d) {
67 my $perl = undef;
68 foreach my $p (File::Spec->path()) {
69 my $f = File::Spec->catfile($p, $f);
70 if (-e $f) {
71 $perl = $f;
72 last;
73 }
74 }
75 if ($perl) {
76 $perl_name = $perl;
77 } else {
78 print "Cannot locate Perl interpreter $perl_name\n";
79 }
80 }
81 }
82 # Make sure it uses forward-slashes even on Windows, else it won't work
83 # in recipes
84 $perl_name =~ tr,\\,/,;
85
86 # %makeENV is the cleaned-out environment.
87 %makeENV = ();
88
89 # %extraENV are any extra environment variables the tests might want to set.
90 # These are RESET AFTER EVERY TEST!
91 %extraENV = ();
92
93 sub vms_get_process_logicals {
94 # On VMS, added/changed %ENV items show up in the process logical
95 # name table. So to track changes, a copy of it needs to be captured.
96
97 my $raw_output = `show log/process/access_mode=supervisor`;
98 my @raw_output_lines = split('\n',$raw_output);
99 my %log_hash;
100 foreach my $line (@raw_output_lines) {
101 if ($line =~ /^\s+"([A-Za-z\$_]+)"\s+=\s+"(.+)"$/) {
102 $log_hash{$1} = $2;
103 }
104 }
105 return \%log_hash
106 }
107
108 # %origENV is the caller's original environment
109 %origENV = %ENV;
110
111 sub resetENV
112 {
113 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
114 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
115 # want to require that here, so just delete each one individually.
116
117 foreach $v (keys %ENV) {
118 delete $ENV{$v};
119 }
120
121 %ENV = %makeENV;
122
123 foreach $v (keys %extraENV) {
124 $ENV{$v} = $extraENV{$v};
125 delete $extraENV{$v};
126 }
127 }
128
129 sub toplevel
130 {
131 # Pull in benign variables from the user's environment
132
133 foreach (# UNIX-specific things
134 'TZ', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
135 'LD_LIBRARY_PATH',
136 # SAN things
137 'ASAN_OPTIONS', 'UBSAN_OPTIONS',
138 # Purify things
139 'PURIFYOPTIONS',
140 # Windows NT-specific stuff
141 'Path', 'SystemRoot',
142 # DJGPP-specific stuff
143 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
144 'FNCASE', '387', 'EMU387', 'GROUP'
145 ) {
146 $makeENV{$_} = $ENV{$_} if $ENV{$_};
147 }
148
149 # Make sure our compares are not foiled by locale differences
150
151 $makeENV{LC_ALL} = 'C';
152
153 # HACK. On Debian, ar is now compiled with determinism, which makes
154 # make tests fail. Pass in the U modifier to revert that behaviour change
155 # If ar has not been changed, this should be a no-op.
156 $makeENV{ARFLAGS} = 'rvU';
157
158 # Replace the environment with the new one
159 #
160 %origENV = %ENV;
161
162 resetENV();
163
164 $| = 1; # unbuffered output
165
166 $debug = 0; # debug flag
167 $profile = 0; # profiling flag
168 $verbose = 0; # verbose mode flag
169 $detail = 0; # detailed verbosity
170 $keep = 0; # keep temp files around
171 $workdir = "work"; # The directory where the test will start running
172 $scriptdir = "scripts"; # The directory where we find the test scripts
173 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
174 $default_output_stack_level = 0; # used by attach_default_output, etc.
175 $default_input_stack_level = 0; # used by attach_default_input, etc.
176 $cwd = "."; # don't we wish we knew
177 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
178
179 &get_osname; # sets $osname, $vos, $pathsep, and $short_filenames
180
181 &set_defaults; # suite-defined
182
183 &parse_command_line (@ARGV);
184
185 print "OS name = '$osname'\n" if $debug;
186
187 $workpath = "$cwdslash$workdir";
188 $scriptpath = "$cwdslash$scriptdir";
189
190 &set_more_defaults; # suite-defined
191
192 &print_banner;
193
194 if (-d $workpath) {
195 print "Clearing $workpath...\n";
196 &remove_directory_tree("$workpath/")
197 or &error ("Couldn't wipe out $workpath: $!\n");
198 } else {
199 mkdir ($workpath, 0777) or &error ("Couldn't mkdir $workpath: $!\n");
200 }
201
202 if (!-d $scriptpath) {
203 &error ("Failed to find $scriptpath containing perl test scripts.\n");
204 }
205
206 if (@TESTS) {
207 print "Making work dirs...\n";
208 foreach $test (@TESTS) {
209 if ($test =~ /^([^\/]+)\//) {
210 $dir = $1;
211 push (@rmdirs, $dir);
212 -d "$workpath/$dir"
213 or mkdir ("$workpath/$dir", 0777)
214 or &error ("Couldn't mkdir $workpath/$dir: $!\n");
215 }
216 }
217 } else {
218 print "Finding tests...\n";
219 opendir (SCRIPTDIR, $scriptpath)
220 or &error ("Couldn't opendir $scriptpath: $!\n");
221 @dirs = grep (!/^(\..*|CVS|RCS)$/, readdir (SCRIPTDIR) );
222 closedir (SCRIPTDIR);
223 foreach my $dir (@dirs) {
224 next if ($dir =~ /^(\..*|CVS|RCS)$/ || ! -d "$scriptpath/$dir");
225 push (@rmdirs, $dir);
226 # VMS can have overlayed file systems, so directories may repeat.
227 next if -d "$workpath/$dir";
228 mkdir ("$workpath/$dir", 0777)
229 or &error ("Couldn't mkdir $workpath/$dir: $!\n");
230 opendir (SCRIPTDIR, "$scriptpath/$dir")
231 or &error ("Couldn't opendir $scriptpath/$dir: $!\n");
232 @files = grep (!/^(\..*|CVS|RCS|.*~)$/, readdir (SCRIPTDIR) );
233 closedir (SCRIPTDIR);
234 foreach my $test (@files) {
235 -d $test and next;
236 push (@TESTS, "$dir/$test");
237 }
238 }
239 }
240
241 if (@TESTS == 0) {
242 &error ("\nNo tests in $scriptpath, and none were specified.\n");
243 }
244
245 print "\n";
246
247 run_all_tests();
248
249 foreach my $dir (@rmdirs) {
250 rmdir ("$workpath/$dir");
251 }
252
253 $| = 1;
254
255 $categories_failed = $categories_run - $categories_passed;
256 $total_tests_failed = $total_tests_run - $total_tests_passed;
257
258 if ($total_tests_failed) {
259 print "\n$total_tests_failed Test";
260 print "s" unless $total_tests_failed == 1;
261 print " in $categories_failed Categor";
262 print ($categories_failed == 1 ? "y" : "ies");
263 print " Failed (See .$diffext* files in $workdir dir for details) :-(\n\n";
264 return 0;
265 }
266
267 print "\n$total_tests_passed Test";
268 print "s" unless $total_tests_passed == 1;
269 print " in $categories_passed Categor";
270 print ($categories_passed == 1 ? "y" : "ies");
271 print " Complete ... No Failures :-)\n\n";
272 return 1;
273 }
274
275 sub get_osname
276 {
277 # Set up an initial value. In perl5 we can do it the easy way.
278 $osname = defined($^O) ? $^O : '';
279
280 # find the type of the port. We do this up front to have a single
281 # point of change if it needs to be tweaked.
282 #
283 # This is probably not specific enough.
284 #
285 if ($osname =~ /MSWin32/i || $osname =~ /Windows/i
286 || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
287 $port_type = 'W32';
288 }
289 # Bleah, the osname is so variable on DOS. This kind of bites.
290 # Well, as far as I can tell if we check for some text at the
291 # beginning of the line with either no spaces or a single space, then
292 # a D, then either "OS", "os", or "ev" and a space. That should
293 # match and be pretty specific.
294 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
295 $port_type = 'DOS';
296 }
297 # Check for OS/2
298 elsif ($osname =~ m%OS/2%) {
299 $port_type = 'OS/2';
300 }
301
302 # VMS has a GNV Unix mode or a DCL mode.
303 # The SHELL environment variable should not be defined in VMS-DCL mode.
304 elsif ($osname eq 'VMS' && !defined $ENV{"SHELL"}) {
305 $port_type = 'VMS-DCL';
306 }
307 # Everything else, right now, is UNIX. Note that we should integrate
308 # the VOS support into this as well and get rid of $vos; we'll do
309 # that next time.
310 else {
311 $port_type = 'UNIX';
312 }
313
314 # Find a path to Perl
315
316 # See if the filesystem supports long file names with multiple
317 # dots. DOS doesn't.
318 $short_filenames = 0;
319 (open (TOUCHFD, "> fancy.file.name") and close (TOUCHFD))
320 or $short_filenames = 1;
321 unlink ("fancy.file.name") or $short_filenames = 1;
322
323 if (! $short_filenames) {
324 # Thanks go to [email protected] (Jim Meyering) for suggesting a
325 # better way of doing this. (We used to test for existence of a /mnt
326 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
327 # Because perl on VOS translates /'s to >'s, we need to test for
328 # VOSness rather than testing for Unixness (ie, try > instead of /).
329
330 mkdir (".ostest", 0777) or &error ("Couldn't create .ostest: $!\n", 1);
331 open (TOUCHFD, "> .ostest>ick") and close (TOUCHFD);
332 chdir (".ostest") or &error ("Couldn't chdir to .ostest: $!\n", 1);
333 }
334
335 if (! $short_filenames && -f "ick") {
336 $osname = "vos";
337 $vos = 1;
338 $pathsep = ">";
339
340 } else {
341 # the following is regrettably gnarly, but it seems to be the only way
342 # to not get ugly error messages if uname can't be found.
343 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
344 # with switches first.
345 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
346 if ($osname =~ /not found/i) {
347 $osname = "(something posixy with no uname)";
348
349 } elsif ($@ ne "" || $?) {
350 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
351 if ($@ ne "" || $?) {
352 $osname = "(something posixy)";
353 }
354 }
355 $vos = 0;
356 $pathsep = "/";
357 }
358
359 if (! $short_filenames) {
360 chdir ("..") or &error ("Couldn't chdir to ..: $!\n", 1);
361 unlink (".ostest>ick");
362 rmdir (".ostest") or &error ("Couldn't rmdir .ostest: $!\n", 1);
363 }
364 }
365
366 sub parse_command_line
367 {
368 @argv = @_;
369
370 # use @ARGV if no args were passed in
371
372 if (@argv == 0) {
373 @argv = @ARGV;
374 }
375
376 # look at each option; if we don't recognize it, maybe the suite-specific
377 # command line parsing code will...
378
379 while (@argv) {
380 $option = shift @argv;
381 if ($option =~ /^-usage$/i) {
382 &print_usage;
383 exit 0;
384 }
385 if ($option =~ /^-(h|help)$/i) {
386 &print_help;
387 exit 0;
388 }
389
390 if ($option =~ /^-debug$/i) {
391 print "\nDEBUG ON\n";
392 $debug = 1;
393
394 } elsif ($option =~ /^-profile$/i) {
395 $profile = 1;
396
397 } elsif ($option =~ /^-verbose$/i) {
398 $verbose = 1;
399
400 } elsif ($option =~ /^-detail$/i) {
401 $detail = 1;
402 $verbose = 1;
403
404 } elsif ($option =~ /^-keep$/i) {
405 $keep = 1;
406
407 } elsif (&valid_option($option)) {
408 # The suite-defined subroutine takes care of the option
409
410 } elsif ($option =~ /^-/) {
411 print "Invalid option: $option\n";
412 &print_usage;
413 exit 0;
414
415 } else { # must be the name of a test
416 $option =~ s/\.pl$//;
417 push(@TESTS,$option);
418 }
419 }
420 }
421
422 sub max
423 {
424 my $num = shift @_;
425 my $newnum;
426
427 while (@_) {
428 $newnum = shift @_;
429 if ($newnum > $num) {
430 $num = $newnum;
431 }
432 }
433
434 return $num;
435 }
436
437 sub print_centered
438 {
439 my ($width, $string) = @_;
440
441 if (length ($string)) {
442 my $pad = " " x ( ($width - length ($string) + 1) / 2);
443 print "$pad$string";
444 }
445 }
446
447 sub print_banner
448 {
449 # $testee is suite-defined
450 my $info = "Running tests for $testee on $osname\n";
451 my $len = &max (length($info), length($testee_version), 73) + 5;
452 my $line = ("-" x $len) . "\n";
453
454 &print_centered ($len, $line);
455 &print_centered ($len, $info);
456 &print_centered ($len, $testee_version);
457 &print_centered ($len, $line);
458 print "\n";
459 }
460
461 sub run_all_tests
462 {
463 # Make sure we always run the tests from the current directory
464 unshift(@INC, cwd());
465
466 $categories_run = 0;
467
468 $lasttest = '';
469 # $testname is published
470 foreach $testname (sort @TESTS) {
471 # Skip duplicates on VMS caused by logical name search lists.
472 next if $testname eq $lasttest;
473 $lasttest = $testname;
474 $suite_passed = 1; # reset by test on failure
475 $num_of_logfiles = 0;
476 $num_of_tmpfiles = 0;
477 $description = "";
478 $details = "";
479 $old_makefile = undef;
480 $testname =~ s/^$scriptpath$pathsep//;
481 $perl_testname = "$scriptpath$pathsep$testname";
482 $testname =~ s/(\.pl|\.perl)$//;
483 $testpath = "$workpath$pathsep$testname";
484
485 chomp($fulltestdir = `pwd`);
486 $testcat = dirname($testname);
487 $fullworkdir = "$fulltestdir$pathsep$workpath$pathsep$testcat";
488
489 # Leave enough space in the extensions to append a number, even
490 # though it needs to fit into 8+3 limits.
491 if ($short_filenames) {
492 $logext = 'l';
493 $diffext = 'd';
494 $baseext = 'b';
495 $runext = 'r';
496 $extext = '';
497 } else {
498 $logext = 'log';
499 $diffext = 'diff';
500 $baseext = 'base';
501 $runext = 'run';
502 $extext = '.';
503 }
504 $log_filename = "$testpath.$logext";
505 $diff_filename = "$testpath.$diffext";
506 $base_filename = "$testpath.$baseext";
507 $run_filename = "$testpath.$runext";
508 $tmp_filename = "$testpath.$tmpfilesuffix";
509
510 -f $perl_testname or die "Invalid test: $testname\n\n";
511
512 setup_for_test();
513
514 $output = "........................................................ ";
515
516 substr($output,0,length($testname)) = "$testname ";
517
518 print $output;
519
520 $tests_run = 0;
521 $tests_passed = 0;
522
523 # Run the test!
524 $code = do $perl_testname;
525
526 ++$categories_run;
527 $total_tests_run += $tests_run;
528 $total_tests_passed += $tests_passed;
529
530 # How did it go?
531 if (!defined($code)) {
532 # Failed to parse or called die
533 if (length ($@)) {
534 warn "\n*** Test died ($testname): $@\n";
535 } else {
536 warn "\n*** Couldn't parse $perl_testname\n";
537 }
538 $status = "FAILED ($tests_passed/$tests_run passed)";
539
540 } elsif ($code == -1) {
541 # Skipped... not supported
542 $status = "N/A";
543 --$categories_run;
544
545 } elsif ($code != 1) {
546 # Bad result... this shouldn't really happen. Usually means that
547 # the suite forgot to end with "1;".
548 warn "\n*** Test returned $code\n";
549 $status = "FAILED ($tests_passed/$tests_run passed)";
550
551 } elsif ($tests_run == 0) {
552 # Nothing was done!!
553 $status = "FAILED (no tests found!)";
554
555 } elsif ($tests_run > $tests_passed) {
556 # Lose!
557 $status = "FAILED ($tests_passed/$tests_run passed)";
558
559 } else {
560 # Win!
561 ++$categories_passed;
562 $status = "ok ($tests_passed passed)";
563
564 # Clean up
565 for ($i = $num_of_tmpfiles; $i; $i--) {
566 rmfiles($tmp_filename . num_suffix($i));
567 }
568 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--) {
569 rmfiles($log_filename . num_suffix($i));
570 rmfiles($base_filename . num_suffix($i));
571 }
572 }
573
574 # If the verbose option has been specified, then a short description
575 # of each test is printed before displaying the results of each test
576 # describing WHAT is being tested.
577
578 if ($verbose) {
579 if ($detail) {
580 print "\nWHAT IS BEING TESTED\n";
581 print "--------------------";
582 }
583 print "\n\n$description\n\n";
584 }
585
586 # If the detail option has been specified, then the details of HOW
587 # the test is testing what it says it is testing in the verbose output
588 # will be displayed here before the results of the test are displayed.
589
590 if ($detail) {
591 print "\nHOW IT IS TESTED\n";
592 print "----------------";
593 print "\n\n$details\n\n";
594 }
595
596 print "$status\n";
597 }
598 }
599
600 # If the keep flag is not set, this subroutine deletes all filenames that
601 # are sent to it.
602
603 sub rmfiles
604 {
605 my (@files) = @_;
606
607 if (!$keep) {
608 return (unlink @files);
609 }
610
611 return 1;
612 }
613
614 sub print_standard_usage
615 {
616 my ($plname, @moreusage) = @_;
617
618 print "usage:\t$plname [testname] [-verbose] [-detail] [-keep]\n";
619 print "\t\t\t[-profile] [-usage] [-help] [-debug]\n";
620 foreach (@moreusage) {
621 print "\t\t\t$_\n";
622 }
623 }
624
625 sub print_standard_help
626 {
627 my (@morehelp) = @_;
628 my $t = " ";
629
630 my $line = "Test Driver For $testee";
631 print "$line\n";
632 $line = "=" x length ($line);
633 print "$line\n";
634
635 print_usage();
636
637 print "\ntestname\n"
638 . "${t}You may, if you wish, run only ONE test if you know the name\n"
639 . "${t}of that test and specify this name anywhere on the command\n"
640 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
641 . "${t}will be run.\n"
642 . "-verbose\n"
643 . "${t}If this option is given, a description of every test is\n"
644 . "${t}displayed before the test is run. (Not all tests may have\n"
645 . "${t}descriptions at this time)\n"
646 . "-detail\n"
647 . "${t}If this option is given, a detailed description of every\n"
648 . "${t}test is displayed before the test is run. (Not all tests\n"
649 . "${t}have descriptions at this time)\n"
650 . "-profile\n"
651 . "${t}If this option is given, then the profile file\n"
652 . "${t}is added to other profiles every time $testee is run.\n"
653 . "${t}This option only works on VOS at this time.\n"
654 . "-keep\n"
655 . "${t}You may give this option if you DO NOT want ANY\n"
656 . "${t}of the files generated by the tests to be deleted. \n"
657 . "${t}Without this option, all files generated by the test will\n"
658 . "${t}be deleted IF THE TEST PASSES.\n"
659 . "-debug\n"
660 . "${t}Use this option if you would like to see all of the system\n"
661 . "${t}calls issued and their return status while running the tests\n"
662 . "${t}This can be helpful if you're having a problem adding a test\n"
663 . "${t}to the suite, or if the test fails!\n";
664
665 foreach $line (@morehelp) {
666 my $tline = $line;
667 if (substr ($tline, 0, 1) eq "\t") {
668 substr ($tline, 0, 1) = $t;
669 }
670 print "$tline\n";
671 }
672 }
673
674 #######################################################################
675 ########### Generic Test Driver Subroutines ###########
676 #######################################################################
677
678 sub get_caller
679 {
680 my $depth = defined ($_[0]) ? $_[0] : 1;
681 my ($pkg, $filename, $linenum) = caller ($depth + 1);
682 return "$filename: $linenum";
683 }
684
685 sub error
686 {
687 my $message = $_[0];
688 my $caller = &get_caller (1);
689
690 if (defined ($_[1])) {
691 $caller = &get_caller ($_[1] + 1) . " -> $caller";
692 }
693
694 die "$caller: $message";
695 }
696
697 sub compare_output
698 {
699 my ($answer,$logfile) = @_;
700 my ($slurp, $answer_matched) = ('', 0);
701
702 ++$tests_run;
703
704 if (! defined $answer) {
705 print "Ignoring output ........ " if $debug;
706 $answer_matched = 1;
707 } else {
708 print "Comparing output ........ " if $debug;
709
710 $slurp = &read_file_into_string ($logfile);
711
712 # For make, get rid of any time skew error before comparing--too bad this
713 # has to go into the "generic" driver code :-/
714 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
715 $slurp =~ s/^.*Clock skew detected.*\n//gm;
716
717 if ($slurp eq $answer) {
718 $answer_matched = 1;
719 } else {
720 # See if it is a slash or CRLF problem
721 my ($answer_mod, $slurp_mod) = ($answer, $slurp);
722
723 $answer_mod =~ tr,\\,/,;
724 $answer_mod =~ s,\r\n,\n,gs;
725
726 $slurp_mod =~ tr,\\,/,;
727 $slurp_mod =~ s,\r\n,\n,gs;
728
729 $answer_matched = ($slurp_mod eq $answer_mod);
730
731 # If it still doesn't match, see if the answer might be a regex.
732 if (!$answer_matched && $answer =~ m,^/(.+)/$,) {
733 $answer_matched = ($slurp =~ /$1/);
734 if (!$answer_matched && $answer_mod =~ m,^/(.+)/$,) {
735 $answer_matched = ($slurp_mod =~ /$1/);
736 }
737 }
738 }
739 }
740
741 if ($answer_matched && $test_passed) {
742 print "ok\n" if $debug;
743 ++$tests_passed;
744 return 1;
745 }
746
747 if (! $answer_matched) {
748 print "DIFFERENT OUTPUT\n" if $debug;
749
750 &create_file (&get_basefile, $answer);
751 &create_file (&get_runfile, $command_string);
752
753 print "\nCreating Difference File ...\n" if $debug;
754
755 # Create the difference file
756
757 my $command = "diff -c " . &get_basefile . " " . $logfile;
758 &run_command_with_output(&get_difffile,$command);
759 }
760
761 return 0;
762 }
763
764 sub compare_output_string
765 {
766 my($answer,$slurp,$logfile) = @_;
767 my($answer_matched) = (0);
768
769 print "Comparing Output ........ " if $debug;
770
771 $slurp = subst_make_string($slurp);
772 $answer = subst_make_string($answer);
773
774 # For make, get rid of any time skew error before comparing--too bad this
775 # has to go into the "generic" driver code :-/
776 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
777 $slurp =~ s/^.*Clock skew detected.*\n//gm;
778
779 ++$tests_run;
780
781 if ($slurp eq $answer) {
782 $answer_matched = 1;
783 } else {
784 # See if it is a slash or CRLF problem
785 my ($answer_mod) = $answer;
786
787 $answer_mod =~ tr,\\,/,;
788 $answer_mod =~ s,\r\n,\n,gs;
789
790 $slurp =~ tr,\\,/,;
791 $slurp =~ s,\r\n,\n,gs;
792
793 $answer_matched = ($slurp eq $answer_mod);
794 }
795
796 if ($answer_matched && $test_passed)
797 {
798 print "ok\n" if $debug;
799 ++$tests_passed;
800 return 1;
801 }
802
803 if (! $answer_matched) {
804 if ($debug) {
805 print "DIFFERENT OUTPUT\n";
806 print "+++1:\n$answer\n";
807 print "+++2:\n$slurp\n";
808 }
809
810 &create_file (&get_basefile, $answer);
811
812 print "\nCreating Difference File ...\n" if $debug;
813
814 # Create the difference file
815
816 open(LOGFILE, ">$logfile") && print(LOGFILE $slurp) && close(LOGFILE);
817
818 local($command) = "diff -c " . &get_basefile . " " . $logfile;
819 &run_command_with_output(&get_difffile,$command);
820 }
821
822 $suite_passed = 0;
823 return 0;
824 }
825
826 sub read_file_into_string
827 {
828 my ($filename) = @_;
829 my $oldslash = $/;
830 undef $/;
831
832 open (RFISFILE, '<', $filename) or return "";
833 my $slurp = <RFISFILE>;
834 close (RFISFILE);
835
836 $/ = $oldslash;
837
838 return $slurp;
839 }
840
841 my @OUTSTACK = ();
842 my @ERRSTACK = ();
843
844 sub attach_default_output
845 {
846 my ($filename) = @_;
847
848 if ($vos)
849 {
850 my $code = system "++attach_default_output_hack $filename";
851 $code == -2 or &error ("adoh death\n", 1);
852 return 1;
853 }
854
855 my $dup = undef;
856 open($dup, '>&', STDOUT) or error("ado: $! duping STDOUT\n", 1);
857 push @OUTSTACK, $dup;
858
859 $dup = undef;
860 open($dup, '>&', STDERR) or error("ado: $! duping STDERR\n", 1);
861 push @ERRSTACK, $dup;
862
863 open(STDOUT, '>', $filename) or error("ado: $filename: $!\n", 1);
864 open(STDERR, ">&STDOUT") or error("ado: $filename: $!\n", 1);
865 }
866
867 # close the current stdout/stderr, and restore the previous ones from
868 # the "stack."
869
870 sub detach_default_output
871 {
872 if ($vos)
873 {
874 my $code = system "++detach_default_output_hack";
875 $code == -2 or &error ("ddoh death\n", 1);
876 return 1;
877 }
878
879 @OUTSTACK or error("default output stack has flown under!\n", 1);
880
881 close(STDOUT);
882 close(STDERR);
883
884
885 open (STDOUT, '>&', pop @OUTSTACK) or error("ddo: $! duping STDOUT\n", 1);
886 open (STDERR, '>&', pop @ERRSTACK) or error("ddo: $! duping STDERR\n", 1);
887 }
888
889 sub _run_with_timeout
890 {
891 my $code;
892
893 if ($port_type eq 'W32') {
894 my $pid = system(1, @_);
895 $pid > 0 or die "Cannot execute $_[0]\n";
896 local $SIG{ALRM} = sub {
897 my $e = $ERRSTACK[0];
898 print $e "\nTest timed out after $test_timeout seconds\n";
899 kill -9, $pid;
900 die "timeout\n";
901 };
902 alarm $test_timeout;
903 my $r = waitpid($pid, 0);
904 alarm 0;
905 $r == -1 and die "No such pid: $pid\n";
906 # This shouldn't happen since we wait forever or timeout via SIGALRM
907 $r == 0 and die "No process exited.\n";
908 $code = $?;
909
910 } else {
911 my $pid = fork();
912 if (! $pid) {
913 exec(@_) or die "exec: Cannot execute $_[0]: $!\n";
914 }
915 local $SIG{ALRM} = sub {
916 my $e = $ERRSTACK[0];
917 print $e "\nTest timed out after $test_timeout seconds\n";
918 # Resend the alarm to our process group to kill the children.
919 $SIG{ALRM} = 'IGNORE';
920 kill -14, $$;
921 die "timeout\n";
922 };
923 alarm $test_timeout;
924 my $r = waitpid($pid, 0);
925 alarm 0;
926 $r == -1 and die "No such pid: $pid\n";
927 # This shouldn't happen since we wait forever or timeout via SIGALRM
928 $r == 0 and die "No process exited.\n";
929 $code = $?;
930 }
931
932 return $code;
933 }
934
935 # This runs a command without any debugging info.
936 sub _run_command
937 {
938 # We reset this before every invocation. On Windows I think there is only
939 # one environment, not one per process, so I think that variables set in
940 # test scripts might leak into subsequent tests if this isn't reset--???
941 resetENV();
942
943 my $orig = $SIG{ALRM};
944 my $code = eval { _run_with_timeout(@_); };
945 $SIG{ALRM} = $orig;
946
947 if ($@) {
948 # The eval failed. If it wasn't SIGALRM then die.
949 $@ eq "timeout\n" or die "Command failed: $@";
950 $code = 14;
951 }
952
953 return $code;
954 }
955
956 # run one command (passed as a list of arg 0 - n), returning 0 on success
957 # and nonzero on failure.
958
959 sub run_command
960 {
961 print "\nrun_command: @_\n" if $debug;
962 my $code = _run_command(@_);
963 print "run_command returned $code.\n" if $debug;
964 return $code;
965 }
966
967 # run one command (passed as a list of arg 0 - n, with arg 0 being the
968 # second arg to this routine), returning 0 on success and non-zero on failure.
969 # The first arg to this routine is a filename to connect to the stdout
970 # & stderr of the child process.
971
972 sub run_command_with_output
973 {
974 my $filename = shift;
975
976 print "\nrun_command_with_output($filename,$runname): @_\n" if $debug;
977 &attach_default_output ($filename);
978 my $code = eval { _run_command(@_) };
979 my $err = $@;
980 &detach_default_output;
981
982 $err and die $err;
983
984 print "run_command_with_output returned $code.\n" if $debug;
985 return $code;
986 }
987
988 # performs the equivalent of an "rm -rf" on the first argument. Like
989 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
990 # deletes it, too.
991
992 sub remove_directory_tree
993 {
994 my ($targetdir) = @_;
995 my ($nuketop) = 1;
996
997 my $ch = substr ($targetdir, length ($targetdir) - 1);
998 if ($ch eq "/" || $ch eq $pathsep) {
999 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
1000 $nuketop = 0;
1001 }
1002
1003 -e $targetdir or return 1;
1004
1005 &remove_directory_tree_inner ("RDT00", $targetdir) or return 0;
1006 if ($nuketop && !rmdir ($targetdir)) {
1007 print "Cannot remove $targetdir: $!\n";
1008 return 0;
1009 }
1010
1011 return 1;
1012 }
1013
1014 sub remove_directory_tree_inner
1015 {
1016 my ($dirhandle, $targetdir) = @_;
1017
1018 opendir ($dirhandle, $targetdir) or return 0;
1019 my $subdirhandle = $dirhandle;
1020 $subdirhandle++;
1021 while (my $object = readdir ($dirhandle)) {
1022 $object =~ /^(\.\.?|CVS|RCS)$/ and next;
1023 $object = "$targetdir$pathsep$object";
1024
1025 lstat ($object);
1026 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object)) {
1027 if (!rmdir($object)) {
1028 print "Cannot remove $object: $!\n";
1029 return 0;
1030 }
1031 } else {
1032 if ($^O ne 'VMS') {
1033 if (!unlink $object) {
1034 print "Cannot unlink $object: $!\n";
1035 return 0;
1036 }
1037 } else {
1038 # VMS can have multiple versions of a file.
1039 1 while unlink $object;
1040 }
1041 }
1042 }
1043 closedir ($dirhandle);
1044 return 1;
1045 }
1046
1047 # We used to use this behavior for this function:
1048 #
1049 #sub touch
1050 #{
1051 # my (@filenames) = @_;
1052 # my $now = time;
1053 #
1054 # foreach my $file (@filenames) {
1055 # utime ($now, $now, $file)
1056 # or (open (TOUCHFD, ">> $file") and close (TOUCHFD))
1057 # or &error ("Couldn't touch $file: $!\n", 1);
1058 # }
1059 # return 1;
1060 #}
1061 #
1062 # But this behaves badly on networked filesystems where the time is
1063 # skewed, because it sets the time of the file based on the _local_
1064 # host. Normally when you modify a file, it's the _remote_ host that
1065 # determines the modtime, based on _its_ clock. So, instead, now we open
1066 # the file and write something into it to force the remote host to set
1067 # the modtime correctly according to its clock.
1068 #
1069
1070 sub touch
1071 {
1072 foreach my $file (@_) {
1073 (open(T, '>>', $file) and print(T "\n") and close(T))
1074 or &error("Couldn't touch $file: $!\n", 1);
1075 }
1076
1077 return @_;
1078 }
1079
1080 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
1081 # access/mod time for each file and apply the offset.
1082
1083 sub utouch
1084 {
1085 my $off = shift;
1086
1087 &touch(@_);
1088
1089 foreach my $f (@_) {
1090 my @s = stat($f);
1091 utime($s[8]+$off, $s[9]+$off, $f);
1092 }
1093
1094 return @_;
1095 }
1096
1097 # open a file, write some stuff to it, and close it.
1098
1099 sub create_file
1100 {
1101 my ($filename, @lines) = @_;
1102
1103 open (CF, "> $filename") or &error ("Couldn't open $filename: $!\n", 1);
1104 foreach $line (@lines) {
1105 print CF $line;
1106 }
1107 close (CF);
1108 }
1109
1110 # create a directory tree described by an associative array, wherein each
1111 # key is a relative pathname (using slashes) and its associated value is
1112 # one of:
1113 # DIR indicates a directory
1114 # FILE:contents indicates a file, which should contain contents +\n
1115 # LINK:target indicates a symlink, pointing to $basedir/target
1116 # The first argument is the dir under which the structure will be created
1117 # (the dir will be made and/or cleaned if necessary); the second argument
1118 # is the associative array.
1119
1120 sub create_dir_tree
1121 {
1122 my ($basedir, %dirtree) = @_;
1123
1124 &remove_directory_tree ("$basedir");
1125 mkdir ($basedir, 0777) or &error ("Couldn't mkdir $basedir: $!\n", 1);
1126
1127 foreach my $path (sort keys (%dirtree)) {
1128 if ($dirtree {$path} =~ /^DIR$/) {
1129 mkdir ("$basedir/$path", 0777)
1130 or &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
1131
1132 } elsif ($dirtree {$path} =~ /^FILE:(.*)$/) {
1133 &create_file ("$basedir/$path", $1 . "\n");
1134
1135 } elsif ($dirtree {$path} =~ /^LINK:(.*)$/) {
1136 symlink ("$basedir/$1", "$basedir/$path")
1137 or &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
1138
1139 } else {
1140 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1141 }
1142 }
1143 if ($just_setup_tree) {
1144 die "Tree is setup...\n";
1145 }
1146 }
1147
1148 # compare a directory tree with an associative array in the format used
1149 # by create_dir_tree, above.
1150 # The first argument is the dir under which the structure should be found;
1151 # the second argument is the associative array.
1152
1153 sub compare_dir_tree
1154 {
1155 my ($basedir, %dirtree) = @_;
1156 my $bogus = 0;
1157
1158 opendir (DIR, $basedir) or &error ("Couldn't open $basedir: $!\n", 1);
1159 my @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
1160 closedir (DIR);
1161 if ($debug) {
1162 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
1163 }
1164
1165 foreach my $path (sort keys (%dirtree))
1166 {
1167 if ($debug) {
1168 print "Checking $path ($dirtree{$path}).\n";
1169 }
1170
1171 my $found = 0;
1172 foreach my $i (0 .. $#allfiles) {
1173 if ($allfiles[$i] eq $path) {
1174 splice (@allfiles, $i, 1); # delete it
1175 if ($debug) {
1176 print " Zapped $path; files now (@allfiles).\n";
1177 }
1178 lstat ("$basedir/$path");
1179 $found = 1;
1180 last;
1181 }
1182 }
1183
1184 if (!$found) {
1185 print "compare_dir_tree: $path does not exist.\n";
1186 $bogus = 1;
1187 next;
1188 }
1189
1190 if ($dirtree {$path} =~ /^DIR$/) {
1191 if (-d _ && opendir (DIR, "$basedir/$path") ) {
1192 my @files = readdir (DIR);
1193 closedir (DIR);
1194 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1195 push (@allfiles, @files);
1196 if ($debug)
1197 {
1198 print " Read in $path; new files (@files).\n";
1199 }
1200
1201 } else {
1202 print "compare_dir_tree: $path is not a dir.\n";
1203 $bogus = 1;
1204 }
1205
1206 } elsif ($dirtree {$path} =~ /^FILE:(.*)$/) {
1207 if (-l _ || !-f _) {
1208 print "compare_dir_tree: $path is not a file.\n";
1209 $bogus = 1;
1210 next;
1211 }
1212
1213 if ($1 ne "*") {
1214 my $contents = &read_file_into_string ("$basedir/$path");
1215 if ($contents ne "$1\n") {
1216 print "compare_dir_tree: $path contains wrong stuff."
1217 . " Is:\n$contentsShould be:\n$1\n";
1218 $bogus = 1;
1219 }
1220 }
1221
1222 } elsif ($dirtree {$path} =~ /^LINK:(.*)$/) {
1223 my $target = $1;
1224 if (!-l _) {
1225 print "compare_dir_tree: $path is not a link.\n";
1226 $bogus = 1;
1227 next;
1228 }
1229
1230 my $contents = readlink ("$basedir/$path");
1231 $contents =~ tr/>/\//;
1232 my $fulltarget = "$basedir/$target";
1233 $fulltarget =~ tr/>/\//;
1234 if (!($contents =~ /$fulltarget$/)) {
1235 if ($debug) {
1236 $target = $fulltarget;
1237 }
1238 print "compare_dir_tree: $path should be link to $target, "
1239 . "not $contents.\n";
1240 $bogus = 1;
1241 }
1242
1243 } else {
1244 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1245 }
1246 }
1247
1248 if ($debug) {
1249 print "leftovers: (@allfiles).\n";
1250 }
1251
1252 foreach my $file (@allfiles) {
1253 print "compare_dir_tree: $file should not exist.\n";
1254 $bogus = 1;
1255 }
1256
1257 return !$bogus;
1258 }
1259
1260 # this subroutine generates the numeric suffix used to keep tmp filenames,
1261 # log filenames, etc., unique. If the number passed in is 1, then a null
1262 # string is returned; otherwise, we return ".n", where n + 1 is the number
1263 # we were given.
1264
1265 sub num_suffix
1266 {
1267 my ($num) = @_;
1268 if (--$num > 0) {
1269 return "$extext$num";
1270 }
1271
1272 return "";
1273 }
1274
1275 # This subroutine returns a log filename with a number appended to
1276 # the end corresponding to how many logfiles have been created in the
1277 # current running test. An optional parameter may be passed (0 or 1).
1278 # If a 1 is passed, then it does NOT increment the logfile counter
1279 # and returns the name of the latest logfile. If either no parameter
1280 # is passed at all or a 0 is passed, then the logfile counter is
1281 # incremented and the new name is returned.
1282
1283 sub get_logfile
1284 {
1285 my ($no_increment) = @_;
1286
1287 $num_of_logfiles += !$no_increment;
1288
1289 return ($log_filename . &num_suffix ($num_of_logfiles));
1290 }
1291
1292 # This subroutine returns a base (answer) filename with a number
1293 # appended to the end corresponding to how many logfiles (and thus
1294 # base files) have been created in the current running test.
1295 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1296
1297 sub get_basefile
1298 {
1299 return ($base_filename . &num_suffix ($num_of_logfiles));
1300 }
1301
1302 # This subroutine returns a difference filename with a number appended
1303 # to the end corresponding to how many logfiles (and thus diff files)
1304 # have been created in the current running test.
1305
1306 sub get_difffile
1307 {
1308 return ($diff_filename . &num_suffix ($num_of_logfiles));
1309 }
1310
1311 # This subroutine returns a command filename with a number appended
1312 # to the end corresponding to how many logfiles (and thus command files)
1313 # have been created in the current running test.
1314
1315 sub get_runfile
1316 {
1317 return ($run_filename . &num_suffix ($num_of_logfiles));
1318 }
1319
1320 # just like logfile, only a generic tmp filename for use by the test.
1321 # they are automatically cleaned up unless -keep was used, or the test fails.
1322 # Pass an argument of 1 to return the same filename as the previous call.
1323
1324 sub get_tmpfile
1325 {
1326 my ($no_increment) = @_;
1327
1328 $num_of_tmpfiles += !$no_increment;
1329
1330 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));
1331 }
1332
1333 1;