4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
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 ) {
21 plan tests => 34 + @makefile_types*$tests_per_type*2;
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 use_ok 'Module::Build';
29 ensure_blib('Module::Build');
32 #########################
34 my $tmp = MBTest->tmpdir;
36 # Create test distribution; set requires and build_requires
38 my $dist = DistGen->new( dir => $tmp );
44 #########################
47 use 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();
69 my $distname = $dist->name;
70 $dist->change_build_pl({
71 module_name => $distname,
84 test_makefile_types( requires => {
90 ######################
92 $dist->change_build_pl({
93 module_name => $distname,
98 # Create M::B instance but don't pollute STDOUT
101 $mb = Module::Build->new_from_context;
103 ok $mb, "Module::Build->new_from_context";
107 # Make sure fake_makefile() can run without 'build_class', as it may be
108 # in older-generated Makefile.PLs
110 local $SIG{__WARN__} = sub { $warning = shift; };
112 my $maketext = eval { Module::Build::Compat->fake_makefile(makefile => $makefile) };
113 is $@, '', "fake_makefile lived";
114 like $maketext, qr/^realclean/m, "found 'realclean' in fake_makefile output";
115 like $warning, qr/build_class/, "saw warning about 'build_class'";
119 # Make sure custom builder subclass is used in the created
120 # Makefile.PL - make sure it fails in the right way here.
121 local @Foo::Builder::ISA = qw(Module::Build);
124 $foo_builder = Foo::Builder->new_from_context;
126 foreach my $style ('passthrough', 'small') {
127 Module::Build::Compat->create_makefile_pl($style, $foo_builder);
128 ok -e 'Makefile.PL', "$style Makefile.PL created";
130 # Should fail with "can't find Foo/Builder.pm"
132 my ($stdout, $stderr ) = stdout_stderr_of (sub {
133 $result = $mb->run_perl_script('Makefile.PL');
135 ok ! $result, "Makefile.PL failed";
136 like $stderr, qr{Foo/Builder.pm}, "custom builder wasn't found";
139 # Now make sure it can actually work.
142 $bar_builder = Module::Build->subclass( class => 'Bar::Builder' )->new_from_context;
144 foreach my $style ('passthrough', 'small') {
145 Module::Build::Compat->create_makefile_pl($style, $bar_builder);
146 ok -e 'Makefile.PL', "$style Makefile.PL created via subclass";
149 $result = $mb->run_perl_script('Makefile.PL');
151 ok $result, "Makefile.PL ran without error";
156 # Make sure various Makefile.PL arguments are supported
157 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
159 my $libdir = File::Spec->catdir( $tmp, 'libdir' );
162 $result = $mb->run_perl_script('Makefile.PL', [],
171 ok $result, "passthrough Makefile.PL ran with arguments";
172 ok -e 'Build.PL', "Build.PL generated";
174 my $new_build = Module::Build->resume();
175 is $new_build->installdirs, 'core', "installdirs is core";
176 is $new_build->verbose, 1, "tests set for verbose";
177 is $new_build->install_destination('lib'), $libdir, "custom libdir";
178 is $new_build->extra_compiler_flags->[0], '-DPERL_POLLUTE', "PERL_POLLUTE set";
180 # Make sure those switches actually had an effect
181 my ($ran_ok, $output);
182 $output = stdout_of( sub { $ran_ok = $new_build->do_system(@make, 'test') } );
183 ok $ran_ok, "make test ran without error";
184 $output =~ s/^/# /gm; # Don't confuse our own test output
185 like $output, qr/(?:# ok \d+\s+)+/, 'Should be verbose';
187 # Make sure various Makefile arguments are supported
188 my $make_macro = 'TEST_VERBOSE=0';
190 # VMS MMK/MMS macros use different syntax.
192 $make_macro = '/macro=("' . $make_macro . '")';
195 $output = stdout_of( sub {
196 $ran_ok = $mb->do_system(@make, 'test', $make_macro)
199 ok $ran_ok, "make test without verbose ran ok";
200 $output =~ s/^/# /gm; # Don't confuse our own test output
202 qr/# .+basic(\.t)?[.\s#]+ok[.\s#]+All tests successful/,
203 'Should be non-verbose';
205 (my $libdir2 = $libdir) =~ s/libdir/lbiidr/;
206 my @make_args = ('INSTALLDIRS=vendor', "INSTALLVENDORLIB=$libdir2");
208 if ($is_vms_mms) { # VMS MMK/MMS macros use different syntax.
209 $make_args[0] = '/macro=("' . join('","',@make_args) . '")';
210 pop @make_args while scalar(@make_args) > 1;
212 ($output) = stdout_stderr_of(
214 $ran_ok = $mb->do_system(@make, 'fakeinstall', @make_args);
218 ok $ran_ok, "make fakeinstall with INSTALLDIRS=vendor ran ok";
219 $output =~ s/^/# /gm; # Don't confuse our own test output
221 qr/\Q$libdir2\E .* Simple\.pm/x,
222 'Should have installdirs=vendor';
224 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
225 ok ! -e $makefile, "$makefile shouldn't exist";
227 1 while unlink 'Makefile.PL';
228 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
230 1 while unlink $libdir, $libdir2;
233 { # Make sure tilde-expansion works
235 # C<glob> on MSWin32 uses $ENV{HOME} if defined to do tilde-expansion
236 local $ENV{HOME} = 'C:/' if $^O =~ /MSWin/ && !exists( $ENV{HOME} );
238 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
241 $mb->run_perl_script('Makefile.PL', [], ['INSTALL_BASE=~/foo']);
243 my $b2 = Module::Build->current;
244 ok $b2->install_base, "install_base set";
245 unlike $b2->install_base, qr/^~/, "Tildes should be expanded";
247 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
248 ok ! -e $makefile, "$makefile shouldn't exist";
250 1 while unlink 'Makefile.PL';
251 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
257 #########################################################
259 sub test_makefile_types {
261 $opts{requires} ||= {};
263 foreach my $type (@makefile_types) {
264 # Create M::B instance
267 $mb = Module::Build->new_from_context;
269 ok $mb, "Module::Build->new_from_context";
271 # Create and test Makefile.PL
272 Module::Build::Compat->create_makefile_pl($type, $mb);
273 ok -e 'Makefile.PL', "$type Makefile.PL created";
274 test_makefile_pl_requires_perl( $opts{requires}{perl} );
275 test_makefile_creation($mb);
276 test_makefile_prereq_pm( $opts{requires} );
278 my ($output,$success);
279 # Capture output to keep our STDOUT clean
280 $output = stdout_of( sub {
281 $success = $mb->do_system(@make);
283 ok $success, "make ran without error";
285 # Can't let 'test' STDOUT go to our STDOUT, or it'll confuse Test::Harness.
286 $output = stdout_of( sub {
287 $success = $mb->do_system(@make, 'test');
289 ok $success, "make test ran without error";
290 like uc $output, qr{DONE\.|SUCCESS}, "make test output indicated success";
292 $output = stdout_of( sub {
293 $success = $mb->do_system(@make, 'realclean');
295 ok $success, "make realclean ran without error";
297 # Try again with some Makefile.PL arguments
298 test_makefile_creation($mb, [], 'INSTALLDIRS=vendor', 1);
300 1 while unlink 'Makefile.PL';
301 ok ! -e 'Makefile.PL', "cleaned up Makefile";
305 sub test_makefile_creation {
306 my ($build, $preargs, $postargs, $cleanup) = @_;
308 my ($output, $result);
309 # capture output to avoid polluting our test output
310 $output = stdout_of( sub {
311 $result = $build->run_perl_script('Makefile.PL', $preargs, $postargs);
313 my $label = "Makefile.PL ran without error";
314 if ( defined $postargs && length $postargs ) {
315 $label .= " (postargs: $postargs)";
318 ok -e $makefile, "$makefile exists";
321 $output = stdout_of( sub {
322 $build->do_system(@make, 'realclean');
324 ok ! -e '$makefile', "$makefile cleaned up";
327 pass '(skipping cleanup)'; # keep test count constant
331 sub test_makefile_prereq_pm {
332 my %requires = %{ $_[0] };
333 delete $requires{perl}; # until EU::MM supports this
335 skip "$makefile not found", 1 unless -e $makefile;
336 my $prereq_pm = find_makefile_prereq_pm();
337 is_deeply $prereq_pm, \%requires,
338 "$makefile has correct PREREQ_PM line";
342 sub test_makefile_pl_requires_perl {
343 my $perl_version = shift || q{};
345 skip 'Makefile.PL not found', 1 unless -e 'Makefile.PL';
346 my $file_contents = slurp 'Makefile.PL';
347 my $found_requires = $file_contents =~ m{^require $perl_version;}ms;
348 if (length $perl_version) {
349 ok $found_requires, "Makefile.PL has 'require $perl_version;'"
350 or diag "Makefile.PL:\n$file_contents";
353 ok ! $found_requires, "Makefile.PL does not require a perl version";
358 # Following subroutine adapted from code in CPAN.pm
359 # by Andreas Koenig and A. Speer.
360 sub find_makefile_prereq_pm {
361 my $fh = IO::File->new( $makefile, 'r' )
362 or die "Can't read $makefile: $!";
367 last if /MakeMaker post_initialize section/;
369 \s+PREREQ_PM\s+=>\s+(.+)
374 while ( $p =~ m/(?:\s)([\w\:]+)=>(q\[.*?\]|undef),?/g ){
376 if ($n =~ /^q\[(.*?)\]$/) {