2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
12 # parse various module $VERSION lines
13 # these will be reversed later to create %modules
15 $undef => <<'---', # no $VERSION line
18 $undef => <<'---', # undefined $VERSION
22 '1.23' => <<'---', # declared & defined on same line with 'our'
24 our $VERSION = '1.23';
26 '1.23' => <<'---', # declared & defined on separate lines with 'our'
31 '1.23' => <<'---', # commented & defined on same line
33 our $VERSION = '1.23'; # our $VERSION = '4.56';
35 '1.23' => <<'---', # commented & defined on separate lines
37 # our $VERSION = '4.56';
38 our $VERSION = '1.23';
40 '1.23' => <<'---', # use vars
42 use vars qw( $VERSION );
45 '1.23' => <<'---', # choose the right default package based on package/file name
46 package Simple::_private;
49 $VERSION = '1.23'; # this should be chosen for version
51 '1.23' => <<'---', # just read the first $VERSION line
53 $VERSION = '1.23'; # we should see this line
54 $VERSION = eval $VERSION; # and ignore this one
56 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
59 package Error::Simple;
63 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
65 package Error::Simple;
70 '1.23' => <<'---', # mentions another module's $VERSION
73 if ( $Other::VERSION ) {
77 '1.23' => <<'---', # mentions another module's $VERSION in a different package
81 if ( $Simple::VERSION ) {
85 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
88 if ( $VERSION =~ /1\.23/ ) {
92 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
95 if ( $VERSION == 3.45 ) {
99 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
103 if ( $Simple::VERSION == 3.45 ) {
107 '1.23' => <<'---', # Fully qualified $VERSION declared in package
109 $Simple::VERSION = 1.23;
111 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
113 $Simple2::VERSION = '999';
114 $Simple::VERSION = 1.23;
116 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
118 $Simple2::VERSION = '999';
121 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
122 $Simple::VERSION = '1.23';
125 $x = $y, $cats = $dogs;
128 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
130 ( $VERSION ) = '1.23';
132 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
136 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
138 __PACKAGE__->mk_accessors(qw(
140 package filename line codeline subroutine finished));
142 our $VERSION = "1.23";
144 '1.23' => <<'---', # $VERSION using version.pm
146 use version; our $VERSION = version->new('1.23');
148 '1.23' => <<'---', # $VERSION using version.pm and qv()
150 use version; our $VERSION = qv('1.230');
152 '1.23' => <<'---', # Two version assignments, should ignore second one
153 $Simple::VERSION = '1.230';
154 $Simple::VERSION = eval $Simple::VERSION;
156 '1.23' => <<'---', # declared & defined on same line with 'our'
158 our $VERSION = '1.23_00_00';
160 '1.23' => <<'---', # package NAME VERSION
163 '1.23_01' => <<'---', # package NAME VERSION
164 package Simple 1.23_01;
166 'v1.2.3' => <<'---', # package NAME VERSION
167 package Simple v1.2.3;
169 'v1.2_3' => <<'---', # package NAME VERSION
170 package Simple v1.2_3;
172 '1.23' => <<'---', # trailing crud
175 $VERSION = '1.23-alpha';
177 '1.23' => <<'---', # trailing crud
182 '1.234' => <<'---', # multi_underscore
185 $VERSION = '1.2_3_4';
187 '0' => <<'---', # non-numeric
190 $VERSION = 'onetwothree';
192 $undef => <<'---', # package NAME BLOCK, undef $VERSION
197 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
199 our $VERSION = '1.23';
202 '1.23' => <<'---', # package NAME VERSION BLOCK
203 package Simple 1.23 {
207 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
208 package Simple v1.2.3_4 {
213 my %modules = reverse @modules;
215 plan tests => 54 + 2 * keys( %modules );
217 require_ok('Module::Metadata');
219 # class method C<find_module_by_name>
220 my $module = Module::Metadata->find_module_by_name(
221 'Module::Metadata' );
222 ok( -e $module, 'find_module_by_name() succeeds' );
224 #########################
226 my $tmp = MBTest->tmpdir;
229 my $dist = DistGen->new( dir => $tmp );
235 # fail on invalid module name
236 my $pm_info = Module::Metadata->new_from_module(
237 'Foo::Bar', inc => [] );
238 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
241 # fail on invalid filename
242 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
243 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
244 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
247 # construct from module filename
248 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
249 $pm_info = Module::Metadata->new_from_file( $file );
250 ok( defined( $pm_info ), 'new_from_file() succeeds' );
252 # construct from filehandle
253 my $handle = IO::File->new($file);
254 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
255 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
256 $pm_info = Module::Metadata->new_from_handle( $handle );
257 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
260 # construct from module name, using custom include path
261 $pm_info = Module::Metadata->new_from_module(
262 $dist->name, inc => [ 'lib', @INC ] );
263 ok( defined( $pm_info ), 'new_from_module() succeeds' );
266 foreach my $module ( sort keys %modules ) {
267 my $expected = $modules{$module};
269 skip( "No our() support until perl 5.6", 2 )
270 if $] < 5.006 && $module =~ /\bour\b/;
271 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
272 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
274 $dist->change_file( 'lib/Simple.pm', $module );
278 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
279 my $pm_info = Module::Metadata->new_from_file( $file );
281 # Test::Builder will prematurely numify objects, so use this form
283 my $got = $pm_info->version;
284 if ( defined $expected ) {
285 ok( $got eq $expected,
286 "correct module version (expected '$expected')" )
290 "correct module version (expected undef)" )
293 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
294 diag "Got: '$got'\nModule contents:\n$module" if $errs;
298 # revert to pristine state
299 $dist->regen( clean => 1 );
301 # Find each package only once
302 $dist->change_file( 'lib/Simple.pm', <<'---' );
305 package Error::Simple;
312 $pm_info = Module::Metadata->new_from_file( $file );
314 my @packages = $pm_info->packages_inside;
315 is( @packages, 2, 'record only one occurence of each package' );
318 # Module 'Simple.pm' does not contain package 'Simple';
319 # constructor should not complain, no default module name or version
320 $dist->change_file( 'lib/Simple.pm', <<'---' );
326 $pm_info = Module::Metadata->new_from_file( $file );
328 is( $pm_info->name, undef, 'no default package' );
329 is( $pm_info->version, undef, 'no version w/o default package' );
331 # Module 'Simple.pm' contains an alpha version
332 # constructor should report first $VERSION found
333 $dist->change_file( 'lib/Simple.pm', <<'---' );
335 $VERSION = '1.23_01';
336 $VERSION = eval $VERSION;
340 $pm_info = Module::Metadata->new_from_file( $file );
342 is( $pm_info->version, '1.23_01', 'alpha version reported');
344 # NOTE the following test has be done this way because Test::Builder is
345 # too smart for our own good and tries to see if the version object is a
346 # dual-var, which breaks with alpha versions:
347 # Argument "1.23_0100" isn't numeric in addition (+) at
348 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
350 ok( $pm_info->version > 1.23, 'alpha version greater than non');
352 # revert to pristine state
353 $dist->regen( clean => 1 );
355 # parse $VERSION lines scripts for package main
357 <<'---', # package main declared
362 <<'---', # on first non-comment line, non declared package main
366 <<'---', # after non-comment line
371 <<'---', # 1st declared package
378 <<'---', # 2nd declared package
385 <<'---', # split package
393 <<'---', # define 'main' version from other package
398 <<'---', # define 'main' version from other package
405 my ( $i, $n ) = ( 1, scalar( @scripts ) );
406 foreach my $script ( @scripts ) {
407 $dist->change_file( 'bin/simple.plx', $script );
409 $pm_info = Module::Metadata->new_from_file(
410 File::Spec->catfile( 'bin', 'simple.plx' ) );
412 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
417 # examine properties of a module: name, pod, etc
418 $dist->change_file( 'lib/Simple.pm', <<'---' );
432 You can find me on the IRC channel
433 #simon on irc.perl.org.
439 $pm_info = Module::Metadata->new_from_module(
440 $dist->name, inc => [ 'lib', @INC ] );
442 is( $pm_info->name, 'Simple', 'found default package' );
443 is( $pm_info->version, '0.01', 'version for default package' );
445 # got correct version for secondary package
446 is( $pm_info->version( 'Simple::Ex' ), '0.02',
447 'version for secondary package' );
449 my $filename = $pm_info->filename;
450 ok( defined( $filename ) && -e $filename,
451 'filename() returns valid path to module file' );
453 @packages = $pm_info->packages_inside;
454 is( @packages, 2, 'found correct number of packages' );
455 is( $packages[0], 'Simple', 'packages stored in order found' );
457 # we can detect presence of pod regardless of whether we are collecting it
458 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
460 my @pod = $pm_info->pod_inside;
461 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
463 is( $pm_info->pod('NONE') , undef,
464 'return undef() if pod section not present' );
466 is( $pm_info->pod('NAME'), undef,
467 'return undef() if pod section not collected' );
471 $pm_info = Module::Metadata->new_from_module(
472 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
476 for my $section (qw(NAME AUTHOR)) {
477 my $content = $pm_info->pod( $section );
479 $content =~ s/^\s+//;
480 $content =~ s/\s+$//;
482 $pod{$section} = $content;
485 NAME => q|Simple - It's easy.|,
486 AUTHOR => <<'EXPECTED'
489 You can find me on the IRC channel
490 #simon on irc.perl.org.
493 for my $text (values %expected) {
497 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
498 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
502 # test things that look like POD, but aren't
503 $dist->change_file( 'lib/Simple.pm', <<'---' );
508 our $VERSION = '999';
512 our $VERSION = '666';
517 =*no_this_does_not_start_pod;
519 our $VERSION = '1.23';
523 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
524 is( $pm_info->name, 'Simple', 'found default package' );
525 is( $pm_info->version, '1.23', 'version for default package' );
529 # Make sure processing stops after __DATA__
530 $dist->change_file( 'lib/Simple.pm', <<'---' );
534 *UNIVERSAL::VERSION = sub {
540 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
541 is( $pm_info->name, 'Simple', 'found default package' );
542 is( $pm_info->version, '0.01', 'version for default package' );
543 my @packages = $pm_info->packages_inside;
544 is_deeply(\@packages, ['Simple'], 'packages inside');
548 # Make sure we handle version.pm $VERSIONs well
549 $dist->change_file( 'lib/Simple.pm', <<'---' );
551 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
552 package Simple::Simon;
553 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
557 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
558 is( $pm_info->name, 'Simple', 'found default package' );
559 is( $pm_info->version, '0.60.128', 'version for default package' );
560 my @packages = $pm_info->packages_inside;
561 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
562 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
565 # check that package_versions_from_directory works
567 $dist->change_file( 'lib/Simple.pm', <<'---' );
573 package main; # should ignore this
576 package DB; # should ignore this
579 package Simple::_private; # should ignore this
596 'file' => 'Simple.pm',
600 'file' => 'Simple.pm',
605 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
607 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
608 or diag explain $got_pvfd;
611 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
614 'file' => 'lib/Simple.pm',
618 'file' => 'lib/Simple.pm',
623 is_deeply( $got_provides, $exp_provides, "provides()" )
624 or diag explain $got_provides;
628 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
631 'file' => 'other/Simple.pm',
635 'file' => 'other/Simple.pm',
640 is_deeply( $got_provides, $exp_provides, "provides()" )
641 or diag explain $got_provides;
644 # Check package_versions_from_directory with regard to case-sensitivity
646 $dist->change_file( 'lib/Simple.pm', <<'---' );
652 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
653 is( $pm_info->name, undef, 'no default package' );
654 is( $pm_info->version, undef, 'version for default package' );
655 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
656 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
658 $dist->change_file( 'lib/Simple.pm', <<'---' );
668 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
669 is( $pm_info->name, 'Simple', 'found default package' );
670 is( $pm_info->version, '0.02', 'version for default package' );
671 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
672 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
673 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );