10 # Don't let our own verbosity/test_file get mixed up with our subprocess's
11 my @makefile_keys = qw(TEST_VERBOSE HARNESS_VERBOSE TEST_FILES MAKEFLAGS);
12 local @ENV{@makefile_keys};
13 delete @ENV{@makefile_keys};
15 my @makefile_types = qw(small passthrough traditional);
16 my $tests_per_type = 15;
18 #find_in_path does not understand VMS.
20 if ( $Config{make} && $^O ne 'VMS' ? find_in_path($Config{make}) : 1 ) {
23 plan skip_all => "Don't know how to invoke 'make'";
26 my $is_vms_mms = ($^O eq 'VMS') && ($Config{make} =~ /MM[SK]/i);
28 blib_load('Module::Build');
29 blib_load('Module::Build::Version');
32 #########################
34 my $tmp = MBTest->tmpdir;
36 # Create test distribution; set requires and build_requires
38 my $dist = DistGen->new( dir => $tmp );
44 #########################
46 blib_load('Module::Build');
47 blib_load('Module::Build::Compat');
49 use Carp; $SIG{__WARN__} = \&Carp::cluck;
51 my @make = $Config{make} eq 'nmake' ? ('nmake', '-nologo') : ($Config{make});
53 my $makefile = 'Makefile';
55 # VMS MMK/MMS by convention use Descrip.MMS
57 $makefile = 'Descrip.MMS';
61 #########################
63 # Test without requires
65 test_makefile_types();
67 # Test with requires and PL_files
69 my $distname = $dist->name;
70 $dist->change_build_pl({
71 module_name => $distname,
81 PL_files => { 'foo.PL' => 'foo' },
84 $dist->add_file("foo.PL", <<'END');
85 open my $fh, ">$ARGV[0]" or die $!;
105 ######################
107 $dist->change_build_pl({
108 module_name => $distname,
113 # Create M::B instance but don't pollute STDOUT
115 stdout_stderr_of( sub {
116 $mb = Module::Build->new_from_context;
118 ok $mb, "Module::Build->new_from_context";
122 # Make sure fake_makefile() can run without 'build_class', as it may be
123 # in older-generated Makefile.PLs
125 local $SIG{__WARN__} = sub { $warning = shift; };
127 my $maketext = eval { Module::Build::Compat->fake_makefile(makefile => $makefile) };
128 is $@, '', "fake_makefile lived";
129 like $maketext, qr/^realclean/m, "found 'realclean' in fake_makefile output";
130 like $warning, qr/build_class/, "saw warning about 'build_class'";
134 # Make sure custom builder subclass is used in the created
135 # Makefile.PL - make sure it fails in the right way here.
136 local @Foo::Builder::ISA = qw(Module::Build);
138 stdout_stderr_of( sub {
139 $foo_builder = Foo::Builder->new_from_context;
141 foreach my $style ('passthrough', 'small') {
142 create_makefile_pl($style, $foo_builder);
144 # Should fail with "can't find Foo/Builder.pm"
146 my ($stdout, $stderr ) = stdout_stderr_of (sub {
147 $result = $mb->run_perl_script('Makefile.PL');
149 ok ! $result, "Makefile.PL failed";
150 like $stderr, qr{Foo/Builder.pm}, "custom builder wasn't found";
153 # Now make sure it can actually work.
155 stdout_stderr_of( sub {
156 $bar_builder = Module::Build->subclass( class => 'Bar::Builder' )->new_from_context;
158 foreach my $style ('passthrough', 'small') {
159 create_makefile_pl($style, $bar_builder);
161 stdout_stderr_of( sub {
162 $result = $mb->run_perl_script('Makefile.PL');
164 ok $result, "Makefile.PL ran without error";
169 # Make sure various Makefile.PL arguments are supported
170 create_makefile_pl('passthrough', $mb);
172 my $libdir = File::Spec->catdir( $tmp, 'libdir' );
174 stdout_stderr_of( sub {
175 $result = $mb->run_perl_script('Makefile.PL', [],
184 ok $result, "passthrough Makefile.PL ran with arguments";
185 ok -e 'Build.PL', "Build.PL generated";
187 my $new_build = Module::Build->resume();
188 is $new_build->installdirs, 'core', "installdirs is core";
189 is $new_build->verbose, 1, "tests set for verbose";
190 is $new_build->install_destination('lib'), $libdir, "custom libdir";
191 is $new_build->extra_compiler_flags->[0], '-DPERL_POLLUTE', "PERL_POLLUTE set";
193 # Make sure those switches actually had an effect
194 my ($ran_ok, $output);
195 $output = stdout_stderr_of( sub { $ran_ok = $new_build->do_system(@make, 'test') } );
196 ok $ran_ok, "make test ran without error";
197 $output =~ s/^/# /gm; # Don't confuse our own test output
198 like $output, qr/(?:# ok \d+\s+)+/, 'Should be verbose';
200 # Make sure various Makefile arguments are supported
201 my $make_macro = 'TEST_VERBOSE=0';
203 # VMS MMK/MMS macros use different syntax.
205 $make_macro = '/macro=("' . $make_macro . '")';
208 $output = stdout_stderr_of( sub {
209 local $ENV{HARNESS_TIMER}; # RT#39635 - timer messes with output
210 $ran_ok = $mb->do_system(@make, 'test', $make_macro)
213 ok $ran_ok, "make test without verbose ran ok";
214 $output =~ s/^/# /gm; # Don't confuse our own test output
216 qr/# .+basic(\.t)?[.\s#]+ok[.\s#]+All tests successful/,
217 'Should be non-verbose';
219 (my $libdir2 = $libdir) =~ s/libdir/lbiidr/;
220 my $libarch2 = File::Spec->catdir($libdir2, 'arch');
225 label => "INSTALLDIRS=vendor",
226 args => [ 'INSTALLDIRS=vendor', "INSTALLVENDORLIB=$libdir2", "INSTALLVENDORARCH=$libarch2"],
227 check => qr/\Q$libdir2\E .* Simple\.pm/ix,
230 label => "PREFIX=\$libdir2",
231 args => [ "PREFIX=$libdir2"],
232 check => qr/\Q$libdir2\E .* Simple\.pm/ix,
235 label => "PREFIX=\$libdir2 LIB=mylib",
236 args => [ "PREFIX=$libdir2", "LIB=mylib" ],
237 check => qr{\Q$libdir2\E[/\\]mylib[/\\]Simple\.pm}ix,
241 require ExtUtils::Install;
242 skip "Needs ExtUtils::Install 1.32 or later", 2 * @cases
243 if ExtUtils::Install->VERSION < 1.32;
245 skip "Needs upstream patch at http://rt.cpan.org/Public/Bug/Display.html?id=55288", 2 * @cases
249 my @make_args = @{$c->{args}};
250 if ($is_vms_mms) { # VMS MMK/MMS macros use different syntax.
251 $make_args[0] = '/macro=("' . join('","',@make_args) . '")';
252 pop @make_args while scalar(@make_args) > 1;
254 ($output) = stdout_stderr_of(
256 $result = $mb->run_perl_script('Makefile.PL', [], \@make_args);
257 $ran_ok = $mb->do_system(@make, 'fakeinstall');
261 ok $ran_ok, "fakeinstall $c->{label} ran ok";
262 $output =~ s/^/# /gm; # Don't confuse our own test output
263 like $output, $c->{check},
264 "Saw destination directory for $c->{label}";
268 stdout_stderr_of( sub { $mb->do_system(@make, 'realclean'); } );
269 ok ! -e $makefile, "$makefile shouldn't exist";
271 1 while unlink 'Makefile.PL';
272 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
274 1 while unlink $libdir, $libdir2;
277 { # Make sure tilde-expansion works
279 # C<glob> on MSWin32 uses $ENV{HOME} if defined to do tilde-expansion
280 local $ENV{HOME} = 'C:/' if $^O =~ /MSWin/ && !exists( $ENV{HOME} );
282 create_makefile_pl('passthrough', $mb);
284 stdout_stderr_of( sub {
285 $mb->run_perl_script('Makefile.PL', [], ['INSTALL_BASE=~/foo']);
287 my $b2 = Module::Build->current;
288 ok $b2->install_base, "install_base set";
289 unlike $b2->install_base, qr/^~/, "Tildes should be expanded";
291 stdout_stderr_of( sub { $mb->do_system(@make, 'realclean'); } );
292 ok ! -e $makefile, "$makefile shouldn't exist";
294 1 while unlink 'Makefile.PL';
295 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
299 $dist->add_file('t/deep/foo.t', q{});
303 stdout_stderr_of( sub {
304 $mb = Module::Build->new_from_context( recursive_test_files => 1 );
307 create_makefile_pl('traditional', $mb);
308 my $args = extract_writemakefile_args() || {};
310 if ( exists $args->{test}->{TESTS} ) {
311 is $args->{test}->{TESTS},
313 File::Spec->catfile(qw(t *.t)),
314 File::Spec->catfile(qw(t deep *.t))
316 'Makefile.PL has correct TESTS line for recursive test files';
318 ok( ! exists $args->{TESTS}, 'Not using incorrect recursive tests key' );
323 #########################################################
326 my ($first, $second) = @_;
327 my $new = { %$first };
328 for my $k (keys %$second) {
329 if ( exists $new->{$k} ) {
330 my ($v1,$v2) = ($new->{$k},$second->{$k});
331 $new->{$k} = ($v1 > $v2 ? $v1 : $v2);
334 $new->{$k} = $second->{$k};
340 sub test_makefile_types {
342 $opts{requires} ||= {};
343 $opts{build_requires} ||= {};
344 $opts{PL_files} ||= {};
346 foreach my $type (@makefile_types) {
347 # Create M::B instance
349 stdout_stderr_of( sub {
350 $mb = Module::Build->new_from_context;
352 ok $mb, "Module::Build->new_from_context";
354 # Create and test Makefile.PL
355 create_makefile_pl($type, $mb);
357 test_makefile_pl_requires_perl( $opts{requires}{perl} );
358 test_makefile_creation($mb);
359 test_makefile_prereq_pm( _merge_prereqs($opts{requires}, $opts{build_requires}) );
360 test_makefile_pl_files( $opts{PL_files} ) if $type eq 'traditional';
362 my ($output,$success);
363 # Capture output to keep our STDOUT clean
364 $output = stdout_stderr_of( sub {
365 $success = $mb->do_system(@make);
367 ok $success, "make ran without error";
369 for my $file (values %{ $opts{PL_files} }) {
370 ok -e $file, "PL_files generated - $file";
373 # Can't let 'test' STDOUT go to our STDOUT, or it'll confuse Test::Harness.
374 $output = stdout_stderr_of( sub {
375 $success = $mb->do_system(@make, 'test');
377 ok $success, "make test ran without error";
378 like uc $output, qr{DONE\.|SUCCESS}, "make test output indicated success";
380 $output = stdout_stderr_of( sub {
381 $success = $mb->do_system(@make, 'realclean');
383 ok $success, "make realclean ran without error";
385 # Try again with some Makefile.PL arguments
386 test_makefile_creation($mb, [], 'INSTALLDIRS=vendor', 'realclean');
388 # Try again using distclean
389 test_makefile_creation($mb, [], '', 'distclean');
391 1 while unlink 'Makefile.PL';
392 ok ! -e 'Makefile.PL', "cleaned up Makefile";
396 sub test_makefile_creation {
397 my ($build, $preargs, $postargs, $cleanup) = @_;
399 my ($output, $result);
400 # capture output to avoid polluting our test output
401 $output = stdout_stderr_of( sub {
402 $result = $build->run_perl_script('Makefile.PL', $preargs, $postargs);
404 my $label = "Makefile.PL ran without error";
405 if ( defined $postargs && length $postargs ) {
406 $label .= " (postargs: $postargs)";
409 ok -e $makefile, "$makefile exists";
412 # default to 'realclean' unless we recognize the clean method
413 $cleanup = 'realclean' unless $cleanup =~ /^(dist|real)clean$/;
414 my ($stdout, $stderr ) = stdout_stderr_of (sub {
415 $build->do_system(@make, $cleanup);
417 ok ! -e $makefile, "$makefile cleaned up with $cleanup";
420 pass '(skipping cleanup)'; # keep test count constant
424 sub test_makefile_prereq_pm {
425 my %requires = %{ $_[0] };
426 delete $requires{perl}; # until EU::MM supports this
428 skip "$makefile not found", 1 unless -e $makefile;
429 my $prereq_pm = find_params_in_makefile()->{PREREQ_PM} || {};
430 is_deeply $prereq_pm, \%requires,
431 "$makefile has correct PREREQ_PM line";
435 sub test_makefile_pl_files {
436 my $expected = shift;
439 skip 1, 'Makefile.PL not found' unless -e 'Makefile.PL';
440 my $args = extract_writemakefile_args() || {};
441 is_deeply $args->{PL_FILES}, $expected,
442 "Makefile.PL has correct PL_FILES line";
446 sub test_makefile_pl_requires_perl {
447 my $perl_version = shift || q{};
449 skip 1, 'Makefile.PL not found' unless -e 'Makefile.PL';
450 my $file_contents = slurp 'Makefile.PL';
451 my $found_requires = $file_contents =~ m{^require $perl_version;}ms;
452 if (length $perl_version) {
453 ok $found_requires, "Makefile.PL has 'require $perl_version;'"
454 or diag "Makefile.PL:\n$file_contents";
457 ok ! $found_requires, "Makefile.PL does not require a perl version";
462 sub find_params_in_makefile {
463 my $fh = IO::File->new( $makefile, 'r' )
464 or die "Can't read $makefile: $!";
469 # Blank line after params.
470 last if keys %params and !/\S+/;
472 next unless m{^\# \s+ ( [A-Z_]+ ) \s+ => \s+ ( .* )$}x;
474 my($key, $val) = ($1, $2);
475 # extract keys and values
476 while ( $val =~ m/(?:\s)(\S+)=>(q\[.*?\]|undef),?/g ) {
478 if ($n =~ /^q\[(.*?)\]$/) {
481 $params{$key}{$m} = $n;
488 sub extract_writemakefile_args {
490 skip 1, 'Makefile.PL not found' unless -e 'Makefile.PL';
491 my $file_contents = slurp 'Makefile.PL';
492 my ($args) = $file_contents =~ m{^WriteMakefile\n\((.*)\).*;}ms;
493 ok $args, "Found WriteMakefile arguments"
494 or diag "Makefile.PL:\n$file_contents";
495 my %args = eval $args or diag $args; ## no critic
500 sub create_makefile_pl {
502 stdout_stderr_of( sub { Module::Build::Compat->create_makefile_pl(@args) } );
503 my $ok = ok -e 'Makefile.PL', "$_[0] Makefile.PL created";
505 # Some really conservative make's, like HP/UX, assume files with the same
506 # timestamp are out of date. Send the Makefile.PL one second into the past
507 # so its older than the Makefile it will generate.
508 # See [rt.cpan.org 45700]
509 my $mtime = (stat("Makefile.PL"))[9];
510 utime $mtime, $mtime - 1, "Makefile.PL";