VirtualBox

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

Last change on this file since 1982 was 1973, checked in by bird, 16 years ago

tests: added a -fast option to indicate kmk_fgmake.

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