Allow '\r' after "#line XXX" directives.
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / compat.t
CommitLineData
bb4e9162 1#!/usr/bin/perl -w
2
3use strict;
4use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5use MBTest;
6use File::Spec;
7a827510 7use IO::File;
bb4e9162 8use Config;
9
10# Don't let our own verbosity/test_file get mixed up with our subprocess's
11my @makefile_keys = qw(TEST_VERBOSE HARNESS_VERBOSE TEST_FILES MAKEFLAGS);
12local @ENV{@makefile_keys};
13delete @ENV{@makefile_keys};
14
15my @makefile_types = qw(small passthrough traditional);
738349a8 16my $tests_per_type = 15;
17
18#find_in_path does not understand VMS.
19
20if ( $Config{make} && $^O ne 'VMS' ? find_in_path($Config{make}) : 1 ) {
21 plan tests => 34 + @makefile_types*$tests_per_type*2;
bb4e9162 22} else {
23 plan skip_all => "Don't know how to invoke 'make'";
24}
738349a8 25
26my $is_vms_mms = ($^O eq 'VMS') && ($Config{make} =~ /MM[SK]/i);
27
28use_ok 'Module::Build';
29ensure_blib('Module::Build');
bb4e9162 30
31
32#########################
33
7a827510 34my $tmp = MBTest->tmpdir;
bb4e9162 35
7a827510 36# Create test distribution; set requires and build_requires
bb4e9162 37use DistGen;
38my $dist = DistGen->new( dir => $tmp );
39$dist->regen;
40
738349a8 41$dist->chdir_in;
bb4e9162 42
43
44#########################
45
46use Module::Build;
47use Module::Build::Compat;
48
49use Carp; $SIG{__WARN__} = \&Carp::cluck;
50
51my @make = $Config{make} eq 'nmake' ? ('nmake', '-nologo') : ($Config{make});
52
738349a8 53my $makefile = 'Makefile';
54
55# VMS MMK/MMS by convention use Descrip.MMS
56if ($is_vms_mms) {
57 $makefile = 'Descrip.MMS';
58}
59
60
bb4e9162 61#########################
62
7a827510 63# Test without requires
bb4e9162 64
7a827510 65test_makefile_types();
66
67# Test with requires
68
69my $distname = $dist->name;
70$dist->change_build_pl({
71 module_name => $distname,
72 license => 'perl',
73 requires => {
74 'perl' => $],
75 'File::Spec' => 0,
76 },
77 build_requires => {
78 'Test::More' => 0,
79 },
80});
81
82$dist->regen;
83
84test_makefile_types( requires => {
85 'perl' => $],
86 'File::Spec' => 0,
87 'Test::More' => 0,
88});
89
90######################
91
92$dist->change_build_pl({
93 module_name => $distname,
94 license => 'perl',
95});
96$dist->regen;
97
98# Create M::B instance but don't pollute STDOUT
99my $mb;
100stdout_of( sub {
101 $mb = Module::Build->new_from_context;
102});
103ok $mb, "Module::Build->new_from_context";
bb4e9162 104
bb4e9162 105
106{
107 # Make sure fake_makefile() can run without 'build_class', as it may be
108 # in older-generated Makefile.PLs
109 my $warning = '';
110 local $SIG{__WARN__} = sub { $warning = shift; };
738349a8 111
112 my $maketext = eval { Module::Build::Compat->fake_makefile(makefile => $makefile) };
7a827510 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'";
bb4e9162 116}
117
118{
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);
7a827510 122 my $foo_builder;
123 stdout_of( sub {
124 $foo_builder = Foo::Builder->new_from_context;
125 });
bb4e9162 126 foreach my $style ('passthrough', 'small') {
127 Module::Build::Compat->create_makefile_pl($style, $foo_builder);
7a827510 128 ok -e 'Makefile.PL', "$style Makefile.PL created";
bb4e9162 129
130 # Should fail with "can't find Foo/Builder.pm"
7a827510 131 my $result;
132 my ($stdout, $stderr ) = stdout_stderr_of (sub {
133 $result = $mb->run_perl_script('Makefile.PL');
134 });
135 ok ! $result, "Makefile.PL failed";
136 like $stderr, qr{Foo/Builder.pm}, "custom builder wasn't found";
bb4e9162 137 }
138
139 # Now make sure it can actually work.
7a827510 140 my $bar_builder;
141 stdout_of( sub {
142 $bar_builder = Module::Build->subclass( class => 'Bar::Builder' )->new_from_context;
143 });
bb4e9162 144 foreach my $style ('passthrough', 'small') {
145 Module::Build::Compat->create_makefile_pl($style, $bar_builder);
7a827510 146 ok -e 'Makefile.PL', "$style Makefile.PL created via subclass";
147 my $result;
148 stdout_of( sub {
149 $result = $mb->run_perl_script('Makefile.PL');
150 });
151 ok $result, "Makefile.PL ran without error";
bb4e9162 152 }
153}
154
155{
156 # Make sure various Makefile.PL arguments are supported
157 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
158
738349a8 159 my $libdir = File::Spec->catdir( $tmp, 'libdir' );
7a827510 160 my $result;
161 stdout_of( sub {
162 $result = $mb->run_perl_script('Makefile.PL', [],
163 [
164 "LIB=$libdir",
165 'TEST_VERBOSE=1',
166 'INSTALLDIRS=perl',
167 'POLLUTE=1',
168 ]
169 );
170 });
171 ok $result, "passthrough Makefile.PL ran with arguments";
172 ok -e 'Build.PL', "Build.PL generated";
bb4e9162 173
174 my $new_build = Module::Build->resume();
7a827510 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";
bb4e9162 179
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') } );
7a827510 183 ok $ran_ok, "make test ran without error";
bb4e9162 184 $output =~ s/^/# /gm; # Don't confuse our own test output
185 like $output, qr/(?:# ok \d+\s+)+/, 'Should be verbose';
186
187 # Make sure various Makefile arguments are supported
738349a8 188 my $make_macro = 'TEST_VERBOSE=0';
189
190 # VMS MMK/MMS macros use different syntax.
191 if ($is_vms_mms) {
192 $make_macro = '/macro=("' . $make_macro . '")';
193 }
194
195 $output = stdout_of( sub {
798f208d 196 local $ENV{HARNESS_TIMER}; # RT#39635 - timer messes with output
738349a8 197 $ran_ok = $mb->do_system(@make, 'test', $make_macro)
198 } );
199
7a827510 200 ok $ran_ok, "make test without verbose ran ok";
bb4e9162 201 $output =~ s/^/# /gm; # Don't confuse our own test output
ba889e8f 202 like $output,
738349a8 203 qr/# .+basic(\.t)?[.\s#]+ok[.\s#]+All tests successful/,
204 'Should be non-verbose';
bb4e9162 205
738349a8 206 (my $libdir2 = $libdir) =~ s/libdir/lbiidr/;
207 my @make_args = ('INSTALLDIRS=vendor', "INSTALLVENDORLIB=$libdir2");
208
209 if ($is_vms_mms) { # VMS MMK/MMS macros use different syntax.
210 $make_args[0] = '/macro=("' . join('","',@make_args) . '")';
211 pop @make_args while scalar(@make_args) > 1;
212 }
213 ($output) = stdout_stderr_of(
214 sub {
215 $ran_ok = $mb->do_system(@make, 'fakeinstall', @make_args);
216 }
217 );
218
219 ok $ran_ok, "make fakeinstall with INSTALLDIRS=vendor ran ok";
220 $output =~ s/^/# /gm; # Don't confuse our own test output
221 like $output,
222 qr/\Q$libdir2\E .* Simple\.pm/x,
223 'Should have installdirs=vendor';
bb4e9162 224
7a827510 225 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
738349a8 226 ok ! -e $makefile, "$makefile shouldn't exist";
bb4e9162 227
228 1 while unlink 'Makefile.PL';
7a827510 229 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
738349a8 230
231 1 while unlink $libdir, $libdir2;
bb4e9162 232}
233
234{ # Make sure tilde-expansion works
235
236 # C<glob> on MSWin32 uses $ENV{HOME} if defined to do tilde-expansion
237 local $ENV{HOME} = 'C:/' if $^O =~ /MSWin/ && !exists( $ENV{HOME} );
238
239 Module::Build::Compat->create_makefile_pl('passthrough', $mb);
240
7a827510 241 stdout_of( sub {
242 $mb->run_perl_script('Makefile.PL', [], ['INSTALL_BASE=~/foo']);
243 });
bb4e9162 244 my $b2 = Module::Build->current;
7a827510 245 ok $b2->install_base, "install_base set";
bb4e9162 246 unlike $b2->install_base, qr/^~/, "Tildes should be expanded";
247
7a827510 248 stdout_of( sub { $mb->do_system(@make, 'realclean'); } );
738349a8 249 ok ! -e $makefile, "$makefile shouldn't exist";
7a827510 250
bb4e9162 251 1 while unlink 'Makefile.PL';
7a827510 252 ok ! -e 'Makefile.PL', "Makefile.PL cleaned up";
bb4e9162 253}
7a827510 254
738349a8 255# cleanup
256$dist->remove;
257
bb4e9162 258#########################################################
259
7a827510 260sub test_makefile_types {
261 my %opts = @_;
262 $opts{requires} ||= {};
263
264 foreach my $type (@makefile_types) {
265 # Create M::B instance
266 my $mb;
267 stdout_of( sub {
268 $mb = Module::Build->new_from_context;
269 });
270 ok $mb, "Module::Build->new_from_context";
271
272 # Create and test Makefile.PL
273 Module::Build::Compat->create_makefile_pl($type, $mb);
274 ok -e 'Makefile.PL', "$type Makefile.PL created";
275 test_makefile_pl_requires_perl( $opts{requires}{perl} );
276 test_makefile_creation($mb);
277 test_makefile_prereq_pm( $opts{requires} );
278
279 my ($output,$success);
280 # Capture output to keep our STDOUT clean
281 $output = stdout_of( sub {
282 $success = $mb->do_system(@make);
283 });
284 ok $success, "make ran without error";
285
286 # Can't let 'test' STDOUT go to our STDOUT, or it'll confuse Test::Harness.
287 $output = stdout_of( sub {
288 $success = $mb->do_system(@make, 'test');
289 });
290 ok $success, "make test ran without error";
291 like uc $output, qr{DONE\.|SUCCESS}, "make test output indicated success";
292
293 $output = stdout_of( sub {
294 $success = $mb->do_system(@make, 'realclean');
295 });
296 ok $success, "make realclean ran without error";
297
298 # Try again with some Makefile.PL arguments
299 test_makefile_creation($mb, [], 'INSTALLDIRS=vendor', 1);
300
301 1 while unlink 'Makefile.PL';
302 ok ! -e 'Makefile.PL', "cleaned up Makefile";
303 }
304}
305
bb4e9162 306sub test_makefile_creation {
307 my ($build, $preargs, $postargs, $cleanup) = @_;
308
7a827510 309 my ($output, $result);
310 # capture output to avoid polluting our test output
311 $output = stdout_of( sub {
312 $result = $build->run_perl_script('Makefile.PL', $preargs, $postargs);
313 });
314 my $label = "Makefile.PL ran without error";
315 if ( defined $postargs && length $postargs ) {
316 $label .= " (postargs: $postargs)";
317 }
318 ok $result, $label;
738349a8 319 ok -e $makefile, "$makefile exists";
bb4e9162 320
321 if ($cleanup) {
7a827510 322 $output = stdout_of( sub {
323 $build->do_system(@make, 'realclean');
324 });
738349a8 325 ok ! -e '$makefile', "$makefile cleaned up";
7a827510 326 }
327 else {
328 pass '(skipping cleanup)'; # keep test count constant
bb4e9162 329 }
330}
331
7a827510 332sub test_makefile_prereq_pm {
333 my %requires = %{ $_[0] };
334 delete $requires{perl}; # until EU::MM supports this
335 SKIP: {
738349a8 336 skip "$makefile not found", 1 unless -e $makefile;
7a827510 337 my $prereq_pm = find_makefile_prereq_pm();
338 is_deeply $prereq_pm, \%requires,
738349a8 339 "$makefile has correct PREREQ_PM line";
7a827510 340 }
341}
342
343sub test_makefile_pl_requires_perl {
344 my $perl_version = shift || q{};
345 SKIP: {
346 skip 'Makefile.PL not found', 1 unless -e 'Makefile.PL';
347 my $file_contents = slurp 'Makefile.PL';
348 my $found_requires = $file_contents =~ m{^require $perl_version;}ms;
349 if (length $perl_version) {
350 ok $found_requires, "Makefile.PL has 'require $perl_version;'"
351 or diag "Makefile.PL:\n$file_contents";
352 }
353 else {
354 ok ! $found_requires, "Makefile.PL does not require a perl version";
355 }
356 }
357}
358
359# Following subroutine adapted from code in CPAN.pm
360# by Andreas Koenig and A. Speer.
361sub find_makefile_prereq_pm {
738349a8 362 my $fh = IO::File->new( $makefile, 'r' )
363 or die "Can't read $makefile: $!";
7a827510 364 my $req = {};
365 local($/) = "\n";
366 while (<$fh>) {
367 # locate PREREQ_PM
368 last if /MakeMaker post_initialize section/;
369 my($p) = m{^[\#]
370 \s+PREREQ_PM\s+=>\s+(.+)
371 }x;
372 next unless $p;
373
374 # extract modules
375 while ( $p =~ m/(?:\s)([\w\:]+)=>(q\[.*?\]|undef),?/g ){
376 my($m,$n) = ($1,$2);
377 if ($n =~ /^q\[(.*?)\]$/) {
378 $n = $1;
379 }
380 $req->{$m} = $n;
381 }
382 last;
383 }
384 return $req;
385}