VirtualBox

source: kBuild/trunk/src/gmakenew/tests/test_driver.pl@ 910

Last change on this file since 910 was 903, checked in by bird, 18 years ago

Merged with the 2007-05-23 CVS. Added rsort and fixed a couple of windows build issues.

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