VirtualBox

source: kBuild/vendor/gnumake/2008-10-28/tests/test_driver.pl

Last change on this file was 1989, checked in by bird, 16 years ago

Load gnumake-2008-10-28-CVS into vendor/gnumake/current.

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