1 package MakeMaker::Test::Utils;
8 our @ISA = qw(Exporter);
10 our $Is_VMS = $^O eq 'VMS';
11 our $Is_MacOS = $^O eq 'MacOS';
13 our @EXPORT = qw(which_perl perl_lib makefile_name makefile_backup
14 make make_run run make_macro calibrate_mtime
22 # Setup the code to clean out %ENV
24 # Environment variables which might effect our testing
25 my @delete_env_keys = qw(
35 # Remember the ENV values because on VMS %ENV is global
36 # to the user, not the process.
40 for my $key (@delete_env_keys) {
41 if( exists $ENV{$key} ) {
42 $restore_env_keys{$key} = delete $ENV{$key};
51 while( my($key, $val) = each %restore_env_keys ) {
61 MakeMaker::Test::Utils - Utility routines for testing MakeMaker
65 use MakeMaker::Test::Utils;
67 my $perl = which_perl;
70 my $makefile = makefile_name;
71 my $makefile_back = makefile_backup;
74 my $make_run = make_run;
75 make_macro($make, $targ, %macros);
77 my $mtime = calibrate_mtime;
81 my $have_compiler = have_compiler();
83 my $text = slurp($filename);
88 A consolidation of little utility functions used through out the
93 The following are exported by default.
99 my $perl = which_perl;
101 Returns a path to perl which is safe to use in a command line, no
102 matter where you chdir to.
110 # VMS should have 'perl' aliased properly
111 return $perl if $Is_VMS;
113 $perl .= $Config{exe_ext} unless $perl =~ m/$Config{exe_ext}$/i;
115 my $perlpath = File::Spec->rel2abs( $perl );
116 unless( $Is_MacOS || -x $perlpath ) {
117 # $^X was probably 'perl'
119 # When building in the core, *don't* go off and find
121 die "Can't find a perl to use (\$^X=$^X), (\$perlpath=$perlpath)"
124 foreach my $path (File::Spec->path) {
125 $perlpath = File::Spec->catfile($path, $perl);
126 last if -x $perlpath;
137 Sets up environment variables so perl can find its libraries.
141 my $old5lib = $ENV{PERL5LIB};
142 my $had5lib = exists $ENV{PERL5LIB};
145 my $lib = $ENV{PERL_CORE} ? qq{../lib}
146 # ExtUtils-MakeMaker/t/
148 $lib = File::Spec->rel2abs($lib);
150 push @libs, $ENV{PERL5LIB} if exists $ENV{PERL5LIB};
151 $ENV{PERL5LIB} = join($Config{path_sep}, @libs);
157 $ENV{PERL5LIB} = $old5lib;
160 delete $ENV{PERL5LIB};
165 =item B<makefile_name>
167 my $makefile = makefile_name;
169 MakeMaker doesn't always generate 'Makefile'. It returns what it
175 return $Is_VMS ? 'Descrip.MMS' : 'Makefile';
178 =item B<makefile_backup>
180 my $makefile_old = makefile_backup;
182 Returns the name MakeMaker will use for a backup of the current
187 sub makefile_backup {
188 my $makefile = makefile_name;
189 return $Is_VMS ? "$makefile".'_old' : "$makefile.old";
196 Returns a good guess at the make to run.
201 my $make = $Config{make};
202 $make = $ENV{MAKE} if exists $ENV{MAKE};
209 my $make_run = make_run;
211 Returns the make to run as with make() plus any necessary switches.
217 $make .= ' -nologo' if $make eq 'nmake';
224 my $make_cmd = make_macro($make, $target, %macros);
226 Returns the command necessary to run $make on the given $target using
229 my $make_test_verbose = make_macro(make_run(), 'test',
232 This is important because VMS's make utilities have a completely
233 different calling convention than Unix or Windows.
235 %macros is actually a list of tuples, so the order will be preserved.
240 my($make, $target) = (shift, shift);
242 my $is_mms = $make =~ /^MM(K|S)/i;
246 while( my($key,$val) = splice(@_, 0, 2) ) {
248 $macros .= qq{/macro="$key=$val"};
251 $macros .= qq{ $key=$val};
255 return $is_mms ? "$make$macros $target" : "$make $target $macros";
258 =item B<calibrate_mtime>
260 my $mtime = calibrate_mtime;
262 When building on NFS, file modification times can often lose touch
263 with reality. This returns the mtime of a file which has just been
268 sub calibrate_mtime {
269 open(FILE, ">calibrate_mtime.tmp") || die $!;
272 my($mtime) = (stat('calibrate_mtime.tmp'))[9];
273 unlink 'calibrate_mtime.tmp';
279 my $out = run($command);
280 my @out = run($command);
282 Runs the given $command as an external program returning at least STDOUT
283 as $out. If possible it will return STDOUT and STDERR combined as you
284 would expect to see on a screen.
293 # Unix, modern Windows and OS/2 from 5.005_54 up can handle can handle 2>&1
294 # This makes our failure diagnostics nicer to read.
295 if( MM->os_flavor_is('Unix') or
296 (MM->os_flavor_is('Win32') and !MM->os_flavor_is('Win9x')) or
297 ($] > 5.00554 and MM->os_flavor_is('OS/2'))
309 my @out = run_ok($cmd);
311 Like run() but it tests that the result exited normally.
313 The output from run() will be used as a diagnostic if it fails.
318 my $tb = Test::Builder->new;
322 $tb->cmp_ok( $?, '==', 0, "run(@_)" ) || $tb->diag(@out);
324 return wantarray ? @out : join "", @out;
327 =item B<setup_mm_test_root>
329 Creates a rooted logical to avoid the 8-level limit on older VMS systems.
330 No action taken on non-VMS systems.
334 sub setup_mm_test_root {
336 # On older systems we might exceed the 8-level directory depth limit
337 # imposed by RMS. We get around this with a rooted logical, but we
338 # can't create logical names with attributes in Perl, so we do it
339 # in a DCL subprocess and put it in the job table so the parent sees it.
340 open( MMTMP, '>mmtesttmp.com' ) ||
341 die "Error creating command file; $!";
342 print MMTMP <<'COMMAND';
343 $ MM_TEST_ROOT = F$PARSE("SYS$DISK:[-]",,,,"NO_CONCEAL")-".][000000"-"]["-"].;"+".]"
344 $ DEFINE/JOB/NOLOG/TRANSLATION=CONCEALED MM_TEST_ROOT 'MM_TEST_ROOT'
348 system '@mmtesttmp.com';
349 1 while unlink 'mmtesttmp.com';
355 $have_compiler = have_compiler;
357 Returns true if there is a compiler available for XS builds.
362 my $have_compiler = 0;
364 # ExtUtils::CBuilder prints its compilation lines to the screen.
367 local *STDOUT = *STDOUT;
368 local *STDERR = *STDERR;
370 tie *STDOUT, 'TieOut';
371 tie *STDERR, 'TieOut';
374 require ExtUtils::CBuilder;
375 my $cb = ExtUtils::CBuilder->new;
377 $have_compiler = $cb->have_compiler;
380 return $have_compiler;
385 $contents = slurp($filename);
387 Returns the $contents of $filename.
389 Will die if $filename cannot be opened.
394 my $filename = shift;
397 open my $fh, $filename or die "Can't open $filename for reading: $!";
408 Michael G Schwern <schwern@pobox.com>