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.
17 # Get a clean environment
21 # Pull in benign variables from the user's environment
23 foreach (# UNIX-specific things
24 'TZ', 'LANG', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
27 # Windows NT-specific stuff
29 # DJGPP-specific stuff
30 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
31 'FNCASE', '387', 'EMU387', 'GROUP'
33 $makeENV{$_} = $ENV{$_} if $ENV{$_};
36 # Replace the environment with the new one
40 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
41 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
42 # want to require that here, so just delete each one individually.
44 foreach $v (keys %ENV) {
50 $| = 1; # unbuffered output
52 $debug = 0; # debug flag
53 $profile = 0; # profiling flag
54 $verbose = 0; # verbose mode flag
55 $detail = 0; # detailed verbosity
56 $keep = 0; # keep temp files around
57 $workdir = "work"; # The directory where the test will start running
58 $scriptdir = "scripts"; # The directory where we find the test scripts
59 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
60 $default_output_stack_level = 0; # used by attach_default_output, etc.
61 $default_input_stack_level = 0; # used by attach_default_input, etc.
62 $cwd = "."; # don't we wish we knew
63 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
65 &get_osname
; # sets $osname, $vos, $pathsep, and $short_filenames
67 &set_defaults
; # suite-defined
69 &parse_command_line
(@ARGV);
71 print "OS name = `$osname'\n" if $debug;
73 $workpath = "$cwdslash$workdir";
74 $scriptpath = "$cwdslash$scriptdir";
76 &set_more_defaults
; # suite-defined
82 print "Clearing $workpath...\n";
83 &remove_directory_tree
("$workpath/")
84 || &error
("Couldn't wipe out $workpath\n");
88 mkdir ($workpath, 0777) || &error
("Couldn't mkdir $workpath: $!\n");
93 &error
("Failed to find $scriptpath containing perl test scripts.\n");
98 print "Making work dirs...\n";
99 foreach $test (@TESTS)
101 if ($test =~ /^([^\/]+)\
//)
104 push (@rmdirs, $dir);
106 || mkdir ("$workpath/$dir", 0777)
107 || &error
("Couldn't mkdir $workpath/$dir: $!\n");
113 print "Finding tests...\n";
114 opendir (SCRIPTDIR
, $scriptpath)
115 || &error
("Couldn't opendir $scriptpath: $!\n");
116 @dirs = grep (!/^(\.\.?|CVS|RCS)$/, readdir (SCRIPTDIR
) );
117 closedir (SCRIPTDIR
);
120 next if ($dir =~ /^\.\.?$/ || $dir eq 'CVS' || $dir eq 'RCS'
121 || ! -d
"$scriptpath/$dir");
122 push (@rmdirs, $dir);
123 mkdir ("$workpath/$dir", 0777)
124 || &error
("Couldn't mkdir $workpath/$dir: $!\n");
125 opendir (SCRIPTDIR
, "$scriptpath/$dir")
126 || &error
("Couldn't opendir $scriptpath/$dir: $!\n");
127 @files = grep (!/^(\.\.?|CVS|RCS)$/, readdir (SCRIPTDIR
) );
128 closedir (SCRIPTDIR
);
129 foreach $test (@files)
131 next if $test =~ /~$/ || -d
$test;
132 push (@TESTS, "$dir/$test");
139 &error
("\nNo tests in $scriptpath, and none were specified.\n");
146 foreach $dir (@rmdirs)
148 rmdir ("$workpath/$dir");
155 print "\n$num_failed Test";
156 print "s" unless $num_failed == 1;
157 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
162 print "\n$counter Test";
163 print "s" unless $counter == 1;
164 print " Complete ... No Failures :-)\n\n";
171 # Set up an initial value. In perl5 we can do it the easy way.
173 $osname = defined($^O
) ?
$^O
: '';
175 # See if the filesystem supports long file names with multiple
177 $short_filenames = 0;
178 (open (TOUCHFD
, "> fancy.file.name") && close (TOUCHFD
))
179 || ($short_filenames = 1);
180 unlink ("fancy.file.name") || ($short_filenames = 1);
182 if (! $short_filenames) {
183 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
184 # better way of doing this. (We used to test for existence of a /mnt
185 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
186 # Because perl on VOS translates /'s to >'s, we need to test for
187 # VOSness rather than testing for Unixness (ie, try > instead of /).
189 mkdir (".ostest", 0777) || &error
("Couldn't create .ostest: $!\n", 1);
190 open (TOUCHFD
, "> .ostest>ick") && close (TOUCHFD
);
191 chdir (".ostest") || &error
("Couldn't chdir to .ostest: $!\n", 1);
194 if (! $short_filenames && -f
"ick")
202 # the following is regrettably knarly, but it seems to be the only way
203 # to not get ugly error messages if uname can't be found.
204 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
205 # with switches first.
206 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
207 if ($osname =~ /not found/i)
209 $osname = "(something unixy with no uname)";
211 elsif ($@
ne "" || $?
)
213 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
216 $osname = "(something unixy)";
223 if (! $short_filenames) {
224 chdir ("..") || &error
("Couldn't chdir to ..: $!\n", 1);
225 unlink (".ostest>ick");
226 rmdir (".ostest") || &error
("Couldn't rmdir .ostest: $!\n", 1);
230 sub parse_command_line
234 # use @ARGV if no args were passed in
241 # look at each option; if we don't recognize it, maybe the suite-specific
242 # command line parsing code will...
246 $option = shift @argv;
247 if ($option =~ /^-debug$/i)
249 print "\nDEBUG ON\n";
252 elsif ($option =~ /^-usage$/i)
257 elsif ($option =~ /^-(h|help)$/i)
262 elsif ($option =~ /^-profile$/i)
266 elsif ($option =~ /^-verbose$/i)
270 elsif ($option =~ /^-detail$/i)
275 elsif ($option =~ /^-keep$/i)
279 elsif (&valid_option
($option))
281 # The suite-defined subroutine takes care of the option
283 elsif ($option =~ /^-/)
285 print "Invalid option: $option\n";
289 else # must be the name of a test
291 $option =~ s/\.pl$//;
292 push(@TESTS,$option);
299 local($num) = shift @_;
316 local($width, $string) = @_;
319 if (length ($string))
321 $pad = " " x
( ($width - length ($string) + 1) / 2);
332 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
333 $len = &max
(length ($line), length ($testee_version),
334 length ($banner_info), 73) + 5;
335 $line = ("-" x
$len) . "\n";
341 &print_centered
($len, $line);
342 &print_centered
($len, $info);
343 &print_centered
($len, $testee_version); # suite-defined
344 &print_centered
($len, $banner_info); # suite-defined
345 &print_centered
($len, $line);
353 foreach $testname (sort @TESTS)
356 $test_passed = 1; # reset by test on failure
357 $num_of_logfiles = 0;
358 $num_of_tmpfiles = 0;
361 $testname =~ s/^$scriptpath$pathsep//;
362 $perl_testname = "$scriptpath$pathsep$testname";
363 $testname =~ s/(\.pl|\.perl)$//;
364 $testpath = "$workpath$pathsep$testname";
365 # Leave enough space in the extensions to append a number, even
366 # though it needs to fit into 8+3 limits.
367 if ($short_filenames) {
379 $log_filename = "$testpath.$logext";
380 $diff_filename = "$testpath.$diffext";
381 $base_filename = "$testpath.$baseext";
382 $tmp_filename = "$testpath.$tmpfilesuffix";
384 &setup_for_test
; # suite-defined
386 $output = "........................................................ ";
388 substr($output,0,length($testname)) = "$testname ";
392 # Run the actual test!
394 $code = do $perl_testname;
400 warn "\n*** Test died ($testname): $@\n";
404 warn "\n*** Couldn't run $perl_testname\n";
407 elsif ($code == -1) {
410 elsif ($code != 1 && $code != -1) {
412 warn "\n*** Test returned $code\n";
417 for ($i = $num_of_tmpfiles; $i; $i--)
419 &delete ($tmp_filename . &num_suffix
($i) );
422 for ($i = $num_of_logfiles ?
$num_of_logfiles : 1; $i; $i--)
424 &delete ($log_filename . &num_suffix
($i) );
425 &delete ($base_filename . &num_suffix
($i) );
437 # If the verbose option has been specified, then a short description
438 # of each test is printed before displaying the results of each test
439 # describing WHAT is being tested.
445 print "\nWHAT IS BEING TESTED\n";
446 print "--------------------";
448 print "\n\n$description\n\n";
451 # If the detail option has been specified, then the details of HOW
452 # the test is testing what it says it is testing in the verbose output
453 # will be displayed here before the results of the test are displayed.
457 print "\nHOW IT IS TESTED\n";
458 print "----------------";
459 print "\n\n$details\n\n";
466 # If the keep flag is not set, this subroutine deletes all filenames that
475 return (unlink @files);
481 sub print_standard_usage
483 local($plname,@moreusage) = @_;
486 print "Usage: perl $plname [testname] [-verbose] [-detail] [-keep]\n";
487 print " [-profile] [-usage] [-help] "
489 foreach $line (@moreusage)
495 sub print_standard_help
497 local(@morehelp) = @_;
502 $line = "Test Driver For $testee";
504 $line = "=" x
length ($line);
510 . "${t}You may, if you wish, run only ONE test if you know the name\n"
511 . "${t}of that test and specify this name anywhere on the command\n"
512 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
513 . "${t}will be run.\n"
515 . "${t}If this option is given, a description of every test is\n"
516 . "${t}displayed before the test is run. (Not all tests may have\n"
517 . "${t}descriptions at this time)\n"
519 . "${t}If this option is given, a detailed description of every\n"
520 . "${t}test is displayed before the test is run. (Not all tests\n"
521 . "${t}have descriptions at this time)\n"
523 . "${t}If this option is given, then the profile file\n"
524 . "${t}is added to other profiles every time $testee is run.\n"
525 . "${t}This option only works on VOS at this time.\n"
527 . "${t}You may give this option if you DO NOT want ANY\n"
528 . "${t}of the files generated by the tests to be deleted. \n"
529 . "${t}Without this option, all files generated by the test will\n"
530 . "${t}be deleted IF THE TEST PASSES.\n"
532 . "${t}Use this option if you would like to see all of the system\n"
533 . "${t}calls issued and their return status while running the tests\n"
534 . "${t}This can be helpful if you're having a problem adding a test\n"
535 . "${t}to the suite, or if the test fails!\n";
537 foreach $line (@morehelp)
540 if (substr ($tline, 0, 1) eq "\t")
542 substr ($tline, 0, 1) = $t;
548 #######################################################################
549 ########### Generic Test Driver Subroutines ###########
550 #######################################################################
559 $depth = defined ($_[0]) ?
$_[0] : 1;
560 ($package, $filename, $linenum) = caller ($depth + 1);
561 return "$filename: $linenum";
566 local($message) = $_[0];
567 local($caller) = &get_caller
(1);
571 $caller = &get_caller
($_[1] + 1) . " -> $caller";
574 die "$caller: $message";
579 local($answer,$logfile) = @_;
584 print "Comparing Output ........ ";
587 $slurp = &read_file_into_string
($logfile);
589 # For make, get rid of any time skew error before comparing--too bad this
590 # has to go into the "generic" driver code :-/
591 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
592 $slurp =~ s/^.*Clock skew detected.*\n//gm;
594 if ($slurp eq $answer)
606 print "DIFFERENT OUTPUT\n";
609 &create_file
(&get_basefile
, $answer);
613 print "\nCreating Difference File ...\n";
615 # Create the difference file
616 local($command) = "diff -c " . &get_basefile
. " " . $logfile;
617 &run_command_with_output
(&get_difffile
,$command);
623 sub read_file_into_string
625 local($filename) = @_;
626 local($oldslash) = $/;
630 open (RFISFILE
, $filename) || return "";
631 local ($slurp) = <RFISFILE
>;
639 sub attach_default_output
641 local ($filename) = @_;
646 $code = system "++attach_default_output_hack $filename";
647 $code == -2 || &error
("adoh death\n", 1);
651 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
652 || &error
("ado: $! duping STDOUT\n", 1);
653 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
654 || &error
("ado: $! duping STDERR\n", 1);
656 open (STDOUT
, "> " . $filename)
657 || &error
("ado: $filename: $!\n", 1);
658 open (STDERR
, ">&STDOUT")
659 || &error
("ado: $filename: $!\n", 1);
661 $default_output_stack_level++;
664 # close the current stdout/stderr, and restore the previous ones from
667 sub detach_default_output
673 $code = system "++detach_default_output_hack";
674 $code == -2 || &error
("ddoh death\n", 1);
678 if (--$default_output_stack_level < 0)
680 &error
("default output stack has flown under!\n", 1);
686 open (STDOUT
, ">&SAVEDOS" . $default_output_stack_level . "out")
687 || &error
("ddo: $! duping STDOUT\n", 1);
688 open (STDERR
, ">&SAVEDOS" . $default_output_stack_level . "err")
689 || &error
("ddo: $! duping STDERR\n", 1);
691 close ("SAVEDOS" . $default_output_stack_level . "out")
692 || &error
("ddo: $! closing SCSDOSout\n", 1);
693 close ("SAVEDOS" . $default_output_stack_level . "err")
694 || &error
("ddo: $! closing SAVEDOSerr\n", 1);
697 # run one command (passed as a list of arg 0 - n), returning 0 on success
698 # and nonzero on failure.
706 print "\nrun_command: @_\n";
708 print "run_command: \"@_\" returned $code.\n";
715 # run one command (passed as a list of arg 0 - n, with arg 0 being the
716 # second arg to this routine), returning 0 on success and non-zero on failure.
717 # The first arg to this routine is a filename to connect to the stdout
718 # & stderr of the child process.
720 sub run_command_with_output
722 local ($filename) = shift;
725 &attach_default_output
($filename);
727 &detach_default_output
;
730 print "run_command_with_output: \"@_\" returned $code.\n";
736 # performs the equivalent of an "rm -rf" on the first argument. Like
737 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
740 sub remove_directory_tree
742 local ($targetdir) = @_;
743 local ($nuketop) = 1;
746 $ch = substr ($targetdir, length ($targetdir) - 1);
747 if ($ch eq "/" || $ch eq $pathsep)
749 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
758 &remove_directory_tree_inner
("RDT00", $targetdir) || return 0;
761 rmdir $targetdir || return 0;
767 sub remove_directory_tree_inner
769 local ($dirhandle, $targetdir) = @_;
771 local ($subdirhandle);
773 opendir ($dirhandle, $targetdir) || return 0;
774 $subdirhandle = $dirhandle;
776 while ($object = readdir ($dirhandle))
778 if ($object =~ /^(\.\.?|CVS|RCS)$/)
783 $object = "$targetdir$pathsep$object";
786 if (-d _
&& &remove_directory_tree_inner
($subdirhandle, $object))
788 rmdir $object || return 0;
792 unlink $object || return 0;
795 closedir ($dirhandle);
799 # We used to use this behavior for this function:
803 # local (@filenames) = @_;
804 # local ($now) = time;
807 # foreach $file (@filenames)
809 # utime ($now, $now, $file)
810 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
811 # || &error ("Couldn't touch $file: $!\n", 1);
816 # But this behaves badly on networked filesystems where the time is
817 # skewed, because it sets the time of the file based on the _local_
818 # host. Normally when you modify a file, it's the _remote_ host that
819 # determines the modtime, based on _its_ clock. So, instead, now we open
820 # the file and write something into it to force the remote host to set
821 # the modtime correctly according to its clock.
829 (open(T
, ">> $file") && print(T
"\n") && close(T
))
830 || &error
("Couldn't touch $file: $!\n", 1);
834 # Touch with a time offset. To DTRT, call touch() then use stat() to get the
835 # access/mod time for each file and apply the offset.
839 local ($off) = shift;
844 local (@s) = stat($_[0]);
846 utime($s[8]+$off, $s[9]+$off, @_);
849 # open a file, write some stuff to it, and close it.
853 local ($filename, @lines) = @_;
855 open (CF
, "> $filename") || &error
("Couldn't open $filename: $!\n", 1);
856 foreach $line (@lines)
863 # create a directory tree described by an associative array, wherein each
864 # key is a relative pathname (using slashes) and its associated value is
866 # DIR indicates a directory
867 # FILE:contents indicates a file, which should contain contents +\n
868 # LINK:target indicates a symlink, pointing to $basedir/target
869 # The first argument is the dir under which the structure will be created
870 # (the dir will be made and/or cleaned if necessary); the second argument
871 # is the associative array.
875 local ($basedir, %dirtree) = @_;
878 &remove_directory_tree
("$basedir");
879 mkdir ($basedir, 0777) || &error
("Couldn't mkdir $basedir: $!\n", 1);
881 foreach $path (sort keys (%dirtree))
883 if ($dirtree {$path} =~ /^DIR$/)
885 mkdir ("$basedir/$path", 0777)
886 || &error
("Couldn't mkdir $basedir/$path: $!\n", 1);
888 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
890 &create_file
("$basedir/$path", $1 . "\n");
892 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
894 symlink ("$basedir/$1", "$basedir/$path")
895 || &error
("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
899 &error
("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
902 if ($just_setup_tree)
904 die "Tree is setup...\n";
908 # compare a directory tree with an associative array in the format used
909 # by create_dir_tree, above.
910 # The first argument is the dir under which the structure should be found;
911 # the second argument is the associative array.
915 local ($basedir, %dirtree) = @_;
926 opendir (DIR
, $basedir) || &error
("Couldn't open $basedir: $!\n", 1);
927 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR
) );
931 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
934 foreach $path (sort keys (%dirtree))
938 print "Checking $path ($dirtree{$path}).\n";
942 foreach $i (0 .. $#allfiles)
944 if ($allfiles[$i] eq $path)
946 splice (@allfiles, $i, 1); # delete it
949 print " Zapped $path; files now (@allfiles).\n";
951 lstat ("$basedir/$path");
959 print "compare_dir_tree: $path does not exist.\n";
964 if ($dirtree {$path} =~ /^DIR$/)
966 if (-d _
&& opendir (DIR
, "$basedir/$path") )
968 @files = readdir (DIR
);
970 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
971 push (@allfiles, @files);
974 print " Read in $path; new files (@files).\n";
979 print "compare_dir_tree: $path is not a dir.\n";
983 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
987 print "compare_dir_tree: $path is not a file.\n";
994 $contents = &read_file_into_string
("$basedir/$path");
995 if ($contents ne "$1\n")
997 print "compare_dir_tree: $path contains wrong stuff."
998 . " Is:\n$contentsShould be:\n$1\n";
1003 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1008 print "compare_dir_tree: $path is not a link.\n";
1013 $contents = readlink ("$basedir/$path");
1014 $contents =~ tr/>/\//;
1015 $fulltarget = "$basedir/$target";
1016 $fulltarget =~ tr/>/\//;
1017 if (!($contents =~ /$fulltarget$/))
1021 $target = $fulltarget;
1023 print "compare_dir_tree: $path should be link to $target, "
1024 . "not $contents.\n";
1030 &error
("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1036 print "leftovers: (@allfiles).\n";
1039 foreach $file (@allfiles)
1041 print "compare_dir_tree: $file should not exist.\n";
1048 # this subroutine generates the numeric suffix used to keep tmp filenames,
1049 # log filenames, etc., unique. If the number passed in is 1, then a null
1050 # string is returned; otherwise, we return ".n", where n + 1 is the number
1058 return "$extext$num";
1064 # This subroutine returns a log filename with a number appended to
1065 # the end corresponding to how many logfiles have been created in the
1066 # current running test. An optional parameter may be passed (0 or 1).
1067 # If a 1 is passed, then it does NOT increment the logfile counter
1068 # and returns the name of the latest logfile. If either no parameter
1069 # is passed at all or a 0 is passed, then the logfile counter is
1070 # incremented and the new name is returned.
1074 local($no_increment) = @_;
1076 $num_of_logfiles += !$no_increment;
1078 return ($log_filename . &num_suffix
($num_of_logfiles));
1081 # This subroutine returns a base (answer) filename with a number
1082 # appended to the end corresponding to how many logfiles (and thus
1083 # base files) have been created in the current running test.
1084 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1088 return ($base_filename . &num_suffix
($num_of_logfiles));
1091 # This subroutine returns a difference filename with a number appended
1092 # to the end corresponding to how many logfiles (and thus diff files)
1093 # have been created in the current running test.
1097 return ($diff_filename . &num_suffix
($num_of_logfiles));
1100 # just like logfile, only a generic tmp filename for use by the test.
1101 # they are automatically cleaned up unless -keep was used, or the test fails.
1102 # Pass an argument of 1 to return the same filename as the previous call.
1106 local($no_increment) = @_;
1108 $num_of_tmpfiles += !$no_increment;
1110 return ($tmp_filename . &num_suffix
($num_of_tmpfiles));