4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest tests => 51;
7 use_ok 'Module::Build';
8 ensure_blib('Module::Build');
10 my $tmp = MBTest->tmpdir;
12 use Module::Build::ConfigData;
16 module_name => 'Simple',
17 dist_version => '3.14159265',
18 dist_author => [ 'Simple Simon <ss\@somewhere.priv>' ],
19 dist_abstract => 'Something interesting',
22 keywords => [qw(super duper something)],
23 resources => {homepage => 'http://foo.example.com'},
29 my $dist = DistGen->new( dir => $tmp );
30 $dist->change_build_pl( \%metadata );
33 my $simple_file = 'lib/Simple.pm';
34 my $simple2_file = 'lib/Simple2.pm';
37 # Traditional VMS will return the file in in lower case, and is_deeply
38 # does exact case comparisons.
39 # When ODS-5 support is active for preserved case file names, this will
42 $simple_file = lc($simple_file);
43 $simple2_file = lc($simple2_file);
50 my $mb = Module::Build->new_from_context;
52 ##################################################
54 # Test for valid META.yml
57 my $node = $mb->prepare_metadata( {} );
59 # exists() doesn't seem to work here
60 is $node->{name}, $metadata{module_name};
61 is $node->{version}, $metadata{dist_version};
62 is $node->{abstract}, $metadata{dist_abstract};
63 is_deeply $node->{author}, $metadata{dist_author};
64 is $node->{license}, $metadata{license};
65 like $node->{generated_by}, qr{Module::Build};
66 ok defined( $node->{'meta-spec'}{version} ),
67 "'meta-spec' -> 'version' field present in META.yml";
68 ok defined( $node->{'meta-spec'}{url} ),
69 "'meta-spec' -> 'url' field present in META.yml";
70 is_deeply $node->{keywords}, $metadata{meta_add}{keywords};
71 is_deeply $node->{resources}, $metadata{meta_add}{resources};
77 ##################################################
79 # Tests to ensure that the correct packages and versions are
80 # recorded for the 'provides' field of META.yml
82 my $provides; # Used a bunch of times below
84 sub new_build { return Module::Build->new_from_context( quiet => 1, @_ ) }
86 ############################## Single Module
88 # File with corresponding package (w/ or w/o version)
89 # Simple.pm => Simple v1.23
91 $dist->change_file( 'lib/Simple.pm', <<'---' );
95 $dist->regen( clean => 1 );
97 is_deeply($mb->find_dist_packages,
98 {'Simple' => {file => $simple_file,
101 $dist->change_file( 'lib/Simple.pm', <<'---' );
104 $dist->regen( clean => 1 );
106 is_deeply($mb->find_dist_packages,
107 {'Simple' => {file => $simple_file}});
109 # File with no corresponding package (w/ or w/o version)
110 # Simple.pm => Foo::Bar v1.23
112 $dist->change_file( 'lib/Simple.pm', <<'---' );
116 $dist->regen( clean => 1 );
118 is_deeply($mb->find_dist_packages,
119 {'Foo::Bar' => { file => $simple_file,
120 version => '1.23' }});
122 $dist->change_file( 'lib/Simple.pm', <<'---' );
125 $dist->regen( clean => 1 );
127 is_deeply($mb->find_dist_packages,
128 {'Foo::Bar' => { file => $simple_file}});
131 # Single file with multiple differing packages (w/ or w/o version)
132 # Simple.pm => Simple
133 # Simple.pm => Foo::Bar
135 $dist->change_file( 'lib/Simple.pm', <<'---' );
141 $dist->regen( clean => 1 );
143 is_deeply($mb->find_dist_packages,
144 {'Simple' => { file => $simple_file,
146 'Foo::Bar' => { file => $simple_file,
147 version => '1.23' }});
150 $dist->change_file( 'lib/Simple.pm', <<'---' );
152 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
153 package Simple::Simon;
154 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
157 my $provides = new_build()->prepare_metadata()->{provides};
158 is $provides->{'Simple'}{version}, '0.60.128', "Check version";
159 is $provides->{'Simple::Simon'}{version}, '0.61.129', "Check version";
160 is ref($provides->{'Simple'}{version}), '', "Versions from prepare_metadata() aren't refs";
161 is ref($provides->{'Simple::Simon'}{version}), '', "Versions from prepare_metadata() aren't refs";
165 # Single file with multiple differing packages, no corresponding package
167 # Simple.pm => Foo::Bar
169 $dist->change_file( 'lib/Simple.pm', <<'---' );
175 $dist->regen( clean => 1 );
177 is_deeply($mb->find_dist_packages,
178 {'Foo' => { file => $simple_file,
180 'Foo::Bar' => { file => $simple_file,
181 version => '1.23' }});
184 # Single file with same package appearing multiple times, no version
185 # only record a single instance
186 # Simple.pm => Simple
187 # Simple.pm => Simple
189 $dist->change_file( 'lib/Simple.pm', <<'---' );
193 $dist->regen( clean => 1 );
195 is_deeply($mb->find_dist_packages,
196 {'Simple' => { file => $simple_file }});
199 # Single file with same package appearing multiple times, single
200 # version 1st package:
201 # Simple.pm => Simple v1.23
202 # Simple.pm => Simple
204 $dist->change_file( 'lib/Simple.pm', <<'---' );
209 $dist->regen( clean => 1 );
211 is_deeply($mb->find_dist_packages,
212 {'Simple' => { file => $simple_file,
213 version => '1.23' }});
216 # Single file with same package appearing multiple times, single
217 # version 2nd package
218 # Simple.pm => Simple
219 # Simple.pm => Simple v1.23
221 $dist->change_file( 'lib/Simple.pm', <<'---' );
226 $dist->regen( clean => 1 );
228 is_deeply($mb->find_dist_packages,
229 {'Simple' => { file => $simple_file,
230 version => '1.23' }});
233 # Single file with same package appearing multiple times, conflicting versions
234 # Simple.pm => Simple v1.23
235 # Simple.pm => Simple v2.34
237 $dist->change_file( 'lib/Simple.pm', <<'---' );
243 $dist->regen( clean => 1 );
245 $err = stderr_of( sub { $mb = new_build() } );
246 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
248 {'Simple' => { file => $simple_file,
249 version => '1.23' }}); # XXX should be 2.34?
250 like( $err, qr/already declared/, ' with conflicting versions reported' );
253 # (Same as above three cases except with no corresponding package)
254 # Simple.pm => Foo v1.23
255 # Simple.pm => Foo v2.34
257 $dist->change_file( 'lib/Simple.pm', <<'---' );
263 $dist->regen( clean => 1 );
264 $err = stderr_of( sub { $mb = new_build() } );
265 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
267 {'Foo' => { file => $simple_file,
268 version => '1.23' }}); # XXX should be 2.34?
269 like( $err, qr/already declared/, ' with conflicting versions reported' );
273 ############################## Multiple Modules
275 # Multiple files with same package, no version
276 # Simple.pm => Simple
277 # Simple2.pm => Simple
279 $dist->change_file( 'lib/Simple.pm', <<'---' );
282 $dist->add_file( 'lib/Simple2.pm', <<'---' );
285 $dist->regen( clean => 1 );
287 is_deeply($mb->find_dist_packages,
288 {'Simple' => { file => $simple_file }});
289 $dist->remove_file( 'lib/Simple2.pm' );
292 # Multiple files with same package, single version in corresponding package
293 # Simple.pm => Simple v1.23
294 # Simple2.pm => Simple
296 $dist->change_file( 'lib/Simple.pm', <<'---' );
300 $dist->add_file( 'lib/Simple2.pm', <<'---' );
303 $dist->regen( clean => 1 );
305 is_deeply($mb->find_dist_packages,
306 {'Simple' => { file => $simple_file,
307 version => '1.23' }});
308 $dist->remove_file( 'lib/Simple2.pm' );
311 # Multiple files with same package,
312 # single version in non-corresponding package
313 # Simple.pm => Simple
314 # Simple2.pm => Simple v1.23
316 $dist->change_file( 'lib/Simple.pm', <<'---' );
319 $dist->add_file( 'lib/Simple2.pm', <<'---' );
323 $dist->regen( clean => 1 );
325 is_deeply($mb->find_dist_packages,
326 {'Simple' => { file => $simple2_file,
327 version => '1.23' }});
328 $dist->remove_file( 'lib/Simple2.pm' );
331 # Multiple files with same package, conflicting versions
332 # Simple.pm => Simple v1.23
333 # Simple2.pm => Simple v2.34
335 $dist->change_file( 'lib/Simple.pm', <<'---' );
339 $dist->add_file( 'lib/Simple2.pm', <<'---' );
343 $dist->regen( clean => 1 );
345 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
347 {'Simple' => { file => $simple_file,
348 version => '1.23' }});
349 like( $err, qr/Found conflicting versions for package/,
350 ' with conflicting versions reported' );
351 $dist->remove_file( 'lib/Simple2.pm' );
354 # Multiple files with same package, multiple agreeing versions
355 # Simple.pm => Simple v1.23
356 # Simple2.pm => Simple v1.23
358 $dist->change_file( 'lib/Simple.pm', <<'---' );
362 $dist->add_file( 'lib/Simple2.pm', <<'---' );
366 $dist->regen( clean => 1 );
368 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
370 {'Simple' => { file => $simple_file,
371 version => '1.23' }});
372 $dist->remove_file( 'lib/Simple2.pm' );
375 ############################################################
377 # (Same as above five cases except with non-corresponding package)
380 # Multiple files with same package, no version
384 $dist->change_file( 'lib/Simple.pm', <<'---' );
387 $dist->add_file( 'lib/Simple2.pm', <<'---' );
390 $dist->regen( clean => 1 );
392 $provides = $mb->find_dist_packages;
393 ok( exists( $provides->{Foo} ) ); # it exist, can't predict which file
394 $dist->remove_file( 'lib/Simple2.pm' );
397 # Multiple files with same package, version in first file
398 # Simple.pm => Foo v1.23
401 $dist->change_file( 'lib/Simple.pm', <<'---' );
405 $dist->add_file( 'lib/Simple2.pm', <<'---' );
408 $dist->regen( clean => 1 );
410 is_deeply($mb->find_dist_packages,
411 {'Foo' => { file => $simple_file,
412 version => '1.23' }});
413 $dist->remove_file( 'lib/Simple2.pm' );
416 # Multiple files with same package, version in second file
418 # Simple2.pm => Foo v1.23
420 $dist->change_file( 'lib/Simple.pm', <<'---' );
423 $dist->add_file( 'lib/Simple2.pm', <<'---' );
427 $dist->regen( clean => 1 );
429 is_deeply($mb->find_dist_packages,
430 {'Foo' => { file => $simple2_file,
431 version => '1.23' }});
432 $dist->remove_file( 'lib/Simple2.pm' );
435 # Multiple files with same package, conflicting versions
436 # Simple.pm => Foo v1.23
437 # Simple2.pm => Foo v2.34
439 $dist->change_file( 'lib/Simple.pm', <<'---' );
443 $dist->add_file( 'lib/Simple2.pm', <<'---' );
447 $dist->regen( clean => 1 );
449 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
450 # XXX Should 'Foo' exist ??? Can't predict values for file & version
451 ok( exists( $provides->{Foo} ) );
452 like( $err, qr/Found conflicting versions for package/,
453 ' with conflicting versions reported' );
454 $dist->remove_file( 'lib/Simple2.pm' );
457 # Multiple files with same package, multiple agreeing versions
458 # Simple.pm => Foo v1.23
459 # Simple2.pm => Foo v1.23
461 $dist->change_file( 'lib/Simple.pm', <<'---' );
465 $dist->add_file( 'lib/Simple2.pm', <<'---' );
469 $dist->regen( clean => 1 );
471 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
472 ok( exists( $provides->{Foo} ) );
473 is( $provides->{Foo}{version}, '1.23' );
474 ok( exists( $provides->{Foo}{file} ) ); # Can't predict which file
475 is( $err, '', ' no conflicts reported' );
476 $dist->remove_file( 'lib/Simple2.pm' );
478 ############################################################
479 # Conflicts among primary & multiple alternatives
481 # multiple files, conflicting version in corresponding file
482 $dist->change_file( 'lib/Simple.pm', <<'---' );
486 $dist->add_file( 'lib/Simple2.pm', <<'---' );
490 $dist->add_file( 'lib/Simple3.pm', <<'---' );
494 $dist->regen( clean => 1 );
495 $err = stderr_of( sub {
498 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
500 {'Simple' => { file => $simple_file,
501 version => '1.23' }});
502 like( $err, qr/Found conflicting versions for package/,
503 ' corresponding package conflicts with multiple alternatives' );
504 $dist->remove_file( 'lib/Simple2.pm' );
505 $dist->remove_file( 'lib/Simple3.pm' );
507 # multiple files, conflicting version in non-corresponding file
508 $dist->change_file( 'lib/Simple.pm', <<'---' );
512 $dist->add_file( 'lib/Simple2.pm', <<'---' );
516 $dist->add_file( 'lib/Simple3.pm', <<'---' );
520 $dist->regen( clean => 1 );
521 $err = stderr_of( sub {
524 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
526 {'Simple' => { file => $simple_file,
527 version => '1.23' }});
528 like( $err, qr/Found conflicting versions for package/,
529 ' only one alternative conflicts with corresponding package' );
530 $dist->remove_file( 'lib/Simple2.pm' );
531 $dist->remove_file( 'lib/Simple3.pm' );
534 ############################################################
535 # Don't record private packages (beginning with underscore)
536 # Simple.pm => Simple::_private
537 # Simple.pm => Simple::_private::too
539 $dist->change_file( 'lib/Simple.pm', <<'---' );
542 package Simple::_private;
544 package Simple::_private::too;
547 $dist->regen( clean => 1 );
549 is_deeply($mb->find_dist_packages,
550 {'Simple' => { file => $simple_file,
551 version => '1.23' }});
554 ############################################################
555 # Files with no packages?
557 # Simple.pm => <empty>
559 $dist->change_file( 'lib/Simple.pm', '' );
560 $dist->regen( clean => 1 );
562 is_deeply( $mb->find_dist_packages, {} );
564 # Simple.pm => =pod..=cut (no package declaration)
565 $dist->change_file( 'lib/Simple.pm', <<'---' );
570 Simple - Pure Documentation
578 $dist->regen( clean => 1 );
580 is_deeply($mb->find_dist_packages, {});
582 ############################################################