Update copyright years.
[make.git] / tests / test_driver.pl
blobb61d87e30e8ff5cdf6af235e61627878b6d9cb72
1 #!/usr/bin/perl
2 # -*-perl-*-
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.
8 # Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
9 # 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software
10 # Foundation, Inc.
11 # This file is part of GNU Make.
13 # GNU Make is free software; you can redistribute it and/or modify it under
14 # the terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 3 of the License, or (at your option) any later
16 # version.
18 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 # details.
23 # You should have received a copy of the GNU General Public License along with
24 # this program. If not, see <http://www.gnu.org/licenses/>.
27 # Test driver routines used by a number of test suites, including
28 # those for SCS, make, roll_dir, and scan_deps (?).
30 # this routine controls the whole mess; each test suite sets up a few
31 # variables and then calls &toplevel, which does all the real work.
33 # $Id: test_driver.pl,v 1.27 2009/10/25 18:56:46 psmith Exp $
36 # The number of test categories we've run
37 $categories_run = 0;
38 # The number of test categroies that have passed
39 $categories_passed = 0;
40 # The total number of individual tests that have been run
41 $total_tests_run = 0;
42 # The total number of individual tests that have passed
43 $total_tests_passed = 0;
44 # The number of tests in this category that have been run
45 $tests_run = 0;
46 # The number of tests in this category that have passed
47 $tests_passed = 0;
50 # Yeesh. This whole test environment is such a hack!
51 $test_passed = 1;
54 # Timeout in seconds. If the test takes longer than this we'll fail it.
55 $test_timeout = 5;
58 # %makeENV is the cleaned-out environment.
59 %makeENV = ();
61 # %extraENV are any extra environment variables the tests might want to set.
62 # These are RESET AFTER EVERY TEST!
63 %extraENV = ();
65 # %origENV is the caller's original environment
66 %origENV = %ENV;
68 sub resetENV
70 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
71 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
72 # want to require that here, so just delete each one individually.
73 foreach $v (keys %ENV) {
74 delete $ENV{$v};
77 %ENV = %makeENV;
78 foreach $v (keys %extraENV) {
79 $ENV{$v} = $extraENV{$v};
80 delete $extraENV{$v};
84 sub toplevel
86 # Pull in benign variables from the user's environment
88 foreach (# UNIX-specific things
89 'TZ', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
90 # Purify things
91 'PURIFYOPTIONS',
92 # Windows NT-specific stuff
93 'Path', 'SystemRoot',
94 # DJGPP-specific stuff
95 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
96 'FNCASE', '387', 'EMU387', 'GROUP'
97 ) {
98 $makeENV{$_} = $ENV{$_} if $ENV{$_};
101 # Make sure our compares are not foiled by locale differences
103 $makeENV{LC_ALL} = 'C';
105 # Replace the environment with the new one
107 %origENV = %ENV;
109 resetENV();
111 $| = 1; # unbuffered output
113 $debug = 0; # debug flag
114 $profile = 0; # profiling flag
115 $verbose = 0; # verbose mode flag
116 $detail = 0; # detailed verbosity
117 $keep = 0; # keep temp files around
118 $workdir = "work"; # The directory where the test will start running
119 $scriptdir = "scripts"; # The directory where we find the test scripts
120 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
121 $default_output_stack_level = 0; # used by attach_default_output, etc.
122 $default_input_stack_level = 0; # used by attach_default_input, etc.
123 $cwd = "."; # don't we wish we knew
124 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
126 &get_osname; # sets $osname, $vos, $pathsep, and $short_filenames
128 &set_defaults; # suite-defined
130 &parse_command_line (@ARGV);
132 print "OS name = `$osname'\n" if $debug;
134 $workpath = "$cwdslash$workdir";
135 $scriptpath = "$cwdslash$scriptdir";
137 &set_more_defaults; # suite-defined
139 &print_banner;
141 if (-d $workpath)
143 print "Clearing $workpath...\n";
144 &remove_directory_tree("$workpath/")
145 || &error ("Couldn't wipe out $workpath\n");
147 else
149 mkdir ($workpath, 0777) || &error ("Couldn't mkdir $workpath: $!\n");
152 if (!-d $scriptpath)
154 &error ("Failed to find $scriptpath containing perl test scripts.\n");
157 if (@TESTS)
159 print "Making work dirs...\n";
160 foreach $test (@TESTS)
162 if ($test =~ /^([^\/]+)\//)
164 $dir = $1;
165 push (@rmdirs, $dir);
166 -d "$workpath/$dir"
167 || mkdir ("$workpath/$dir", 0777)
168 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
172 else
174 print "Finding tests...\n";
175 opendir (SCRIPTDIR, $scriptpath)
176 || &error ("Couldn't opendir $scriptpath: $!\n");
177 @dirs = grep (!/^(\..*|CVS|RCS)$/, readdir (SCRIPTDIR) );
178 closedir (SCRIPTDIR);
179 foreach $dir (@dirs)
181 next if ($dir =~ /^(\..*|CVS|RCS)$/ || ! -d "$scriptpath/$dir");
182 push (@rmdirs, $dir);
183 mkdir ("$workpath/$dir", 0777)
184 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
185 opendir (SCRIPTDIR, "$scriptpath/$dir")
186 || &error ("Couldn't opendir $scriptpath/$dir: $!\n");
187 @files = grep (!/^(\..*|CVS|RCS|.*~)$/, readdir (SCRIPTDIR) );
188 closedir (SCRIPTDIR);
189 foreach $test (@files)
191 -d $test and next;
192 push (@TESTS, "$dir/$test");
197 if (@TESTS == 0)
199 &error ("\nNo tests in $scriptpath, and none were specified.\n");
202 print "\n";
204 &run_each_test;
206 foreach $dir (@rmdirs)
208 rmdir ("$workpath/$dir");
211 $| = 1;
213 $categories_failed = $categories_run - $categories_passed;
214 $total_tests_failed = $total_tests_run - $total_tests_passed;
216 if ($total_tests_failed)
218 print "\n$total_tests_failed Test";
219 print "s" unless $total_tests_failed == 1;
220 print " in $categories_failed Categor";
221 print ($categories_failed == 1 ? "y" : "ies");
222 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
223 return 0;
225 else
227 print "\n$total_tests_passed Test";
228 print "s" unless $total_tests_passed == 1;
229 print " in $categories_passed Categor";
230 print ($categories_passed == 1 ? "y" : "ies");
231 print " Complete ... No Failures :-)\n\n";
232 return 1;
236 sub get_osname
238 # Set up an initial value. In perl5 we can do it the easy way.
240 $osname = defined($^O) ? $^O : '';
242 # See if the filesystem supports long file names with multiple
243 # dots. DOS doesn't.
244 $short_filenames = 0;
245 (open (TOUCHFD, "> fancy.file.name") && close (TOUCHFD))
246 || ($short_filenames = 1);
247 unlink ("fancy.file.name") || ($short_filenames = 1);
249 if (! $short_filenames) {
250 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
251 # better way of doing this. (We used to test for existence of a /mnt
252 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
253 # Because perl on VOS translates /'s to >'s, we need to test for
254 # VOSness rather than testing for Unixness (ie, try > instead of /).
256 mkdir (".ostest", 0777) || &error ("Couldn't create .ostest: $!\n", 1);
257 open (TOUCHFD, "> .ostest>ick") && close (TOUCHFD);
258 chdir (".ostest") || &error ("Couldn't chdir to .ostest: $!\n", 1);
261 if (! $short_filenames && -f "ick")
263 $osname = "vos";
264 $vos = 1;
265 $pathsep = ">";
267 else
269 # the following is regrettably knarly, but it seems to be the only way
270 # to not get ugly error messages if uname can't be found.
271 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
272 # with switches first.
273 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
274 if ($osname =~ /not found/i)
276 $osname = "(something unixy with no uname)";
278 elsif ($@ ne "" || $?)
280 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
281 if ($@ ne "" || $?)
283 $osname = "(something unixy)";
286 $vos = 0;
287 $pathsep = "/";
290 if (! $short_filenames) {
291 chdir ("..") || &error ("Couldn't chdir to ..: $!\n", 1);
292 unlink (".ostest>ick");
293 rmdir (".ostest") || &error ("Couldn't rmdir .ostest: $!\n", 1);
297 sub parse_command_line
299 @argv = @_;
301 # use @ARGV if no args were passed in
303 if (@argv == 0)
305 @argv = @ARGV;
308 # look at each option; if we don't recognize it, maybe the suite-specific
309 # command line parsing code will...
311 while (@argv)
313 $option = shift @argv;
314 if ($option =~ /^-debug$/i)
316 print "\nDEBUG ON\n";
317 $debug = 1;
319 elsif ($option =~ /^-usage$/i)
321 &print_usage;
322 exit 0;
324 elsif ($option =~ /^-(h|help)$/i)
326 &print_help;
327 exit 0;
329 elsif ($option =~ /^-profile$/i)
331 $profile = 1;
333 elsif ($option =~ /^-verbose$/i)
335 $verbose = 1;
337 elsif ($option =~ /^-detail$/i)
339 $detail = 1;
340 $verbose = 1;
342 elsif ($option =~ /^-keep$/i)
344 $keep = 1;
346 elsif (&valid_option($option))
348 # The suite-defined subroutine takes care of the option
350 elsif ($option =~ /^-/)
352 print "Invalid option: $option\n";
353 &print_usage;
354 exit 0;
356 else # must be the name of a test
358 $option =~ s/\.pl$//;
359 push(@TESTS,$option);
364 sub max
366 local($num) = shift @_;
367 local($newnum);
369 while (@_)
371 $newnum = shift @_;
372 if ($newnum > $num)
374 $num = $newnum;
378 return $num;
381 sub print_centered
383 local($width, $string) = @_;
384 local($pad);
386 if (length ($string))
388 $pad = " " x ( ($width - length ($string) + 1) / 2);
389 print "$pad$string";
393 sub print_banner
395 local($info);
396 local($line);
397 local($len);
399 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
400 $len = &max (length ($line), length ($testee_version),
401 length ($banner_info), 73) + 5;
402 $line = ("-" x $len) . "\n";
403 if ($len < 78)
405 $len = 78;
408 &print_centered ($len, $line);
409 &print_centered ($len, $info);
410 &print_centered ($len, $testee_version); # suite-defined
411 &print_centered ($len, $banner_info); # suite-defined
412 &print_centered ($len, $line);
413 print "\n";
416 sub run_each_test
418 $categories_run = 0;
420 foreach $testname (sort @TESTS)
422 ++$categories_run;
423 $suite_passed = 1; # reset by test on failure
424 $num_of_logfiles = 0;
425 $num_of_tmpfiles = 0;
426 $description = "";
427 $details = "";
428 $old_makefile = undef;
429 $testname =~ s/^$scriptpath$pathsep//;
430 $perl_testname = "$scriptpath$pathsep$testname";
431 $testname =~ s/(\.pl|\.perl)$//;
432 $testpath = "$workpath$pathsep$testname";
433 # Leave enough space in the extensions to append a number, even
434 # though it needs to fit into 8+3 limits.
435 if ($short_filenames) {
436 $logext = 'l';
437 $diffext = 'd';
438 $baseext = 'b';
439 $runext = 'r';
440 $extext = '';
441 } else {
442 $logext = 'log';
443 $diffext = 'diff';
444 $baseext = 'base';
445 $runext = 'run';
446 $extext = '.';
448 $log_filename = "$testpath.$logext";
449 $diff_filename = "$testpath.$diffext";
450 $base_filename = "$testpath.$baseext";
451 $run_filename = "$testpath.$runext";
452 $tmp_filename = "$testpath.$tmpfilesuffix";
454 &setup_for_test; # suite-defined
456 $output = "........................................................ ";
458 substr($output,0,length($testname)) = "$testname ";
460 print $output;
462 # Run the actual test!
463 $tests_run = 0;
464 $tests_passed = 0;
466 $code = do $perl_testname;
468 $total_tests_run += $tests_run;
469 $total_tests_passed += $tests_passed;
471 # How did it go?
472 if (!defined($code))
474 $suite_passed = 0;
475 if (length ($@)) {
476 warn "\n*** Test died ($testname): $@\n";
477 } else {
478 warn "\n*** Couldn't run $perl_testname\n";
481 elsif ($code == -1) {
482 $suite_passed = 0;
484 elsif ($code != 1 && $code != -1) {
485 $suite_passed = 0;
486 warn "\n*** Test returned $code\n";
489 if ($suite_passed) {
490 ++$categories_passed;
491 $status = "ok ($tests_passed passed)";
492 for ($i = $num_of_tmpfiles; $i; $i--)
494 &rmfiles ($tmp_filename . &num_suffix ($i) );
497 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--)
499 &rmfiles ($log_filename . &num_suffix ($i) );
500 &rmfiles ($base_filename . &num_suffix ($i) );
503 elsif (!defined $code || $code > 0) {
504 $status = "FAILED ($tests_passed/$tests_run passed)";
506 elsif ($code < 0) {
507 $status = "N/A";
508 --$categories_run;
511 # If the verbose option has been specified, then a short description
512 # of each test is printed before displaying the results of each test
513 # describing WHAT is being tested.
515 if ($verbose)
517 if ($detail)
519 print "\nWHAT IS BEING TESTED\n";
520 print "--------------------";
522 print "\n\n$description\n\n";
525 # If the detail option has been specified, then the details of HOW
526 # the test is testing what it says it is testing in the verbose output
527 # will be displayed here before the results of the test are displayed.
529 if ($detail)
531 print "\nHOW IT IS TESTED\n";
532 print "----------------";
533 print "\n\n$details\n\n";
536 print "$status\n";
540 # If the keep flag is not set, this subroutine deletes all filenames that
541 # are sent to it.
543 sub rmfiles
545 local(@files) = @_;
547 if (!$keep)
549 return (unlink @files);
552 return 1;
555 sub print_standard_usage
557 local($plname,@moreusage) = @_;
558 local($line);
560 print "usage:\t$plname [testname] [-verbose] [-detail] [-keep]\n";
561 print "\t\t\t[-profile] [-usage] [-help] [-debug]\n";
562 foreach (@moreusage) {
563 print "\t\t\t$_\n";
567 sub print_standard_help
569 local(@morehelp) = @_;
570 local($line);
571 local($tline);
572 local($t) = " ";
574 $line = "Test Driver For $testee";
575 print "$line\n";
576 $line = "=" x length ($line);
577 print "$line\n";
579 &print_usage;
581 print "\ntestname\n"
582 . "${t}You may, if you wish, run only ONE test if you know the name\n"
583 . "${t}of that test and specify this name anywhere on the command\n"
584 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
585 . "${t}will be run.\n"
586 . "-verbose\n"
587 . "${t}If this option is given, a description of every test is\n"
588 . "${t}displayed before the test is run. (Not all tests may have\n"
589 . "${t}descriptions at this time)\n"
590 . "-detail\n"
591 . "${t}If this option is given, a detailed description of every\n"
592 . "${t}test is displayed before the test is run. (Not all tests\n"
593 . "${t}have descriptions at this time)\n"
594 . "-profile\n"
595 . "${t}If this option is given, then the profile file\n"
596 . "${t}is added to other profiles every time $testee is run.\n"
597 . "${t}This option only works on VOS at this time.\n"
598 . "-keep\n"
599 . "${t}You may give this option if you DO NOT want ANY\n"
600 . "${t}of the files generated by the tests to be deleted. \n"
601 . "${t}Without this option, all files generated by the test will\n"
602 . "${t}be deleted IF THE TEST PASSES.\n"
603 . "-debug\n"
604 . "${t}Use this option if you would like to see all of the system\n"
605 . "${t}calls issued and their return status while running the tests\n"
606 . "${t}This can be helpful if you're having a problem adding a test\n"
607 . "${t}to the suite, or if the test fails!\n";
609 foreach $line (@morehelp)
611 $tline = $line;
612 if (substr ($tline, 0, 1) eq "\t")
614 substr ($tline, 0, 1) = $t;
616 print "$tline\n";
620 #######################################################################
621 ########### Generic Test Driver Subroutines ###########
622 #######################################################################
624 sub get_caller
626 local($depth);
627 local($package);
628 local($filename);
629 local($linenum);
631 $depth = defined ($_[0]) ? $_[0] : 1;
632 ($package, $filename, $linenum) = caller ($depth + 1);
633 return "$filename: $linenum";
636 sub error
638 local($message) = $_[0];
639 local($caller) = &get_caller (1);
641 if (defined ($_[1]))
643 $caller = &get_caller ($_[1] + 1) . " -> $caller";
646 die "$caller: $message";
649 sub compare_output
651 local($answer,$logfile) = @_;
652 local($slurp, $answer_matched) = ('', 0);
654 print "Comparing Output ........ " if $debug;
656 $slurp = &read_file_into_string ($logfile);
658 # For make, get rid of any time skew error before comparing--too bad this
659 # has to go into the "generic" driver code :-/
660 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
661 $slurp =~ s/^.*Clock skew detected.*\n//gm;
663 ++$tests_run;
665 if ($slurp eq $answer) {
666 $answer_matched = 1;
667 } else {
668 # See if it is a slash or CRLF problem
669 local ($answer_mod, $slurp_mod) = ($answer, $slurp);
671 $answer_mod =~ tr,\\,/,;
672 $answer_mod =~ s,\r\n,\n,gs;
674 $slurp_mod =~ tr,\\,/,;
675 $slurp_mod =~ s,\r\n,\n,gs;
677 $answer_matched = ($slurp_mod eq $answer_mod);
679 # If it still doesn't match, see if the answer might be a regex.
680 if (!$answer_matched && $answer =~ m,^/(.+)/$,) {
681 $answer_matched = ($slurp =~ /$1/);
682 if (!$answer_matched && $answer_mod =~ m,^/(.+)/$,) {
683 $answer_matched = ($slurp_mod =~ /$1/);
688 if ($answer_matched && $test_passed)
690 print "ok\n" if $debug;
691 ++$tests_passed;
692 return 1;
695 if (! $answer_matched) {
696 print "DIFFERENT OUTPUT\n" if $debug;
698 &create_file (&get_basefile, $answer);
699 &create_file (&get_runfile, $command_string);
701 print "\nCreating Difference File ...\n" if $debug;
703 # Create the difference file
705 local($command) = "diff -c " . &get_basefile . " " . $logfile;
706 &run_command_with_output(&get_difffile,$command);
707 } else {
708 &rmfiles ();
711 $suite_passed = 0;
712 return 0;
715 sub read_file_into_string
717 local($filename) = @_;
718 local($oldslash) = $/;
720 undef $/;
722 open (RFISFILE, $filename) || return "";
723 local ($slurp) = <RFISFILE>;
724 close (RFISFILE);
726 $/ = $oldslash;
728 return $slurp;
731 sub attach_default_output
733 local ($filename) = @_;
734 local ($code);
736 if ($vos)
738 $code = system "++attach_default_output_hack $filename";
739 $code == -2 || &error ("adoh death\n", 1);
740 return 1;
743 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
744 || &error ("ado: $! duping STDOUT\n", 1);
745 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
746 || &error ("ado: $! duping STDERR\n", 1);
748 open (STDOUT, "> " . $filename)
749 || &error ("ado: $filename: $!\n", 1);
750 open (STDERR, ">&STDOUT")
751 || &error ("ado: $filename: $!\n", 1);
753 $default_output_stack_level++;
756 # close the current stdout/stderr, and restore the previous ones from
757 # the "stack."
759 sub detach_default_output
761 local ($code);
763 if ($vos)
765 $code = system "++detach_default_output_hack";
766 $code == -2 || &error ("ddoh death\n", 1);
767 return 1;
770 if (--$default_output_stack_level < 0)
772 &error ("default output stack has flown under!\n", 1);
775 close (STDOUT);
776 close (STDERR);
778 open (STDOUT, ">&SAVEDOS" . $default_output_stack_level . "out")
779 || &error ("ddo: $! duping STDOUT\n", 1);
780 open (STDERR, ">&SAVEDOS" . $default_output_stack_level . "err")
781 || &error ("ddo: $! duping STDERR\n", 1);
783 close ("SAVEDOS" . $default_output_stack_level . "out")
784 || &error ("ddo: $! closing SCSDOSout\n", 1);
785 close ("SAVEDOS" . $default_output_stack_level . "err")
786 || &error ("ddo: $! closing SAVEDOSerr\n", 1);
789 # This runs a command without any debugging info.
790 sub _run_command
792 my $code;
794 # We reset this before every invocation. On Windows I think there is only
795 # one environment, not one per process, so I think that variables set in
796 # test scripts might leak into subsequent tests if this isn't reset--???
797 resetENV();
799 eval {
800 local $SIG{ALRM} = sub { die "timeout\n"; };
801 alarm $test_timeout;
802 $code = system @_;
803 alarm 0;
805 if ($@) {
806 # The eval failed. If it wasn't SIGALRM then die.
807 $@ eq "timeout\n" or die;
809 # Timed out. Resend the alarm to our process group to kill the children.
810 $SIG{ALRM} = 'IGNORE';
811 kill -14, $$;
812 $code = 14;
815 return $code;
818 # run one command (passed as a list of arg 0 - n), returning 0 on success
819 # and nonzero on failure.
821 sub run_command
823 print "\nrun_command: @_\n" if $debug;
824 my $code = _run_command(@_);
825 print "run_command returned $code.\n" if $debug;
827 return $code;
830 # run one command (passed as a list of arg 0 - n, with arg 0 being the
831 # second arg to this routine), returning 0 on success and non-zero on failure.
832 # The first arg to this routine is a filename to connect to the stdout
833 # & stderr of the child process.
835 sub run_command_with_output
837 my $filename = shift;
839 print "\nrun_command_with_output($filename,$runname): @_\n" if $debug;
840 &attach_default_output ($filename);
841 my $code = _run_command(@_);
842 &detach_default_output;
843 print "run_command_with_output returned $code.\n" if $debug;
845 return $code;
848 # performs the equivalent of an "rm -rf" on the first argument. Like
849 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
850 # deletes it, too.
852 sub remove_directory_tree
854 local ($targetdir) = @_;
855 local ($nuketop) = 1;
856 local ($ch);
858 $ch = substr ($targetdir, length ($targetdir) - 1);
859 if ($ch eq "/" || $ch eq $pathsep)
861 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
862 $nuketop = 0;
865 if (! -e $targetdir)
867 return 1;
870 &remove_directory_tree_inner ("RDT00", $targetdir) || return 0;
871 if ($nuketop)
873 rmdir $targetdir || return 0;
876 return 1;
879 sub remove_directory_tree_inner
881 local ($dirhandle, $targetdir) = @_;
882 local ($object);
883 local ($subdirhandle);
885 opendir ($dirhandle, $targetdir) || return 0;
886 $subdirhandle = $dirhandle;
887 $subdirhandle++;
888 while ($object = readdir ($dirhandle))
890 if ($object =~ /^(\.\.?|CVS|RCS)$/)
892 next;
895 $object = "$targetdir$pathsep$object";
896 lstat ($object);
898 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object))
900 rmdir $object || return 0;
902 else
904 unlink $object || return 0;
907 closedir ($dirhandle);
908 return 1;
911 # We used to use this behavior for this function:
913 #sub touch
915 # local (@filenames) = @_;
916 # local ($now) = time;
917 # local ($file);
919 # foreach $file (@filenames)
921 # utime ($now, $now, $file)
922 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
923 # || &error ("Couldn't touch $file: $!\n", 1);
925 # return 1;
928 # But this behaves badly on networked filesystems where the time is
929 # skewed, because it sets the time of the file based on the _local_
930 # host. Normally when you modify a file, it's the _remote_ host that
931 # determines the modtime, based on _its_ clock. So, instead, now we open
932 # the file and write something into it to force the remote host to set
933 # the modtime correctly according to its clock.
936 sub touch
938 local ($file);
940 foreach $file (@_) {
941 (open(T, ">> $file") && print(T "\n") && close(T))
942 || &error("Couldn't touch $file: $!\n", 1);
946 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
947 # access/mod time for each file and apply the offset.
949 sub utouch
951 local ($off) = shift;
952 local ($file);
954 &touch(@_);
956 local (@s) = stat($_[0]);
958 utime($s[8]+$off, $s[9]+$off, @_);
961 # open a file, write some stuff to it, and close it.
963 sub create_file
965 local ($filename, @lines) = @_;
967 open (CF, "> $filename") || &error ("Couldn't open $filename: $!\n", 1);
968 foreach $line (@lines)
970 print CF $line;
972 close (CF);
975 # create a directory tree described by an associative array, wherein each
976 # key is a relative pathname (using slashes) and its associated value is
977 # one of:
978 # DIR indicates a directory
979 # FILE:contents indicates a file, which should contain contents +\n
980 # LINK:target indicates a symlink, pointing to $basedir/target
981 # The first argument is the dir under which the structure will be created
982 # (the dir will be made and/or cleaned if necessary); the second argument
983 # is the associative array.
985 sub create_dir_tree
987 local ($basedir, %dirtree) = @_;
988 local ($path);
990 &remove_directory_tree ("$basedir");
991 mkdir ($basedir, 0777) || &error ("Couldn't mkdir $basedir: $!\n", 1);
993 foreach $path (sort keys (%dirtree))
995 if ($dirtree {$path} =~ /^DIR$/)
997 mkdir ("$basedir/$path", 0777)
998 || &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
1000 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1002 &create_file ("$basedir/$path", $1 . "\n");
1004 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1006 symlink ("$basedir/$1", "$basedir/$path")
1007 || &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
1009 else
1011 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1014 if ($just_setup_tree)
1016 die "Tree is setup...\n";
1020 # compare a directory tree with an associative array in the format used
1021 # by create_dir_tree, above.
1022 # The first argument is the dir under which the structure should be found;
1023 # the second argument is the associative array.
1025 sub compare_dir_tree
1027 local ($basedir, %dirtree) = @_;
1028 local ($path);
1029 local ($i);
1030 local ($bogus) = 0;
1031 local ($contents);
1032 local ($target);
1033 local ($fulltarget);
1034 local ($found);
1035 local (@files);
1036 local (@allfiles);
1038 opendir (DIR, $basedir) || &error ("Couldn't open $basedir: $!\n", 1);
1039 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
1040 closedir (DIR);
1041 if ($debug)
1043 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
1046 foreach $path (sort keys (%dirtree))
1048 if ($debug)
1050 print "Checking $path ($dirtree{$path}).\n";
1053 $found = 0;
1054 foreach $i (0 .. $#allfiles)
1056 if ($allfiles[$i] eq $path)
1058 splice (@allfiles, $i, 1); # delete it
1059 if ($debug)
1061 print " Zapped $path; files now (@allfiles).\n";
1063 lstat ("$basedir/$path");
1064 $found = 1;
1065 last;
1069 if (!$found)
1071 print "compare_dir_tree: $path does not exist.\n";
1072 $bogus = 1;
1073 next;
1076 if ($dirtree {$path} =~ /^DIR$/)
1078 if (-d _ && opendir (DIR, "$basedir/$path") )
1080 @files = readdir (DIR);
1081 closedir (DIR);
1082 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1083 push (@allfiles, @files);
1084 if ($debug)
1086 print " Read in $path; new files (@files).\n";
1089 else
1091 print "compare_dir_tree: $path is not a dir.\n";
1092 $bogus = 1;
1095 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1097 if (-l _ || !-f _)
1099 print "compare_dir_tree: $path is not a file.\n";
1100 $bogus = 1;
1101 next;
1104 if ($1 ne "*")
1106 $contents = &read_file_into_string ("$basedir/$path");
1107 if ($contents ne "$1\n")
1109 print "compare_dir_tree: $path contains wrong stuff."
1110 . " Is:\n$contentsShould be:\n$1\n";
1111 $bogus = 1;
1115 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1117 $target = $1;
1118 if (!-l _)
1120 print "compare_dir_tree: $path is not a link.\n";
1121 $bogus = 1;
1122 next;
1125 $contents = readlink ("$basedir/$path");
1126 $contents =~ tr/>/\//;
1127 $fulltarget = "$basedir/$target";
1128 $fulltarget =~ tr/>/\//;
1129 if (!($contents =~ /$fulltarget$/))
1131 if ($debug)
1133 $target = $fulltarget;
1135 print "compare_dir_tree: $path should be link to $target, "
1136 . "not $contents.\n";
1137 $bogus = 1;
1140 else
1142 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1146 if ($debug)
1148 print "leftovers: (@allfiles).\n";
1151 foreach $file (@allfiles)
1153 print "compare_dir_tree: $file should not exist.\n";
1154 $bogus = 1;
1157 return !$bogus;
1160 # this subroutine generates the numeric suffix used to keep tmp filenames,
1161 # log filenames, etc., unique. If the number passed in is 1, then a null
1162 # string is returned; otherwise, we return ".n", where n + 1 is the number
1163 # we were given.
1165 sub num_suffix
1167 local($num) = @_;
1169 if (--$num > 0) {
1170 return "$extext$num";
1173 return "";
1176 # This subroutine returns a log filename with a number appended to
1177 # the end corresponding to how many logfiles have been created in the
1178 # current running test. An optional parameter may be passed (0 or 1).
1179 # If a 1 is passed, then it does NOT increment the logfile counter
1180 # and returns the name of the latest logfile. If either no parameter
1181 # is passed at all or a 0 is passed, then the logfile counter is
1182 # incremented and the new name is returned.
1184 sub get_logfile
1186 local($no_increment) = @_;
1188 $num_of_logfiles += !$no_increment;
1190 return ($log_filename . &num_suffix ($num_of_logfiles));
1193 # This subroutine returns a base (answer) filename with a number
1194 # appended to the end corresponding to how many logfiles (and thus
1195 # base files) have been created in the current running test.
1196 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1198 sub get_basefile
1200 return ($base_filename . &num_suffix ($num_of_logfiles));
1203 # This subroutine returns a difference filename with a number appended
1204 # to the end corresponding to how many logfiles (and thus diff files)
1205 # have been created in the current running test.
1207 sub get_difffile
1209 return ($diff_filename . &num_suffix ($num_of_logfiles));
1212 # This subroutine returns a command filename with a number appended
1213 # to the end corresponding to how many logfiles (and thus command files)
1214 # have been created in the current running test.
1216 sub get_runfile
1218 return ($run_filename . &num_suffix ($num_of_logfiles));
1221 # just like logfile, only a generic tmp filename for use by the test.
1222 # they are automatically cleaned up unless -keep was used, or the test fails.
1223 # Pass an argument of 1 to return the same filename as the previous call.
1225 sub get_tmpfile
1227 local($no_increment) = @_;
1229 $num_of_tmpfiles += !$no_increment;
1231 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));