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 # format: expected version => code snippet
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+)/);
222 # format: expected package name => code snippet
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;
262 # 2 tests per each pair of @modules, @pkg_names entry
263 plan tests => 63 + ( @modules ) + ( @pkg_names );
265 require_ok('Module::Metadata');
268 # class method C<find_module_by_name>
269 my $module = Module::Metadata->find_module_by_name(
270 'Module::Metadata' );
271 ok( -e $module, 'find_module_by_name() succeeds' );
274 #########################
276 my $tmp = MBTest->tmpdir;
279 my $dist = DistGen->new( dir => $tmp );
285 # fail on invalid module name
286 my $pm_info = Module::Metadata->new_from_module(
287 'Foo::Bar', inc => [] );
288 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
291 # fail on invalid filename
292 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
293 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
294 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
297 # construct from module filename
298 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
299 $pm_info = Module::Metadata->new_from_file( $file );
300 ok( defined( $pm_info ), 'new_from_file() succeeds' );
302 # construct from filehandle
303 my $handle = IO::File->new($file);
304 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
305 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
306 $pm_info = Module::Metadata->new_from_handle( $handle );
307 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
310 # construct from module name, using custom include path
311 $pm_info = Module::Metadata->new_from_module(
312 $dist->name, inc => [ 'lib', @INC ] );
313 ok( defined( $pm_info ), 'new_from_module() succeeds' );
316 # iterate through @modules pairwise
318 while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
320 skip( "No our() support until perl 5.6", 2 )
321 if $] < 5.006 && $code =~ /\bour\b/;
322 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
323 if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
325 $dist->change_file( 'lib/Simple.pm', $code );
329 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
330 my $pm_info = Module::Metadata->new_from_file( $file );
332 # Test::Builder will prematurely numify objects, so use this form
334 my $got = $pm_info->version;
335 if ( defined $expected_version ) {
336 ok( $got eq $expected_version,
337 "case $test_case: correct module version (expected '$expected_version')" )
341 "case $test_case: correct module version (expected undef)" )
344 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
345 diag "Got: '$got'\nModule contents:\n$code" if $errs;
349 # revert to pristine state
350 $dist->regen( clean => 1 );
353 while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
354 $dist->change_file( 'lib/Simple.pm', $code);
358 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
359 my $pm_info = Module::Metadata->new_from_file( $file );
361 # Test::Builder will prematurely numify objects, so use this form
363 my @got = $pm_info->packages_inside();
364 is_deeply( \@got, $expected_name,
365 "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
367 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
368 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
371 # revert to pristine state
372 $dist->regen( clean => 1 );
374 # Find each package only once
375 $dist->change_file( 'lib/Simple.pm', <<'---' );
378 package Error::Simple;
385 $pm_info = Module::Metadata->new_from_file( $file );
387 my @packages = $pm_info->packages_inside;
388 is( @packages, 2, 'record only one occurence of each package' );
391 # Module 'Simple.pm' does not contain package 'Simple';
392 # constructor should not complain, no default module name or version
393 $dist->change_file( 'lib/Simple.pm', <<'---' );
399 $pm_info = Module::Metadata->new_from_file( $file );
401 is( $pm_info->name, undef, 'no default package' );
402 is( $pm_info->version, undef, 'no version w/o default package' );
404 # Module 'Simple.pm' contains an alpha version
405 # constructor should report first $VERSION found
406 $dist->change_file( 'lib/Simple.pm', <<'---' );
408 $VERSION = '1.23_01';
409 $VERSION = eval $VERSION;
413 $pm_info = Module::Metadata->new_from_file( $file );
415 is( $pm_info->version, '1.23_01', 'alpha version reported');
417 # NOTE the following test has be done this way because Test::Builder is
418 # too smart for our own good and tries to see if the version object is a
419 # dual-var, which breaks with alpha versions:
420 # Argument "1.23_0100" isn't numeric in addition (+) at
421 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
423 ok( $pm_info->version > 1.23, 'alpha version greater than non');
425 # revert to pristine state
426 $dist->regen( clean => 1 );
428 # parse $VERSION lines scripts for package main
430 <<'---', # package main declared
435 <<'---', # on first non-comment line, non declared package main
439 <<'---', # after non-comment line
444 <<'---', # 1st declared package
451 <<'---', # 2nd declared package
458 <<'---', # split package
466 <<'---', # define 'main' version from other package
471 <<'---', # define 'main' version from other package
478 my ( $i, $n ) = ( 1, scalar( @scripts ) );
479 foreach my $script ( @scripts ) {
480 $dist->change_file( 'bin/simple.plx', $script );
482 $pm_info = Module::Metadata->new_from_file(
483 File::Spec->catfile( 'bin', 'simple.plx' ) );
485 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
490 # examine properties of a module: name, pod, etc
491 $dist->change_file( 'lib/Simple.pm', <<'---' );
505 You can find me on the IRC channel
506 #simon on irc.perl.org.
512 $pm_info = Module::Metadata->new_from_module(
513 $dist->name, inc => [ 'lib', @INC ] );
515 is( $pm_info->name, 'Simple', 'found default package' );
516 is( $pm_info->version, '0.01', 'version for default package' );
518 # got correct version for secondary package
519 is( $pm_info->version( 'Simple::Ex' ), '0.02',
520 'version for secondary package' );
522 my $filename = $pm_info->filename;
523 ok( defined( $filename ) && -e $filename,
524 'filename() returns valid path to module file' );
526 @packages = $pm_info->packages_inside;
527 is( @packages, 2, 'found correct number of packages' );
528 is( $packages[0], 'Simple', 'packages stored in order found' );
530 # we can detect presence of pod regardless of whether we are collecting it
531 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
533 my @pod = $pm_info->pod_inside;
534 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
536 is( $pm_info->pod('NONE') , undef,
537 'return undef() if pod section not present' );
539 is( $pm_info->pod('NAME'), undef,
540 'return undef() if pod section not collected' );
544 $pm_info = Module::Metadata->new_from_module(
545 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
549 for my $section (qw(NAME AUTHOR)) {
550 my $content = $pm_info->pod( $section );
552 $content =~ s/^\s+//;
553 $content =~ s/\s+$//;
555 $pod{$section} = $content;
558 NAME => q|Simple - It's easy.|,
559 AUTHOR => <<'EXPECTED'
562 You can find me on the IRC channel
563 #simon on irc.perl.org.
566 for my $text (values %expected) {
570 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
571 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
575 # test things that look like POD, but aren't
576 $dist->change_file( 'lib/Simple.pm', <<'---' );
581 our $VERSION = '999';
585 our $VERSION = '666';
590 =*no_this_does_not_start_pod;
592 our $VERSION = '1.23';
596 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
597 is( $pm_info->name, 'Simple', 'found default package' );
598 is( $pm_info->version, '1.23', 'version for default package' );
602 # Make sure processing stops after __DATA__
603 $dist->change_file( 'lib/Simple.pm', <<'---' );
607 *UNIVERSAL::VERSION = sub {
613 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
614 is( $pm_info->name, 'Simple', 'found default package' );
615 is( $pm_info->version, '0.01', 'version for default package' );
616 my @packages = $pm_info->packages_inside;
617 is_deeply(\@packages, ['Simple'], 'packages inside');
621 # Make sure we handle version.pm $VERSIONs well
622 $dist->change_file( 'lib/Simple.pm', <<'---' );
624 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
625 package Simple::Simon;
626 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
630 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
631 is( $pm_info->name, 'Simple', 'found default package' );
632 is( $pm_info->version, '0.60.128', 'version for default package' );
633 my @packages = $pm_info->packages_inside;
634 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
635 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
638 # check that package_versions_from_directory works
640 $dist->change_file( 'lib/Simple.pm', <<'---' );
646 package main; # should ignore this
649 package DB; # should ignore this
652 package Simple::_private; # should ignore this
669 'file' => 'Simple.pm',
673 'file' => 'Simple.pm',
678 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
680 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
681 or diag explain $got_pvfd;
684 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
687 'file' => 'lib/Simple.pm',
691 'file' => 'lib/Simple.pm',
696 is_deeply( $got_provides, $exp_provides, "provides()" )
697 or diag explain $got_provides;
701 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
704 'file' => 'other/Simple.pm',
708 'file' => 'other/Simple.pm',
713 is_deeply( $got_provides, $exp_provides, "provides()" )
714 or diag explain $got_provides;
717 # Check package_versions_from_directory with regard to case-sensitivity
719 $dist->change_file( 'lib/Simple.pm', <<'---' );
725 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
726 is( $pm_info->name, undef, 'no default package' );
727 is( $pm_info->version, undef, 'version for default package' );
728 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
729 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
730 ok( $pm_info->is_indexable(), 'an indexable package is found' );
731 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
732 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
734 $dist->change_file( 'lib/Simple.pm', <<'---' );
744 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
745 is( $pm_info->name, 'Simple', 'found default package' );
746 is( $pm_info->version, '0.02', 'version for default package' );
747 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
748 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
749 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
750 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
751 ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
753 $dist->change_file( 'lib/Simple.pm', <<'---' );
754 package ## hide from PAUSE
761 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
762 is( $pm_info->name, undef, 'no package names found' );
763 ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
764 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
765 ok( !$pm_info->is_indexable(), 'no indexable package is found' );