- Update manual description for pattern rule search algorithm
[make.git] / tests / test_driver.pl
blob0fb9669c8ab33953adcdc635de85975f6269a453
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 Free Software Foundation, Inc.
10 # This file is part of GNU Make.
12 # GNU Make is free software; you can redistribute it and/or modify it under
13 # the terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 3 of the License, or (at your option) any later
15 # version.
17 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 # details.
22 # You should have received a copy of the GNU General Public License along with
23 # this program. If not, see <http://www.gnu.org/licenses/>.
26 # Test driver routines used by a number of test suites, including
27 # those for SCS, make, roll_dir, and scan_deps (?).
29 # this routine controls the whole mess; each test suite sets up a few
30 # variables and then calls &toplevel, which does all the real work.
32 # $Id: test_driver.pl,v 1.26 2009/09/28 23:08:50 psmith Exp $
35 # The number of test categories we've run
36 $categories_run = 0;
37 # The number of test categroies that have passed
38 $categories_passed = 0;
39 # The total number of individual tests that have been run
40 $total_tests_run = 0;
41 # The total number of individual tests that have passed
42 $total_tests_passed = 0;
43 # The number of tests in this category that have been run
44 $tests_run = 0;
45 # The number of tests in this category that have passed
46 $tests_passed = 0;
49 # Yeesh. This whole test environment is such a hack!
50 $test_passed = 1;
53 # Timeout in seconds. If the test takes longer than this we'll fail it.
54 $test_timeout = 5;
57 # %makeENV is the cleaned-out environment.
58 %makeENV = ();
60 # %extraENV are any extra environment variables the tests might want to set.
61 # These are RESET AFTER EVERY TEST!
62 %extraENV = ();
64 # %origENV is the caller's original environment
65 %origENV = %ENV;
67 sub resetENV
69 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
70 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
71 # want to require that here, so just delete each one individually.
72 foreach $v (keys %ENV) {
73 delete $ENV{$v};
76 %ENV = %makeENV;
77 foreach $v (keys %extraENV) {
78 $ENV{$v} = $extraENV{$v};
79 delete $extraENV{$v};
83 sub toplevel
85 # Pull in benign variables from the user's environment
87 foreach (# UNIX-specific things
88 'TZ', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
89 # Purify things
90 'PURIFYOPTIONS',
91 # Windows NT-specific stuff
92 'Path', 'SystemRoot',
93 # DJGPP-specific stuff
94 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
95 'FNCASE', '387', 'EMU387', 'GROUP'
96 ) {
97 $makeENV{$_} = $ENV{$_} if $ENV{$_};
100 # Make sure our compares are not foiled by locale differences
102 $makeENV{LC_ALL} = 'C';
104 # Replace the environment with the new one
106 %origENV = %ENV;
108 resetENV();
110 $| = 1; # unbuffered output
112 $debug = 0; # debug flag
113 $profile = 0; # profiling flag
114 $verbose = 0; # verbose mode flag
115 $detail = 0; # detailed verbosity
116 $keep = 0; # keep temp files around
117 $workdir = "work"; # The directory where the test will start running
118 $scriptdir = "scripts"; # The directory where we find the test scripts
119 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
120 $default_output_stack_level = 0; # used by attach_default_output, etc.
121 $default_input_stack_level = 0; # used by attach_default_input, etc.
122 $cwd = "."; # don't we wish we knew
123 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
125 &get_osname; # sets $osname, $vos, $pathsep, and $short_filenames
127 &set_defaults; # suite-defined
129 &parse_command_line (@ARGV);
131 print "OS name = `$osname'\n" if $debug;
133 $workpath = "$cwdslash$workdir";
134 $scriptpath = "$cwdslash$scriptdir";
136 &set_more_defaults; # suite-defined
138 &print_banner;
140 if (-d $workpath)
142 print "Clearing $workpath...\n";
143 &remove_directory_tree("$workpath/")
144 || &error ("Couldn't wipe out $workpath\n");
146 else
148 mkdir ($workpath, 0777) || &error ("Couldn't mkdir $workpath: $!\n");
151 if (!-d $scriptpath)
153 &error ("Failed to find $scriptpath containing perl test scripts.\n");
156 if (@TESTS)
158 print "Making work dirs...\n";
159 foreach $test (@TESTS)
161 if ($test =~ /^([^\/]+)\//)
163 $dir = $1;
164 push (@rmdirs, $dir);
165 -d "$workpath/$dir"
166 || mkdir ("$workpath/$dir", 0777)
167 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
171 else
173 print "Finding tests...\n";
174 opendir (SCRIPTDIR, $scriptpath)
175 || &error ("Couldn't opendir $scriptpath: $!\n");
176 @dirs = grep (!/^(\..*|CVS|RCS)$/, readdir (SCRIPTDIR) );
177 closedir (SCRIPTDIR);
178 foreach $dir (@dirs)
180 next if ($dir =~ /^(\..*|CVS|RCS)$/ || ! -d "$scriptpath/$dir");
181 push (@rmdirs, $dir);
182 mkdir ("$workpath/$dir", 0777)
183 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
184 opendir (SCRIPTDIR, "$scriptpath/$dir")
185 || &error ("Couldn't opendir $scriptpath/$dir: $!\n");
186 @files = grep (!/^(\..*|CVS|RCS|.*~)$/, readdir (SCRIPTDIR) );
187 closedir (SCRIPTDIR);
188 foreach $test (@files)
190 -d $test and next;
191 push (@TESTS, "$dir/$test");
196 if (@TESTS == 0)
198 &error ("\nNo tests in $scriptpath, and none were specified.\n");
201 print "\n";
203 &run_each_test;
205 foreach $dir (@rmdirs)
207 rmdir ("$workpath/$dir");
210 $| = 1;
212 $categories_failed = $categories_run - $categories_passed;
213 $total_tests_failed = $total_tests_run - $total_tests_passed;
215 if ($total_tests_failed)
217 print "\n$total_tests_failed Test";
218 print "s" unless $total_tests_failed == 1;
219 print " in $categories_failed Categor";
220 print ($categories_failed == 1 ? "y" : "ies");
221 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
222 return 0;
224 else
226 print "\n$total_tests_passed Test";
227 print "s" unless $total_tests_passed == 1;
228 print " in $categories_passed Categor";
229 print ($categories_passed == 1 ? "y" : "ies");
230 print " Complete ... No Failures :-)\n\n";
231 return 1;
235 sub get_osname
237 # Set up an initial value. In perl5 we can do it the easy way.
239 $osname = defined($^O) ? $^O : '';
241 # See if the filesystem supports long file names with multiple
242 # dots. DOS doesn't.
243 $short_filenames = 0;
244 (open (TOUCHFD, "> fancy.file.name") && close (TOUCHFD))
245 || ($short_filenames = 1);
246 unlink ("fancy.file.name") || ($short_filenames = 1);
248 if (! $short_filenames) {
249 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
250 # better way of doing this. (We used to test for existence of a /mnt
251 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
252 # Because perl on VOS translates /'s to >'s, we need to test for
253 # VOSness rather than testing for Unixness (ie, try > instead of /).
255 mkdir (".ostest", 0777) || &error ("Couldn't create .ostest: $!\n", 1);
256 open (TOUCHFD, "> .ostest>ick") && close (TOUCHFD);
257 chdir (".ostest") || &error ("Couldn't chdir to .ostest: $!\n", 1);
260 if (! $short_filenames && -f "ick")
262 $osname = "vos";
263 $vos = 1;
264 $pathsep = ">";
266 else
268 # the following is regrettably knarly, but it seems to be the only way
269 # to not get ugly error messages if uname can't be found.
270 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
271 # with switches first.
272 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
273 if ($osname =~ /not found/i)
275 $osname = "(something unixy with no uname)";
277 elsif ($@ ne "" || $?)
279 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
280 if ($@ ne "" || $?)
282 $osname = "(something unixy)";
285 $vos = 0;
286 $pathsep = "/";
289 if (! $short_filenames) {
290 chdir ("..") || &error ("Couldn't chdir to ..: $!\n", 1);
291 unlink (".ostest>ick");
292 rmdir (".ostest") || &error ("Couldn't rmdir .ostest: $!\n", 1);
296 sub parse_command_line
298 @argv = @_;
300 # use @ARGV if no args were passed in
302 if (@argv == 0)
304 @argv = @ARGV;
307 # look at each option; if we don't recognize it, maybe the suite-specific
308 # command line parsing code will...
310 while (@argv)
312 $option = shift @argv;
313 if ($option =~ /^-debug$/i)
315 print "\nDEBUG ON\n";
316 $debug = 1;
318 elsif ($option =~ /^-usage$/i)
320 &print_usage;
321 exit 0;
323 elsif ($option =~ /^-(h|help)$/i)
325 &print_help;
326 exit 0;
328 elsif ($option =~ /^-profile$/i)
330 $profile = 1;
332 elsif ($option =~ /^-verbose$/i)
334 $verbose = 1;
336 elsif ($option =~ /^-detail$/i)
338 $detail = 1;
339 $verbose = 1;
341 elsif ($option =~ /^-keep$/i)
343 $keep = 1;
345 elsif (&valid_option($option))
347 # The suite-defined subroutine takes care of the option
349 elsif ($option =~ /^-/)
351 print "Invalid option: $option\n";
352 &print_usage;
353 exit 0;
355 else # must be the name of a test
357 $option =~ s/\.pl$//;
358 push(@TESTS,$option);
363 sub max
365 local($num) = shift @_;
366 local($newnum);
368 while (@_)
370 $newnum = shift @_;
371 if ($newnum > $num)
373 $num = $newnum;
377 return $num;
380 sub print_centered
382 local($width, $string) = @_;
383 local($pad);
385 if (length ($string))
387 $pad = " " x ( ($width - length ($string) + 1) / 2);
388 print "$pad$string";
392 sub print_banner
394 local($info);
395 local($line);
396 local($len);
398 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
399 $len = &max (length ($line), length ($testee_version),
400 length ($banner_info), 73) + 5;
401 $line = ("-" x $len) . "\n";
402 if ($len < 78)
404 $len = 78;
407 &print_centered ($len, $line);
408 &print_centered ($len, $info);
409 &print_centered ($len, $testee_version); # suite-defined
410 &print_centered ($len, $banner_info); # suite-defined
411 &print_centered ($len, $line);
412 print "\n";
415 sub run_each_test
417 $categories_run = 0;
419 foreach $testname (sort @TESTS)
421 ++$categories_run;
422 $suite_passed = 1; # reset by test on failure
423 $num_of_logfiles = 0;
424 $num_of_tmpfiles = 0;
425 $description = "";
426 $details = "";
427 $old_makefile = undef;
428 $testname =~ s/^$scriptpath$pathsep//;
429 $perl_testname = "$scriptpath$pathsep$testname";
430 $testname =~ s/(\.pl|\.perl)$//;
431 $testpath = "$workpath$pathsep$testname";
432 # Leave enough space in the extensions to append a number, even
433 # though it needs to fit into 8+3 limits.
434 if ($short_filenames) {
435 $logext = 'l';
436 $diffext = 'd';
437 $baseext = 'b';
438 $runext = 'r';
439 $extext = '';
440 } else {
441 $logext = 'log';
442 $diffext = 'diff';
443 $baseext = 'base';
444 $runext = 'run';
445 $extext = '.';
447 $log_filename = "$testpath.$logext";
448 $diff_filename = "$testpath.$diffext";
449 $base_filename = "$testpath.$baseext";
450 $run_filename = "$testpath.$runext";
451 $tmp_filename = "$testpath.$tmpfilesuffix";
453 &setup_for_test; # suite-defined
455 $output = "........................................................ ";
457 substr($output,0,length($testname)) = "$testname ";
459 print $output;
461 # Run the actual test!
462 $tests_run = 0;
463 $tests_passed = 0;
465 $code = do $perl_testname;
467 $total_tests_run += $tests_run;
468 $total_tests_passed += $tests_passed;
470 # How did it go?
471 if (!defined($code))
473 $suite_passed = 0;
474 if (length ($@)) {
475 warn "\n*** Test died ($testname): $@\n";
476 } else {
477 warn "\n*** Couldn't run $perl_testname\n";
480 elsif ($code == -1) {
481 $suite_passed = 0;
483 elsif ($code != 1 && $code != -1) {
484 $suite_passed = 0;
485 warn "\n*** Test returned $code\n";
488 if ($suite_passed) {
489 ++$categories_passed;
490 $status = "ok ($tests_passed passed)";
491 for ($i = $num_of_tmpfiles; $i; $i--)
493 &rmfiles ($tmp_filename . &num_suffix ($i) );
496 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--)
498 &rmfiles ($log_filename . &num_suffix ($i) );
499 &rmfiles ($base_filename . &num_suffix ($i) );
502 elsif (!defined $code || $code > 0) {
503 $status = "FAILED ($tests_passed/$tests_run passed)";
505 elsif ($code < 0) {
506 $status = "N/A";
507 --$categories_run;
510 # If the verbose option has been specified, then a short description
511 # of each test is printed before displaying the results of each test
512 # describing WHAT is being tested.
514 if ($verbose)
516 if ($detail)
518 print "\nWHAT IS BEING TESTED\n";
519 print "--------------------";
521 print "\n\n$description\n\n";
524 # If the detail option has been specified, then the details of HOW
525 # the test is testing what it says it is testing in the verbose output
526 # will be displayed here before the results of the test are displayed.
528 if ($detail)
530 print "\nHOW IT IS TESTED\n";
531 print "----------------";
532 print "\n\n$details\n\n";
535 print "$status\n";
539 # If the keep flag is not set, this subroutine deletes all filenames that
540 # are sent to it.
542 sub rmfiles
544 local(@files) = @_;
546 if (!$keep)
548 return (unlink @files);
551 return 1;
554 sub print_standard_usage
556 local($plname,@moreusage) = @_;
557 local($line);
559 print "usage:\t$plname [testname] [-verbose] [-detail] [-keep]\n";
560 print "\t\t\t[-profile] [-usage] [-help] [-debug]\n";
561 foreach (@moreusage) {
562 print "\t\t\t$_\n";
566 sub print_standard_help
568 local(@morehelp) = @_;
569 local($line);
570 local($tline);
571 local($t) = " ";
573 $line = "Test Driver For $testee";
574 print "$line\n";
575 $line = "=" x length ($line);
576 print "$line\n";
578 &print_usage;
580 print "\ntestname\n"
581 . "${t}You may, if you wish, run only ONE test if you know the name\n"
582 . "${t}of that test and specify this name anywhere on the command\n"
583 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
584 . "${t}will be run.\n"
585 . "-verbose\n"
586 . "${t}If this option is given, a description of every test is\n"
587 . "${t}displayed before the test is run. (Not all tests may have\n"
588 . "${t}descriptions at this time)\n"
589 . "-detail\n"
590 . "${t}If this option is given, a detailed description of every\n"
591 . "${t}test is displayed before the test is run. (Not all tests\n"
592 . "${t}have descriptions at this time)\n"
593 . "-profile\n"
594 . "${t}If this option is given, then the profile file\n"
595 . "${t}is added to other profiles every time $testee is run.\n"
596 . "${t}This option only works on VOS at this time.\n"
597 . "-keep\n"
598 . "${t}You may give this option if you DO NOT want ANY\n"
599 . "${t}of the files generated by the tests to be deleted. \n"
600 . "${t}Without this option, all files generated by the test will\n"
601 . "${t}be deleted IF THE TEST PASSES.\n"
602 . "-debug\n"
603 . "${t}Use this option if you would like to see all of the system\n"
604 . "${t}calls issued and their return status while running the tests\n"
605 . "${t}This can be helpful if you're having a problem adding a test\n"
606 . "${t}to the suite, or if the test fails!\n";
608 foreach $line (@morehelp)
610 $tline = $line;
611 if (substr ($tline, 0, 1) eq "\t")
613 substr ($tline, 0, 1) = $t;
615 print "$tline\n";
619 #######################################################################
620 ########### Generic Test Driver Subroutines ###########
621 #######################################################################
623 sub get_caller
625 local($depth);
626 local($package);
627 local($filename);
628 local($linenum);
630 $depth = defined ($_[0]) ? $_[0] : 1;
631 ($package, $filename, $linenum) = caller ($depth + 1);
632 return "$filename: $linenum";
635 sub error
637 local($message) = $_[0];
638 local($caller) = &get_caller (1);
640 if (defined ($_[1]))
642 $caller = &get_caller ($_[1] + 1) . " -> $caller";
645 die "$caller: $message";
648 sub compare_output
650 local($answer,$logfile) = @_;
651 local($slurp, $answer_matched) = ('', 0);
653 print "Comparing Output ........ " if $debug;
655 $slurp = &read_file_into_string ($logfile);
657 # For make, get rid of any time skew error before comparing--too bad this
658 # has to go into the "generic" driver code :-/
659 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
660 $slurp =~ s/^.*Clock skew detected.*\n//gm;
662 ++$tests_run;
664 if ($slurp eq $answer) {
665 $answer_matched = 1;
666 } else {
667 # See if it is a slash or CRLF problem
668 local ($answer_mod, $slurp_mod) = ($answer, $slurp);
670 $answer_mod =~ tr,\\,/,;
671 $answer_mod =~ s,\r\n,\n,gs;
673 $slurp_mod =~ tr,\\,/,;
674 $slurp_mod =~ s,\r\n,\n,gs;
676 $answer_matched = ($slurp_mod eq $answer_mod);
678 # If it still doesn't match, see if the answer might be a regex.
679 if (!$answer_matched && $answer =~ m,^/(.+)/$,) {
680 $answer_matched = ($slurp =~ /$1/);
681 if (!$answer_matched && $answer_mod =~ m,^/(.+)/$,) {
682 $answer_matched = ($slurp_mod =~ /$1/);
687 if ($answer_matched && $test_passed)
689 print "ok\n" if $debug;
690 ++$tests_passed;
691 return 1;
694 if (! $answer_matched) {
695 print "DIFFERENT OUTPUT\n" if $debug;
697 &create_file (&get_basefile, $answer);
698 &create_file (&get_runfile, $command_string);
700 print "\nCreating Difference File ...\n" if $debug;
702 # Create the difference file
704 local($command) = "diff -c " . &get_basefile . " " . $logfile;
705 &run_command_with_output(&get_difffile,$command);
706 } else {
707 &rmfiles ();
710 $suite_passed = 0;
711 return 0;
714 sub read_file_into_string
716 local($filename) = @_;
717 local($oldslash) = $/;
719 undef $/;
721 open (RFISFILE, $filename) || return "";
722 local ($slurp) = <RFISFILE>;
723 close (RFISFILE);
725 $/ = $oldslash;
727 return $slurp;
730 sub attach_default_output
732 local ($filename) = @_;
733 local ($code);
735 if ($vos)
737 $code = system "++attach_default_output_hack $filename";
738 $code == -2 || &error ("adoh death\n", 1);
739 return 1;
742 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
743 || &error ("ado: $! duping STDOUT\n", 1);
744 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
745 || &error ("ado: $! duping STDERR\n", 1);
747 open (STDOUT, "> " . $filename)
748 || &error ("ado: $filename: $!\n", 1);
749 open (STDERR, ">&STDOUT")
750 || &error ("ado: $filename: $!\n", 1);
752 $default_output_stack_level++;
755 # close the current stdout/stderr, and restore the previous ones from
756 # the "stack."
758 sub detach_default_output
760 local ($code);
762 if ($vos)
764 $code = system "++detach_default_output_hack";
765 $code == -2 || &error ("ddoh death\n", 1);
766 return 1;
769 if (--$default_output_stack_level < 0)
771 &error ("default output stack has flown under!\n", 1);
774 close (STDOUT);
775 close (STDERR);
777 open (STDOUT, ">&SAVEDOS" . $default_output_stack_level . "out")
778 || &error ("ddo: $! duping STDOUT\n", 1);
779 open (STDERR, ">&SAVEDOS" . $default_output_stack_level . "err")
780 || &error ("ddo: $! duping STDERR\n", 1);
782 close ("SAVEDOS" . $default_output_stack_level . "out")
783 || &error ("ddo: $! closing SCSDOSout\n", 1);
784 close ("SAVEDOS" . $default_output_stack_level . "err")
785 || &error ("ddo: $! closing SAVEDOSerr\n", 1);
788 # This runs a command without any debugging info.
789 sub _run_command
791 my $code;
793 # We reset this before every invocation. On Windows I think there is only
794 # one environment, not one per process, so I think that variables set in
795 # test scripts might leak into subsequent tests if this isn't reset--???
796 resetENV();
798 eval {
799 local $SIG{ALRM} = sub { die "timeout\n"; };
800 alarm $test_timeout;
801 $code = system @_;
802 alarm 0;
804 if ($@) {
805 # The eval failed. If it wasn't SIGALRM then die.
806 $@ eq "timeout\n" or die;
808 # Timed out. Resend the alarm to our process group to kill the children.
809 $SIG{ALRM} = 'IGNORE';
810 kill -14, $$;
811 $code = 14;
814 return $code;
817 # run one command (passed as a list of arg 0 - n), returning 0 on success
818 # and nonzero on failure.
820 sub run_command
822 print "\nrun_command: @_\n" if $debug;
823 my $code = _run_command(@_);
824 print "run_command returned $code.\n" if $debug;
826 return $code;
829 # run one command (passed as a list of arg 0 - n, with arg 0 being the
830 # second arg to this routine), returning 0 on success and non-zero on failure.
831 # The first arg to this routine is a filename to connect to the stdout
832 # & stderr of the child process.
834 sub run_command_with_output
836 my $filename = shift;
838 print "\nrun_command_with_output($filename,$runname): @_\n" if $debug;
839 &attach_default_output ($filename);
840 my $code = _run_command(@_);
841 &detach_default_output;
842 print "run_command_with_output returned $code.\n" if $debug;
844 return $code;
847 # performs the equivalent of an "rm -rf" on the first argument. Like
848 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
849 # deletes it, too.
851 sub remove_directory_tree
853 local ($targetdir) = @_;
854 local ($nuketop) = 1;
855 local ($ch);
857 $ch = substr ($targetdir, length ($targetdir) - 1);
858 if ($ch eq "/" || $ch eq $pathsep)
860 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
861 $nuketop = 0;
864 if (! -e $targetdir)
866 return 1;
869 &remove_directory_tree_inner ("RDT00", $targetdir) || return 0;
870 if ($nuketop)
872 rmdir $targetdir || return 0;
875 return 1;
878 sub remove_directory_tree_inner
880 local ($dirhandle, $targetdir) = @_;
881 local ($object);
882 local ($subdirhandle);
884 opendir ($dirhandle, $targetdir) || return 0;
885 $subdirhandle = $dirhandle;
886 $subdirhandle++;
887 while ($object = readdir ($dirhandle))
889 if ($object =~ /^(\.\.?|CVS|RCS)$/)
891 next;
894 $object = "$targetdir$pathsep$object";
895 lstat ($object);
897 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object))
899 rmdir $object || return 0;
901 else
903 unlink $object || return 0;
906 closedir ($dirhandle);
907 return 1;
910 # We used to use this behavior for this function:
912 #sub touch
914 # local (@filenames) = @_;
915 # local ($now) = time;
916 # local ($file);
918 # foreach $file (@filenames)
920 # utime ($now, $now, $file)
921 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
922 # || &error ("Couldn't touch $file: $!\n", 1);
924 # return 1;
927 # But this behaves badly on networked filesystems where the time is
928 # skewed, because it sets the time of the file based on the _local_
929 # host. Normally when you modify a file, it's the _remote_ host that
930 # determines the modtime, based on _its_ clock. So, instead, now we open
931 # the file and write something into it to force the remote host to set
932 # the modtime correctly according to its clock.
935 sub touch
937 local ($file);
939 foreach $file (@_) {
940 (open(T, ">> $file") && print(T "\n") && close(T))
941 || &error("Couldn't touch $file: $!\n", 1);
945 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
946 # access/mod time for each file and apply the offset.
948 sub utouch
950 local ($off) = shift;
951 local ($file);
953 &touch(@_);
955 local (@s) = stat($_[0]);
957 utime($s[8]+$off, $s[9]+$off, @_);
960 # open a file, write some stuff to it, and close it.
962 sub create_file
964 local ($filename, @lines) = @_;
966 open (CF, "> $filename") || &error ("Couldn't open $filename: $!\n", 1);
967 foreach $line (@lines)
969 print CF $line;
971 close (CF);
974 # create a directory tree described by an associative array, wherein each
975 # key is a relative pathname (using slashes) and its associated value is
976 # one of:
977 # DIR indicates a directory
978 # FILE:contents indicates a file, which should contain contents +\n
979 # LINK:target indicates a symlink, pointing to $basedir/target
980 # The first argument is the dir under which the structure will be created
981 # (the dir will be made and/or cleaned if necessary); the second argument
982 # is the associative array.
984 sub create_dir_tree
986 local ($basedir, %dirtree) = @_;
987 local ($path);
989 &remove_directory_tree ("$basedir");
990 mkdir ($basedir, 0777) || &error ("Couldn't mkdir $basedir: $!\n", 1);
992 foreach $path (sort keys (%dirtree))
994 if ($dirtree {$path} =~ /^DIR$/)
996 mkdir ("$basedir/$path", 0777)
997 || &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
999 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1001 &create_file ("$basedir/$path", $1 . "\n");
1003 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1005 symlink ("$basedir/$1", "$basedir/$path")
1006 || &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
1008 else
1010 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1013 if ($just_setup_tree)
1015 die "Tree is setup...\n";
1019 # compare a directory tree with an associative array in the format used
1020 # by create_dir_tree, above.
1021 # The first argument is the dir under which the structure should be found;
1022 # the second argument is the associative array.
1024 sub compare_dir_tree
1026 local ($basedir, %dirtree) = @_;
1027 local ($path);
1028 local ($i);
1029 local ($bogus) = 0;
1030 local ($contents);
1031 local ($target);
1032 local ($fulltarget);
1033 local ($found);
1034 local (@files);
1035 local (@allfiles);
1037 opendir (DIR, $basedir) || &error ("Couldn't open $basedir: $!\n", 1);
1038 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
1039 closedir (DIR);
1040 if ($debug)
1042 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
1045 foreach $path (sort keys (%dirtree))
1047 if ($debug)
1049 print "Checking $path ($dirtree{$path}).\n";
1052 $found = 0;
1053 foreach $i (0 .. $#allfiles)
1055 if ($allfiles[$i] eq $path)
1057 splice (@allfiles, $i, 1); # delete it
1058 if ($debug)
1060 print " Zapped $path; files now (@allfiles).\n";
1062 lstat ("$basedir/$path");
1063 $found = 1;
1064 last;
1068 if (!$found)
1070 print "compare_dir_tree: $path does not exist.\n";
1071 $bogus = 1;
1072 next;
1075 if ($dirtree {$path} =~ /^DIR$/)
1077 if (-d _ && opendir (DIR, "$basedir/$path") )
1079 @files = readdir (DIR);
1080 closedir (DIR);
1081 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1082 push (@allfiles, @files);
1083 if ($debug)
1085 print " Read in $path; new files (@files).\n";
1088 else
1090 print "compare_dir_tree: $path is not a dir.\n";
1091 $bogus = 1;
1094 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1096 if (-l _ || !-f _)
1098 print "compare_dir_tree: $path is not a file.\n";
1099 $bogus = 1;
1100 next;
1103 if ($1 ne "*")
1105 $contents = &read_file_into_string ("$basedir/$path");
1106 if ($contents ne "$1\n")
1108 print "compare_dir_tree: $path contains wrong stuff."
1109 . " Is:\n$contentsShould be:\n$1\n";
1110 $bogus = 1;
1114 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1116 $target = $1;
1117 if (!-l _)
1119 print "compare_dir_tree: $path is not a link.\n";
1120 $bogus = 1;
1121 next;
1124 $contents = readlink ("$basedir/$path");
1125 $contents =~ tr/>/\//;
1126 $fulltarget = "$basedir/$target";
1127 $fulltarget =~ tr/>/\//;
1128 if (!($contents =~ /$fulltarget$/))
1130 if ($debug)
1132 $target = $fulltarget;
1134 print "compare_dir_tree: $path should be link to $target, "
1135 . "not $contents.\n";
1136 $bogus = 1;
1139 else
1141 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1145 if ($debug)
1147 print "leftovers: (@allfiles).\n";
1150 foreach $file (@allfiles)
1152 print "compare_dir_tree: $file should not exist.\n";
1153 $bogus = 1;
1156 return !$bogus;
1159 # this subroutine generates the numeric suffix used to keep tmp filenames,
1160 # log filenames, etc., unique. If the number passed in is 1, then a null
1161 # string is returned; otherwise, we return ".n", where n + 1 is the number
1162 # we were given.
1164 sub num_suffix
1166 local($num) = @_;
1168 if (--$num > 0) {
1169 return "$extext$num";
1172 return "";
1175 # This subroutine returns a log filename with a number appended to
1176 # the end corresponding to how many logfiles have been created in the
1177 # current running test. An optional parameter may be passed (0 or 1).
1178 # If a 1 is passed, then it does NOT increment the logfile counter
1179 # and returns the name of the latest logfile. If either no parameter
1180 # is passed at all or a 0 is passed, then the logfile counter is
1181 # incremented and the new name is returned.
1183 sub get_logfile
1185 local($no_increment) = @_;
1187 $num_of_logfiles += !$no_increment;
1189 return ($log_filename . &num_suffix ($num_of_logfiles));
1192 # This subroutine returns a base (answer) filename with a number
1193 # appended to the end corresponding to how many logfiles (and thus
1194 # base files) have been created in the current running test.
1195 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1197 sub get_basefile
1199 return ($base_filename . &num_suffix ($num_of_logfiles));
1202 # This subroutine returns a difference filename with a number appended
1203 # to the end corresponding to how many logfiles (and thus diff files)
1204 # have been created in the current running test.
1206 sub get_difffile
1208 return ($diff_filename . &num_suffix ($num_of_logfiles));
1211 # This subroutine returns a command filename with a number appended
1212 # to the end corresponding to how many logfiles (and thus command files)
1213 # have been created in the current running test.
1215 sub get_runfile
1217 return ($run_filename . &num_suffix ($num_of_logfiles));
1220 # just like logfile, only a generic tmp filename for use by the test.
1221 # they are automatically cleaned up unless -keep was used, or the test fails.
1222 # Pass an argument of 1 to return the same filename as the previous call.
1224 sub get_tmpfile
1226 local($no_increment) = @_;
1228 $num_of_tmpfiles += !$no_increment;
1230 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));