7 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10 @EXPORT = qw(setup indir app test run);
11 @EXPORT_OK = qw(top_dir top_file pipe with cmdstr quotify));
15 use File::Spec::Functions qw/file_name_is_absolute curdir canonpath splitdir
16 catdir catfile splitpath catpath devnull abs2rel
18 use File::Path qw/remove_tree mkpath/;
22 my $test_name = undef;
24 my %directories = (); # Directories we want to keep track of
25 # TOP, APPS, TEST and RESULTS are the
26 # ones we're interested in, corresponding
27 # to the environment variables TOP (mandatory),
28 # BIN_D, TEST_D and RESULT_D.
33 BAIL_OUT("Must run setup() first") if (! $test_name);
36 return catfile($directories{TOP},@_,$f);
40 BAIL_OUT("Must run setup() first") if (! $test_name);
43 return catfile($directories{TEST},@_,$f);
47 BAIL_OUT("Must run setup() first") if (! $test_name);
50 return catfile($directories{APPS},@_,$f);
54 BAIL_OUT("Must run setup() first") if (! $test_name);
57 return catfile($directories{RESULTS},@_,$f);
61 return __results_file("$test_name.log");
65 return __top_file(@_, ""); # This caters for operating systems that have
66 # a very distinct syntax for directories.
69 return __top_file(@_);
75 my $abscurdir = rel2abs(curdir());
76 my $absdir = rel2abs($dir);
77 my $reverse = abs2rel($abscurdir, $absdir);
79 # PARANOIA: if we're not moving anywhere, we do nothing more
80 if ($abscurdir eq $absdir) {
84 # Do not support a move to a different volume for now. Maybe later.
85 BAIL_OUT("FAILURE: \"$dir\" moves to a different volume, not supported")
86 if $reverse eq $abscurdir;
88 # If someone happened to give a directory that leads back to the current,
89 # it's extremely silly to do anything more, so just simulate that we did
91 # In this case, we won't even clean it out, for safety's sake.
92 return "." if $reverse eq "";
94 $dir = canonpath($dir);
99 # Should we just bail out here as well? I'm unsure.
100 return undef unless chdir($dir);
102 if ($opts{cleanup}) {
103 remove_tree(".", { safe => 0, keep_root => 1 });
106 # For each of these directory variables, figure out where they are relative
107 # to the directory we want to move to if they aren't absolute (if they are,
108 # they don't change!)
109 my @dirtags = ("TOP", "TEST", "APPS", "RESULTS");
111 if (!file_name_is_absolute($directories{$_})) {
112 my $newpath = abs2rel(rel2abs($directories{$_}), rel2abs($dir));
113 $directories{$_} = $newpath;
118 print STDERR "DEBUG: __cwd(), directories and files:\n";
119 print STDERR " \$directories{TEST} = \"$directories{TEST}\"\n";
120 print STDERR " \$directories{RESULTS} = \"$directories{RESULTS}\"\n";
121 print STDERR " \$directories{APPS} = \"$directories{APPS}\"\n";
122 print STDERR " \$directories{TOP} = \"$directories{TOP}\"\n";
123 print STDERR " \$test_log = \"",__test_log(),"\"\n";
125 print STDERR " current directory is \"",curdir(),"\"\n";
126 print STDERR " the way back is \"$reverse\"\n";
135 BAIL_OUT("setup() must receive a name") unless $test_name;
136 BAIL_OUT("setup() needs \$TOP to be defined") unless $ENV{TOP};
138 $directories{TOP} = $ENV{TOP},
139 $directories{APPS} = $ENV{BIN_D} || catdir($directories{TOP},"apps");
140 $directories{TEST} = $ENV{TEST_D} || catdir($directories{TOP},"test");
141 $directories{RESULTS} = $ENV{RESULT_D} || $directories{TEST};
143 BAIL_OUT("setup() expects the file Configure in the \$TOP directory")
144 unless -f top_file("Configure");
146 __cwd($directories{RESULTS});
148 # Loop in case we're on a platform with more than one file generation
149 1 while unlink(__test_log());
154 my $codeblock = shift;
157 my $reverse = __cwd($subdir,%opts);
158 BAIL_OUT("FAILURE: indir, \"$subdir\" wasn't possible to move into")
165 if ($opts{cleanup}) {
166 remove_tree($subdir, { safe => 0 });
171 exit_checker => sub { return shift == 0 ? 1 : 0 }
177 my $codeblock = shift;
179 my %saved_hooks = ();
181 foreach (keys %opts) {
182 $saved_hooks{$_} = $hooks{$_} if exists($hooks{$_});
183 $hooks{$_} = $opts{$_};
188 foreach (keys %saved_hooks) {
189 $hooks{$_} = $saved_hooks{$_};
196 my $prefix = __top_file("util", "shlib_wrap.sh")." ";
197 my $ext = $ENV{"EXE_EXT"} || "";
199 if ( $^O eq "VMS" ) { # VMS
202 } elsif ($^O eq "MSWin32") { # Windows
207 # We test both with and without extension. The reason
208 # is that we might, for example, be passed a Perl script
210 my $file = "$prog$ext";
212 return $prefix.$file;
213 } elsif ( -f $prog ) {
217 print STDERR "$prog not found\n";
222 BAIL_OUT("Must run setup() first") if (! $test_name);
225 my $path_builder = shift;
226 my $cmd = __fixup_cmd($path_builder->(shift @{$_[0]}));
227 my @args = @{$_[0]}; shift;
233 my $null = devnull();
236 $arg_str = " ".join(" ", quotify @args) if @args;
238 my $fileornull = sub { $_[0] ? $_[0] : $null; };
242 my $saved_stderr = undef;
243 $stdin = " < ".$fileornull->($opts{stdin}) if exists($opts{stdin});
244 $stdout= " > ".$fileornull->($opts{stdout}) if exists($opts{stdout});
245 $stderr=" 2> ".$fileornull->($opts{stderr}) if exists($opts{stderr});
247 $saved_stderr = $opts{stderr} if defined($opts{stderr});
249 my $errlog = $num ? "$test_name.$num.tmp_err" : "$test_name.tmp_err";
250 my $display_cmd = "$cmd$arg_str$stdin$stdout$stderr";
251 $cmd .= "$arg_str$stdin$stdout 2> $errlog";
253 return ($cmd, $display_cmd, $errlog => $saved_stderr);
259 return sub { my $num = shift;
260 return __build_cmd($num, \&__apps_file, $cmd, %opts); }
266 return sub { my $num = shift;
267 return __build_cmd($num, \&__test_file, $cmd, %opts); }
271 my ($cmd, $display_cmd, %errlogs) = shift->(0);
277 my ($cmd, $display_cmd, %errlogs) = shift->(0);
283 if ( $^O eq "VMS" ) { # VMS
285 } elsif ($^O eq "MSWin32") { # MSYS
292 if ($opts{capture}) {
296 system("$prefix$cmd");
298 $r = $hooks{exit_checker}->($e);
301 # At this point, $? stops being interesting, and unfortunately,
302 # there are Test::More versions that get picky if we leave it
306 open ERR, ">>", __test_log();
307 { local $| = 1; print ERR "$display_cmd => $e\n"; }
308 foreach (keys %errlogs) {
310 copy($_,$errlogs{$_}) if defined($errlogs{$_});
315 if ($opts{capture}) {
331 my ($c, $dc, @el) = $_->(++$counter);
347 # Utility functions, some of which are exported on request
350 # Unix setup (default if nothing else is mentioned)
352 sub { $_ = shift; /\s|[\{\}\\\$\[\]\*\?\|\&:;<>]/ ? "'$_'" : $_ };
354 if ( $^O eq "VMS") { # VMS setup
355 $arg_formatter = sub {
357 if (/\s|["[:upper:]]/) {
364 } elsif ( $^O eq "MSWin32") { # MSWin setup
365 $arg_formatter = sub {
367 if (/\s|["\|\&\*\;<>]/) {
376 return map { $arg_formatter->($_) } @_;