2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
14 # parse various module $VERSION lines
15 # these will be reversed later to create %modules
17 $undef => <<'---', # no $VERSION line
20 $undef => <<'---', # undefined $VERSION
24 '1.23' => <<'---', # declared & defined on same line with 'our'
26 our $VERSION = '1.23';
28 '1.23' => <<'---', # declared & defined on separate lines with 'our'
33 '1.23' => <<'---', # commented & defined on same line
35 our $VERSION = '1.23'; # our $VERSION = '4.56';
37 '1.23' => <<'---', # commented & defined on separate lines
39 # our $VERSION = '4.56';
40 our $VERSION = '1.23';
42 '1.23' => <<'---', # use vars
44 use vars qw( $VERSION );
47 '1.23' => <<'---', # choose the right default package based on package/file name
48 package Simple::_private;
51 $VERSION = '1.23'; # this should be chosen for version
53 '1.23' => <<'---', # just read the first $VERSION line
55 $VERSION = '1.23'; # we should see this line
56 $VERSION = eval $VERSION; # and ignore this one
58 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
61 package Error::Simple;
65 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
67 package Error::Simple;
72 '1.23' => <<'---', # mentions another module's $VERSION
75 if ( $Other::VERSION ) {
79 '1.23' => <<'---', # mentions another module's $VERSION in a different package
83 if ( $Simple::VERSION ) {
87 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
90 if ( $VERSION =~ /1\.23/ ) {
94 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
97 if ( $VERSION == 3.45 ) {
101 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
105 if ( $Simple::VERSION == 3.45 ) {
109 '1.23' => <<'---', # Fully qualified $VERSION declared in package
111 $Simple::VERSION = 1.23;
113 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
115 $Simple2::VERSION = '999';
116 $Simple::VERSION = 1.23;
118 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
120 $Simple2::VERSION = '999';
123 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
124 $Simple::VERSION = '1.23';
127 $x = $y, $cats = $dogs;
130 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
132 ( $VERSION ) = '1.23';
134 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
138 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
140 __PACKAGE__->mk_accessors(qw(
142 package filename line codeline subroutine finished));
144 our $VERSION = "1.23";
146 '1.23' => <<'---', # $VERSION using version.pm
148 use version; our $VERSION = version->new('1.23');
150 '1.23' => <<'---', # $VERSION using version.pm and qv()
152 use version; our $VERSION = qv('1.230');
154 '1.23' => <<'---', # Two version assignments, should ignore second one
155 $Simple::VERSION = '1.230';
156 $Simple::VERSION = eval $Simple::VERSION;
158 '1.23' => <<'---', # declared & defined on same line with 'our'
160 our $VERSION = '1.23_00_00';
162 '1.23' => <<'---', # package NAME VERSION
165 '1.23_01' => <<'---', # package NAME VERSION
166 package Simple 1.23_01;
168 'v1.2.3' => <<'---', # package NAME VERSION
169 package Simple v1.2.3;
171 'v1.2_3' => <<'---', # package NAME VERSION
172 package Simple v1.2_3;
174 '1.23' => <<'---', # trailing crud
177 $VERSION = '1.23-alpha';
179 '1.23' => <<'---', # trailing crud
184 '1.234' => <<'---', # multi_underscore
187 $VERSION = '1.2_3_4';
189 '0' => <<'---', # non-numeric
192 $VERSION = 'onetwothree';
194 $undef => <<'---', # package NAME BLOCK, undef $VERSION
199 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
201 our $VERSION = '1.23';
204 '1.23' => <<'---', # package NAME VERSION BLOCK
205 package Simple 1.23 {
209 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
210 package Simple v1.2.3_4 {
214 '0' => <<'---', # set from separately-initialised variable
216 our $CVSVERSION = '$Revision: 1.7 $';
217 our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
221 my %modules = reverse @modules;
224 [ 'Simple' ] => <<'---', # package NAME
227 [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
228 package Simple::Edward;
230 [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
231 package Simple::Edward::;
233 [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
234 package Simple'Edward;
236 [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
237 package Simple'Edward::;
239 [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
240 package Simple::::Edward;
242 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
243 package ::Simple::Edward;
245 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
246 package Simple:Edward;
248 [ 'main' ] => <<'---', # package NAME' (fail)
251 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
252 package Simple::Edward';
254 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
255 package Simple''Edward;
257 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
258 package Simple-Edward;
261 my %pkg_names = reverse @pkg_names;
263 plan tests => 63 + (2 * keys( %modules )) + (2 * keys( %pkg_names ));
265 require_ok('Module::Metadata');
267 # class method C<find_module_by_name>
268 my $module = Module::Metadata->find_module_by_name(
269 'Module::Metadata' );
270 ok( -e $module, 'find_module_by_name() succeeds' );
272 #########################
274 my $tmp = MBTest->tmpdir;
277 my $dist = DistGen->new( dir => $tmp );
283 # fail on invalid module name
284 my $pm_info = Module::Metadata->new_from_module(
285 'Foo::Bar', inc => [] );
286 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
289 # fail on invalid filename
290 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
291 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
292 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
295 # construct from module filename
296 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
297 $pm_info = Module::Metadata->new_from_file( $file );
298 ok( defined( $pm_info ), 'new_from_file() succeeds' );
300 # construct from filehandle
301 my $handle = IO::File->new($file);
302 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
303 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
304 $pm_info = Module::Metadata->new_from_handle( $handle );
305 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
308 # construct from module name, using custom include path
309 $pm_info = Module::Metadata->new_from_module(
310 $dist->name, inc => [ 'lib', @INC ] );
311 ok( defined( $pm_info ), 'new_from_module() succeeds' );
314 foreach my $module ( sort keys %modules ) {
315 my $expected = $modules{$module};
317 skip( "No our() support until perl 5.6", 2 )
318 if $] < 5.006 && $module =~ /\bour\b/;
319 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
320 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
322 $dist->change_file( 'lib/Simple.pm', $module );
326 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
327 my $pm_info = Module::Metadata->new_from_file( $file );
329 # Test::Builder will prematurely numify objects, so use this form
331 my $got = $pm_info->version;
332 if ( defined $expected ) {
333 ok( $got eq $expected,
334 "correct module version (expected '$expected')" )
338 "correct module version (expected undef)" )
341 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
342 diag "Got: '$got'\nModule contents:\n$module" if $errs;
346 # revert to pristine state
347 $dist->regen( clean => 1 );
349 foreach my $pkg_name ( sort keys %pkg_names ) {
350 my $expected = $pkg_names{$pkg_name};
352 $dist->change_file( 'lib/Simple.pm', $pkg_name );
356 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
357 my $pm_info = Module::Metadata->new_from_file( $file );
359 # Test::Builder will prematurely numify objects, so use this form
361 my @got = $pm_info->packages_inside();
362 is_deeply( \@got, $expected,
363 "correct package names (expected '" . join(', ', @$expected) . "')" )
365 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
366 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_name" if $errs;
369 # revert to pristine state
370 $dist->regen( clean => 1 );
372 # Find each package only once
373 $dist->change_file( 'lib/Simple.pm', <<'---' );
376 package Error::Simple;
383 $pm_info = Module::Metadata->new_from_file( $file );
385 my @packages = $pm_info->packages_inside;
386 is( @packages, 2, 'record only one occurence of each package' );
389 # Module 'Simple.pm' does not contain package 'Simple';
390 # constructor should not complain, no default module name or version
391 $dist->change_file( 'lib/Simple.pm', <<'---' );
397 $pm_info = Module::Metadata->new_from_file( $file );
399 is( $pm_info->name, undef, 'no default package' );
400 is( $pm_info->version, undef, 'no version w/o default package' );
402 # Module 'Simple.pm' contains an alpha version
403 # constructor should report first $VERSION found
404 $dist->change_file( 'lib/Simple.pm', <<'---' );
406 $VERSION = '1.23_01';
407 $VERSION = eval $VERSION;
411 $pm_info = Module::Metadata->new_from_file( $file );
413 is( $pm_info->version, '1.23_01', 'alpha version reported');
415 # NOTE the following test has be done this way because Test::Builder is
416 # too smart for our own good and tries to see if the version object is a
417 # dual-var, which breaks with alpha versions:
418 # Argument "1.23_0100" isn't numeric in addition (+) at
419 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
421 ok( $pm_info->version > 1.23, 'alpha version greater than non');
423 # revert to pristine state
424 $dist->regen( clean => 1 );
426 # parse $VERSION lines scripts for package main
428 <<'---', # package main declared
433 <<'---', # on first non-comment line, non declared package main
437 <<'---', # after non-comment line
442 <<'---', # 1st declared package
449 <<'---', # 2nd declared package
456 <<'---', # split package
464 <<'---', # define 'main' version from other package
469 <<'---', # define 'main' version from other package
476 my ( $i, $n ) = ( 1, scalar( @scripts ) );
477 foreach my $script ( @scripts ) {
478 $dist->change_file( 'bin/simple.plx', $script );
480 $pm_info = Module::Metadata->new_from_file(
481 File::Spec->catfile( 'bin', 'simple.plx' ) );
483 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
488 # examine properties of a module: name, pod, etc
489 $dist->change_file( 'lib/Simple.pm', <<'---' );
503 You can find me on the IRC channel
504 #simon on irc.perl.org.
510 $pm_info = Module::Metadata->new_from_module(
511 $dist->name, inc => [ 'lib', @INC ] );
513 is( $pm_info->name, 'Simple', 'found default package' );
514 is( $pm_info->version, '0.01', 'version for default package' );
516 # got correct version for secondary package
517 is( $pm_info->version( 'Simple::Ex' ), '0.02',
518 'version for secondary package' );
520 my $filename = $pm_info->filename;
521 ok( defined( $filename ) && -e $filename,
522 'filename() returns valid path to module file' );
524 @packages = $pm_info->packages_inside;
525 is( @packages, 2, 'found correct number of packages' );
526 is( $packages[0], 'Simple', 'packages stored in order found' );
528 # we can detect presence of pod regardless of whether we are collecting it
529 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
531 my @pod = $pm_info->pod_inside;
532 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
534 is( $pm_info->pod('NONE') , undef,
535 'return undef() if pod section not present' );
537 is( $pm_info->pod('NAME'), undef,
538 'return undef() if pod section not collected' );
542 $pm_info = Module::Metadata->new_from_module(
543 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
547 for my $section (qw(NAME AUTHOR)) {
548 my $content = $pm_info->pod( $section );
550 $content =~ s/^\s+//;
551 $content =~ s/\s+$//;
553 $pod{$section} = $content;
556 NAME => q|Simple - It's easy.|,
557 AUTHOR => <<'EXPECTED'
560 You can find me on the IRC channel
561 #simon on irc.perl.org.
564 for my $text (values %expected) {
568 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
569 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
573 # test things that look like POD, but aren't
574 $dist->change_file( 'lib/Simple.pm', <<'---' );
579 our $VERSION = '999';
583 our $VERSION = '666';
588 =*no_this_does_not_start_pod;
590 our $VERSION = '1.23';
594 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
595 is( $pm_info->name, 'Simple', 'found default package' );
596 is( $pm_info->version, '1.23', 'version for default package' );
600 # Make sure processing stops after __DATA__
601 $dist->change_file( 'lib/Simple.pm', <<'---' );
605 *UNIVERSAL::VERSION = sub {
611 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
612 is( $pm_info->name, 'Simple', 'found default package' );
613 is( $pm_info->version, '0.01', 'version for default package' );
614 my @packages = $pm_info->packages_inside;
615 is_deeply(\@packages, ['Simple'], 'packages inside');
619 # Make sure we handle version.pm $VERSIONs well
620 $dist->change_file( 'lib/Simple.pm', <<'---' );
622 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
623 package Simple::Simon;
624 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
628 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
629 is( $pm_info->name, 'Simple', 'found default package' );
630 is( $pm_info->version, '0.60.128', 'version for default package' );
631 my @packages = $pm_info->packages_inside;
632 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
633 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
636 # check that package_versions_from_directory works
638 $dist->change_file( 'lib/Simple.pm', <<'---' );
644 package main; # should ignore this
647 package DB; # should ignore this
650 package Simple::_private; # should ignore this
667 'file' => 'Simple.pm',
671 'file' => 'Simple.pm',
676 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
678 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
679 or diag explain $got_pvfd;
682 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
685 'file' => 'lib/Simple.pm',
689 'file' => 'lib/Simple.pm',
694 is_deeply( $got_provides, $exp_provides, "provides()" )
695 or diag explain $got_provides;
699 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
702 'file' => 'other/Simple.pm',
706 'file' => 'other/Simple.pm',
711 is_deeply( $got_provides, $exp_provides, "provides()" )
712 or diag explain $got_provides;
715 # Check package_versions_from_directory with regard to case-sensitivity
717 $dist->change_file( 'lib/Simple.pm', <<'---' );
723 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
724 is( $pm_info->name, undef, 'no default package' );
725 is( $pm_info->version, undef, 'version for default package' );
726 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
727 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
728 ok( $pm_info->is_indexable(), 'an indexable package is found' );
729 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
730 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
732 $dist->change_file( 'lib/Simple.pm', <<'---' );
742 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
743 is( $pm_info->name, 'Simple', 'found default package' );
744 is( $pm_info->version, '0.02', 'version for default package' );
745 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
746 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
747 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
748 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
749 ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
751 $dist->change_file( 'lib/Simple.pm', <<'---' );
752 package ## hide from PAUSE
759 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
760 is( $pm_info->name, undef, 'no package names found' );
761 ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
762 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
763 ok( !$pm_info->is_indexable(), 'no indexable package is found' );