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 ) {
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();
67 # Test with requires and PL_files
69 my $distname = $dist->name;
70 $dist->change_build_pl({
71 module_name => $distname,
80 PL_files => { 'foo.PL' => 'foo' },
83 $dist->add_file("foo.PL", <<'END');
84 open my $fh, ">$ARGV[0]" or die $!;
101 ######################
103 $dist->change_build_pl({
104 module_name => $distname,
109 # Create M::B instance but don't pollute STDOUT
112 $mb = Module::Build->new_from_context;
114 ok $mb, "Module::Build->new_from_context";
118 # Make sure fake_makefile() can run without 'build_class', as it may be
119 # in older-generated Makefile.PLs
121 local $SIG{__WARN__} = sub { $warning = shift; };
123 my $maketext = eval { Module::Build::Compat->fake_makefile(makefile => $makefile) };
124 is $@, '', "fake_makefile lived";
125 like $maketext, qr/^realclean/m, "found 'realclean' in fake_makefile output";
126 like $warning, qr/build_class/, "saw warning about 'build_class'";
130 # Make sure custom builder subclass is used in the created
131 # Makefile.PL - make sure it fails in the right way here.
132 local @Foo::Builder::ISA = qw(Module::Build);
135 $foo_builder = Foo::Builder->new_from_context;
137 foreach my $style ('passthrough', 'small') {
138 Module::Build::Compat->create_makefile_pl($style, $foo_builder);
139 ok -e 'Makefile.PL', "$style Makefile.PL created";
141 # Should fail with "can't find Foo/Builder.pm"
143 my ($stdout, $stderr ) = stdout_stderr_of (sub {
144 $result = $mb->run_perl_script('Makefile.PL');
146 ok ! $result, "Makefile.PL failed";
147 like $stderr, qr{Foo/Builder.pm}, "custom builder wasn't found";
150 # Now make sure it can actually work.
153 $bar_builder = Module::Build->subclass( class => 'Bar::Builder' )->new_from_context;
155 foreach my $style ('passthrough', 'small') {
156 Module::Build::Compat->create_makefile_pl($style, $bar_builder);
157 ok -e 'Makefile.PL', "$style Makefile.PL created via subclass";
160 $result = $mb->run_perl_script('Makefile.PL');
162 ok $result, "Makefile.PL ran without error";
167 # Make sure various Makefile.PL arguments are supported
168 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
170 my $libdir = File::Spec->catdir( $tmp, 'libdir' );
173 $result = $mb->run_perl_script('Makefile.PL', [],
182 ok $result, "passthrough Makefile.PL ran with arguments";
183 ok -e 'Build.PL', "Build.PL generated";
185 my $new_build = Module::Build->resume();
186 is $new_build->installdirs, 'core', "installdirs is core";
187 is $new_build->verbose, 1, "tests set for verbose";
188 is $new_build->install_destination('lib'), $libdir, "custom libdir";
189 is $new_build->extra_compiler_flags->[0], '-DPERL_POLLUTE', "PERL_POLLUTE set";
191 # Make sure those switches actually had an effect
192 my ($ran_ok, $output);
193 $output = stdout_of( sub { $ran_ok = $new_build->do_system(@make, 'test') } );
194 ok $ran_ok, "make test ran without error";
195 $output =~ s/^/# /gm; # Don't confuse our own test output
196 like $output, qr/(?:# ok \d+\s+)+/, 'Should be verbose';
198 # Make sure various Makefile arguments are supported
199 my $make_macro = 'TEST_VERBOSE=0';
201 # VMS MMK/MMS macros use different syntax.
203 $make_macro = '/macro=("' . $make_macro . '")';
206 $output = stdout_of( sub {
207 local $ENV{HARNESS_TIMER}; # RT#39635 - timer messes with output
208 $ran_ok = $mb->do_system(@make, 'test', $make_macro)
211 ok $ran_ok, "make test without verbose ran ok";
212 $output =~ s/^/# /gm; # Don't confuse our own test output
214 qr/# .+basic(\.t)?[.\s#]+ok[.\s#]+All tests successful/,
215 'Should be non-verbose';
217 (my $libdir2 = $libdir) =~ s/libdir/lbiidr/;
218 my @make_args = ('INSTALLDIRS=vendor', "INSTALLVENDORLIB=$libdir2");
220 if ($is_vms_mms) { # VMS MMK/MMS macros use different syntax.
221 $make_args[0] = '/macro=("' . join('","',@make_args) . '")';
222 pop @make_args while scalar(@make_args) > 1;
224 ($output) = stdout_stderr_of(
226 $ran_ok = $mb->do_system(@make, 'fakeinstall', @make_args);
230 ok $ran_ok, "make fakeinstall with INSTALLDIRS=vendor ran ok";
231 $output =~ s/^/# /gm; # Don't confuse our own test output
233 qr/\Q$libdir2\E .* Simple\.pm/x,
234 'Should have installdirs=vendor';
236 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
237 ok ! -e $makefile, "$makefile shouldn't exist";
239 1 while unlink 'Makefile.PL';
240 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
242 1 while unlink $libdir, $libdir2;
245 { # Make sure tilde-expansion works
247 # C<glob> on MSWin32 uses $ENV{HOME} if defined to do tilde-expansion
248 local $ENV{HOME} = 'C:/' if $^O =~ /MSWin/ && !exists( $ENV{HOME} );
250 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
253 $mb->run_perl_script('Makefile.PL', [], ['INSTALL_BASE=~/foo']);
255 my $b2 = Module::Build->current;
256 ok $b2->install_base, "install_base set";
257 unlike $b2->install_base, qr/^~/, "Tildes should be expanded";
259 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
260 ok ! -e $makefile, "$makefile shouldn't exist";
262 1 while unlink 'Makefile.PL';
263 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
269 #########################################################
271 sub test_makefile_types {
273 $opts{requires} ||= {};
274 $opts{PL_files} ||= {};
276 foreach my $type (@makefile_types) {
277 # Create M::B instance
280 $mb = Module::Build->new_from_context;
282 ok $mb, "Module::Build->new_from_context";
284 # Create and test Makefile.PL
285 Module::Build::Compat->create_makefile_pl($type, $mb);
286 ok -e 'Makefile.PL', "$type Makefile.PL created";
287 test_makefile_pl_requires_perl( $opts{requires}{perl} );
288 test_makefile_creation($mb);
289 test_makefile_prereq_pm( $opts{requires} );
290 test_makefile_pl_files( $opts{PL_files} ) if $type eq 'traditional';
292 my ($output,$success);
293 # Capture output to keep our STDOUT clean
294 $output = stdout_of( sub {
295 $success = $mb->do_system(@make);
297 ok $success, "make ran without error";
299 for my $file (values %{ $opts{PL_files} }) {
300 ok -e $file, "PL_files generated - $file";
303 # Can't let 'test' STDOUT go to our STDOUT, or it'll confuse Test::Harness.
304 $output = stdout_of( sub {
305 $success = $mb->do_system(@make, 'test');
307 ok $success, "make test ran without error";
308 like uc $output, qr{DONE\.|SUCCESS}, "make test output indicated success";
310 $output = stdout_of( sub {
311 $success = $mb->do_system(@make, 'realclean');
313 ok $success, "make realclean ran without error";
315 # Try again with some Makefile.PL arguments
316 test_makefile_creation($mb, [], 'INSTALLDIRS=vendor', 1);
318 1 while unlink 'Makefile.PL';
319 ok ! -e 'Makefile.PL', "cleaned up Makefile";
323 sub test_makefile_creation {
324 my ($build, $preargs, $postargs, $cleanup) = @_;
326 my ($output, $result);
327 # capture output to avoid polluting our test output
328 $output = stdout_of( sub {
329 $result = $build->run_perl_script('Makefile.PL', $preargs, $postargs);
331 my $label = "Makefile.PL ran without error";
332 if ( defined $postargs && length $postargs ) {
333 $label .= " (postargs: $postargs)";
336 ok -e $makefile, "$makefile exists";
339 $output = stdout_of( sub {
340 $build->do_system(@make, 'realclean');
342 ok ! -e '$makefile', "$makefile cleaned up";
345 pass '(skipping cleanup)'; # keep test count constant
349 sub test_makefile_prereq_pm {
350 my %requires = %{ $_[0] };
351 delete $requires{perl}; # until EU::MM supports this
353 skip "$makefile not found", 1 unless -e $makefile;
354 my $prereq_pm = find_params_in_makefile()->{PREREQ_PM} || {};
355 is_deeply $prereq_pm, \%requires,
356 "$makefile has correct PREREQ_PM line";
360 sub test_makefile_pl_files {
361 my $expected = shift;
364 skip "$makefile not found", 1 unless -e $makefile;
365 my $pl_files = find_params_in_makefile()->{PL_FILES} || {};
366 is_deeply $pl_files, $expected,
367 "$makefile has correct PL_FILES line";
371 sub test_makefile_pl_requires_perl {
372 my $perl_version = shift || q{};
374 skip 'Makefile.PL not found', 1 unless -e 'Makefile.PL';
375 my $file_contents = slurp 'Makefile.PL';
376 my $found_requires = $file_contents =~ m{^require $perl_version;}ms;
377 if (length $perl_version) {
378 ok $found_requires, "Makefile.PL has 'require $perl_version;'"
379 or diag "Makefile.PL:\n$file_contents";
382 ok ! $found_requires, "Makefile.PL does not require a perl version";
387 sub find_params_in_makefile {
388 my $fh = IO::File->new( $makefile, 'r' )
389 or die "Can't read $makefile: $!";
394 # Blank line after params.
395 last if keys %params and !/\S+/;
397 next unless m{^\# \s+ ( [A-Z_]+ ) \s+ => \s+ ( .* )$}x;
399 my($key, $val) = ($1, $2);
400 # extract keys and values
401 while ( $val =~ m/(?:\s)(\S+)=>(q\[.*?\]|undef),?/g ) {
403 if ($n =~ /^q\[(.*?)\]$/) {
406 $params{$key}{$m} = $n;