4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest tests => 47;
9 my $tmp = File::Spec->catdir( $cwd, 't', '_tmp' );
13 use Module::Build::ConfigData;
14 my $has_YAML = Module::Build::ConfigData->feature('YAML_support');
18 my $dist = DistGen->new( dir => $tmp );
19 $dist->change_file( 'Build.PL', <<"---" );
21 my \$builder = Module::Build->new(
22 module_name => '@{[$dist->name]}',
23 dist_version => '3.14159265',
24 dist_author => [ 'Simple Simon <ss\@somewhere.priv>' ],
25 dist_abstract => 'Something interesting',
29 \$builder->create_build_script();
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);
47 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
50 my $mb = Module::Build->new_from_context;
52 ##################################################
54 # Test for valid META.yml
57 skip( 'YAML_support feature is not enabled', 8 ) unless $has_YAML;
61 my $node = YAML::Node->new({});
62 $node = $mb->prepare_metadata( $node );
64 # exists() doesn't seem to work here
65 ok defined( $node->{name} ), "'name' field present in META.yml";
66 ok defined( $node->{version} ), "'version' field present in META.yml";
67 ok defined( $node->{abstract} ), "'abstract' field present in META.yml";
68 ok defined( $node->{author} ), "'author' field present in META.yml";
69 ok defined( $node->{license} ), "'license' field present in META.yml";
70 ok defined( $node->{generated_by} ),
71 "'generated_by' field present in META.yml";
72 ok defined( $node->{'meta-spec'}{version} ),
73 "'meta-spec' -> 'version' field present in META.yml";
74 ok defined( $node->{'meta-spec'}{url} ),
75 "'meta-spec' -> 'url' field present in META.yml";
77 # TODO : find a way to test for failure when above fields are not present
83 ##################################################
85 # Tests to ensure that the correct packages and versions are
86 # recorded for the 'provides' field of META.yml
88 my $provides; # Used a bunch of times below
90 sub new_build { return Module::Build->new_from_context( quiet => 1, @_ ) }
92 ############################## Single Module
94 # File with corresponding package (w/ or w/o version)
95 # Simple.pm => Simple v1.23
97 $dist->change_file( 'lib/Simple.pm', <<'---' );
101 $dist->regen( clean => 1 );
103 is_deeply($mb->find_dist_packages,
104 {'Simple' => {file => $simple_file,
105 version => '1.23'}});
107 $dist->change_file( 'lib/Simple.pm', <<'---' );
110 $dist->regen( clean => 1 );
112 is_deeply($mb->find_dist_packages,
113 {'Simple' => {file => $simple_file}});
115 # File with no corresponding package (w/ or w/o version)
116 # Simple.pm => Foo::Bar v1.23
118 $dist->change_file( 'lib/Simple.pm', <<'---' );
122 $dist->regen( clean => 1 );
124 is_deeply($mb->find_dist_packages,
125 {'Foo::Bar' => { file => $simple_file,
126 version => '1.23' }});
128 $dist->change_file( 'lib/Simple.pm', <<'---' );
131 $dist->regen( clean => 1 );
133 is_deeply($mb->find_dist_packages,
134 {'Foo::Bar' => { file => $simple_file}});
137 # Single file with multiple differing packages (w/ or w/o version)
138 # Simple.pm => Simple
139 # Simple.pm => Foo::Bar
141 $dist->change_file( 'lib/Simple.pm', <<'---' );
147 $dist->regen( clean => 1 );
149 is_deeply($mb->find_dist_packages,
150 {'Simple' => { file => $simple_file,
152 'Foo::Bar' => { file => $simple_file,
153 version => '1.23' }});
156 $dist->change_file( 'lib/Simple.pm', <<'---' );
158 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
159 package Simple::Simon;
160 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
163 my $provides = new_build()->prepare_metadata()->{provides};
164 is $provides->{'Simple'}{version}, '0.60.128', "Check version";
165 is $provides->{'Simple::Simon'}{version}, '0.61.129', "Check version";
166 is ref($provides->{'Simple'}{version}), '', "Versions from prepare_metadata() aren't refs";
167 is ref($provides->{'Simple::Simon'}{version}), '', "Versions from prepare_metadata() aren't refs";
171 # Single file with multiple differing packages, no corresponding package
173 # Simple.pm => Foo::Bar
175 $dist->change_file( 'lib/Simple.pm', <<'---' );
181 $dist->regen( clean => 1 );
183 is_deeply($mb->find_dist_packages,
184 {'Foo' => { file => $simple_file,
186 'Foo::Bar' => { file => $simple_file,
187 version => '1.23' }});
190 # Single file with same package appearing multiple times, no version
191 # only record a single instance
192 # Simple.pm => Simple
193 # Simple.pm => Simple
195 $dist->change_file( 'lib/Simple.pm', <<'---' );
199 $dist->regen( clean => 1 );
201 is_deeply($mb->find_dist_packages,
202 {'Simple' => { file => $simple_file }});
205 # Single file with same package appearing multiple times, single
206 # version 1st package:
207 # Simple.pm => Simple v1.23
208 # Simple.pm => Simple
210 $dist->change_file( 'lib/Simple.pm', <<'---' );
215 $dist->regen( clean => 1 );
217 is_deeply($mb->find_dist_packages,
218 {'Simple' => { file => $simple_file,
219 version => '1.23' }});
222 # Single file with same package appearing multiple times, single
223 # version 2nd package
224 # Simple.pm => Simple
225 # Simple.pm => Simple v1.23
227 $dist->change_file( 'lib/Simple.pm', <<'---' );
232 $dist->regen( clean => 1 );
234 is_deeply($mb->find_dist_packages,
235 {'Simple' => { file => $simple_file,
236 version => '1.23' }});
239 # Single file with same package appearing multiple times, conflicting versions
240 # Simple.pm => Simple v1.23
241 # Simple.pm => Simple v2.34
243 $dist->change_file( 'lib/Simple.pm', <<'---' );
249 $dist->regen( clean => 1 );
251 $err = stderr_of( sub { $mb = new_build() } );
252 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
254 {'Simple' => { file => $simple_file,
255 version => '1.23' }}); # XXX should be 2.34?
256 like( $err, qr/already declared/, ' with conflicting versions reported' );
259 # (Same as above three cases except with no corresponding package)
260 # Simple.pm => Foo v1.23
261 # Simple.pm => Foo v2.34
263 $dist->change_file( 'lib/Simple.pm', <<'---' );
269 $dist->regen( clean => 1 );
270 $err = stderr_of( sub { $mb = new_build() } );
271 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
273 {'Foo' => { file => $simple_file,
274 version => '1.23' }}); # XXX should be 2.34?
275 like( $err, qr/already declared/, ' with conflicting versions reported' );
279 ############################## Multiple Modules
281 # Multiple files with same package, no version
282 # Simple.pm => Simple
283 # Simple2.pm => Simple
285 $dist->change_file( 'lib/Simple.pm', <<'---' );
288 $dist->add_file( 'lib/Simple2.pm', <<'---' );
291 $dist->regen( clean => 1 );
293 is_deeply($mb->find_dist_packages,
294 {'Simple' => { file => $simple_file }});
295 $dist->remove_file( 'lib/Simple2.pm' );
298 # Multiple files with same package, single version in corresponding package
299 # Simple.pm => Simple v1.23
300 # Simple2.pm => Simple
302 $dist->change_file( 'lib/Simple.pm', <<'---' );
306 $dist->add_file( 'lib/Simple2.pm', <<'---' );
309 $dist->regen( clean => 1 );
311 is_deeply($mb->find_dist_packages,
312 {'Simple' => { file => $simple_file,
313 version => '1.23' }});
314 $dist->remove_file( 'lib/Simple2.pm' );
317 # Multiple files with same package,
318 # single version in non-corresponding package
319 # Simple.pm => Simple
320 # Simple2.pm => Simple v1.23
322 $dist->change_file( 'lib/Simple.pm', <<'---' );
325 $dist->add_file( 'lib/Simple2.pm', <<'---' );
329 $dist->regen( clean => 1 );
331 is_deeply($mb->find_dist_packages,
332 {'Simple' => { file => $simple2_file,
333 version => '1.23' }});
334 $dist->remove_file( 'lib/Simple2.pm' );
337 # Multiple files with same package, conflicting versions
338 # Simple.pm => Simple v1.23
339 # Simple2.pm => Simple v2.34
341 $dist->change_file( 'lib/Simple.pm', <<'---' );
345 $dist->add_file( 'lib/Simple2.pm', <<'---' );
349 $dist->regen( clean => 1 );
351 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
353 {'Simple' => { file => $simple_file,
354 version => '1.23' }});
355 like( $err, qr/Found conflicting versions for package/,
356 ' with conflicting versions reported' );
357 $dist->remove_file( 'lib/Simple2.pm' );
360 # Multiple files with same package, multiple agreeing versions
361 # Simple.pm => Simple v1.23
362 # Simple2.pm => Simple v1.23
364 $dist->change_file( 'lib/Simple.pm', <<'---' );
368 $dist->add_file( 'lib/Simple2.pm', <<'---' );
372 $dist->regen( clean => 1 );
374 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
376 {'Simple' => { file => $simple_file,
377 version => '1.23' }});
378 $dist->remove_file( 'lib/Simple2.pm' );
381 ############################################################
383 # (Same as above five cases except with non-corresponding package)
386 # Multiple files with same package, no version
390 $dist->change_file( 'lib/Simple.pm', <<'---' );
393 $dist->add_file( 'lib/Simple2.pm', <<'---' );
396 $dist->regen( clean => 1 );
398 $provides = $mb->find_dist_packages;
399 ok( exists( $provides->{Foo} ) ); # it exist, can't predict which file
400 $dist->remove_file( 'lib/Simple2.pm' );
403 # Multiple files with same package, version in first file
404 # Simple.pm => Foo v1.23
407 $dist->change_file( 'lib/Simple.pm', <<'---' );
411 $dist->add_file( 'lib/Simple2.pm', <<'---' );
414 $dist->regen( clean => 1 );
416 is_deeply($mb->find_dist_packages,
417 {'Foo' => { file => $simple_file,
418 version => '1.23' }});
419 $dist->remove_file( 'lib/Simple2.pm' );
422 # Multiple files with same package, version in second file
424 # Simple2.pm => Foo v1.23
426 $dist->change_file( 'lib/Simple.pm', <<'---' );
429 $dist->add_file( 'lib/Simple2.pm', <<'---' );
433 $dist->regen( clean => 1 );
435 is_deeply($mb->find_dist_packages,
436 {'Foo' => { file => $simple2_file,
437 version => '1.23' }});
438 $dist->remove_file( 'lib/Simple2.pm' );
441 # Multiple files with same package, conflicting versions
442 # Simple.pm => Foo v1.23
443 # Simple2.pm => Foo v2.34
445 $dist->change_file( 'lib/Simple.pm', <<'---' );
449 $dist->add_file( 'lib/Simple2.pm', <<'---' );
453 $dist->regen( clean => 1 );
455 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
456 # XXX Should 'Foo' exist ??? Can't predict values for file & version
457 ok( exists( $provides->{Foo} ) );
458 like( $err, qr/Found conflicting versions for package/,
459 ' with conflicting versions reported' );
460 $dist->remove_file( 'lib/Simple2.pm' );
463 # Multiple files with same package, multiple agreeing versions
464 # Simple.pm => Foo v1.23
465 # Simple2.pm => Foo v1.23
467 $dist->change_file( 'lib/Simple.pm', <<'---' );
471 $dist->add_file( 'lib/Simple2.pm', <<'---' );
475 $dist->regen( clean => 1 );
477 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
478 ok( exists( $provides->{Foo} ) );
479 is( $provides->{Foo}{version}, '1.23' );
480 ok( exists( $provides->{Foo}{file} ) ); # Can't predict which file
481 is( $err, '', ' no conflicts reported' );
482 $dist->remove_file( 'lib/Simple2.pm' );
484 ############################################################
485 # Conflicts among primary & multiple alternatives
487 # multiple files, conflicting version in corresponding file
488 $dist->change_file( 'lib/Simple.pm', <<'---' );
492 $dist->add_file( 'lib/Simple2.pm', <<'---' );
496 $dist->add_file( 'lib/Simple3.pm', <<'---' );
500 $dist->regen( clean => 1 );
501 $err = stderr_of( sub {
504 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
506 {'Simple' => { file => $simple_file,
507 version => '1.23' }});
508 like( $err, qr/Found conflicting versions for package/,
509 ' corresponding package conflicts with multiple alternatives' );
510 $dist->remove_file( 'lib/Simple2.pm' );
511 $dist->remove_file( 'lib/Simple3.pm' );
513 # multiple files, conflicting version in non-corresponding file
514 $dist->change_file( 'lib/Simple.pm', <<'---' );
518 $dist->add_file( 'lib/Simple2.pm', <<'---' );
522 $dist->add_file( 'lib/Simple3.pm', <<'---' );
526 $dist->regen( clean => 1 );
527 $err = stderr_of( sub {
530 $err = stderr_of( sub { $provides = $mb->find_dist_packages } );
532 {'Simple' => { file => $simple_file,
533 version => '1.23' }});
534 like( $err, qr/Found conflicting versions for package/,
535 ' only one alternative conflicts with corresponding package' );
536 $dist->remove_file( 'lib/Simple2.pm' );
537 $dist->remove_file( 'lib/Simple3.pm' );
540 ############################################################
541 # Don't record private packages (beginning with underscore)
542 # Simple.pm => Simple::_private
543 # Simple.pm => Simple::_private::too
545 $dist->change_file( 'lib/Simple.pm', <<'---' );
548 package Simple::_private;
550 package Simple::_private::too;
553 $dist->regen( clean => 1 );
555 is_deeply($mb->find_dist_packages,
556 {'Simple' => { file => $simple_file,
557 version => '1.23' }});
560 ############################################################
561 # Files with no packages?
563 # Simple.pm => <empty>
565 $dist->change_file( 'lib/Simple.pm', '' );
566 $dist->regen( clean => 1 );
568 is_deeply( $mb->find_dist_packages, {} );
570 # Simple.pm => =pod..=cut (no package declaration)
571 $dist->change_file( 'lib/Simple.pm', <<'---' );
576 Simple - Pure Documentation
584 $dist->regen( clean => 1 );
586 is_deeply($mb->find_dist_packages, {});
588 ############################################################
590 chdir( $cwd ) or die "Can't chdir to '$cwd': $!";