Upgrade to Module::Build 0.2808_01
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / metadata.t
CommitLineData
bb4e9162 1#!/usr/bin/perl -w
2
3use strict;
4use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
7a827510 5use MBTest tests => 49;
bb4e9162 6
7use Cwd ();
8my $cwd = Cwd::cwd;
7a827510 9my $tmp = MBTest->tmpdir;
bb4e9162 10
11
12use Module::Build;
13use Module::Build::ConfigData;
7a827510 14
15my %metadata =
16 (
17 module_name => 'Simple',
18 dist_version => '3.14159265',
19 dist_author => [ 'Simple Simon <ss\@somewhere.priv>' ],
20 dist_abstract => 'Something interesting',
21 license => 'perl',
22 meta_add => {
23 keywords => [qw(super duper something)],
24 resources => {homepage => 'http://foo.example.com'},
25 },
26 );
bb4e9162 27
28
29use DistGen;
30my $dist = DistGen->new( dir => $tmp );
7a827510 31$dist->change_build_pl( \%metadata );
bb4e9162 32$dist->regen;
33
d1bd4ef0 34my $simple_file = 'lib/Simple.pm';
35my $simple2_file = 'lib/Simple2.pm';
36
37 #TODO:
38 # Traditional VMS will return the file in in lower case, and is_deeply
39 # does exact case comparisons.
40 # When ODS-5 support is active for preserved case file names, this will
41 # need to be changed.
42 if ($^O eq 'VMS') {
43 $simple_file = lc($simple_file);
44 $simple2_file = lc($simple2_file);
45 }
46
47
bb4e9162 48chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
49
50use Module::Build;
51my $mb = Module::Build->new_from_context;
52
53##################################################
54#
55# Test for valid META.yml
56
7a827510 57{
58 my $node = $mb->prepare_metadata( {} );
bb4e9162 59
60 # exists() doesn't seem to work here
7a827510 61 is $node->{name}, $metadata{module_name};
62 is $node->{version}, $metadata{dist_version};
63 is $node->{abstract}, $metadata{dist_abstract};
64 is_deeply $node->{author}, $metadata{dist_author};
65 is $node->{license}, $metadata{license};
66 like $node->{generated_by}, qr{Module::Build};
bb4e9162 67 ok defined( $node->{'meta-spec'}{version} ),
68 "'meta-spec' -> 'version' field present in META.yml";
69 ok defined( $node->{'meta-spec'}{url} ),
70 "'meta-spec' -> 'url' field present in META.yml";
7a827510 71 is_deeply $node->{keywords}, $metadata{meta_add}{keywords};
72 is_deeply $node->{resources}, $metadata{meta_add}{resources};
bb4e9162 73}
74
75$dist->clean;
76
77
78##################################################
79#
80# Tests to ensure that the correct packages and versions are
81# recorded for the 'provides' field of META.yml
82
83my $provides; # Used a bunch of times below
84
85sub new_build { return Module::Build->new_from_context( quiet => 1, @_ ) }
86
87############################## Single Module
88
89# File with corresponding package (w/ or w/o version)
90# Simple.pm => Simple v1.23
91
92$dist->change_file( 'lib/Simple.pm', <<'---' );
93package Simple;
94$VERSION = '1.23';
95---
96$dist->regen( clean => 1 );
97$mb = new_build();
98is_deeply($mb->find_dist_packages,
d1bd4ef0 99 {'Simple' => {file => $simple_file,
bb4e9162 100 version => '1.23'}});
101
102$dist->change_file( 'lib/Simple.pm', <<'---' );
103package Simple;
104---
105$dist->regen( clean => 1 );
106$mb = new_build();
107is_deeply($mb->find_dist_packages,
d1bd4ef0 108 {'Simple' => {file => $simple_file}});
bb4e9162 109
110# File with no corresponding package (w/ or w/o version)
111# Simple.pm => Foo::Bar v1.23
112
113$dist->change_file( 'lib/Simple.pm', <<'---' );
114package Foo::Bar;
115$VERSION = '1.23';
116---
117$dist->regen( clean => 1 );
118$mb = new_build();
119is_deeply($mb->find_dist_packages,
d1bd4ef0 120 {'Foo::Bar' => { file => $simple_file,
bb4e9162 121 version => '1.23' }});
122
123$dist->change_file( 'lib/Simple.pm', <<'---' );
124package Foo::Bar;
125---
126$dist->regen( clean => 1 );
127$mb = new_build();
128is_deeply($mb->find_dist_packages,
d1bd4ef0 129 {'Foo::Bar' => { file => $simple_file}});
bb4e9162 130
131
132# Single file with multiple differing packages (w/ or w/o version)
133# Simple.pm => Simple
134# Simple.pm => Foo::Bar
135
136$dist->change_file( 'lib/Simple.pm', <<'---' );
137package Simple;
138$VERSION = '1.23';
139package Foo::Bar;
140$VERSION = '1.23';
141---
142$dist->regen( clean => 1 );
143$mb = new_build();
144is_deeply($mb->find_dist_packages,
d1bd4ef0 145 {'Simple' => { file => $simple_file,
bb4e9162 146 version => '1.23' },
d1bd4ef0 147 'Foo::Bar' => { file => $simple_file,
bb4e9162 148 version => '1.23' }});
149
77e96e88 150{
151 $dist->change_file( 'lib/Simple.pm', <<'---' );
152package Simple;
7253302f 153$VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
77e96e88 154package Simple::Simon;
7253302f 155$VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
77e96e88 156---
157 $dist->regen;
158 my $provides = new_build()->prepare_metadata()->{provides};
8cb289bd 159 is $provides->{'Simple'}{version}, '0.60.128', "Check version";
160 is $provides->{'Simple::Simon'}{version}, '0.61.129', "Check version";
77e96e88 161 is ref($provides->{'Simple'}{version}), '', "Versions from prepare_metadata() aren't refs";
162 is ref($provides->{'Simple::Simon'}{version}), '', "Versions from prepare_metadata() aren't refs";
163}
164
bb4e9162 165
166# Single file with multiple differing packages, no corresponding package
167# Simple.pm => Foo
168# Simple.pm => Foo::Bar
169
170$dist->change_file( 'lib/Simple.pm', <<'---' );
171package Foo;
172$VERSION = '1.23';
173package Foo::Bar;
174$VERSION = '1.23';
175---
176$dist->regen( clean => 1 );
177$mb = new_build();
178is_deeply($mb->find_dist_packages,
d1bd4ef0 179 {'Foo' => { file => $simple_file,
bb4e9162 180 version => '1.23' },
d1bd4ef0 181 'Foo::Bar' => { file => $simple_file,
bb4e9162 182 version => '1.23' }});
183
184
185# Single file with same package appearing multiple times, no version
186# only record a single instance
187# Simple.pm => Simple
188# Simple.pm => Simple
189
190$dist->change_file( 'lib/Simple.pm', <<'---' );
191package Simple;
192package Simple;
193---
194$dist->regen( clean => 1 );
195$mb = new_build();
196is_deeply($mb->find_dist_packages,
d1bd4ef0 197 {'Simple' => { file => $simple_file }});
bb4e9162 198
199
200# Single file with same package appearing multiple times, single
201# version 1st package:
202# Simple.pm => Simple v1.23
203# Simple.pm => Simple
204
205$dist->change_file( 'lib/Simple.pm', <<'---' );
206package Simple;
207$VERSION = '1.23';
208package Simple;
209---
210$dist->regen( clean => 1 );
211$mb = new_build();
212is_deeply($mb->find_dist_packages,
d1bd4ef0 213 {'Simple' => { file => $simple_file,
bb4e9162 214 version => '1.23' }});
215
216
217# Single file with same package appearing multiple times, single
218# version 2nd package
219# Simple.pm => Simple
220# Simple.pm => Simple v1.23
221
222$dist->change_file( 'lib/Simple.pm', <<'---' );
223package Simple;
224package Simple;
225$VERSION = '1.23';
226---
227$dist->regen( clean => 1 );
228$mb = new_build();
229is_deeply($mb->find_dist_packages,
d1bd4ef0 230 {'Simple' => { file => $simple_file,
bb4e9162 231 version => '1.23' }});
232
233
234# Single file with same package appearing multiple times, conflicting versions
235# Simple.pm => Simple v1.23
236# Simple.pm => Simple v2.34
237
238$dist->change_file( 'lib/Simple.pm', <<'---' );
239package Simple;
240$VERSION = '1.23';
241package Simple;
242$VERSION = '2.34';
243---
244$dist->regen( clean => 1 );
245my $err = '';
246$err = stderr_of( sub { $mb = new_build() } );
247$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
248is_deeply($provides,
d1bd4ef0 249 {'Simple' => { file => $simple_file,
bb4e9162 250 version => '1.23' }}); # XXX should be 2.34?
251like( $err, qr/already declared/, ' with conflicting versions reported' );
252
253
254# (Same as above three cases except with no corresponding package)
255# Simple.pm => Foo v1.23
256# Simple.pm => Foo v2.34
257
258$dist->change_file( 'lib/Simple.pm', <<'---' );
259package Foo;
260$VERSION = '1.23';
261package Foo;
262$VERSION = '2.34';
263---
264$dist->regen( clean => 1 );
265$err = stderr_of( sub { $mb = new_build() } );
266$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
267is_deeply($provides,
d1bd4ef0 268 {'Foo' => { file => $simple_file,
bb4e9162 269 version => '1.23' }}); # XXX should be 2.34?
270like( $err, qr/already declared/, ' with conflicting versions reported' );
271
272
273
274############################## Multiple Modules
275
276# Multiple files with same package, no version
277# Simple.pm => Simple
278# Simple2.pm => Simple
279
280$dist->change_file( 'lib/Simple.pm', <<'---' );
281package Simple;
282---
283$dist->add_file( 'lib/Simple2.pm', <<'---' );
284package Simple;
285---
286$dist->regen( clean => 1 );
287$mb = new_build();
288is_deeply($mb->find_dist_packages,
d1bd4ef0 289 {'Simple' => { file => $simple_file }});
bb4e9162 290$dist->remove_file( 'lib/Simple2.pm' );
291
292
293# Multiple files with same package, single version in corresponding package
294# Simple.pm => Simple v1.23
295# Simple2.pm => Simple
296
297$dist->change_file( 'lib/Simple.pm', <<'---' );
298package Simple;
299$VERSION = '1.23';
300---
301$dist->add_file( 'lib/Simple2.pm', <<'---' );
302package Simple;
303---
304$dist->regen( clean => 1 );
305$mb = new_build();
306is_deeply($mb->find_dist_packages,
d1bd4ef0 307 {'Simple' => { file => $simple_file,
bb4e9162 308 version => '1.23' }});
309$dist->remove_file( 'lib/Simple2.pm' );
310
311
312# Multiple files with same package,
313# single version in non-corresponding package
314# Simple.pm => Simple
315# Simple2.pm => Simple v1.23
316
317$dist->change_file( 'lib/Simple.pm', <<'---' );
318package Simple;
319---
320$dist->add_file( 'lib/Simple2.pm', <<'---' );
321package Simple;
322$VERSION = '1.23';
323---
324$dist->regen( clean => 1 );
325$mb = new_build();
326is_deeply($mb->find_dist_packages,
d1bd4ef0 327 {'Simple' => { file => $simple2_file,
bb4e9162 328 version => '1.23' }});
329$dist->remove_file( 'lib/Simple2.pm' );
330
331
332# Multiple files with same package, conflicting versions
333# Simple.pm => Simple v1.23
334# Simple2.pm => Simple v2.34
335
336$dist->change_file( 'lib/Simple.pm', <<'---' );
337package Simple;
338$VERSION = '1.23';
339---
340$dist->add_file( 'lib/Simple2.pm', <<'---' );
341package Simple;
342$VERSION = '2.34';
343---
344$dist->regen( clean => 1 );
345$mb = new_build();
346$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
347is_deeply($provides,
d1bd4ef0 348 {'Simple' => { file => $simple_file,
bb4e9162 349 version => '1.23' }});
350like( $err, qr/Found conflicting versions for package/,
351 ' with conflicting versions reported' );
352$dist->remove_file( 'lib/Simple2.pm' );
353
354
355# Multiple files with same package, multiple agreeing versions
356# Simple.pm => Simple v1.23
357# Simple2.pm => Simple v1.23
358
359$dist->change_file( 'lib/Simple.pm', <<'---' );
360package Simple;
361$VERSION = '1.23';
362---
363$dist->add_file( 'lib/Simple2.pm', <<'---' );
364package Simple;
365$VERSION = '1.23';
366---
367$dist->regen( clean => 1 );
368$mb = new_build();
369$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
370is_deeply($provides,
d1bd4ef0 371 {'Simple' => { file => $simple_file,
bb4e9162 372 version => '1.23' }});
373$dist->remove_file( 'lib/Simple2.pm' );
374
375
376############################################################
377#
378# (Same as above five cases except with non-corresponding package)
379#
380
381# Multiple files with same package, no version
382# Simple.pm => Foo
383# Simple2.pm => Foo
384
385$dist->change_file( 'lib/Simple.pm', <<'---' );
386package Foo;
387---
388$dist->add_file( 'lib/Simple2.pm', <<'---' );
389package Foo;
390---
391$dist->regen( clean => 1 );
392$mb = new_build();
393$provides = $mb->find_dist_packages;
394ok( exists( $provides->{Foo} ) ); # it exist, can't predict which file
395$dist->remove_file( 'lib/Simple2.pm' );
396
397
398# Multiple files with same package, version in first file
399# Simple.pm => Foo v1.23
400# Simple2.pm => Foo
401
402$dist->change_file( 'lib/Simple.pm', <<'---' );
403package Foo;
404$VERSION = '1.23';
405---
406$dist->add_file( 'lib/Simple2.pm', <<'---' );
407package Foo;
408---
409$dist->regen( clean => 1 );
410$mb = new_build();
411is_deeply($mb->find_dist_packages,
d1bd4ef0 412 {'Foo' => { file => $simple_file,
bb4e9162 413 version => '1.23' }});
414$dist->remove_file( 'lib/Simple2.pm' );
415
416
417# Multiple files with same package, version in second file
418# Simple.pm => Foo
419# Simple2.pm => Foo v1.23
420
421$dist->change_file( 'lib/Simple.pm', <<'---' );
422package Foo;
423---
424$dist->add_file( 'lib/Simple2.pm', <<'---' );
425package Foo;
426$VERSION = '1.23';
427---
428$dist->regen( clean => 1 );
429$mb = new_build();
430is_deeply($mb->find_dist_packages,
d1bd4ef0 431 {'Foo' => { file => $simple2_file,
bb4e9162 432 version => '1.23' }});
433$dist->remove_file( 'lib/Simple2.pm' );
434
435
436# Multiple files with same package, conflicting versions
437# Simple.pm => Foo v1.23
438# Simple2.pm => Foo v2.34
439
440$dist->change_file( 'lib/Simple.pm', <<'---' );
441package Foo;
442$VERSION = '1.23';
443---
444$dist->add_file( 'lib/Simple2.pm', <<'---' );
445package Foo;
446$VERSION = '2.34';
447---
448$dist->regen( clean => 1 );
449$mb = new_build();
450$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
451# XXX Should 'Foo' exist ??? Can't predict values for file & version
452ok( exists( $provides->{Foo} ) );
453like( $err, qr/Found conflicting versions for package/,
454 ' with conflicting versions reported' );
455$dist->remove_file( 'lib/Simple2.pm' );
456
457
458# Multiple files with same package, multiple agreeing versions
459# Simple.pm => Foo v1.23
460# Simple2.pm => Foo v1.23
461
462$dist->change_file( 'lib/Simple.pm', <<'---' );
463package Foo;
464$VERSION = '1.23';
465---
466$dist->add_file( 'lib/Simple2.pm', <<'---' );
467package Foo;
468$VERSION = '1.23';
469---
470$dist->regen( clean => 1 );
471$mb = new_build();
472$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
473ok( exists( $provides->{Foo} ) );
474is( $provides->{Foo}{version}, '1.23' );
475ok( exists( $provides->{Foo}{file} ) ); # Can't predict which file
476is( $err, '', ' no conflicts reported' );
477$dist->remove_file( 'lib/Simple2.pm' );
478
479############################################################
480# Conflicts among primary & multiple alternatives
481
482# multiple files, conflicting version in corresponding file
483$dist->change_file( 'lib/Simple.pm', <<'---' );
484package Simple;
485$VERSION = '1.23';
486---
487$dist->add_file( 'lib/Simple2.pm', <<'---' );
488package Simple;
489$VERSION = '2.34';
490---
491$dist->add_file( 'lib/Simple3.pm', <<'---' );
492package Simple;
493$VERSION = '2.34';
494---
495$dist->regen( clean => 1 );
496$err = stderr_of( sub {
497 $mb = new_build();
498} );
499$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
500is_deeply($provides,
d1bd4ef0 501 {'Simple' => { file => $simple_file,
bb4e9162 502 version => '1.23' }});
503like( $err, qr/Found conflicting versions for package/,
504 ' corresponding package conflicts with multiple alternatives' );
505$dist->remove_file( 'lib/Simple2.pm' );
506$dist->remove_file( 'lib/Simple3.pm' );
507
508# multiple files, conflicting version in non-corresponding file
509$dist->change_file( 'lib/Simple.pm', <<'---' );
510package Simple;
511$VERSION = '1.23';
512---
513$dist->add_file( 'lib/Simple2.pm', <<'---' );
514package Simple;
515$VERSION = '1.23';
516---
517$dist->add_file( 'lib/Simple3.pm', <<'---' );
518package Simple;
519$VERSION = '2.34';
520---
521$dist->regen( clean => 1 );
522$err = stderr_of( sub {
523 $mb = new_build();
524} );
525$err = stderr_of( sub { $provides = $mb->find_dist_packages } );
526is_deeply($provides,
d1bd4ef0 527 {'Simple' => { file => $simple_file,
bb4e9162 528 version => '1.23' }});
529like( $err, qr/Found conflicting versions for package/,
530 ' only one alternative conflicts with corresponding package' );
531$dist->remove_file( 'lib/Simple2.pm' );
532$dist->remove_file( 'lib/Simple3.pm' );
533
534
535############################################################
536# Don't record private packages (beginning with underscore)
537# Simple.pm => Simple::_private
538# Simple.pm => Simple::_private::too
539
540$dist->change_file( 'lib/Simple.pm', <<'---' );
541package Simple;
542$VERSION = '1.23';
543package Simple::_private;
544$VERSION = '2.34';
545package Simple::_private::too;
546$VERSION = '3.45';
547---
548$dist->regen( clean => 1 );
549$mb = new_build();
550is_deeply($mb->find_dist_packages,
d1bd4ef0 551 {'Simple' => { file => $simple_file,
bb4e9162 552 version => '1.23' }});
553
554
555############################################################
556# Files with no packages?
557
558# Simple.pm => <empty>
559
560$dist->change_file( 'lib/Simple.pm', '' );
561$dist->regen( clean => 1 );
562$mb = new_build();
563is_deeply( $mb->find_dist_packages, {} );
564
565# Simple.pm => =pod..=cut (no package declaration)
566$dist->change_file( 'lib/Simple.pm', <<'---' );
567=pod
568
569=head1 NAME
570
571Simple - Pure Documentation
572
573=head1 DESCRIPTION
574
575Doesn't do anything.
576
577=cut
578---
579$dist->regen( clean => 1 );
580$mb = new_build();
581is_deeply($mb->find_dist_packages, {});
582
bb4e9162 583############################################################
584# cleanup
585chdir( $cwd ) or die "Can't chdir to '$cwd': $!";
586$dist->remove;
587
588use File::Path;
589rmtree( $tmp );