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 = 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 = 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//g;
592 $slurp =~ s/\n.*modification time in the future.*//g;
593 $slurp =~ s/^.*Clock skew detected.*\n//g;
594 $slurp =~ s/\n.*Clock skew detected.*//g;
596 if ($slurp eq $answer)
608 print "DIFFERENT OUTPUT\n";
611 &create_file
(&get_basefile
, $answer);
615 print "\nCreating Difference File ...\n";
617 # Create the difference file
618 local($command) = "diff -c " . &get_basefile
. " " . $logfile;
619 &run_command_with_output
(&get_difffile
,$command);
625 sub read_file_into_string
627 local($filename) = @_;
628 local($oldslash) = $/;
632 open (RFISFILE
, $filename) || return "";
633 local ($slurp) = <RFISFILE
>;
641 sub attach_default_output
643 local ($filename) = @_;
648 $code = system "++attach_default_output_hack $filename";
649 $code == -2 || &error
("adoh death\n", 1);
653 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
654 || &error
("ado: $! duping STDOUT\n", 1);
655 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
656 || &error
("ado: $! duping STDERR\n", 1);
658 open (STDOUT
, "> " . $filename)
659 || &error
("ado: $filename: $!\n", 1);
660 open (STDERR
, ">&STDOUT")
661 || &error
("ado: $filename: $!\n", 1);
663 $default_output_stack_level++;
666 # close the current stdout/stderr, and restore the previous ones from
669 sub detach_default_output
675 $code = system "++detach_default_output_hack";
676 $code == -2 || &error
("ddoh death\n", 1);
680 if (--$default_output_stack_level < 0)
682 &error
("default output stack has flown under!\n", 1);
688 open (STDOUT
, ">&SAVEDOS" . $default_output_stack_level . "out")
689 || &error
("ddo: $! duping STDOUT\n", 1);
690 open (STDERR
, ">&SAVEDOS" . $default_output_stack_level . "err")
691 || &error
("ddo: $! duping STDERR\n", 1);
693 close ("SAVEDOS" . $default_output_stack_level . "out")
694 || &error
("ddo: $! closing SCSDOSout\n", 1);
695 close ("SAVEDOS" . $default_output_stack_level . "err")
696 || &error
("ddo: $! closing SAVEDOSerr\n", 1);
699 # run one command (passed as a list of arg 0 - n), returning 0 on success
700 # and nonzero on failure.
708 print "\nrun_command: @_\n";
710 print "run_command: \"@_\" returned $code.\n";
717 # run one command (passed as a list of arg 0 - n, with arg 0 being the
718 # second arg to this routine), returning 0 on success and non-zero on failure.
719 # The first arg to this routine is a filename to connect to the stdout
720 # & stderr of the child process.
722 sub run_command_with_output
724 local ($filename) = shift;
727 &attach_default_output
($filename);
729 &detach_default_output
;
732 print "run_command_with_output: \"@_\" returned $code.\n";
738 # performs the equivalent of an "rm -rf" on the first argument. Like
739 # rm, if the path ends in /, leaves the (now empty) directory; otherwise
742 sub remove_directory_tree
744 local ($targetdir) = @_;
745 local ($nuketop) = 1;
748 $ch = substr ($targetdir, length ($targetdir) - 1);
749 if ($ch eq "/" || $ch eq $pathsep)
751 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
760 &remove_directory_tree_inner
("RDT00", $targetdir) || return 0;
763 rmdir $targetdir || return 0;
769 sub remove_directory_tree_inner
771 local ($dirhandle, $targetdir) = @_;
773 local ($subdirhandle);
775 opendir ($dirhandle, $targetdir) || return 0;
776 $subdirhandle = $dirhandle;
778 while ($object = readdir ($dirhandle))
780 if ($object eq "." || $object eq "..")
785 $object = "$targetdir$pathsep$object";
788 if (-d _
&& &remove_directory_tree_inner
($subdirhandle, $object))
790 rmdir $object || return 0;
794 unlink $object || return 0;
797 closedir ($dirhandle);
801 # We used to use this behavior for this function:
805 # local (@filenames) = @_;
806 # local ($now) = time;
809 # foreach $file (@filenames)
811 # utime ($now, $now, $file)
812 # || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
813 # || &error ("Couldn't touch $file: $!\n", 1);
818 # But this behaves badly on networked filesystems where the time is
819 # skewed, because it sets the time of the file based on the _local_
820 # host. Normally when you modify a file, it's the _remote_ host that
821 # determines the modtime, based on _its_ clock. So, instead, now we open
822 # the file and write something into it to force the remote host to set
823 # the modtime correctly according to its clock.
828 local (@filenames) = @_;
831 foreach $file (@filenames) {
832 (open(T
, ">> $file") && print(T
"\n") && close(T
))
833 || &error
("Couldn't touch $file: $!\n", 1);
837 # open a file, write some stuff to it, and close it.
841 local ($filename, @lines) = @_;
843 open (CF
, "> $filename") || &error
("Couldn't open $filename: $!\n", 1);
844 foreach $line (@lines)
851 # create a directory tree described by an associative array, wherein each
852 # key is a relative pathname (using slashes) and its associated value is
854 # DIR indicates a directory
855 # FILE:contents indicates a file, which should contain contents +\n
856 # LINK:target indicates a symlink, pointing to $basedir/target
857 # The first argument is the dir under which the structure will be created
858 # (the dir will be made and/or cleaned if necessary); the second argument
859 # is the associative array.
863 local ($basedir, %dirtree) = @_;
866 &remove_directory_tree
("$basedir");
867 mkdir ($basedir, 0777) || &error
("Couldn't mkdir $basedir: $!\n", 1);
869 foreach $path (sort keys (%dirtree))
871 if ($dirtree {$path} =~ /^DIR$/)
873 mkdir ("$basedir/$path", 0777)
874 || &error
("Couldn't mkdir $basedir/$path: $!\n", 1);
876 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
878 &create_file
("$basedir/$path", $1 . "\n");
880 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
882 symlink ("$basedir/$1", "$basedir/$path")
883 || &error
("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
887 &error
("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
890 if ($just_setup_tree)
892 die "Tree is setup...\n";
896 # compare a directory tree with an associative array in the format used
897 # by create_dir_tree, above.
898 # The first argument is the dir under which the structure should be found;
899 # the second argument is the associative array.
903 local ($basedir, %dirtree) = @_;
914 opendir (DIR
, $basedir) || &error
("Couldn't open $basedir: $!\n", 1);
915 @allfiles = grep (!/^\.\.?$/, readdir (DIR
) );
919 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
922 foreach $path (sort keys (%dirtree))
926 print "Checking $path ($dirtree{$path}).\n";
930 foreach $i (0 .. $#allfiles)
932 if ($allfiles[$i] eq $path)
934 splice (@allfiles, $i, 1); # delete it
937 print " Zapped $path; files now (@allfiles).\n";
939 lstat ("$basedir/$path");
947 print "compare_dir_tree: $path does not exist.\n";
952 if ($dirtree {$path} =~ /^DIR$/)
954 if (-d _
&& opendir (DIR
, "$basedir/$path") )
956 @files = readdir (DIR
);
958 @files = grep (!/^\.\.?$/ && ($_ = "$path/$_"), @files);
959 push (@allfiles, @files);
962 print " Read in $path; new files (@files).\n";
967 print "compare_dir_tree: $path is not a dir.\n";
971 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
975 print "compare_dir_tree: $path is not a file.\n";
982 $contents = &read_file_into_string
("$basedir/$path");
983 if ($contents ne "$1\n")
985 print "compare_dir_tree: $path contains wrong stuff."
986 . " Is:\n$contentsShould be:\n$1\n";
991 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
996 print "compare_dir_tree: $path is not a link.\n";
1001 $contents = readlink ("$basedir/$path");
1002 $contents =~ tr/>/\//;
1003 $fulltarget = "$basedir/$target";
1004 $fulltarget =~ tr/>/\//;
1005 if (!($contents =~ /$fulltarget$/))
1009 $target = $fulltarget;
1011 print "compare_dir_tree: $path should be link to $target, "
1012 . "not $contents.\n";
1018 &error
("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1024 print "leftovers: (@allfiles).\n";
1027 foreach $file (@allfiles)
1029 print "compare_dir_tree: $file should not exist.\n";
1036 # this subroutine generates the numeric suffix used to keep tmp filenames,
1037 # log filenames, etc., unique. If the number passed in is 1, then a null
1038 # string is returned; otherwise, we return ".n", where n + 1 is the number
1046 return "$extext$num";
1052 # This subroutine returns a log filename with a number appended to
1053 # the end corresponding to how many logfiles have been created in the
1054 # current running test. An optional parameter may be passed (0 or 1).
1055 # If a 1 is passed, then it does NOT increment the logfile counter
1056 # and returns the name of the latest logfile. If either no parameter
1057 # is passed at all or a 0 is passed, then the logfile counter is
1058 # incremented and the new name is returned.
1062 local($no_increment) = @_;
1064 $num_of_logfiles += !$no_increment;
1066 return ($log_filename . &num_suffix
($num_of_logfiles));
1069 # This subroutine returns a base (answer) filename with a number
1070 # appended to the end corresponding to how many logfiles (and thus
1071 # base files) have been created in the current running test.
1072 # NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1076 return ($base_filename . &num_suffix
($num_of_logfiles));
1079 # This subroutine returns a difference filename with a number appended
1080 # to the end corresponding to how many logfiles (and thus diff files)
1081 # have been created in the current running test.
1085 return ($diff_filename . &num_suffix
($num_of_logfiles));
1088 # just like logfile, only a generic tmp filename for use by the test.
1089 # they are automatically cleaned up unless -keep was used, or the test fails.
1090 # Pass an argument of 1 to return the same filename as the previous call.
1094 local($no_increment) = @_;
1096 $num_of_tmpfiles += !$no_increment;
1098 return ($tmp_filename . &num_suffix
($num_of_tmpfiles));