Ensure private variables are not used when appending target-specific
[make.git] / tests / test_driver.pl
blobdec869d8f27dc073b8999ed7a438deaa790f2399
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, 2010 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.30 2010/07/28 05:39:50 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;
57 # Path to Perl
58 $perl_name = $^X;
60 # %makeENV is the cleaned-out environment.
61 %makeENV = ();
63 # %extraENV are any extra environment variables the tests might want to set.
64 # These are RESET AFTER EVERY TEST!
65 %extraENV = ();
67 # %origENV is the caller's original environment
68 %origENV = %ENV;
70 sub resetENV
72 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
73 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
74 # want to require that here, so just delete each one individually.
75 foreach $v (keys %ENV) {
76 delete $ENV{$v};
79 %ENV = %makeENV;
80 foreach $v (keys %extraENV) {
81 $ENV{$v} = $extraENV{$v};
82 delete $extraENV{$v};
86 sub toplevel
88 # Pull in benign variables from the user's environment
90 foreach (# UNIX-specific things
91 'TZ', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
92 # Purify things
93 'PURIFYOPTIONS',
94 # Windows NT-specific stuff
95 'Path', 'SystemRoot',
96 # DJGPP-specific stuff
97 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
98 'FNCASE', '387', 'EMU387', 'GROUP'
99 ) {
100 $makeENV{$_} = $ENV{$_} if $ENV{$_};
103 # Make sure our compares are not foiled by locale differences
105 $makeENV{LC_ALL} = 'C';
107 # Replace the environment with the new one
109 %origENV = %ENV;
111 resetENV();
113 $| = 1; # unbuffered output
115 $debug = 0; # debug flag
116 $profile = 0; # profiling flag
117 $verbose = 0; # verbose mode flag
118 $detail = 0; # detailed verbosity
119 $keep = 0; # keep temp files around
120 $workdir = "work"; # The directory where the test will start running
121 $scriptdir = "scripts"; # The directory where we find the test scripts
122 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
123 $default_output_stack_level = 0; # used by attach_default_output, etc.
124 $default_input_stack_level = 0; # used by attach_default_input, etc.
125 $cwd = "."; # don't we wish we knew
126 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
128 &get_osname; # sets $osname, $vos, $pathsep, and $short_filenames
130 &set_defaults; # suite-defined
132 &parse_command_line (@ARGV);
134 print "OS name = `$osname'\n" if $debug;
136 $workpath = "$cwdslash$workdir";
137 $scriptpath = "$cwdslash$scriptdir";
139 &set_more_defaults; # suite-defined
141 &print_banner;
143 if (-d $workpath)
145 print "Clearing $workpath...\n";
146 &remove_directory_tree("$workpath/")
147 || &error ("Couldn't wipe out $workpath\n");
149 else
151 mkdir ($workpath, 0777) || &error ("Couldn't mkdir $workpath: $!\n");
154 if (!-d $scriptpath)
156 &error ("Failed to find $scriptpath containing perl test scripts.\n");
159 if (@TESTS)
161 print "Making work dirs...\n";
162 foreach $test (@TESTS)
164 if ($test =~ /^([^\/]+)\//)
166 $dir = $1;
167 push (@rmdirs, $dir);
168 -d "$workpath/$dir"
169 || mkdir ("$workpath/$dir", 0777)
170 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
174 else
176 print "Finding tests...\n";
177 opendir (SCRIPTDIR, $scriptpath)
178 || &error ("Couldn't opendir $scriptpath: $!\n");
179 @dirs = grep (!/^(\..*|CVS|RCS)$/, readdir (SCRIPTDIR) );
180 closedir (SCRIPTDIR);
181 foreach $dir (@dirs)
183 next if ($dir =~ /^(\..*|CVS|RCS)$/ || ! -d "$scriptpath/$dir");
184 push (@rmdirs, $dir);
185 mkdir ("$workpath/$dir", 0777)
186 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
187 opendir (SCRIPTDIR, "$scriptpath/$dir")
188 || &error ("Couldn't opendir $scriptpath/$dir: $!\n");
189 @files = grep (!/^(\..*|CVS|RCS|.*~)$/, readdir (SCRIPTDIR) );
190 closedir (SCRIPTDIR);
191 foreach $test (@files)
193 -d $test and next;
194 push (@TESTS, "$dir/$test");
199 if (@TESTS == 0)
201 &error ("\nNo tests in $scriptpath, and none were specified.\n");
204 print "\n";
206 &run_each_test;
208 foreach $dir (@rmdirs)
210 rmdir ("$workpath/$dir");
213 $| = 1;
215 $categories_failed = $categories_run - $categories_passed;
216 $total_tests_failed = $total_tests_run - $total_tests_passed;
218 if ($total_tests_failed)
220 print "\n$total_tests_failed Test";
221 print "s" unless $total_tests_failed == 1;
222 print " in $categories_failed Categor";
223 print ($categories_failed == 1 ? "y" : "ies");
224 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
225 return 0;
227 else
229 print "\n$total_tests_passed Test";
230 print "s" unless $total_tests_passed == 1;
231 print " in $categories_passed Categor";
232 print ($categories_passed == 1 ? "y" : "ies");
233 print " Complete ... No Failures :-)\n\n";
234 return 1;
238 sub get_osname
240 # Set up an initial value. In perl5 we can do it the easy way.
241 $osname = defined($^O) ? $^O : '';
243 # Find a path to Perl
245 # See if the filesystem supports long file names with multiple
246 # dots. DOS doesn't.
247 $short_filenames = 0;
248 (open (TOUCHFD, "> fancy.file.name") && close (TOUCHFD))
249 || ($short_filenames = 1);
250 unlink ("fancy.file.name") || ($short_filenames = 1);
252 if (! $short_filenames) {
253 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
254 # better way of doing this. (We used to test for existence of a /mnt
255 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
256 # Because perl on VOS translates /'s to >'s, we need to test for
257 # VOSness rather than testing for Unixness (ie, try > instead of /).
259 mkdir (".ostest", 0777) || &error ("Couldn't create .ostest: $!\n", 1);
260 open (TOUCHFD, "> .ostest>ick") && close (TOUCHFD);
261 chdir (".ostest") || &error ("Couldn't chdir to .ostest: $!\n", 1);
264 if (! $short_filenames && -f "ick")
266 $osname = "vos";
267 $vos = 1;
268 $pathsep = ">";
270 else
272 # the following is regrettably knarly, but it seems to be the only way
273 # to not get ugly error messages if uname can't be found.
274 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
275 # with switches first.
276 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
277 if ($osname =~ /not found/i)
279 $osname = "(something posixy with no uname)";
281 elsif ($@ ne "" || $?)
283 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
284 if ($@ ne "" || $?)
286 $osname = "(something posixy)";
289 $vos = 0;
290 $pathsep = "/";
293 if (! $short_filenames) {
294 chdir ("..") || &error ("Couldn't chdir to ..: $!\n", 1);
295 unlink (".ostest>ick");
296 rmdir (".ostest") || &error ("Couldn't rmdir .ostest: $!\n", 1);
300 sub parse_command_line
302 @argv = @_;
304 # use @ARGV if no args were passed in
306 if (@argv == 0)
308 @argv = @ARGV;
311 # look at each option; if we don't recognize it, maybe the suite-specific
312 # command line parsing code will...
314 while (@argv)
316 $option = shift @argv;
317 if ($option =~ /^-debug$/i)
319 print "\nDEBUG ON\n";
320 $debug = 1;
322 elsif ($option =~ /^-usage$/i)
324 &print_usage;
325 exit 0;
327 elsif ($option =~ /^-(h|help)$/i)
329 &print_help;
330 exit 0;
332 elsif ($option =~ /^-profile$/i)
334 $profile = 1;
336 elsif ($option =~ /^-verbose$/i)
338 $verbose = 1;
340 elsif ($option =~ /^-detail$/i)
342 $detail = 1;
343 $verbose = 1;
345 elsif ($option =~ /^-keep$/i)
347 $keep = 1;
349 elsif (&valid_option($option))
351 # The suite-defined subroutine takes care of the option
353 elsif ($option =~ /^-/)
355 print "Invalid option: $option\n";
356 &print_usage;
357 exit 0;
359 else # must be the name of a test
361 $option =~ s/\.pl$//;
362 push(@TESTS,$option);
367 sub max
369 local($num) = shift @_;
370 local($newnum);
372 while (@_)
374 $newnum = shift @_;
375 if ($newnum > $num)
377 $num = $newnum;
381 return $num;
384 sub print_centered
386 local($width, $string) = @_;
387 local($pad);
389 if (length ($string))
391 $pad = " " x ( ($width - length ($string) + 1) / 2);
392 print "$pad$string";
396 sub print_banner
398 local($info);
399 local($line);
400 local($len);
402 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
403 $len = &max (length ($line), length ($testee_version),
404 length ($banner_info), 73) + 5;
405 $line = ("-" x $len) . "\n";
406 if ($len < 78)
408 $len = 78;
411 &print_centered ($len, $line);
412 &print_centered ($len, $info);
413 &print_centered ($len, $testee_version); # suite-defined
414 &print_centered ($len, $banner_info); # suite-defined
415 &print_centered ($len, $line);
416 print "\n";
419 sub run_each_test
421 $categories_run = 0;
423 foreach $testname (sort @TESTS)
425 ++$categories_run;
426 $suite_passed = 1; # reset by test on failure
427 $num_of_logfiles = 0;
428 $num_of_tmpfiles = 0;
429 $description = "";
430 $details = "";
431 $old_makefile = undef;
432 $testname =~ s/^$scriptpath$pathsep//;
433 $perl_testname = "$scriptpath$pathsep$testname";
434 $testname =~ s/(\.pl|\.perl)$//;
435 $testpath = "$workpath$pathsep$testname";
436 # Leave enough space in the extensions to append a number, even
437 # though it needs to fit into 8+3 limits.
438 if ($short_filenames) {
439 $logext = 'l';
440 $diffext = 'd';
441 $baseext = 'b';
442 $runext = 'r';
443 $extext = '';
444 } else {
445 $logext = 'log';
446 $diffext = 'diff';
447 $baseext = 'base';
448 $runext = 'run';
449 $extext = '.';
451 $log_filename = "$testpath.$logext";
452 $diff_filename = "$testpath.$diffext";
453 $base_filename = "$testpath.$baseext";
454 $run_filename = "$testpath.$runext";
455 $tmp_filename = "$testpath.$tmpfilesuffix";
457 &setup_for_test; # suite-defined
459 $output = "........................................................ ";
461 substr($output,0,length($testname)) = "$testname ";
463 print $output;
465 # Run the actual test!
466 $tests_run = 0;
467 $tests_passed = 0;
469 $code = do $perl_testname;
471 $total_tests_run += $tests_run;
472 $total_tests_passed += $tests_passed;
474 # How did it go?
475 if (!defined($code))
477 $suite_passed = 0;
478 if (length ($@)) {
479 warn "\n*** Test died ($testname): $@\n";
480 } else {
481 warn "\n*** Couldn't run $perl_testname\n";
484 elsif ($code == -1) {
485 $suite_passed = 0;
487 elsif ($code != 1 && $code != -1) {
488 $suite_passed = 0;
489 warn "\n*** Test returned $code\n";
492 if ($suite_passed) {
493 ++$categories_passed;
494 $status = "ok ($tests_passed passed)";
495 for ($i = $num_of_tmpfiles; $i; $i--)
497 &rmfiles ($tmp_filename . &num_suffix ($i) );
500 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--)
502 &rmfiles ($log_filename . &num_suffix ($i) );
503 &rmfiles ($base_filename . &num_suffix ($i) );
506 elsif (!defined $code || $code > 0) {
507 $status = "FAILED ($tests_passed/$tests_run passed)";
509 elsif ($code < 0) {
510 $status = "N/A";
511 --$categories_run;
514 # If the verbose option has been specified, then a short description
515 # of each test is printed before displaying the results of each test
516 # describing WHAT is being tested.
518 if ($verbose)
520 if ($detail)
522 print "\nWHAT IS BEING TESTED\n";
523 print "--------------------";
525 print "\n\n$description\n\n";
528 # If the detail option has been specified, then the details of HOW
529 # the test is testing what it says it is testing in the verbose output
530 # will be displayed here before the results of the test are displayed.
532 if ($detail)
534 print "\nHOW IT IS TESTED\n";
535 print "----------------";
536 print "\n\n$details\n\n";
539 print "$status\n";
543 # If the keep flag is not set, this subroutine deletes all filenames that
544 # are sent to it.
546 sub rmfiles
548 local(@files) = @_;
550 if (!$keep)
552 return (unlink @files);
555 return 1;
558 sub print_standard_usage
560 local($plname,@moreusage) = @_;
561 local($line);
563 print "usage:\t$plname [testname] [-verbose] [-detail] [-keep]\n";
564 print "\t\t\t[-profile] [-usage] [-help] [-debug]\n";
565 foreach (@moreusage) {
566 print "\t\t\t$_\n";
570 sub print_standard_help
572 local(@morehelp) = @_;
573 local($line);
574 local($tline);
575 local($t) = " ";
577 $line = "Test Driver For $testee";
578 print "$line\n";
579 $line = "=" x length ($line);
580 print "$line\n";
582 &print_usage;
584 print "\ntestname\n"
585 . "${t}You may, if you wish, run only ONE test if you know the name\n"
586 . "${t}of that test and specify this name anywhere on the command\n"
587 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
588 . "${t}will be run.\n"
589 . "-verbose\n"
590 . "${t}If this option is given, a description of every test is\n"
591 . "${t}displayed before the test is run. (Not all tests may have\n"
592 . "${t}descriptions at this time)\n"
593 . "-detail\n"
594 . "${t}If this option is given, a detailed description of every\n"
595 . "${t}test is displayed before the test is run. (Not all tests\n"
596 . "${t}have descriptions at this time)\n"
597 . "-profile\n"
598 . "${t}If this option is given, then the profile file\n"
599 . "${t}is added to other profiles every time $testee is run.\n"
600 . "${t}This option only works on VOS at this time.\n"
601 . "-keep\n"
602 . "${t}You may give this option if you DO NOT want ANY\n"
603 . "${t}of the files generated by the tests to be deleted. \n"
604 . "${t}Without this option, all files generated by the test will\n"
605 . "${t}be deleted IF THE TEST PASSES.\n"
606 . "-debug\n"
607 . "${t}Use this option if you would like to see all of the system\n"
608 . "${t}calls issued and their return status while running the tests\n"
609 . "${t}This can be helpful if you're having a problem adding a test\n"
610 . "${t}to the suite, or if the test fails!\n";
612 foreach $line (@morehelp)
614 $tline = $line;
615 if (substr ($tline, 0, 1) eq "\t")
617 substr ($tline, 0, 1) = $t;
619 print "$tline\n";
623 #######################################################################
624 ########### Generic Test Driver Subroutines ###########
625 #######################################################################
627 sub get_caller
629 local($depth);
630 local($package);
631 local($filename);
632 local($linenum);
634 $depth = defined ($_[0]) ? $_[0] : 1;
635 ($package, $filename, $linenum) = caller ($depth + 1);
636 return "$filename: $linenum";
639 sub error
641 local($message) = $_[0];
642 local($caller) = &get_caller (1);
644 if (defined ($_[1]))
646 $caller = &get_caller ($_[1] + 1) . " -> $caller";
649 die "$caller: $message";
652 sub compare_output
654 local($answer,$logfile) = @_;
655 local($slurp, $answer_matched) = ('', 0);
657 print "Comparing Output ........ " if $debug;
659 $slurp = &read_file_into_string ($logfile);
661 # For make, get rid of any time skew error before comparing--too bad this
662 # has to go into the "generic" driver code :-/
663 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
664 $slurp =~ s/^.*Clock skew detected.*\n//gm;
666 ++$tests_run;
668 if ($slurp eq $answer) {
669 $answer_matched = 1;
670 } else {
671 # See if it is a slash or CRLF problem
672 local ($answer_mod, $slurp_mod) = ($answer, $slurp);
674 $answer_mod =~ tr,\\,/,;
675 $answer_mod =~ s,\r\n,\n,gs;
677 $slurp_mod =~ tr,\\,/,;
678 $slurp_mod =~ s,\r\n,\n,gs;
680 $answer_matched = ($slurp_mod eq $answer_mod);
682 # If it still doesn't match, see if the answer might be a regex.
683 if (!$answer_matched && $answer =~ m,^/(.+)/$,) {
684 $answer_matched = ($slurp =~ /$1/);
685 if (!$answer_matched && $answer_mod =~ m,^/(.+)/$,) {
686 $answer_matched = ($slurp_mod =~ /$1/);
691 if ($answer_matched && $test_passed)
693 print "ok\n" if $debug;
694 ++$tests_passed;
695 return 1;
698 if (! $answer_matched) {
699 print "DIFFERENT OUTPUT\n" if $debug;
701 &create_file (&get_basefile, $answer);
702 &create_file (&get_runfile, $command_string);
704 print "\nCreating Difference File ...\n" if $debug;
706 # Create the difference file
708 local($command) = "diff -c " . &get_basefile . " " . $logfile;
709 &run_command_with_output(&get_difffile,$command);
710 } else {
711 &rmfiles ();
714 $suite_passed = 0;
715 return 0;
718 sub read_file_into_string
720 local($filename) = @_;
721 local($oldslash) = $/;
723 undef $/;
725 open (RFISFILE, $filename) || return "";
726 local ($slurp) = <RFISFILE>;
727 close (RFISFILE);
729 $/ = $oldslash;
731 return $slurp;
734 sub attach_default_output
736 local ($filename) = @_;
737 local ($code);
739 if ($vos)
741 $code = system "++attach_default_output_hack $filename";
742 $code == -2 || &error ("adoh death\n", 1);
743 return 1;
746 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
747 || &error ("ado: $! duping STDOUT\n", 1);
748 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
749 || &error ("ado: $! duping STDERR\n", 1);
751 open (STDOUT, "> " . $filename)
752 || &error ("ado: $filename: $!\n", 1);
753 open (STDERR, ">&STDOUT")
754 || &error ("ado: $filename: $!\n", 1);
756 $default_output_stack_level++;
759 # close the current stdout/stderr, and restore the previous ones from
760 # the "stack."
762 sub detach_default_output
764 local ($code);
766 if ($vos)
768 $code = system "++detach_default_output_hack";
769 $code == -2 || &error ("ddoh death\n", 1);
770 return 1;
773 if (--$default_output_stack_level < 0)
775 &error ("default output stack has flown under!\n", 1);
778 close (STDOUT);
779 close (STDERR);
781 open (STDOUT, ">&SAVEDOS" . $default_output_stack_level . "out")
782 || &error ("ddo: $! duping STDOUT\n", 1);
783 open (STDERR, ">&SAVEDOS" . $default_output_stack_level . "err")
784 || &error ("ddo: $! duping STDERR\n", 1);
786 close ("SAVEDOS" . $default_output_stack_level . "out")
787 || &error ("ddo: $! closing SCSDOSout\n", 1);
788 close ("SAVEDOS" . $default_output_stack_level . "err")
789 || &error ("ddo: $! closing SAVEDOSerr\n", 1);
792 # This runs a command without any debugging info.
793 sub _run_command
795 my $code;
797 # We reset this before every invocation. On Windows I think there is only
798 # one environment, not one per process, so I think that variables set in
799 # test scripts might leak into subsequent tests if this isn't reset--???
800 resetENV();
802 eval {
803 local $SIG{ALRM} = sub { die "timeout\n"; };
804 alarm $test_timeout;
805 $code = system(@_);
806 alarm 0;
808 if ($@) {
809 # The eval failed. If it wasn't SIGALRM then die.
810 $@ eq "timeout\n" or die;
812 # Timed out. Resend the alarm to our process group to kill the children.
813 $SIG{ALRM} = 'IGNORE';
814 kill -14, $$;
815 $code = 14;
818 return $code;
821 # run one command (passed as a list of arg 0 - n), returning 0 on success
822 # and nonzero on failure.
824 sub run_command
826 print "\nrun_command: @_\n" if $debug;
827 my $code = _run_command(@_);
828 print "run_command returned $code.\n" if $debug;
830 return $code;
833 # run one command (passed as a list of arg 0 - n, with arg 0 being the
834 # second arg to this routine), returning 0 on success and non-zero on failure.
835 # The first arg to this routine is a filename to connect to the stdout
836 # & stderr of the child process.
838 sub run_command_with_output
840 my $filename = shift;
842 print "\nrun_command_with_output($filename,$runname): @_\n" if $debug;
843 &attach_default_output ($filename);
844 my $code = _run_command(@_);
845 &detach_default_output;
846 print "run_command_with_output returned $code.\n" if $debug;
848 return $code;
851 # performs the equivalent of an "rm -rf" on the first argument. Like
852 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
853 # deletes it, too.
855 sub remove_directory_tree
857 local ($targetdir) = @_;
858 local ($nuketop) = 1;
859 local ($ch);
861 $ch = substr ($targetdir, length ($targetdir) - 1);
862 if ($ch eq "/" || $ch eq $pathsep)
864 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
865 $nuketop = 0;
868 if (! -e $targetdir)
870 return 1;
873 &remove_directory_tree_inner ("RDT00", $targetdir) || return 0;
874 if ($nuketop)
876 rmdir $targetdir || return 0;
879 return 1;
882 sub remove_directory_tree_inner
884 local ($dirhandle, $targetdir) = @_;
885 local ($object);
886 local ($subdirhandle);
888 opendir ($dirhandle, $targetdir) || return 0;
889 $subdirhandle = $dirhandle;
890 $subdirhandle++;
891 while ($object = readdir ($dirhandle))
893 if ($object =~ /^(\.\.?|CVS|RCS)$/)
895 next;
898 $object = "$targetdir$pathsep$object";
899 lstat ($object);
901 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object))
903 rmdir $object || return 0;
905 else
907 unlink $object || return 0;
910 closedir ($dirhandle);
911 return 1;
914 # We used to use this behavior for this function:
916 #sub touch
918 # local (@filenames) = @_;
919 # local ($now) = time;
920 # local ($file);
922 # foreach $file (@filenames)
924 # utime ($now, $now, $file)
925 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
926 # || &error ("Couldn't touch $file: $!\n", 1);
928 # return 1;
931 # But this behaves badly on networked filesystems where the time is
932 # skewed, because it sets the time of the file based on the _local_
933 # host. Normally when you modify a file, it's the _remote_ host that
934 # determines the modtime, based on _its_ clock. So, instead, now we open
935 # the file and write something into it to force the remote host to set
936 # the modtime correctly according to its clock.
939 sub touch
941 local ($file);
943 foreach $file (@_) {
944 (open(T, ">> $file") && print(T "\n") && close(T))
945 || &error("Couldn't touch $file: $!\n", 1);
949 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
950 # access/mod time for each file and apply the offset.
952 sub utouch
954 local ($off) = shift;
955 local ($file);
957 &touch(@_);
959 local (@s) = stat($_[0]);
961 utime($s[8]+$off, $s[9]+$off, @_);
964 # open a file, write some stuff to it, and close it.
966 sub create_file
968 local ($filename, @lines) = @_;
970 open (CF, "> $filename") || &error ("Couldn't open $filename: $!\n", 1);
971 foreach $line (@lines)
973 print CF $line;
975 close (CF);
978 # create a directory tree described by an associative array, wherein each
979 # key is a relative pathname (using slashes) and its associated value is
980 # one of:
981 # DIR indicates a directory
982 # FILE:contents indicates a file, which should contain contents +\n
983 # LINK:target indicates a symlink, pointing to $basedir/target
984 # The first argument is the dir under which the structure will be created
985 # (the dir will be made and/or cleaned if necessary); the second argument
986 # is the associative array.
988 sub create_dir_tree
990 local ($basedir, %dirtree) = @_;
991 local ($path);
993 &remove_directory_tree ("$basedir");
994 mkdir ($basedir, 0777) || &error ("Couldn't mkdir $basedir: $!\n", 1);
996 foreach $path (sort keys (%dirtree))
998 if ($dirtree {$path} =~ /^DIR$/)
1000 mkdir ("$basedir/$path", 0777)
1001 || &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
1003 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1005 &create_file ("$basedir/$path", $1 . "\n");
1007 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1009 symlink ("$basedir/$1", "$basedir/$path")
1010 || &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
1012 else
1014 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1017 if ($just_setup_tree)
1019 die "Tree is setup...\n";
1023 # compare a directory tree with an associative array in the format used
1024 # by create_dir_tree, above.
1025 # The first argument is the dir under which the structure should be found;
1026 # the second argument is the associative array.
1028 sub compare_dir_tree
1030 local ($basedir, %dirtree) = @_;
1031 local ($path);
1032 local ($i);
1033 local ($bogus) = 0;
1034 local ($contents);
1035 local ($target);
1036 local ($fulltarget);
1037 local ($found);
1038 local (@files);
1039 local (@allfiles);
1041 opendir (DIR, $basedir) || &error ("Couldn't open $basedir: $!\n", 1);
1042 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
1043 closedir (DIR);
1044 if ($debug)
1046 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
1049 foreach $path (sort keys (%dirtree))
1051 if ($debug)
1053 print "Checking $path ($dirtree{$path}).\n";
1056 $found = 0;
1057 foreach $i (0 .. $#allfiles)
1059 if ($allfiles[$i] eq $path)
1061 splice (@allfiles, $i, 1); # delete it
1062 if ($debug)
1064 print " Zapped $path; files now (@allfiles).\n";
1066 lstat ("$basedir/$path");
1067 $found = 1;
1068 last;
1072 if (!$found)
1074 print "compare_dir_tree: $path does not exist.\n";
1075 $bogus = 1;
1076 next;
1079 if ($dirtree {$path} =~ /^DIR$/)
1081 if (-d _ && opendir (DIR, "$basedir/$path") )
1083 @files = readdir (DIR);
1084 closedir (DIR);
1085 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1086 push (@allfiles, @files);
1087 if ($debug)
1089 print " Read in $path; new files (@files).\n";
1092 else
1094 print "compare_dir_tree: $path is not a dir.\n";
1095 $bogus = 1;
1098 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1100 if (-l _ || !-f _)
1102 print "compare_dir_tree: $path is not a file.\n";
1103 $bogus = 1;
1104 next;
1107 if ($1 ne "*")
1109 $contents = &read_file_into_string ("$basedir/$path");
1110 if ($contents ne "$1\n")
1112 print "compare_dir_tree: $path contains wrong stuff."
1113 . " Is:\n$contentsShould be:\n$1\n";
1114 $bogus = 1;
1118 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1120 $target = $1;
1121 if (!-l _)
1123 print "compare_dir_tree: $path is not a link.\n";
1124 $bogus = 1;
1125 next;
1128 $contents = readlink ("$basedir/$path");
1129 $contents =~ tr/>/\//;
1130 $fulltarget = "$basedir/$target";
1131 $fulltarget =~ tr/>/\//;
1132 if (!($contents =~ /$fulltarget$/))
1134 if ($debug)
1136 $target = $fulltarget;
1138 print "compare_dir_tree: $path should be link to $target, "
1139 . "not $contents.\n";
1140 $bogus = 1;
1143 else
1145 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1149 if ($debug)
1151 print "leftovers: (@allfiles).\n";
1154 foreach $file (@allfiles)
1156 print "compare_dir_tree: $file should not exist.\n";
1157 $bogus = 1;
1160 return !$bogus;
1163 # this subroutine generates the numeric suffix used to keep tmp filenames,
1164 # log filenames, etc., unique. If the number passed in is 1, then a null
1165 # string is returned; otherwise, we return ".n", where n + 1 is the number
1166 # we were given.
1168 sub num_suffix
1170 local($num) = @_;
1172 if (--$num > 0) {
1173 return "$extext$num";
1176 return "";
1179 # This subroutine returns a log filename with a number appended to
1180 # the end corresponding to how many logfiles have been created in the
1181 # current running test. An optional parameter may be passed (0 or 1).
1182 # If a 1 is passed, then it does NOT increment the logfile counter
1183 # and returns the name of the latest logfile. If either no parameter
1184 # is passed at all or a 0 is passed, then the logfile counter is
1185 # incremented and the new name is returned.
1187 sub get_logfile
1189 local($no_increment) = @_;
1191 $num_of_logfiles += !$no_increment;
1193 return ($log_filename . &num_suffix ($num_of_logfiles));
1196 # This subroutine returns a base (answer) filename with a number
1197 # appended to the end corresponding to how many logfiles (and thus
1198 # base files) have been created in the current running test.
1199 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1201 sub get_basefile
1203 return ($base_filename . &num_suffix ($num_of_logfiles));
1206 # This subroutine returns a difference filename with a number appended
1207 # to the end corresponding to how many logfiles (and thus diff files)
1208 # have been created in the current running test.
1210 sub get_difffile
1212 return ($diff_filename . &num_suffix ($num_of_logfiles));
1215 # This subroutine returns a command filename with a number appended
1216 # to the end corresponding to how many logfiles (and thus command files)
1217 # have been created in the current running test.
1219 sub get_runfile
1221 return ($run_filename . &num_suffix ($num_of_logfiles));
1224 # just like logfile, only a generic tmp filename for use by the test.
1225 # they are automatically cleaned up unless -keep was used, or the test fails.
1226 # Pass an argument of 1 to return the same filename as the previous call.
1228 sub get_tmpfile
1230 local($no_increment) = @_;
1232 $num_of_tmpfiles += !$no_increment;
1234 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));