VirtualBox

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

Last change on this file since 1969 was 1953, checked in by bird, 16 years ago

test_driver.pl: added -kmk option, setting $is_kmk.

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