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 = 14;
17 if ( $Config{make} && find_in_path($Config{make}) ) {
18 plan tests => 38 + @makefile_types*$tests_per_type*2;
20 plan skip_all => "Don't know how to invoke 'make'";
25 #########################
29 my $tmp = MBTest->tmpdir;
31 # Create test distribution; set requires and build_requires
33 my $dist = DistGen->new( dir => $tmp );
36 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
39 #########################
42 use Module::Build::Compat;
44 use Carp; $SIG{__WARN__} = \&Carp::cluck;
46 my @make = $Config{make} eq 'nmake' ? ('nmake', '-nologo') : ($Config{make});
48 #########################
50 # Test without requires
52 test_makefile_types();
56 my $distname = $dist->name;
57 $dist->change_build_pl({
58 module_name => $distname,
71 test_makefile_types( requires => {
77 ######################
79 $dist->change_build_pl({
80 module_name => $distname,
85 # Create M::B instance but don't pollute STDOUT
88 $mb = Module::Build->new_from_context;
90 ok $mb, "Module::Build->new_from_context";
94 # Make sure fake_makefile() can run without 'build_class', as it may be
95 # in older-generated Makefile.PLs
97 local $SIG{__WARN__} = sub { $warning = shift; };
98 my $maketext = eval { Module::Build::Compat->fake_makefile(makefile => 'Makefile') };
99 is $@, '', "fake_makefile lived";
100 like $maketext, qr/^realclean/m, "found 'realclean' in fake_makefile output";
101 like $warning, qr/build_class/, "saw warning about 'build_class'";
105 # Make sure custom builder subclass is used in the created
106 # Makefile.PL - make sure it fails in the right way here.
107 local @Foo::Builder::ISA = qw(Module::Build);
110 $foo_builder = Foo::Builder->new_from_context;
112 foreach my $style ('passthrough', 'small') {
113 Module::Build::Compat->create_makefile_pl($style, $foo_builder);
114 ok -e 'Makefile.PL', "$style Makefile.PL created";
116 # Should fail with "can't find Foo/Builder.pm"
118 my ($stdout, $stderr ) = stdout_stderr_of (sub {
119 $result = $mb->run_perl_script('Makefile.PL');
121 ok ! $result, "Makefile.PL failed";
122 like $stderr, qr{Foo/Builder.pm}, "custom builder wasn't found";
125 # Now make sure it can actually work.
128 $bar_builder = Module::Build->subclass( class => 'Bar::Builder' )->new_from_context;
130 foreach my $style ('passthrough', 'small') {
131 Module::Build::Compat->create_makefile_pl($style, $bar_builder);
132 ok -e 'Makefile.PL', "$style Makefile.PL created via subclass";
135 $result = $mb->run_perl_script('Makefile.PL');
137 ok $result, "Makefile.PL ran without error";
142 # Make sure various Makefile.PL arguments are supported
143 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
145 my $libdir = File::Spec->catdir( $cwd, 't', 'libdir' );
148 $result = $mb->run_perl_script('Makefile.PL', [],
157 ok $result, "passthrough Makefile.PL ran with arguments";
158 ok -e 'Build.PL', "Build.PL generated";
160 my $new_build = Module::Build->resume();
161 is $new_build->installdirs, 'core', "installdirs is core";
162 is $new_build->verbose, 1, "tests set for verbose";
163 is $new_build->install_destination('lib'), $libdir, "custom libdir";
164 is $new_build->extra_compiler_flags->[0], '-DPERL_POLLUTE', "PERL_POLLUTE set";
166 # Make sure those switches actually had an effect
167 my ($ran_ok, $output);
168 $output = stdout_of( sub { $ran_ok = $new_build->do_system(@make, 'test') } );
169 ok $ran_ok, "make test ran without error";
170 $output =~ s/^/# /gm; # Don't confuse our own test output
171 like $output, qr/(?:# ok \d+\s+)+/, 'Should be verbose';
173 # Make sure various Makefile arguments are supported
174 $output = stdout_of( sub { $ran_ok = $mb->do_system(@make, 'test', 'TEST_VERBOSE=0') } );
175 ok $ran_ok, "make test without verbose ran ok";
176 $output =~ s/^/# /gm; # Don't confuse our own test output
177 like $output, qr/(?:# .+basic\.+ok\s+(?:[\d.]+\s*m?s\s*)?)# All tests/,
178 'Should be non-verbose';
180 $mb->delete_filetree($libdir);
181 ok ! -e $libdir, "Sample installation directory should be cleaned up";
183 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
184 ok ! -e 'Makefile', "Makefile shouldn't exist";
186 1 while unlink 'Makefile.PL';
187 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
190 { # Make sure tilde-expansion works
192 # C<glob> on MSWin32 uses $ENV{HOME} if defined to do tilde-expansion
193 local $ENV{HOME} = 'C:/' if $^O =~ /MSWin/ && !exists( $ENV{HOME} );
195 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
198 $mb->run_perl_script('Makefile.PL', [], ['INSTALL_BASE=~/foo']);
200 my $b2 = Module::Build->current;
201 ok $b2->install_base, "install_base set";
202 unlike $b2->install_base, qr/^~/, "Tildes should be expanded";
204 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
205 ok ! -e 'Makefile', "Makefile shouldn't exist";
207 1 while unlink 'Makefile.PL';
208 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
211 #########################################################
213 sub test_makefile_types {
215 $opts{requires} ||= {};
217 foreach my $type (@makefile_types) {
218 # Create M::B instance
221 $mb = Module::Build->new_from_context;
223 ok $mb, "Module::Build->new_from_context";
225 # Create and test Makefile.PL
226 Module::Build::Compat->create_makefile_pl($type, $mb);
227 ok -e 'Makefile.PL', "$type Makefile.PL created";
228 test_makefile_pl_requires_perl( $opts{requires}{perl} );
229 test_makefile_creation($mb);
230 test_makefile_prereq_pm( $opts{requires} );
232 my ($output,$success);
233 # Capture output to keep our STDOUT clean
234 $output = stdout_of( sub {
235 $success = $mb->do_system(@make);
237 ok $success, "make ran without error";
239 # Can't let 'test' STDOUT go to our STDOUT, or it'll confuse Test::Harness.
240 $output = stdout_of( sub {
241 $success = $mb->do_system(@make, 'test');
243 ok $success, "make test ran without error";
244 like uc $output, qr{DONE\.|SUCCESS}, "make test output indicated success";
246 $output = stdout_of( sub {
247 $success = $mb->do_system(@make, 'realclean');
249 ok $success, "make realclean ran without error";
251 # Try again with some Makefile.PL arguments
252 test_makefile_creation($mb, [], 'INSTALLDIRS=vendor', 1);
254 1 while unlink 'Makefile.PL';
255 ok ! -e 'Makefile.PL', "cleaned up Makefile";
259 sub test_makefile_creation {
260 my ($build, $preargs, $postargs, $cleanup) = @_;
262 my ($output, $result);
263 # capture output to avoid polluting our test output
264 $output = stdout_of( sub {
265 $result = $build->run_perl_script('Makefile.PL', $preargs, $postargs);
267 my $label = "Makefile.PL ran without error";
268 if ( defined $postargs && length $postargs ) {
269 $label .= " (postargs: $postargs)";
272 ok -e 'Makefile', "Makefile exists";
275 $output = stdout_of( sub {
276 $build->do_system(@make, 'realclean');
278 ok ! -e 'Makefile', "Makefile cleaned up";
281 pass '(skipping cleanup)'; # keep test count constant
285 sub test_makefile_prereq_pm {
286 my %requires = %{ $_[0] };
287 delete $requires{perl}; # until EU::MM supports this
289 skip 'Makefile not found', 1 unless -e 'Makefile';
290 my $prereq_pm = find_makefile_prereq_pm();
291 is_deeply $prereq_pm, \%requires,
292 "Makefile has correct PREREQ_PM line";
296 sub test_makefile_pl_requires_perl {
297 my $perl_version = shift || q{};
299 skip 'Makefile.PL not found', 1 unless -e 'Makefile.PL';
300 my $file_contents = slurp 'Makefile.PL';
301 my $found_requires = $file_contents =~ m{^require $perl_version;}ms;
302 if (length $perl_version) {
303 ok $found_requires, "Makefile.PL has 'require $perl_version;'"
304 or diag "Makefile.PL:\n$file_contents";
307 ok ! $found_requires, "Makefile.PL does not require a perl version";
312 # Following subroutine adapted from code in CPAN.pm
313 # by Andreas Koenig and A. Speer.
314 sub find_makefile_prereq_pm {
315 my $fh = IO::File->new( 'Makefile', 'r' )
316 or die "Can't read Makefile: $!";
321 last if /MakeMaker post_initialize section/;
323 \s+PREREQ_PM\s+=>\s+(.+)
328 while ( $p =~ m/(?:\s)([\w\:]+)=>(q\[.*?\]|undef),?/g ){
330 if ($n =~ /^q\[(.*?)\]$/) {
341 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";