Various minor updates and code cleanups.
[make.git] / tests / test_driver.pl
blobc373b8f15e150b0a07103f330065f8c66c1ec66f
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.
7 # End of modification history
9 # Test driver routines used by a number of test suites, including
10 # those for SCS, make, roll_dir, and scan_deps (?).
12 # this routine controls the whole mess; each test suite sets up a few
13 # variables and then calls &toplevel, which does all the real work.
15 # $Id: test_driver.pl,v 1.15 2005/07/12 04:35:13 psmith Exp $
18 # The number of test categories we've run
19 $categories_run = 0;
20 # The number of test categroies that have passed
21 $categories_passed = 0;
22 # The total number of individual tests that have been run
23 $total_tests_run = 0;
24 # The total number of individual tests that have passed
25 $total_tests_passed = 0;
26 # The number of tests in this category that have been run
27 $tests_run = 0;
28 # The number of tests in this category that have passed
29 $tests_passed = 0;
32 # Yeesh. This whole test environment is such a hack!
33 $test_passed = 1;
36 # %makeENV is the cleaned-out environment.
37 %makeENV = ();
39 # %extraENV are any extra environment variables the tests might want to set.
40 # These are RESET AFTER EVERY TEST!
41 %extraENV = ();
43 # %origENV is the caller's original environment
44 %origENV = %ENV;
46 sub resetENV
48 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
49 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
50 # want to require that here, so just delete each one individually.
51 foreach $v (keys %ENV) {
52 delete $ENV{$v};
55 %ENV = %makeENV;
56 foreach $v (keys %extraENV) {
57 $ENV{$v} = $extraENV{$v};
58 delete $extraENV{$v};
62 sub toplevel
64 # Pull in benign variables from the user's environment
66 foreach (# UNIX-specific things
67 'TZ', 'LANG', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
68 # Purify things
69 'PURIFYOPTIONS',
70 # Windows NT-specific stuff
71 'Path', 'SystemRoot',
72 # DJGPP-specific stuff
73 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
74 'FNCASE', '387', 'EMU387', 'GROUP'
75 ) {
76 $makeENV{$_} = $ENV{$_} if $ENV{$_};
79 # Replace the environment with the new one
81 %origENV = %ENV;
83 resetENV();
85 $| = 1; # unbuffered output
87 $debug = 0; # debug flag
88 $profile = 0; # profiling flag
89 $verbose = 0; # verbose mode flag
90 $detail = 0; # detailed verbosity
91 $keep = 0; # keep temp files around
92 $workdir = "work"; # The directory where the test will start running
93 $scriptdir = "scripts"; # The directory where we find the test scripts
94 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
95 $default_output_stack_level = 0; # used by attach_default_output, etc.
96 $default_input_stack_level = 0; # used by attach_default_input, etc.
97 $cwd = "."; # don't we wish we knew
98 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
100 &get_osname; # sets $osname, $vos, $pathsep, and $short_filenames
102 &set_defaults; # suite-defined
104 &parse_command_line (@ARGV);
106 print "OS name = `$osname'\n" if $debug;
108 $workpath = "$cwdslash$workdir";
109 $scriptpath = "$cwdslash$scriptdir";
111 &set_more_defaults; # suite-defined
113 &print_banner;
115 if (-d $workpath)
117 print "Clearing $workpath...\n";
118 &remove_directory_tree("$workpath/")
119 || &error ("Couldn't wipe out $workpath\n");
121 else
123 mkdir ($workpath, 0777) || &error ("Couldn't mkdir $workpath: $!\n");
126 if (!-d $scriptpath)
128 &error ("Failed to find $scriptpath containing perl test scripts.\n");
131 if (@TESTS)
133 print "Making work dirs...\n";
134 foreach $test (@TESTS)
136 if ($test =~ /^([^\/]+)\//)
138 $dir = $1;
139 push (@rmdirs, $dir);
140 -d "$workpath/$dir"
141 || mkdir ("$workpath/$dir", 0777)
142 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
146 else
148 print "Finding tests...\n";
149 opendir (SCRIPTDIR, $scriptpath)
150 || &error ("Couldn't opendir $scriptpath: $!\n");
151 @dirs = grep (!/^(\.\.?|CVS|RCS)$/, readdir (SCRIPTDIR) );
152 closedir (SCRIPTDIR);
153 foreach $dir (@dirs)
155 next if ($dir =~ /^\.\.?$/ || $dir eq 'CVS' || $dir eq 'RCS'
156 || ! -d "$scriptpath/$dir");
157 push (@rmdirs, $dir);
158 mkdir ("$workpath/$dir", 0777)
159 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
160 opendir (SCRIPTDIR, "$scriptpath/$dir")
161 || &error ("Couldn't opendir $scriptpath/$dir: $!\n");
162 @files = grep (!/^(\.\.?|CVS|RCS)$/, readdir (SCRIPTDIR) );
163 closedir (SCRIPTDIR);
164 foreach $test (@files)
166 next if $test =~ /~$/ || -d $test;
167 push (@TESTS, "$dir/$test");
172 if (@TESTS == 0)
174 &error ("\nNo tests in $scriptpath, and none were specified.\n");
177 print "\n";
179 &run_each_test;
181 foreach $dir (@rmdirs)
183 rmdir ("$workpath/$dir");
186 $| = 1;
188 $categories_failed = $categories_run - $categories_passed;
189 $total_tests_failed = $total_tests_run - $total_tests_passed;
191 if ($total_tests_failed)
193 print "\n$total_tests_failed Test";
194 print "s" unless $total_tests_failed == 1;
195 print " in $categories_failed Categor";
196 print ($categories_failed == 1 ? "y" : "ies");
197 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
198 return 0;
200 else
202 print "\n$total_tests_passed Test";
203 print "s" unless $total_tests_passed == 1;
204 print " in $categories_passed Categor";
205 print ($categories_passed == 1 ? "y" : "ies");
206 print " Complete ... No Failures :-)\n\n";
207 return 1;
211 sub get_osname
213 # Set up an initial value. In perl5 we can do it the easy way.
215 $osname = defined($^O) ? $^O : '';
217 # See if the filesystem supports long file names with multiple
218 # dots. DOS doesn't.
219 $short_filenames = 0;
220 (open (TOUCHFD, "> fancy.file.name") && close (TOUCHFD))
221 || ($short_filenames = 1);
222 unlink ("fancy.file.name") || ($short_filenames = 1);
224 if (! $short_filenames) {
225 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
226 # better way of doing this. (We used to test for existence of a /mnt
227 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
228 # Because perl on VOS translates /'s to >'s, we need to test for
229 # VOSness rather than testing for Unixness (ie, try > instead of /).
231 mkdir (".ostest", 0777) || &error ("Couldn't create .ostest: $!\n", 1);
232 open (TOUCHFD, "> .ostest>ick") && close (TOUCHFD);
233 chdir (".ostest") || &error ("Couldn't chdir to .ostest: $!\n", 1);
236 if (! $short_filenames && -f "ick")
238 $osname = "vos";
239 $vos = 1;
240 $pathsep = ">";
242 else
244 # the following is regrettably knarly, but it seems to be the only way
245 # to not get ugly error messages if uname can't be found.
246 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
247 # with switches first.
248 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
249 if ($osname =~ /not found/i)
251 $osname = "(something unixy with no uname)";
253 elsif ($@ ne "" || $?)
255 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
256 if ($@ ne "" || $?)
258 $osname = "(something unixy)";
261 $vos = 0;
262 $pathsep = "/";
265 if (! $short_filenames) {
266 chdir ("..") || &error ("Couldn't chdir to ..: $!\n", 1);
267 unlink (".ostest>ick");
268 rmdir (".ostest") || &error ("Couldn't rmdir .ostest: $!\n", 1);
272 sub parse_command_line
274 @argv = @_;
276 # use @ARGV if no args were passed in
278 if (@argv == 0)
280 @argv = @ARGV;
283 # look at each option; if we don't recognize it, maybe the suite-specific
284 # command line parsing code will...
286 while (@argv)
288 $option = shift @argv;
289 if ($option =~ /^-debug$/i)
291 print "\nDEBUG ON\n";
292 $debug = 1;
294 elsif ($option =~ /^-usage$/i)
296 &print_usage;
297 exit 0;
299 elsif ($option =~ /^-(h|help)$/i)
301 &print_help;
302 exit 0;
304 elsif ($option =~ /^-profile$/i)
306 $profile = 1;
308 elsif ($option =~ /^-verbose$/i)
310 $verbose = 1;
312 elsif ($option =~ /^-detail$/i)
314 $detail = 1;
315 $verbose = 1;
317 elsif ($option =~ /^-keep$/i)
319 $keep = 1;
321 elsif (&valid_option($option))
323 # The suite-defined subroutine takes care of the option
325 elsif ($option =~ /^-/)
327 print "Invalid option: $option\n";
328 &print_usage;
329 exit 0;
331 else # must be the name of a test
333 $option =~ s/\.pl$//;
334 push(@TESTS,$option);
339 sub max
341 local($num) = shift @_;
342 local($newnum);
344 while (@_)
346 $newnum = shift @_;
347 if ($newnum > $num)
349 $num = $newnum;
353 return $num;
356 sub print_centered
358 local($width, $string) = @_;
359 local($pad);
361 if (length ($string))
363 $pad = " " x ( ($width - length ($string) + 1) / 2);
364 print "$pad$string";
368 sub print_banner
370 local($info);
371 local($line);
372 local($len);
374 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
375 $len = &max (length ($line), length ($testee_version),
376 length ($banner_info), 73) + 5;
377 $line = ("-" x $len) . "\n";
378 if ($len < 78)
380 $len = 78;
383 &print_centered ($len, $line);
384 &print_centered ($len, $info);
385 &print_centered ($len, $testee_version); # suite-defined
386 &print_centered ($len, $banner_info); # suite-defined
387 &print_centered ($len, $line);
388 print "\n";
391 sub run_each_test
393 $categories_run = 0;
395 foreach $testname (sort @TESTS)
397 ++$categories_run;
398 $suite_passed = 1; # reset by test on failure
399 $num_of_logfiles = 0;
400 $num_of_tmpfiles = 0;
401 $description = "";
402 $details = "";
403 $old_makefile = undef;
404 $testname =~ s/^$scriptpath$pathsep//;
405 $perl_testname = "$scriptpath$pathsep$testname";
406 $testname =~ s/(\.pl|\.perl)$//;
407 $testpath = "$workpath$pathsep$testname";
408 # Leave enough space in the extensions to append a number, even
409 # though it needs to fit into 8+3 limits.
410 if ($short_filenames) {
411 $logext = 'l';
412 $diffext = 'd';
413 $baseext = 'b';
414 $extext = '';
416 else {
417 $logext = 'log';
418 $diffext = 'diff';
419 $baseext = 'base';
420 $extext = '.';
422 $log_filename = "$testpath.$logext";
423 $diff_filename = "$testpath.$diffext";
424 $base_filename = "$testpath.$baseext";
425 $tmp_filename = "$testpath.$tmpfilesuffix";
427 &setup_for_test; # suite-defined
429 $output = "........................................................ ";
431 substr($output,0,length($testname)) = "$testname ";
433 print $output;
435 # Run the actual test!
436 $tests_run = 0;
437 $tests_passed = 0;
438 $code = do $perl_testname;
440 $total_tests_run += $tests_run;
441 $total_tests_passed += $tests_passed;
443 # How did it go?
444 if (!defined($code))
446 $suite_passed = 0;
447 if (length ($@))
449 warn "\n*** Test died ($testname): $@\n";
451 else
453 warn "\n*** Couldn't run $perl_testname\n";
456 elsif ($code == -1) {
457 $suite_passed = 0;
459 elsif ($code != 1 && $code != -1) {
460 $suite_passed = 0;
461 warn "\n*** Test returned $code\n";
464 if ($suite_passed) {
465 ++$categories_passed;
466 $status = "ok ($tests_passed passed)";
467 for ($i = $num_of_tmpfiles; $i; $i--)
469 &rmfiles ($tmp_filename . &num_suffix ($i) );
472 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--)
474 &rmfiles ($log_filename . &num_suffix ($i) );
475 &rmfiles ($base_filename . &num_suffix ($i) );
478 elsif ($code > 0) {
479 $status = "FAILED ($tests_passed/$tests_run passed)";
481 elsif ($code < 0) {
482 $status = "N/A";
483 --$categories_run;
486 # If the verbose option has been specified, then a short description
487 # of each test is printed before displaying the results of each test
488 # describing WHAT is being tested.
490 if ($verbose)
492 if ($detail)
494 print "\nWHAT IS BEING TESTED\n";
495 print "--------------------";
497 print "\n\n$description\n\n";
500 # If the detail option has been specified, then the details of HOW
501 # the test is testing what it says it is testing in the verbose output
502 # will be displayed here before the results of the test are displayed.
504 if ($detail)
506 print "\nHOW IT IS TESTED\n";
507 print "----------------";
508 print "\n\n$details\n\n";
511 print "$status\n";
515 # If the keep flag is not set, this subroutine deletes all filenames that
516 # are sent to it.
518 sub rmfiles
520 local(@files) = @_;
522 if (!$keep)
524 return (unlink @files);
527 return 1;
530 sub print_standard_usage
532 local($plname,@moreusage) = @_;
533 local($line);
535 print "Usage: perl $plname [testname] [-verbose] [-detail] [-keep]\n";
536 print " [-profile] [-usage] [-help] "
537 . "[-debug]\n";
538 foreach $line (@moreusage)
540 print " $line\n";
544 sub print_standard_help
546 local(@morehelp) = @_;
547 local($line);
548 local($tline);
549 local($t) = " ";
551 $line = "Test Driver For $testee";
552 print "$line\n";
553 $line = "=" x length ($line);
554 print "$line\n";
556 &print_usage;
558 print "\ntestname\n"
559 . "${t}You may, if you wish, run only ONE test if you know the name\n"
560 . "${t}of that test and specify this name anywhere on the command\n"
561 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
562 . "${t}will be run.\n"
563 . "-verbose\n"
564 . "${t}If this option is given, a description of every test is\n"
565 . "${t}displayed before the test is run. (Not all tests may have\n"
566 . "${t}descriptions at this time)\n"
567 . "-detail\n"
568 . "${t}If this option is given, a detailed description of every\n"
569 . "${t}test is displayed before the test is run. (Not all tests\n"
570 . "${t}have descriptions at this time)\n"
571 . "-profile\n"
572 . "${t}If this option is given, then the profile file\n"
573 . "${t}is added to other profiles every time $testee is run.\n"
574 . "${t}This option only works on VOS at this time.\n"
575 . "-keep\n"
576 . "${t}You may give this option if you DO NOT want ANY\n"
577 . "${t}of the files generated by the tests to be deleted. \n"
578 . "${t}Without this option, all files generated by the test will\n"
579 . "${t}be deleted IF THE TEST PASSES.\n"
580 . "-debug\n"
581 . "${t}Use this option if you would like to see all of the system\n"
582 . "${t}calls issued and their return status while running the tests\n"
583 . "${t}This can be helpful if you're having a problem adding a test\n"
584 . "${t}to the suite, or if the test fails!\n";
586 foreach $line (@morehelp)
588 $tline = $line;
589 if (substr ($tline, 0, 1) eq "\t")
591 substr ($tline, 0, 1) = $t;
593 print "$tline\n";
597 #######################################################################
598 ########### Generic Test Driver Subroutines ###########
599 #######################################################################
601 sub get_caller
603 local($depth);
604 local($package);
605 local($filename);
606 local($linenum);
608 $depth = defined ($_[0]) ? $_[0] : 1;
609 ($package, $filename, $linenum) = caller ($depth + 1);
610 return "$filename: $linenum";
613 sub error
615 local($message) = $_[0];
616 local($caller) = &get_caller (1);
618 if (defined ($_[1]))
620 $caller = &get_caller ($_[1] + 1) . " -> $caller";
623 die "$caller: $message";
626 sub compare_output
628 local($answer,$logfile) = @_;
629 local($slurp, $answer_matched) = ('', 0);
631 print "Comparing Output ........ " if $debug;
633 $slurp = &read_file_into_string ($logfile);
635 # For make, get rid of any time skew error before comparing--too bad this
636 # has to go into the "generic" driver code :-/
637 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
638 $slurp =~ s/^.*Clock skew detected.*\n//gm;
640 ++$tests_run;
642 if ($slurp eq $answer) {
643 $answer_matched = 1;
644 } else {
645 # See if it is a slash or CRLF problem
646 local ($answer_mod) = $answer;
648 $answer_mod =~ tr,\\,/,;
649 $answer_mod =~ s,\r\n,\n,gs;
651 $slurp =~ tr,\\,/,;
652 $slurp =~ s,\r\n,\n,gs;
654 $answer_matched = ($slurp eq $answer_mod);
657 if ($answer_matched && $test_passed)
659 print "ok\n" if $debug;
660 ++$tests_passed;
661 return 1;
664 if (! $answer_matched) {
665 print "DIFFERENT OUTPUT\n" if $debug;
667 &create_file (&get_basefile, $answer);
669 print "\nCreating Difference File ...\n" if $debug;
671 # Create the difference file
673 local($command) = "diff -c " . &get_basefile . " " . $logfile;
674 &run_command_with_output(&get_difffile,$command);
677 $suite_passed = 0;
678 return 0;
681 sub read_file_into_string
683 local($filename) = @_;
684 local($oldslash) = $/;
686 undef $/;
688 open (RFISFILE, $filename) || return "";
689 local ($slurp) = <RFISFILE>;
690 close (RFISFILE);
692 $/ = $oldslash;
694 return $slurp;
697 sub attach_default_output
699 local ($filename) = @_;
700 local ($code);
702 if ($vos)
704 $code = system "++attach_default_output_hack $filename";
705 $code == -2 || &error ("adoh death\n", 1);
706 return 1;
709 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
710 || &error ("ado: $! duping STDOUT\n", 1);
711 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
712 || &error ("ado: $! duping STDERR\n", 1);
714 open (STDOUT, "> " . $filename)
715 || &error ("ado: $filename: $!\n", 1);
716 open (STDERR, ">&STDOUT")
717 || &error ("ado: $filename: $!\n", 1);
719 $default_output_stack_level++;
722 # close the current stdout/stderr, and restore the previous ones from
723 # the "stack."
725 sub detach_default_output
727 local ($code);
729 if ($vos)
731 $code = system "++detach_default_output_hack";
732 $code == -2 || &error ("ddoh death\n", 1);
733 return 1;
736 if (--$default_output_stack_level < 0)
738 &error ("default output stack has flown under!\n", 1);
741 close (STDOUT);
742 close (STDERR);
744 open (STDOUT, ">&SAVEDOS" . $default_output_stack_level . "out")
745 || &error ("ddo: $! duping STDOUT\n", 1);
746 open (STDERR, ">&SAVEDOS" . $default_output_stack_level . "err")
747 || &error ("ddo: $! duping STDERR\n", 1);
749 close ("SAVEDOS" . $default_output_stack_level . "out")
750 || &error ("ddo: $! closing SCSDOSout\n", 1);
751 close ("SAVEDOS" . $default_output_stack_level . "err")
752 || &error ("ddo: $! closing SAVEDOSerr\n", 1);
755 # run one command (passed as a list of arg 0 - n), returning 0 on success
756 # and nonzero on failure.
758 sub run_command
760 local ($code);
762 # We reset this before every invocation. On Windows I think there is only
763 # one environment, not one per process, so I think that variables set in
764 # test scripts might leak into subsequent tests if this isn't reset--???
765 resetENV();
767 print "\nrun_command: @_\n" if $debug;
768 $code = system @_;
769 print "run_command: \"@_\" returned $code.\n" if $debug;
771 return $code;
774 # run one command (passed as a list of arg 0 - n, with arg 0 being the
775 # second arg to this routine), returning 0 on success and non-zero on failure.
776 # The first arg to this routine is a filename to connect to the stdout
777 # & stderr of the child process.
779 sub run_command_with_output
781 local ($filename) = shift;
782 local ($code);
784 # We reset this before every invocation. On Windows I think there is only
785 # one environment, not one per process, so I think that variables set in
786 # test scripts might leak into subsequent tests if this isn't reset--???
787 resetENV();
789 &attach_default_output ($filename);
790 $code = system @_;
791 &detach_default_output;
793 print "run_command_with_output: '@_' returned $code.\n" if $debug;
795 return $code;
798 # performs the equivalent of an "rm -rf" on the first argument. Like
799 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
800 # deletes it, too.
802 sub remove_directory_tree
804 local ($targetdir) = @_;
805 local ($nuketop) = 1;
806 local ($ch);
808 $ch = substr ($targetdir, length ($targetdir) - 1);
809 if ($ch eq "/" || $ch eq $pathsep)
811 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
812 $nuketop = 0;
815 if (! -e $targetdir)
817 return 1;
820 &remove_directory_tree_inner ("RDT00", $targetdir) || return 0;
821 if ($nuketop)
823 rmdir $targetdir || return 0;
826 return 1;
829 sub remove_directory_tree_inner
831 local ($dirhandle, $targetdir) = @_;
832 local ($object);
833 local ($subdirhandle);
835 opendir ($dirhandle, $targetdir) || return 0;
836 $subdirhandle = $dirhandle;
837 $subdirhandle++;
838 while ($object = readdir ($dirhandle))
840 if ($object =~ /^(\.\.?|CVS|RCS)$/)
842 next;
845 $object = "$targetdir$pathsep$object";
846 lstat ($object);
848 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object))
850 rmdir $object || return 0;
852 else
854 unlink $object || return 0;
857 closedir ($dirhandle);
858 return 1;
861 # We used to use this behavior for this function:
863 #sub touch
865 # local (@filenames) = @_;
866 # local ($now) = time;
867 # local ($file);
869 # foreach $file (@filenames)
871 # utime ($now, $now, $file)
872 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
873 # || &error ("Couldn't touch $file: $!\n", 1);
875 # return 1;
878 # But this behaves badly on networked filesystems where the time is
879 # skewed, because it sets the time of the file based on the _local_
880 # host. Normally when you modify a file, it's the _remote_ host that
881 # determines the modtime, based on _its_ clock. So, instead, now we open
882 # the file and write something into it to force the remote host to set
883 # the modtime correctly according to its clock.
886 sub touch
888 local ($file);
890 foreach $file (@_) {
891 (open(T, ">> $file") && print(T "\n") && close(T))
892 || &error("Couldn't touch $file: $!\n", 1);
896 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
897 # access/mod time for each file and apply the offset.
899 sub utouch
901 local ($off) = shift;
902 local ($file);
904 &touch(@_);
906 local (@s) = stat($_[0]);
908 utime($s[8]+$off, $s[9]+$off, @_);
911 # open a file, write some stuff to it, and close it.
913 sub create_file
915 local ($filename, @lines) = @_;
917 open (CF, "> $filename") || &error ("Couldn't open $filename: $!\n", 1);
918 foreach $line (@lines)
920 print CF $line;
922 close (CF);
925 # create a directory tree described by an associative array, wherein each
926 # key is a relative pathname (using slashes) and its associated value is
927 # one of:
928 # DIR indicates a directory
929 # FILE:contents indicates a file, which should contain contents +\n
930 # LINK:target indicates a symlink, pointing to $basedir/target
931 # The first argument is the dir under which the structure will be created
932 # (the dir will be made and/or cleaned if necessary); the second argument
933 # is the associative array.
935 sub create_dir_tree
937 local ($basedir, %dirtree) = @_;
938 local ($path);
940 &remove_directory_tree ("$basedir");
941 mkdir ($basedir, 0777) || &error ("Couldn't mkdir $basedir: $!\n", 1);
943 foreach $path (sort keys (%dirtree))
945 if ($dirtree {$path} =~ /^DIR$/)
947 mkdir ("$basedir/$path", 0777)
948 || &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
950 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
952 &create_file ("$basedir/$path", $1 . "\n");
954 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
956 symlink ("$basedir/$1", "$basedir/$path")
957 || &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
959 else
961 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
964 if ($just_setup_tree)
966 die "Tree is setup...\n";
970 # compare a directory tree with an associative array in the format used
971 # by create_dir_tree, above.
972 # The first argument is the dir under which the structure should be found;
973 # the second argument is the associative array.
975 sub compare_dir_tree
977 local ($basedir, %dirtree) = @_;
978 local ($path);
979 local ($i);
980 local ($bogus) = 0;
981 local ($contents);
982 local ($target);
983 local ($fulltarget);
984 local ($found);
985 local (@files);
986 local (@allfiles);
988 opendir (DIR, $basedir) || &error ("Couldn't open $basedir: $!\n", 1);
989 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
990 closedir (DIR);
991 if ($debug)
993 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
996 foreach $path (sort keys (%dirtree))
998 if ($debug)
1000 print "Checking $path ($dirtree{$path}).\n";
1003 $found = 0;
1004 foreach $i (0 .. $#allfiles)
1006 if ($allfiles[$i] eq $path)
1008 splice (@allfiles, $i, 1); # delete it
1009 if ($debug)
1011 print " Zapped $path; files now (@allfiles).\n";
1013 lstat ("$basedir/$path");
1014 $found = 1;
1015 last;
1019 if (!$found)
1021 print "compare_dir_tree: $path does not exist.\n";
1022 $bogus = 1;
1023 next;
1026 if ($dirtree {$path} =~ /^DIR$/)
1028 if (-d _ && opendir (DIR, "$basedir/$path") )
1030 @files = readdir (DIR);
1031 closedir (DIR);
1032 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1033 push (@allfiles, @files);
1034 if ($debug)
1036 print " Read in $path; new files (@files).\n";
1039 else
1041 print "compare_dir_tree: $path is not a dir.\n";
1042 $bogus = 1;
1045 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1047 if (-l _ || !-f _)
1049 print "compare_dir_tree: $path is not a file.\n";
1050 $bogus = 1;
1051 next;
1054 if ($1 ne "*")
1056 $contents = &read_file_into_string ("$basedir/$path");
1057 if ($contents ne "$1\n")
1059 print "compare_dir_tree: $path contains wrong stuff."
1060 . " Is:\n$contentsShould be:\n$1\n";
1061 $bogus = 1;
1065 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1067 $target = $1;
1068 if (!-l _)
1070 print "compare_dir_tree: $path is not a link.\n";
1071 $bogus = 1;
1072 next;
1075 $contents = readlink ("$basedir/$path");
1076 $contents =~ tr/>/\//;
1077 $fulltarget = "$basedir/$target";
1078 $fulltarget =~ tr/>/\//;
1079 if (!($contents =~ /$fulltarget$/))
1081 if ($debug)
1083 $target = $fulltarget;
1085 print "compare_dir_tree: $path should be link to $target, "
1086 . "not $contents.\n";
1087 $bogus = 1;
1090 else
1092 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1096 if ($debug)
1098 print "leftovers: (@allfiles).\n";
1101 foreach $file (@allfiles)
1103 print "compare_dir_tree: $file should not exist.\n";
1104 $bogus = 1;
1107 return !$bogus;
1110 # this subroutine generates the numeric suffix used to keep tmp filenames,
1111 # log filenames, etc., unique. If the number passed in is 1, then a null
1112 # string is returned; otherwise, we return ".n", where n + 1 is the number
1113 # we were given.
1115 sub num_suffix
1117 local($num) = @_;
1119 if (--$num > 0) {
1120 return "$extext$num";
1123 return "";
1126 # This subroutine returns a log filename with a number appended to
1127 # the end corresponding to how many logfiles have been created in the
1128 # current running test. An optional parameter may be passed (0 or 1).
1129 # If a 1 is passed, then it does NOT increment the logfile counter
1130 # and returns the name of the latest logfile. If either no parameter
1131 # is passed at all or a 0 is passed, then the logfile counter is
1132 # incremented and the new name is returned.
1134 sub get_logfile
1136 local($no_increment) = @_;
1138 $num_of_logfiles += !$no_increment;
1140 return ($log_filename . &num_suffix ($num_of_logfiles));
1143 # This subroutine returns a base (answer) filename with a number
1144 # appended to the end corresponding to how many logfiles (and thus
1145 # base files) have been created in the current running test.
1146 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1148 sub get_basefile
1150 return ($base_filename . &num_suffix ($num_of_logfiles));
1153 # This subroutine returns a difference filename with a number appended
1154 # to the end corresponding to how many logfiles (and thus diff files)
1155 # have been created in the current running test.
1157 sub get_difffile
1159 return ($diff_filename . &num_suffix ($num_of_logfiles));
1162 # just like logfile, only a generic tmp filename for use by the test.
1163 # they are automatically cleaned up unless -keep was used, or the test fails.
1164 # Pass an argument of 1 to return the same filename as the previous call.
1166 sub get_tmpfile
1168 local($no_increment) = @_;
1170 $num_of_tmpfiles += !$no_increment;
1172 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));