- New code capability: a read-only string cache. Start of solution for
[make.git] / tests / test_driver.pl
blob9a809c65f2db5409ecabf887782efcaeaf8f30e3
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.16 2006/02/10 05:29:00 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 =~ /^(\..*|CVS|RCS)$/ || ! -d "$scriptpath/$dir");
156 push (@rmdirs, $dir);
157 mkdir ("$workpath/$dir", 0777)
158 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
159 opendir (SCRIPTDIR, "$scriptpath/$dir")
160 || &error ("Couldn't opendir $scriptpath/$dir: $!\n");
161 @files = grep (!/^(\..*|CVS|RCS|.*~)$/, readdir (SCRIPTDIR) );
162 closedir (SCRIPTDIR);
163 foreach $test (@files)
165 -d $test and next;
166 push (@TESTS, "$dir/$test");
171 if (@TESTS == 0)
173 &error ("\nNo tests in $scriptpath, and none were specified.\n");
176 print "\n";
178 &run_each_test;
180 foreach $dir (@rmdirs)
182 rmdir ("$workpath/$dir");
185 $| = 1;
187 $categories_failed = $categories_run - $categories_passed;
188 $total_tests_failed = $total_tests_run - $total_tests_passed;
190 if ($total_tests_failed)
192 print "\n$total_tests_failed Test";
193 print "s" unless $total_tests_failed == 1;
194 print " in $categories_failed Categor";
195 print ($categories_failed == 1 ? "y" : "ies");
196 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
197 return 0;
199 else
201 print "\n$total_tests_passed Test";
202 print "s" unless $total_tests_passed == 1;
203 print " in $categories_passed Categor";
204 print ($categories_passed == 1 ? "y" : "ies");
205 print " Complete ... No Failures :-)\n\n";
206 return 1;
210 sub get_osname
212 # Set up an initial value. In perl5 we can do it the easy way.
214 $osname = defined($^O) ? $^O : '';
216 # See if the filesystem supports long file names with multiple
217 # dots. DOS doesn't.
218 $short_filenames = 0;
219 (open (TOUCHFD, "> fancy.file.name") && close (TOUCHFD))
220 || ($short_filenames = 1);
221 unlink ("fancy.file.name") || ($short_filenames = 1);
223 if (! $short_filenames) {
224 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
225 # better way of doing this. (We used to test for existence of a /mnt
226 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
227 # Because perl on VOS translates /'s to >'s, we need to test for
228 # VOSness rather than testing for Unixness (ie, try > instead of /).
230 mkdir (".ostest", 0777) || &error ("Couldn't create .ostest: $!\n", 1);
231 open (TOUCHFD, "> .ostest>ick") && close (TOUCHFD);
232 chdir (".ostest") || &error ("Couldn't chdir to .ostest: $!\n", 1);
235 if (! $short_filenames && -f "ick")
237 $osname = "vos";
238 $vos = 1;
239 $pathsep = ">";
241 else
243 # the following is regrettably knarly, but it seems to be the only way
244 # to not get ugly error messages if uname can't be found.
245 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
246 # with switches first.
247 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
248 if ($osname =~ /not found/i)
250 $osname = "(something unixy with no uname)";
252 elsif ($@ ne "" || $?)
254 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
255 if ($@ ne "" || $?)
257 $osname = "(something unixy)";
260 $vos = 0;
261 $pathsep = "/";
264 if (! $short_filenames) {
265 chdir ("..") || &error ("Couldn't chdir to ..: $!\n", 1);
266 unlink (".ostest>ick");
267 rmdir (".ostest") || &error ("Couldn't rmdir .ostest: $!\n", 1);
271 sub parse_command_line
273 @argv = @_;
275 # use @ARGV if no args were passed in
277 if (@argv == 0)
279 @argv = @ARGV;
282 # look at each option; if we don't recognize it, maybe the suite-specific
283 # command line parsing code will...
285 while (@argv)
287 $option = shift @argv;
288 if ($option =~ /^-debug$/i)
290 print "\nDEBUG ON\n";
291 $debug = 1;
293 elsif ($option =~ /^-usage$/i)
295 &print_usage;
296 exit 0;
298 elsif ($option =~ /^-(h|help)$/i)
300 &print_help;
301 exit 0;
303 elsif ($option =~ /^-profile$/i)
305 $profile = 1;
307 elsif ($option =~ /^-verbose$/i)
309 $verbose = 1;
311 elsif ($option =~ /^-detail$/i)
313 $detail = 1;
314 $verbose = 1;
316 elsif ($option =~ /^-keep$/i)
318 $keep = 1;
320 elsif (&valid_option($option))
322 # The suite-defined subroutine takes care of the option
324 elsif ($option =~ /^-/)
326 print "Invalid option: $option\n";
327 &print_usage;
328 exit 0;
330 else # must be the name of a test
332 $option =~ s/\.pl$//;
333 push(@TESTS,$option);
338 sub max
340 local($num) = shift @_;
341 local($newnum);
343 while (@_)
345 $newnum = shift @_;
346 if ($newnum > $num)
348 $num = $newnum;
352 return $num;
355 sub print_centered
357 local($width, $string) = @_;
358 local($pad);
360 if (length ($string))
362 $pad = " " x ( ($width - length ($string) + 1) / 2);
363 print "$pad$string";
367 sub print_banner
369 local($info);
370 local($line);
371 local($len);
373 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
374 $len = &max (length ($line), length ($testee_version),
375 length ($banner_info), 73) + 5;
376 $line = ("-" x $len) . "\n";
377 if ($len < 78)
379 $len = 78;
382 &print_centered ($len, $line);
383 &print_centered ($len, $info);
384 &print_centered ($len, $testee_version); # suite-defined
385 &print_centered ($len, $banner_info); # suite-defined
386 &print_centered ($len, $line);
387 print "\n";
390 sub run_each_test
392 $categories_run = 0;
394 foreach $testname (sort @TESTS)
396 ++$categories_run;
397 $suite_passed = 1; # reset by test on failure
398 $num_of_logfiles = 0;
399 $num_of_tmpfiles = 0;
400 $description = "";
401 $details = "";
402 $old_makefile = undef;
403 $testname =~ s/^$scriptpath$pathsep//;
404 $perl_testname = "$scriptpath$pathsep$testname";
405 $testname =~ s/(\.pl|\.perl)$//;
406 $testpath = "$workpath$pathsep$testname";
407 # Leave enough space in the extensions to append a number, even
408 # though it needs to fit into 8+3 limits.
409 if ($short_filenames) {
410 $logext = 'l';
411 $diffext = 'd';
412 $baseext = 'b';
413 $extext = '';
415 else {
416 $logext = 'log';
417 $diffext = 'diff';
418 $baseext = 'base';
419 $extext = '.';
421 $log_filename = "$testpath.$logext";
422 $diff_filename = "$testpath.$diffext";
423 $base_filename = "$testpath.$baseext";
424 $tmp_filename = "$testpath.$tmpfilesuffix";
426 &setup_for_test; # suite-defined
428 $output = "........................................................ ";
430 substr($output,0,length($testname)) = "$testname ";
432 print $output;
434 # Run the actual test!
435 $tests_run = 0;
436 $tests_passed = 0;
437 $code = do $perl_testname;
439 $total_tests_run += $tests_run;
440 $total_tests_passed += $tests_passed;
442 # How did it go?
443 if (!defined($code))
445 $suite_passed = 0;
446 if (length ($@))
448 warn "\n*** Test died ($testname): $@\n";
450 else
452 warn "\n*** Couldn't run $perl_testname\n";
455 elsif ($code == -1) {
456 $suite_passed = 0;
458 elsif ($code != 1 && $code != -1) {
459 $suite_passed = 0;
460 warn "\n*** Test returned $code\n";
463 if ($suite_passed) {
464 ++$categories_passed;
465 $status = "ok ($tests_passed passed)";
466 for ($i = $num_of_tmpfiles; $i; $i--)
468 &rmfiles ($tmp_filename . &num_suffix ($i) );
471 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--)
473 &rmfiles ($log_filename . &num_suffix ($i) );
474 &rmfiles ($base_filename . &num_suffix ($i) );
477 elsif ($code > 0) {
478 $status = "FAILED ($tests_passed/$tests_run passed)";
480 elsif ($code < 0) {
481 $status = "N/A";
482 --$categories_run;
485 # If the verbose option has been specified, then a short description
486 # of each test is printed before displaying the results of each test
487 # describing WHAT is being tested.
489 if ($verbose)
491 if ($detail)
493 print "\nWHAT IS BEING TESTED\n";
494 print "--------------------";
496 print "\n\n$description\n\n";
499 # If the detail option has been specified, then the details of HOW
500 # the test is testing what it says it is testing in the verbose output
501 # will be displayed here before the results of the test are displayed.
503 if ($detail)
505 print "\nHOW IT IS TESTED\n";
506 print "----------------";
507 print "\n\n$details\n\n";
510 print "$status\n";
514 # If the keep flag is not set, this subroutine deletes all filenames that
515 # are sent to it.
517 sub rmfiles
519 local(@files) = @_;
521 if (!$keep)
523 return (unlink @files);
526 return 1;
529 sub print_standard_usage
531 local($plname,@moreusage) = @_;
532 local($line);
534 print "Usage: perl $plname [testname] [-verbose] [-detail] [-keep]\n";
535 print " [-profile] [-usage] [-help] "
536 . "[-debug]\n";
537 foreach $line (@moreusage)
539 print " $line\n";
543 sub print_standard_help
545 local(@morehelp) = @_;
546 local($line);
547 local($tline);
548 local($t) = " ";
550 $line = "Test Driver For $testee";
551 print "$line\n";
552 $line = "=" x length ($line);
553 print "$line\n";
555 &print_usage;
557 print "\ntestname\n"
558 . "${t}You may, if you wish, run only ONE test if you know the name\n"
559 . "${t}of that test and specify this name anywhere on the command\n"
560 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
561 . "${t}will be run.\n"
562 . "-verbose\n"
563 . "${t}If this option is given, a description of every test is\n"
564 . "${t}displayed before the test is run. (Not all tests may have\n"
565 . "${t}descriptions at this time)\n"
566 . "-detail\n"
567 . "${t}If this option is given, a detailed description of every\n"
568 . "${t}test is displayed before the test is run. (Not all tests\n"
569 . "${t}have descriptions at this time)\n"
570 . "-profile\n"
571 . "${t}If this option is given, then the profile file\n"
572 . "${t}is added to other profiles every time $testee is run.\n"
573 . "${t}This option only works on VOS at this time.\n"
574 . "-keep\n"
575 . "${t}You may give this option if you DO NOT want ANY\n"
576 . "${t}of the files generated by the tests to be deleted. \n"
577 . "${t}Without this option, all files generated by the test will\n"
578 . "${t}be deleted IF THE TEST PASSES.\n"
579 . "-debug\n"
580 . "${t}Use this option if you would like to see all of the system\n"
581 . "${t}calls issued and their return status while running the tests\n"
582 . "${t}This can be helpful if you're having a problem adding a test\n"
583 . "${t}to the suite, or if the test fails!\n";
585 foreach $line (@morehelp)
587 $tline = $line;
588 if (substr ($tline, 0, 1) eq "\t")
590 substr ($tline, 0, 1) = $t;
592 print "$tline\n";
596 #######################################################################
597 ########### Generic Test Driver Subroutines ###########
598 #######################################################################
600 sub get_caller
602 local($depth);
603 local($package);
604 local($filename);
605 local($linenum);
607 $depth = defined ($_[0]) ? $_[0] : 1;
608 ($package, $filename, $linenum) = caller ($depth + 1);
609 return "$filename: $linenum";
612 sub error
614 local($message) = $_[0];
615 local($caller) = &get_caller (1);
617 if (defined ($_[1]))
619 $caller = &get_caller ($_[1] + 1) . " -> $caller";
622 die "$caller: $message";
625 sub compare_output
627 local($answer,$logfile) = @_;
628 local($slurp, $answer_matched) = ('', 0);
630 print "Comparing Output ........ " if $debug;
632 $slurp = &read_file_into_string ($logfile);
634 # For make, get rid of any time skew error before comparing--too bad this
635 # has to go into the "generic" driver code :-/
636 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
637 $slurp =~ s/^.*Clock skew detected.*\n//gm;
639 ++$tests_run;
641 if ($slurp eq $answer) {
642 $answer_matched = 1;
643 } else {
644 # See if it is a slash or CRLF problem
645 local ($answer_mod) = $answer;
647 $answer_mod =~ tr,\\,/,;
648 $answer_mod =~ s,\r\n,\n,gs;
650 $slurp =~ tr,\\,/,;
651 $slurp =~ s,\r\n,\n,gs;
653 $answer_matched = ($slurp eq $answer_mod);
656 if ($answer_matched && $test_passed)
658 print "ok\n" if $debug;
659 ++$tests_passed;
660 return 1;
663 if (! $answer_matched) {
664 print "DIFFERENT OUTPUT\n" if $debug;
666 &create_file (&get_basefile, $answer);
668 print "\nCreating Difference File ...\n" if $debug;
670 # Create the difference file
672 local($command) = "diff -c " . &get_basefile . " " . $logfile;
673 &run_command_with_output(&get_difffile,$command);
676 $suite_passed = 0;
677 return 0;
680 sub read_file_into_string
682 local($filename) = @_;
683 local($oldslash) = $/;
685 undef $/;
687 open (RFISFILE, $filename) || return "";
688 local ($slurp) = <RFISFILE>;
689 close (RFISFILE);
691 $/ = $oldslash;
693 return $slurp;
696 sub attach_default_output
698 local ($filename) = @_;
699 local ($code);
701 if ($vos)
703 $code = system "++attach_default_output_hack $filename";
704 $code == -2 || &error ("adoh death\n", 1);
705 return 1;
708 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
709 || &error ("ado: $! duping STDOUT\n", 1);
710 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
711 || &error ("ado: $! duping STDERR\n", 1);
713 open (STDOUT, "> " . $filename)
714 || &error ("ado: $filename: $!\n", 1);
715 open (STDERR, ">&STDOUT")
716 || &error ("ado: $filename: $!\n", 1);
718 $default_output_stack_level++;
721 # close the current stdout/stderr, and restore the previous ones from
722 # the "stack."
724 sub detach_default_output
726 local ($code);
728 if ($vos)
730 $code = system "++detach_default_output_hack";
731 $code == -2 || &error ("ddoh death\n", 1);
732 return 1;
735 if (--$default_output_stack_level < 0)
737 &error ("default output stack has flown under!\n", 1);
740 close (STDOUT);
741 close (STDERR);
743 open (STDOUT, ">&SAVEDOS" . $default_output_stack_level . "out")
744 || &error ("ddo: $! duping STDOUT\n", 1);
745 open (STDERR, ">&SAVEDOS" . $default_output_stack_level . "err")
746 || &error ("ddo: $! duping STDERR\n", 1);
748 close ("SAVEDOS" . $default_output_stack_level . "out")
749 || &error ("ddo: $! closing SCSDOSout\n", 1);
750 close ("SAVEDOS" . $default_output_stack_level . "err")
751 || &error ("ddo: $! closing SAVEDOSerr\n", 1);
754 # run one command (passed as a list of arg 0 - n), returning 0 on success
755 # and nonzero on failure.
757 sub run_command
759 local ($code);
761 # We reset this before every invocation. On Windows I think there is only
762 # one environment, not one per process, so I think that variables set in
763 # test scripts might leak into subsequent tests if this isn't reset--???
764 resetENV();
766 print "\nrun_command: @_\n" if $debug;
767 $code = system @_;
768 print "run_command: \"@_\" returned $code.\n" if $debug;
770 return $code;
773 # run one command (passed as a list of arg 0 - n, with arg 0 being the
774 # second arg to this routine), returning 0 on success and non-zero on failure.
775 # The first arg to this routine is a filename to connect to the stdout
776 # & stderr of the child process.
778 sub run_command_with_output
780 local ($filename) = shift;
781 local ($code);
783 # We reset this before every invocation. On Windows I think there is only
784 # one environment, not one per process, so I think that variables set in
785 # test scripts might leak into subsequent tests if this isn't reset--???
786 resetENV();
788 &attach_default_output ($filename);
789 $code = system @_;
790 &detach_default_output;
792 print "run_command_with_output: '@_' returned $code.\n" if $debug;
794 return $code;
797 # performs the equivalent of an "rm -rf" on the first argument. Like
798 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
799 # deletes it, too.
801 sub remove_directory_tree
803 local ($targetdir) = @_;
804 local ($nuketop) = 1;
805 local ($ch);
807 $ch = substr ($targetdir, length ($targetdir) - 1);
808 if ($ch eq "/" || $ch eq $pathsep)
810 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
811 $nuketop = 0;
814 if (! -e $targetdir)
816 return 1;
819 &remove_directory_tree_inner ("RDT00", $targetdir) || return 0;
820 if ($nuketop)
822 rmdir $targetdir || return 0;
825 return 1;
828 sub remove_directory_tree_inner
830 local ($dirhandle, $targetdir) = @_;
831 local ($object);
832 local ($subdirhandle);
834 opendir ($dirhandle, $targetdir) || return 0;
835 $subdirhandle = $dirhandle;
836 $subdirhandle++;
837 while ($object = readdir ($dirhandle))
839 if ($object =~ /^(\.\.?|CVS|RCS)$/)
841 next;
844 $object = "$targetdir$pathsep$object";
845 lstat ($object);
847 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object))
849 rmdir $object || return 0;
851 else
853 unlink $object || return 0;
856 closedir ($dirhandle);
857 return 1;
860 # We used to use this behavior for this function:
862 #sub touch
864 # local (@filenames) = @_;
865 # local ($now) = time;
866 # local ($file);
868 # foreach $file (@filenames)
870 # utime ($now, $now, $file)
871 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
872 # || &error ("Couldn't touch $file: $!\n", 1);
874 # return 1;
877 # But this behaves badly on networked filesystems where the time is
878 # skewed, because it sets the time of the file based on the _local_
879 # host. Normally when you modify a file, it's the _remote_ host that
880 # determines the modtime, based on _its_ clock. So, instead, now we open
881 # the file and write something into it to force the remote host to set
882 # the modtime correctly according to its clock.
885 sub touch
887 local ($file);
889 foreach $file (@_) {
890 (open(T, ">> $file") && print(T "\n") && close(T))
891 || &error("Couldn't touch $file: $!\n", 1);
895 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
896 # access/mod time for each file and apply the offset.
898 sub utouch
900 local ($off) = shift;
901 local ($file);
903 &touch(@_);
905 local (@s) = stat($_[0]);
907 utime($s[8]+$off, $s[9]+$off, @_);
910 # open a file, write some stuff to it, and close it.
912 sub create_file
914 local ($filename, @lines) = @_;
916 open (CF, "> $filename") || &error ("Couldn't open $filename: $!\n", 1);
917 foreach $line (@lines)
919 print CF $line;
921 close (CF);
924 # create a directory tree described by an associative array, wherein each
925 # key is a relative pathname (using slashes) and its associated value is
926 # one of:
927 # DIR indicates a directory
928 # FILE:contents indicates a file, which should contain contents +\n
929 # LINK:target indicates a symlink, pointing to $basedir/target
930 # The first argument is the dir under which the structure will be created
931 # (the dir will be made and/or cleaned if necessary); the second argument
932 # is the associative array.
934 sub create_dir_tree
936 local ($basedir, %dirtree) = @_;
937 local ($path);
939 &remove_directory_tree ("$basedir");
940 mkdir ($basedir, 0777) || &error ("Couldn't mkdir $basedir: $!\n", 1);
942 foreach $path (sort keys (%dirtree))
944 if ($dirtree {$path} =~ /^DIR$/)
946 mkdir ("$basedir/$path", 0777)
947 || &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
949 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
951 &create_file ("$basedir/$path", $1 . "\n");
953 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
955 symlink ("$basedir/$1", "$basedir/$path")
956 || &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
958 else
960 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
963 if ($just_setup_tree)
965 die "Tree is setup...\n";
969 # compare a directory tree with an associative array in the format used
970 # by create_dir_tree, above.
971 # The first argument is the dir under which the structure should be found;
972 # the second argument is the associative array.
974 sub compare_dir_tree
976 local ($basedir, %dirtree) = @_;
977 local ($path);
978 local ($i);
979 local ($bogus) = 0;
980 local ($contents);
981 local ($target);
982 local ($fulltarget);
983 local ($found);
984 local (@files);
985 local (@allfiles);
987 opendir (DIR, $basedir) || &error ("Couldn't open $basedir: $!\n", 1);
988 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
989 closedir (DIR);
990 if ($debug)
992 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
995 foreach $path (sort keys (%dirtree))
997 if ($debug)
999 print "Checking $path ($dirtree{$path}).\n";
1002 $found = 0;
1003 foreach $i (0 .. $#allfiles)
1005 if ($allfiles[$i] eq $path)
1007 splice (@allfiles, $i, 1); # delete it
1008 if ($debug)
1010 print " Zapped $path; files now (@allfiles).\n";
1012 lstat ("$basedir/$path");
1013 $found = 1;
1014 last;
1018 if (!$found)
1020 print "compare_dir_tree: $path does not exist.\n";
1021 $bogus = 1;
1022 next;
1025 if ($dirtree {$path} =~ /^DIR$/)
1027 if (-d _ && opendir (DIR, "$basedir/$path") )
1029 @files = readdir (DIR);
1030 closedir (DIR);
1031 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1032 push (@allfiles, @files);
1033 if ($debug)
1035 print " Read in $path; new files (@files).\n";
1038 else
1040 print "compare_dir_tree: $path is not a dir.\n";
1041 $bogus = 1;
1044 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1046 if (-l _ || !-f _)
1048 print "compare_dir_tree: $path is not a file.\n";
1049 $bogus = 1;
1050 next;
1053 if ($1 ne "*")
1055 $contents = &read_file_into_string ("$basedir/$path");
1056 if ($contents ne "$1\n")
1058 print "compare_dir_tree: $path contains wrong stuff."
1059 . " Is:\n$contentsShould be:\n$1\n";
1060 $bogus = 1;
1064 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1066 $target = $1;
1067 if (!-l _)
1069 print "compare_dir_tree: $path is not a link.\n";
1070 $bogus = 1;
1071 next;
1074 $contents = readlink ("$basedir/$path");
1075 $contents =~ tr/>/\//;
1076 $fulltarget = "$basedir/$target";
1077 $fulltarget =~ tr/>/\//;
1078 if (!($contents =~ /$fulltarget$/))
1080 if ($debug)
1082 $target = $fulltarget;
1084 print "compare_dir_tree: $path should be link to $target, "
1085 . "not $contents.\n";
1086 $bogus = 1;
1089 else
1091 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1095 if ($debug)
1097 print "leftovers: (@allfiles).\n";
1100 foreach $file (@allfiles)
1102 print "compare_dir_tree: $file should not exist.\n";
1103 $bogus = 1;
1106 return !$bogus;
1109 # this subroutine generates the numeric suffix used to keep tmp filenames,
1110 # log filenames, etc., unique. If the number passed in is 1, then a null
1111 # string is returned; otherwise, we return ".n", where n + 1 is the number
1112 # we were given.
1114 sub num_suffix
1116 local($num) = @_;
1118 if (--$num > 0) {
1119 return "$extext$num";
1122 return "";
1125 # This subroutine returns a log filename with a number appended to
1126 # the end corresponding to how many logfiles have been created in the
1127 # current running test. An optional parameter may be passed (0 or 1).
1128 # If a 1 is passed, then it does NOT increment the logfile counter
1129 # and returns the name of the latest logfile. If either no parameter
1130 # is passed at all or a 0 is passed, then the logfile counter is
1131 # incremented and the new name is returned.
1133 sub get_logfile
1135 local($no_increment) = @_;
1137 $num_of_logfiles += !$no_increment;
1139 return ($log_filename . &num_suffix ($num_of_logfiles));
1142 # This subroutine returns a base (answer) filename with a number
1143 # appended to the end corresponding to how many logfiles (and thus
1144 # base files) have been created in the current running test.
1145 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1147 sub get_basefile
1149 return ($base_filename . &num_suffix ($num_of_logfiles));
1152 # This subroutine returns a difference filename with a number appended
1153 # to the end corresponding to how many logfiles (and thus diff files)
1154 # have been created in the current running test.
1156 sub get_difffile
1158 return ($diff_filename . &num_suffix ($num_of_logfiles));
1161 # just like logfile, only a generic tmp filename for use by the test.
1162 # they are automatically cleaned up unless -keep was used, or the test fails.
1163 # Pass an argument of 1 to return the same filename as the previous call.
1165 sub get_tmpfile
1167 local($no_increment) = @_;
1169 $num_of_tmpfiles += !$no_increment;
1171 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));