VirtualBox

source: kBuild/trunk/src/kmk/tests/test_driver.pl@ 2591

Last change on this file since 2591 was 2591, checked in by bird, 12 years ago

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

  • Property svn:eol-style set to native
File size: 31.4 KB
Line 
1#!/usr/bin/perl
2# -*-perl-*-
3#
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#
8# Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
9# 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
10# Foundation, Inc.
11# This file is part of GNU Make.
12#
13# GNU Make is free software; you can redistribute it and/or modify it under
14# the terms of the GNU General Public License as published by the Free Software
15# Foundation; either version 3 of the License, or (at your option) any later
16# version.
17#
18# GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
19# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21# details.
22#
23# You should have received a copy of the GNU General Public License along with
24# this program. If not, see <http://www.gnu.org/licenses/>.
25
26
27# Test driver routines used by a number of test suites, including
28# those for SCS, make, roll_dir, and scan_deps (?).
29#
30# this routine controls the whole mess; each test suite sets up a few
31# variables and then calls &toplevel, which does all the real work.
32
33# $Id: test_driver.pl,v 1.30 2010/07/28 05:39:50 psmith Exp $
34
35
36# The number of test categories we've run
37$categories_run = 0;
38# The number of test categroies that have passed
39$categories_passed = 0;
40# The total number of individual tests that have been run
41$total_tests_run = 0;
42# The total number of individual tests that have passed
43$total_tests_passed = 0;
44# The number of tests in this category that have been run
45$tests_run = 0;
46# The number of tests in this category that have passed
47$tests_passed = 0;
48
49
50# Yeesh. This whole test environment is such a hack!
51$test_passed = 1;
52
53
54# Timeout in seconds. If the test takes longer than this we'll fail it.
55$test_timeout = 5;
56
57# Path to Perl
58$perl_name = $^X;
59
60# %makeENV is the cleaned-out environment.
61%makeENV = ();
62
63# %extraENV are any extra environment variables the tests might want to set.
64# These are RESET AFTER EVERY TEST!
65%extraENV = ();
66
67# %origENV is the caller's original environment
68%origENV = %ENV;
69
70sub resetENV
71{
72 # We used to say "%ENV = ();" but this doesn't work in Perl 5.000
73 # through Perl 5.004. It was fixed in Perl 5.004_01, but we don't
74 # want to require that here, so just delete each one individually.
75 foreach $v (keys %ENV) {
76 delete $ENV{$v};
77 }
78
79 %ENV = %makeENV;
80 foreach $v (keys %extraENV) {
81 $ENV{$v} = $extraENV{$v};
82 delete $extraENV{$v};
83 }
84}
85
86sub toplevel
87{
88 # Pull in benign variables from the user's environment
89
90 foreach (# UNIX-specific things
91 'TZ', 'TMPDIR', 'HOME', 'USER', 'LOGNAME', 'PATH',
92 # Purify things
93 'PURIFYOPTIONS',
94 # Windows NT-specific stuff
95 'Path', 'SystemRoot',
96 # DJGPP-specific stuff
97 'DJDIR', 'DJGPP', 'SHELL', 'COMSPEC', 'HOSTNAME', 'LFN',
98 'FNCASE', '387', 'EMU387', 'GROUP'
99 ) {
100 $makeENV{$_} = $ENV{$_} if $ENV{$_};
101 }
102
103 # Make sure our compares are not foiled by locale differences
104
105 $makeENV{LC_ALL} = 'C';
106
107 # Replace the environment with the new one
108 #
109 %origENV = %ENV;
110
111 resetENV();
112
113 $| = 1; # unbuffered output
114
115 $debug = 0; # debug flag
116 $profile = 0; # profiling flag
117 $verbose = 0; # verbose mode flag
118 $detail = 0; # detailed verbosity
119 $keep = 0; # keep temp files around
120 $workdir = "work"; # The directory where the test will start running
121 $scriptdir = "scripts"; # The directory where we find the test scripts
122 $tmpfilesuffix = "t"; # the suffix used on tmpfiles
123 $default_output_stack_level = 0; # used by attach_default_output, etc.
124 $default_input_stack_level = 0; # used by attach_default_input, etc.
125 $cwd = "."; # don't we wish we knew
126 $cwdslash = ""; # $cwd . $pathsep, but "" rather than "./"
127 $is_kmk = 0; # kmk flag.
128 $is_fast = 0; # kmk_fgmake flag.
129
130 &get_osname; # sets $osname, $vos, $pathsep, $short_filenames,
131 # and $case_insensitive_fs
132
133 &set_defaults; # suite-defined
134
135 &parse_command_line (@ARGV);
136
137 print "OS name = `$osname'\n" if $debug;
138
139 $workpath = "$cwdslash$workdir";
140 $scriptpath = "$cwdslash$scriptdir";
141
142 &set_more_defaults; # suite-defined
143
144 &print_banner;
145
146 if (-d $workpath)
147 {
148 print "Clearing $workpath...\n";
149 &remove_directory_tree("$workpath/")
150 || &error ("Couldn't wipe out $workpath\n");
151 }
152 else
153 {
154 mkdir ($workpath, 0777) || &error ("Couldn't mkdir $workpath: $!\n");
155 }
156
157 if (!-d $scriptpath)
158 {
159 &error ("Failed to find $scriptpath containing perl test scripts.\n");
160 }
161
162 if (@TESTS)
163 {
164 print "Making work dirs...\n";
165 foreach $test (@TESTS)
166 {
167 if ($test =~ /^([^\/]+)\//)
168 {
169 $dir = $1;
170 push (@rmdirs, $dir);
171 -d "$workpath/$dir"
172 || mkdir ("$workpath/$dir", 0777)
173 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
174 }
175 }
176 }
177 else
178 {
179 print "Finding tests...\n";
180 opendir (SCRIPTDIR, $scriptpath)
181 || &error ("Couldn't opendir $scriptpath: $!\n");
182 @dirs = grep (!/^(\..*|CVS|RCS)$/, readdir (SCRIPTDIR) );
183 closedir (SCRIPTDIR);
184 foreach $dir (@dirs)
185 {
186 next if ($dir =~ /^(\..*|CVS|RCS)$/ || ! -d "$scriptpath/$dir");
187 push (@rmdirs, $dir);
188 mkdir ("$workpath/$dir", 0777)
189 || &error ("Couldn't mkdir $workpath/$dir: $!\n");
190 opendir (SCRIPTDIR, "$scriptpath/$dir")
191 || &error ("Couldn't opendir $scriptpath/$dir: $!\n");
192 @files = grep (!/^(\..*|CVS|RCS|.*~)$/, readdir (SCRIPTDIR) );
193 closedir (SCRIPTDIR);
194 foreach $test (@files)
195 {
196 -d $test and next;
197 push (@TESTS, "$dir/$test");
198 }
199 }
200 }
201
202 if (@TESTS == 0)
203 {
204 &error ("\nNo tests in $scriptpath, and none were specified.\n");
205 }
206
207 print "\n";
208
209 &run_each_test;
210
211 foreach $dir (@rmdirs)
212 {
213 rmdir ("$workpath/$dir");
214 }
215
216 $| = 1;
217
218 $categories_failed = $categories_run - $categories_passed;
219 $total_tests_failed = $total_tests_run - $total_tests_passed;
220
221 if ($total_tests_failed)
222 {
223 print "\n$total_tests_failed Test";
224 print "s" unless $total_tests_failed == 1;
225 print " in $categories_failed Categor";
226 print ($categories_failed == 1 ? "y" : "ies");
227 print " Failed (See .$diffext files in $workdir dir for details) :-(\n\n";
228 return 0;
229 }
230 else
231 {
232 print "\n$total_tests_passed Test";
233 print "s" unless $total_tests_passed == 1;
234 print " in $categories_passed Categor";
235 print ($categories_passed == 1 ? "y" : "ies");
236 print " Complete ... No Failures :-)\n\n";
237 return 1;
238 }
239}
240
241sub get_osname
242{
243 # Set up an initial value. In perl5 we can do it the easy way.
244 $osname = defined($^O) ? $^O : '';
245
246 # Find a path to Perl
247
248 # See if the filesystem supports long file names with multiple
249 # dots. DOS doesn't.
250 $short_filenames = 0;
251 (open (TOUCHFD, "> fancy.file.name") && close (TOUCHFD))
252 || ($short_filenames = 1);
253 unlink ("fancy.file.name") || ($short_filenames = 1);
254
255 if (! $short_filenames) {
256 # Thanks go to meyering@cs.utexas.edu (Jim Meyering) for suggesting a
257 # better way of doing this. (We used to test for existence of a /mnt
258 # dir, but that apparently fails on an SGI Indigo (whatever that is).)
259 # Because perl on VOS translates /'s to >'s, we need to test for
260 # VOSness rather than testing for Unixness (ie, try > instead of /).
261
262 mkdir (".ostest", 0777) || &error ("Couldn't create .ostest: $!\n", 1);
263 open (TOUCHFD, "> .ostest>ick") && close (TOUCHFD);
264 chdir (".ostest") || &error ("Couldn't chdir to .ostest: $!\n", 1);
265 }
266
267 if (! $short_filenames && -f "ick")
268 {
269 $osname = "vos";
270 $vos = 1;
271 $pathsep = ">";
272 }
273 else
274 {
275 # the following is regrettably knarly, but it seems to be the only way
276 # to not get ugly error messages if uname can't be found.
277 # Hmmm, BSD/OS 2.0's uname -a is excessively verbose. Let's try it
278 # with switches first.
279 eval "chop (\$osname = `sh -c 'uname -nmsr 2>&1'`)";
280 if ($osname =~ /not found/i)
281 {
282 $osname = "(something posixy with no uname)";
283 }
284 elsif ($@ ne "" || $?)
285 {
286 eval "chop (\$osname = `sh -c 'uname -a 2>&1'`)";
287 if ($@ ne "" || $?)
288 {
289 $osname = "(something posixy)";
290 }
291 }
292 $vos = 0;
293 $pathsep = "/";
294 }
295
296 if (! $short_filenames) {
297 chdir ("..") || &error ("Couldn't chdir to ..: $!\n", 1);
298 unlink (".ostest>ick");
299 rmdir (".ostest") || &error ("Couldn't rmdir .ostest: $!\n", 1);
300 }
301
302 # Check for case insensitive file system (bird)
303 # The deal is that the 2nd unlink will fail because the first one
304 # will already have removed the file if the fs ignore case.
305 $case_insensitive_fs = 0;
306 my $testfile1 = $short_filenames ? "CaseFs.rmt" : "CaseInSensitiveFs.check";
307 my $testfile2 = $short_filenames ? "casEfS.rmt" : "casEiNsensitivEfS.Check";
308 (open (TOUCHFD, "> $testfile1") && close (TOUCHFD))
309 || &error ("Couldn't create $testfile1: $!\n", 1);
310 (open (TOUCHFD, "> $testfile2") && close (TOUCHFD))
311 || &error ("Couldn't create $testfile2: $!\n", 1);
312 unlink ($testfile1) || &error ("Couldn't unlink $testfile1: $!\n", 1);
313 unlink ($testfile2) || ($case_insensitive_fs = 1);
314}
315
316sub parse_command_line
317{
318 @argv = @_;
319
320 # use @ARGV if no args were passed in
321
322 if (@argv == 0)
323 {
324 @argv = @ARGV;
325 }
326
327 # look at each option; if we don't recognize it, maybe the suite-specific
328 # command line parsing code will...
329
330 while (@argv)
331 {
332 $option = shift @argv;
333 if ($option =~ /^-debug$/i)
334 {
335 print "\nDEBUG ON\n";
336 $debug = 1;
337 }
338 elsif ($option =~ /^-usage$/i)
339 {
340 &print_usage;
341 exit 0;
342 }
343 elsif ($option =~ /^-(h|help)$/i)
344 {
345 &print_help;
346 exit 0;
347 }
348 elsif ($option =~ /^-profile$/i)
349 {
350 $profile = 1;
351 }
352 elsif ($option =~ /^-verbose$/i)
353 {
354 $verbose = 1;
355 }
356 elsif ($option =~ /^-detail$/i)
357 {
358 $detail = 1;
359 $verbose = 1;
360 }
361 elsif ($option =~ /^-keep$/i)
362 {
363 $keep = 1;
364 }
365 elsif ($option =~ /^-kmk/i)
366 {
367 $is_kmk = 1;
368 }
369 elsif ($option =~ /^-fast/i)
370 {
371 $is_fast = 1;
372 }
373 elsif (&valid_option($option))
374 {
375 # The suite-defined subroutine takes care of the option
376 }
377 elsif ($option =~ /^-/)
378 {
379 print "Invalid option: $option\n";
380 &print_usage;
381 exit 0;
382 }
383 else # must be the name of a test
384 {
385 $option =~ s/\.pl$//;
386 push(@TESTS,$option);
387 }
388 }
389}
390
391sub max
392{
393 local($num) = shift @_;
394 local($newnum);
395
396 while (@_)
397 {
398 $newnum = shift @_;
399 if ($newnum > $num)
400 {
401 $num = $newnum;
402 }
403 }
404
405 return $num;
406}
407
408sub print_centered
409{
410 local($width, $string) = @_;
411 local($pad);
412
413 if (length ($string))
414 {
415 $pad = " " x ( ($width - length ($string) + 1) / 2);
416 print "$pad$string";
417 }
418}
419
420sub print_banner
421{
422 local($info);
423 local($line);
424 local($len);
425
426 $info = "Running tests for $testee on $osname\n"; # $testee is suite-defined
427 $len = &max (length ($line), length ($testee_version),
428 length ($banner_info), 73) + 5;
429 $line = ("-" x $len) . "\n";
430 if ($len < 78)
431 {
432 $len = 78;
433 }
434
435 &print_centered ($len, $line);
436 &print_centered ($len, $info);
437 &print_centered ($len, $testee_version); # suite-defined
438 &print_centered ($len, $banner_info); # suite-defined
439 &print_centered ($len, $line);
440 print "\n";
441}
442
443sub run_each_test
444{
445 $categories_run = 0;
446
447 foreach $testname (sort @TESTS)
448 {
449 ++$categories_run;
450 $suite_passed = 1; # reset by test on failure
451 $num_of_logfiles = 0;
452 $num_of_tmpfiles = 0;
453 $description = "";
454 $details = "";
455 $old_makefile = undef;
456 $testname =~ s/^$scriptpath$pathsep//;
457 $perl_testname = "$scriptpath$pathsep$testname";
458 $testname =~ s/(\.pl|\.perl)$//;
459 $testpath = "$workpath$pathsep$testname";
460 # Leave enough space in the extensions to append a number, even
461 # though it needs to fit into 8+3 limits.
462 if ($short_filenames) {
463 $logext = 'l';
464 $diffext = 'd';
465 $baseext = 'b';
466 $runext = 'r';
467 $extext = '';
468 } else {
469 $logext = 'log';
470 $diffext = 'diff';
471 $baseext = 'base';
472 $runext = 'run';
473 $extext = '.';
474 }
475 $log_filename = "$testpath.$logext";
476 $diff_filename = "$testpath.$diffext";
477 $base_filename = "$testpath.$baseext";
478 $run_filename = "$testpath.$runext";
479 $tmp_filename = "$testpath.$tmpfilesuffix";
480
481 &setup_for_test; # suite-defined
482
483 $output = "........................................................ ";
484
485 substr($output,0,length($testname)) = "$testname ";
486
487 print $output;
488
489 # Run the actual test!
490 $tests_run = 0;
491 $tests_passed = 0;
492
493 $code = do $perl_testname;
494
495 $total_tests_run += $tests_run;
496 $total_tests_passed += $tests_passed;
497
498 # How did it go?
499 if (!defined($code))
500 {
501 $suite_passed = 0;
502 if (length ($@)) {
503 warn "\n*** Test died ($testname): $@\n";
504 } else {
505 warn "\n*** Couldn't run $perl_testname\n";
506 }
507 }
508 elsif ($code == -1) {
509 $suite_passed = 0;
510 }
511 elsif ($code != 1 && $code != -1) {
512 $suite_passed = 0;
513 warn "\n*** Test returned $code\n";
514 }
515
516 if ($suite_passed) {
517 ++$categories_passed;
518 $status = "ok ($tests_passed passed)";
519 for ($i = $num_of_tmpfiles; $i; $i--)
520 {
521 &rmfiles ($tmp_filename . &num_suffix ($i) );
522 }
523
524 for ($i = $num_of_logfiles ? $num_of_logfiles : 1; $i; $i--)
525 {
526 &rmfiles ($log_filename . &num_suffix ($i) );
527 &rmfiles ($base_filename . &num_suffix ($i) );
528 }
529 }
530 elsif (!defined $code || $code > 0) {
531 $status = "FAILED ($tests_passed/$tests_run passed)";
532 }
533 elsif ($code < 0) {
534 $status = "N/A";
535 --$categories_run;
536 }
537
538 # If the verbose option has been specified, then a short description
539 # of each test is printed before displaying the results of each test
540 # describing WHAT is being tested.
541
542 if ($verbose)
543 {
544 if ($detail)
545 {
546 print "\nWHAT IS BEING TESTED\n";
547 print "--------------------";
548 }
549 print "\n\n$description\n\n";
550 }
551
552 # If the detail option has been specified, then the details of HOW
553 # the test is testing what it says it is testing in the verbose output
554 # will be displayed here before the results of the test are displayed.
555
556 if ($detail)
557 {
558 print "\nHOW IT IS TESTED\n";
559 print "----------------";
560 print "\n\n$details\n\n";
561 }
562
563 print "$status\n";
564 }
565}
566
567# If the keep flag is not set, this subroutine deletes all filenames that
568# are sent to it.
569
570sub rmfiles
571{
572 local(@files) = @_;
573
574 if (!$keep)
575 {
576 return (unlink @files);
577 }
578
579 return 1;
580}
581
582sub print_standard_usage
583{
584 local($plname,@moreusage) = @_;
585 local($line);
586
587 print "usage:\t$plname [testname] [-verbose] [-detail] [-keep]\n";
588 print "\t\t\t[-profile] [-usage] [-help] [-debug]\n";
589 foreach (@moreusage) {
590 print "\t\t\t$_\n";
591 }
592}
593
594sub print_standard_help
595{
596 local(@morehelp) = @_;
597 local($line);
598 local($tline);
599 local($t) = " ";
600
601 $line = "Test Driver For $testee";
602 print "$line\n";
603 $line = "=" x length ($line);
604 print "$line\n";
605
606 &print_usage;
607
608 print "\ntestname\n"
609 . "${t}You may, if you wish, run only ONE test if you know the name\n"
610 . "${t}of that test and specify this name anywhere on the command\n"
611 . "${t}line. Otherwise ALL existing tests in the scripts directory\n"
612 . "${t}will be run.\n"
613 . "-verbose\n"
614 . "${t}If this option is given, a description of every test is\n"
615 . "${t}displayed before the test is run. (Not all tests may have\n"
616 . "${t}descriptions at this time)\n"
617 . "-detail\n"
618 . "${t}If this option is given, a detailed description of every\n"
619 . "${t}test is displayed before the test is run. (Not all tests\n"
620 . "${t}have descriptions at this time)\n"
621 . "-profile\n"
622 . "${t}If this option is given, then the profile file\n"
623 . "${t}is added to other profiles every time $testee is run.\n"
624 . "${t}This option only works on VOS at this time.\n"
625 . "-keep\n"
626 . "${t}You may give this option if you DO NOT want ANY\n"
627 . "${t}of the files generated by the tests to be deleted. \n"
628 . "${t}Without this option, all files generated by the test will\n"
629 . "${t}be deleted IF THE TEST PASSES.\n"
630 . "-debug\n"
631 . "${t}Use this option if you would like to see all of the system\n"
632 . "${t}calls issued and their return status while running the tests\n"
633 . "${t}This can be helpful if you're having a problem adding a test\n"
634 . "${t}to the suite, or if the test fails!\n";
635
636 foreach $line (@morehelp)
637 {
638 $tline = $line;
639 if (substr ($tline, 0, 1) eq "\t")
640 {
641 substr ($tline, 0, 1) = $t;
642 }
643 print "$tline\n";
644 }
645}
646
647#######################################################################
648########### Generic Test Driver Subroutines ###########
649#######################################################################
650
651sub get_caller
652{
653 local($depth);
654 local($package);
655 local($filename);
656 local($linenum);
657
658 $depth = defined ($_[0]) ? $_[0] : 1;
659 ($package, $filename, $linenum) = caller ($depth + 1);
660 return "$filename: $linenum";
661}
662
663sub error
664{
665 local($message) = $_[0];
666 local($caller) = &get_caller (1);
667
668 if (defined ($_[1]))
669 {
670 $caller = &get_caller ($_[1] + 1) . " -> $caller";
671 }
672
673 die "$caller: $message";
674}
675
676sub compare_output
677{
678 local($answer,$logfile) = @_;
679 local($slurp, $answer_matched) = ('', 0);
680
681 print "Comparing Output ........ " if $debug;
682
683 $slurp = &read_file_into_string ($logfile);
684
685 # For make, get rid of any time skew error before comparing--too bad this
686 # has to go into the "generic" driver code :-/
687 $slurp =~ s/^.*modification time .*in the future.*\n//gm;
688 $slurp =~ s/^.*Clock skew detected.*\n//gm;
689
690 ++$tests_run;
691
692 if ($slurp eq $answer) {
693 $answer_matched = 1;
694 } else {
695 # See if it is a slash or CRLF problem
696 local ($answer_mod, $slurp_mod) = ($answer, $slurp);
697
698 $answer_mod =~ tr,\\,/,;
699 $answer_mod =~ s,\r\n,\n,gs;
700
701 $slurp_mod =~ tr,\\,/,;
702 $slurp_mod =~ s,\r\n,\n,gs;
703
704 $answer_matched = ($slurp_mod eq $answer_mod);
705
706 # If it still doesn't match, see if the answer might be a regex.
707 if (!$answer_matched && $answer =~ m,^/(.+)/$,) {
708 $answer_matched = ($slurp =~ /$1/);
709 if (!$answer_matched && $answer_mod =~ m,^/(.+)/$,) {
710 $answer_matched = ($slurp_mod =~ /$1/);
711 }
712 }
713 }
714
715 if ($answer_matched && $test_passed)
716 {
717 print "ok\n" if $debug;
718 ++$tests_passed;
719 return 1;
720 }
721
722 if (! $answer_matched) {
723 print "DIFFERENT OUTPUT\n" if $debug;
724
725 &create_file (&get_basefile, $answer);
726 &create_file (&get_runfile, $command_string);
727
728 print "\nCreating Difference File ...\n" if $debug;
729
730 # Create the difference file
731
732 local($command) = "diff -c " . &get_basefile . " " . $logfile;
733 &run_command_with_output(&get_difffile,$command);
734 } else {
735 &rmfiles ();
736 }
737
738 $suite_passed = 0;
739 return 0;
740}
741
742sub read_file_into_string
743{
744 local($filename) = @_;
745 local($oldslash) = $/;
746
747 undef $/;
748
749 open (RFISFILE, $filename) || return "";
750 local ($slurp) = <RFISFILE>;
751 close (RFISFILE);
752
753 $/ = $oldslash;
754
755 return $slurp;
756}
757
758sub attach_default_output
759{
760 local ($filename) = @_;
761 local ($code);
762
763 if ($vos)
764 {
765 $code = system "++attach_default_output_hack $filename";
766 $code == -2 || &error ("adoh death\n", 1);
767 return 1;
768 }
769
770 open ("SAVEDOS" . $default_output_stack_level . "out", ">&STDOUT")
771 || &error ("ado: $! duping STDOUT\n", 1);
772 open ("SAVEDOS" . $default_output_stack_level . "err", ">&STDERR")
773 || &error ("ado: $! duping STDERR\n", 1);
774
775 open (STDOUT, "> " . $filename)
776 || &error ("ado: $filename: $!\n", 1);
777 open (STDERR, ">&STDOUT")
778 || &error ("ado: $filename: $!\n", 1);
779
780 $default_output_stack_level++;
781}
782
783# close the current stdout/stderr, and restore the previous ones from
784# the "stack."
785
786sub detach_default_output
787{
788 local ($code);
789
790 if ($vos)
791 {
792 $code = system "++detach_default_output_hack";
793 $code == -2 || &error ("ddoh death\n", 1);
794 return 1;
795 }
796
797 if (--$default_output_stack_level < 0)
798 {
799 &error ("default output stack has flown under!\n", 1);
800 }
801
802 close (STDOUT);
803 close (STDERR);
804
805 open (STDOUT, ">&SAVEDOS" . $default_output_stack_level . "out")
806 || &error ("ddo: $! duping STDOUT\n", 1);
807 open (STDERR, ">&SAVEDOS" . $default_output_stack_level . "err")
808 || &error ("ddo: $! duping STDERR\n", 1);
809
810 close ("SAVEDOS" . $default_output_stack_level . "out")
811 || &error ("ddo: $! closing SCSDOSout\n", 1);
812 close ("SAVEDOS" . $default_output_stack_level . "err")
813 || &error ("ddo: $! closing SAVEDOSerr\n", 1);
814}
815
816# This runs a command without any debugging info.
817sub _run_command
818{
819 my $code;
820
821 # We reset this before every invocation. On Windows I think there is only
822 # one environment, not one per process, so I think that variables set in
823 # test scripts might leak into subsequent tests if this isn't reset--???
824 resetENV();
825
826 eval {
827 local $SIG{ALRM} = sub { die "timeout\n"; };
828 alarm $test_timeout;
829 $code = system(@_);
830 alarm 0;
831 };
832 if ($@) {
833 # The eval failed. If it wasn't SIGALRM then die.
834 $@ eq "timeout\n" or die;
835
836 # Timed out. Resend the alarm to our process group to kill the children.
837 $SIG{ALRM} = 'IGNORE';
838 kill -14, $$;
839 $code = 14;
840 }
841
842 return $code;
843}
844
845# run one command (passed as a list of arg 0 - n), returning 0 on success
846# and nonzero on failure.
847
848sub run_command
849{
850 print "\nrun_command: @_\n" if $debug;
851 my $code = _run_command(@_);
852 print "run_command returned $code.\n" if $debug;
853
854 return $code;
855}
856
857# run one command (passed as a list of arg 0 - n, with arg 0 being the
858# second arg to this routine), returning 0 on success and non-zero on failure.
859# The first arg to this routine is a filename to connect to the stdout
860# & stderr of the child process.
861
862sub run_command_with_output
863{
864 my $filename = shift;
865
866 print "\nrun_command_with_output($filename,$runname): @_\n" if $debug;
867 &attach_default_output ($filename);
868 my $code = _run_command(@_);
869 &detach_default_output;
870 print "run_command_with_output returned $code.\n" if $debug;
871
872 return $code;
873}
874
875# performs the equivalent of an "rm -rf" on the first argument. Like
876# rm, if the path ends in /, leaves the (now empty) directory; otherwise
877# deletes it, too.
878
879sub remove_directory_tree
880{
881 local ($targetdir) = @_;
882 local ($nuketop) = 1;
883 local ($ch);
884
885 $ch = substr ($targetdir, length ($targetdir) - 1);
886 if ($ch eq "/" || $ch eq $pathsep)
887 {
888 $targetdir = substr ($targetdir, 0, length ($targetdir) - 1);
889 $nuketop = 0;
890 }
891
892 if (! -e $targetdir)
893 {
894 return 1;
895 }
896
897 &remove_directory_tree_inner ("RDT00", $targetdir) || return 0;
898 if ($nuketop)
899 {
900 rmdir $targetdir || return 0;
901 }
902
903 return 1;
904}
905
906sub remove_directory_tree_inner
907{
908 local ($dirhandle, $targetdir) = @_;
909 local ($object);
910 local ($subdirhandle);
911
912 opendir ($dirhandle, $targetdir) || return 0;
913 $subdirhandle = $dirhandle;
914 $subdirhandle++;
915 while ($object = readdir ($dirhandle))
916 {
917 if ($object =~ /^(\.\.?|CVS|RCS)$/)
918 {
919 next;
920 }
921
922 $object = "$targetdir$pathsep$object";
923 lstat ($object);
924
925 if (-d _ && &remove_directory_tree_inner ($subdirhandle, $object))
926 {
927 rmdir $object || return 0;
928 }
929 else
930 {
931 unlink $object || return 0;
932 }
933 }
934 closedir ($dirhandle);
935 return 1;
936}
937
938# We used to use this behavior for this function:
939#
940#sub touch
941#{
942# local (@filenames) = @_;
943# local ($now) = time;
944# local ($file);
945#
946# foreach $file (@filenames)
947# {
948# utime ($now, $now, $file)
949# || (open (TOUCHFD, ">> $file") && close (TOUCHFD))
950# || &error ("Couldn't touch $file: $!\n", 1);
951# }
952# return 1;
953#}
954#
955# But this behaves badly on networked filesystems where the time is
956# skewed, because it sets the time of the file based on the _local_
957# host. Normally when you modify a file, it's the _remote_ host that
958# determines the modtime, based on _its_ clock. So, instead, now we open
959# the file and write something into it to force the remote host to set
960# the modtime correctly according to its clock.
961#
962
963sub touch
964{
965 local ($file);
966
967 foreach $file (@_) {
968 (open(T, ">> $file") && print(T "\n") && close(T))
969 || &error("Couldn't touch $file: $!\n", 1);
970 }
971}
972
973# Touch with a time offset. To DTRT, call touch() then use stat() to get the
974# access/mod time for each file and apply the offset.
975
976sub utouch
977{
978 local ($off) = shift;
979 local ($file);
980
981 &touch(@_);
982
983 local (@s) = stat($_[0]);
984
985 utime($s[8]+$off, $s[9]+$off, @_);
986}
987
988# open a file, write some stuff to it, and close it.
989
990sub create_file
991{
992 local ($filename, @lines) = @_;
993
994 open (CF, "> $filename") || &error ("Couldn't open $filename: $!\n", 1);
995 foreach $line (@lines)
996 {
997 print CF $line;
998 }
999 close (CF);
1000}
1001
1002# create a directory tree described by an associative array, wherein each
1003# key is a relative pathname (using slashes) and its associated value is
1004# one of:
1005# DIR indicates a directory
1006# FILE:contents indicates a file, which should contain contents +\n
1007# LINK:target indicates a symlink, pointing to $basedir/target
1008# The first argument is the dir under which the structure will be created
1009# (the dir will be made and/or cleaned if necessary); the second argument
1010# is the associative array.
1011
1012sub create_dir_tree
1013{
1014 local ($basedir, %dirtree) = @_;
1015 local ($path);
1016
1017 &remove_directory_tree ("$basedir");
1018 mkdir ($basedir, 0777) || &error ("Couldn't mkdir $basedir: $!\n", 1);
1019
1020 foreach $path (sort keys (%dirtree))
1021 {
1022 if ($dirtree {$path} =~ /^DIR$/)
1023 {
1024 mkdir ("$basedir/$path", 0777)
1025 || &error ("Couldn't mkdir $basedir/$path: $!\n", 1);
1026 }
1027 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1028 {
1029 &create_file ("$basedir/$path", $1 . "\n");
1030 }
1031 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1032 {
1033 symlink ("$basedir/$1", "$basedir/$path")
1034 || &error ("Couldn't symlink $basedir/$path -> $basedir/$1: $!\n", 1);
1035 }
1036 else
1037 {
1038 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1039 }
1040 }
1041 if ($just_setup_tree)
1042 {
1043 die "Tree is setup...\n";
1044 }
1045}
1046
1047# compare a directory tree with an associative array in the format used
1048# by create_dir_tree, above.
1049# The first argument is the dir under which the structure should be found;
1050# the second argument is the associative array.
1051
1052sub compare_dir_tree
1053{
1054 local ($basedir, %dirtree) = @_;
1055 local ($path);
1056 local ($i);
1057 local ($bogus) = 0;
1058 local ($contents);
1059 local ($target);
1060 local ($fulltarget);
1061 local ($found);
1062 local (@files);
1063 local (@allfiles);
1064
1065 opendir (DIR, $basedir) || &error ("Couldn't open $basedir: $!\n", 1);
1066 @allfiles = grep (!/^(\.\.?|CVS|RCS)$/, readdir (DIR) );
1067 closedir (DIR);
1068 if ($debug)
1069 {
1070 print "dirtree: (%dirtree)\n$basedir: (@allfiles)\n";
1071 }
1072
1073 foreach $path (sort keys (%dirtree))
1074 {
1075 if ($debug)
1076 {
1077 print "Checking $path ($dirtree{$path}).\n";
1078 }
1079
1080 $found = 0;
1081 foreach $i (0 .. $#allfiles)
1082 {
1083 if ($allfiles[$i] eq $path)
1084 {
1085 splice (@allfiles, $i, 1); # delete it
1086 if ($debug)
1087 {
1088 print " Zapped $path; files now (@allfiles).\n";
1089 }
1090 lstat ("$basedir/$path");
1091 $found = 1;
1092 last;
1093 }
1094 }
1095
1096 if (!$found)
1097 {
1098 print "compare_dir_tree: $path does not exist.\n";
1099 $bogus = 1;
1100 next;
1101 }
1102
1103 if ($dirtree {$path} =~ /^DIR$/)
1104 {
1105 if (-d _ && opendir (DIR, "$basedir/$path") )
1106 {
1107 @files = readdir (DIR);
1108 closedir (DIR);
1109 @files = grep (!/^(\.\.?|CVS|RCS)$/ && ($_ = "$path/$_"), @files);
1110 push (@allfiles, @files);
1111 if ($debug)
1112 {
1113 print " Read in $path; new files (@files).\n";
1114 }
1115 }
1116 else
1117 {
1118 print "compare_dir_tree: $path is not a dir.\n";
1119 $bogus = 1;
1120 }
1121 }
1122 elsif ($dirtree {$path} =~ /^FILE:(.*)$/)
1123 {
1124 if (-l _ || !-f _)
1125 {
1126 print "compare_dir_tree: $path is not a file.\n";
1127 $bogus = 1;
1128 next;
1129 }
1130
1131 if ($1 ne "*")
1132 {
1133 $contents = &read_file_into_string ("$basedir/$path");
1134 if ($contents ne "$1\n")
1135 {
1136 print "compare_dir_tree: $path contains wrong stuff."
1137 . " Is:\n$contentsShould be:\n$1\n";
1138 $bogus = 1;
1139 }
1140 }
1141 }
1142 elsif ($dirtree {$path} =~ /^LINK:(.*)$/)
1143 {
1144 $target = $1;
1145 if (!-l _)
1146 {
1147 print "compare_dir_tree: $path is not a link.\n";
1148 $bogus = 1;
1149 next;
1150 }
1151
1152 $contents = readlink ("$basedir/$path");
1153 $contents =~ tr/>/\//;
1154 $fulltarget = "$basedir/$target";
1155 $fulltarget =~ tr/>/\//;
1156 if (!($contents =~ /$fulltarget$/))
1157 {
1158 if ($debug)
1159 {
1160 $target = $fulltarget;
1161 }
1162 print "compare_dir_tree: $path should be link to $target, "
1163 . "not $contents.\n";
1164 $bogus = 1;
1165 }
1166 }
1167 else
1168 {
1169 &error ("Bogus dirtree type: \"$dirtree{$path}\"\n", 1);
1170 }
1171 }
1172
1173 if ($debug)
1174 {
1175 print "leftovers: (@allfiles).\n";
1176 }
1177
1178 foreach $file (@allfiles)
1179 {
1180 print "compare_dir_tree: $file should not exist.\n";
1181 $bogus = 1;
1182 }
1183
1184 return !$bogus;
1185}
1186
1187# this subroutine generates the numeric suffix used to keep tmp filenames,
1188# log filenames, etc., unique. If the number passed in is 1, then a null
1189# string is returned; otherwise, we return ".n", where n + 1 is the number
1190# we were given.
1191
1192sub num_suffix
1193{
1194 local($num) = @_;
1195
1196 if (--$num > 0) {
1197 return "$extext$num";
1198 }
1199
1200 return "";
1201}
1202
1203# This subroutine returns a log filename with a number appended to
1204# the end corresponding to how many logfiles have been created in the
1205# current running test. An optional parameter may be passed (0 or 1).
1206# If a 1 is passed, then it does NOT increment the logfile counter
1207# and returns the name of the latest logfile. If either no parameter
1208# is passed at all or a 0 is passed, then the logfile counter is
1209# incremented and the new name is returned.
1210
1211sub get_logfile
1212{
1213 local($no_increment) = @_;
1214
1215 $num_of_logfiles += !$no_increment;
1216
1217 return ($log_filename . &num_suffix ($num_of_logfiles));
1218}
1219
1220# This subroutine returns a base (answer) filename with a number
1221# appended to the end corresponding to how many logfiles (and thus
1222# base files) have been created in the current running test.
1223# NO PARAMETERS ARE PASSED TO THIS SUBROUTINE.
1224
1225sub get_basefile
1226{
1227 return ($base_filename . &num_suffix ($num_of_logfiles));
1228}
1229
1230# This subroutine returns a difference filename with a number appended
1231# to the end corresponding to how many logfiles (and thus diff files)
1232# have been created in the current running test.
1233
1234sub get_difffile
1235{
1236 return ($diff_filename . &num_suffix ($num_of_logfiles));
1237}
1238
1239# This subroutine returns a command filename with a number appended
1240# to the end corresponding to how many logfiles (and thus command files)
1241# have been created in the current running test.
1242
1243sub get_runfile
1244{
1245 return ($run_filename . &num_suffix ($num_of_logfiles));
1246}
1247
1248# just like logfile, only a generic tmp filename for use by the test.
1249# they are automatically cleaned up unless -keep was used, or the test fails.
1250# Pass an argument of 1 to return the same filename as the previous call.
1251
1252sub get_tmpfile
1253{
1254 local($no_increment) = @_;
1255
1256 $num_of_tmpfiles += !$no_increment;
1257
1258 return ($tmp_filename . &num_suffix ($num_of_tmpfiles));
1259}
1260
12611;
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette