VirtualBox

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

Last change on this file since 1952 was 1937, checked in by bird, 16 years ago

GNU Make tests: added check for case insensitive file system, fixed default_names so it'll for on HFS+.

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