2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
18 # parse various module $VERSION lines
19 # format: expected version => code snippet
21 $undef => <<'---', # no $VERSION line
24 $undef => <<'---', # undefined $VERSION
28 '1.23' => <<'---', # declared & defined on same line with 'our'
30 our $VERSION = '1.23';
32 '1.23' => <<'---', # declared & defined on separate lines with 'our'
37 '1.23' => <<'---', # commented & defined on same line
39 our $VERSION = '1.23'; # our $VERSION = '4.56';
41 '1.23' => <<'---', # commented & defined on separate lines
43 # our $VERSION = '4.56';
44 our $VERSION = '1.23';
46 '1.23' => <<'---', # use vars
48 use vars qw( $VERSION );
51 '1.23' => <<'---', # choose the right default package based on package/file name
52 package Simple::_private;
55 $VERSION = '1.23'; # this should be chosen for version
57 '1.23' => <<'---', # just read the first $VERSION line
59 $VERSION = '1.23'; # we should see this line
60 $VERSION = eval $VERSION; # and ignore this one
62 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
65 package Error::Simple;
69 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
71 package Error::Simple;
76 '1.23' => <<'---', # mentions another module's $VERSION
79 if ( $Other::VERSION ) {
83 '1.23' => <<'---', # mentions another module's $VERSION in a different package
87 if ( $Simple::VERSION ) {
91 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
94 if ( $VERSION =~ /1\.23/ ) {
98 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
101 if ( $VERSION == 3.45 ) {
105 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
109 if ( $Simple::VERSION == 3.45 ) {
113 '1.23' => <<'---', # Fully qualified $VERSION declared in package
115 $Simple::VERSION = 1.23;
117 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
119 $Simple2::VERSION = '999';
120 $Simple::VERSION = 1.23;
122 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
124 $Simple2::VERSION = '999';
127 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
128 $Simple::VERSION = '1.23';
131 $x = $y, $cats = $dogs;
134 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
136 ( $VERSION ) = '1.23';
138 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
142 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
144 __PACKAGE__->mk_accessors(qw(
146 package filename line codeline subroutine finished));
148 our $VERSION = "1.23";
150 '1.23' => <<'---', # $VERSION using version.pm
152 use version; our $VERSION = version->new('1.23');
154 'v1.230' => <<'---', # $VERSION using version.pm and qv()
156 use version; our $VERSION = qv('1.230');
158 '1.230' => <<'---', # Two version assignments, should ignore second one
159 $Simple::VERSION = '1.230';
160 $Simple::VERSION = eval $Simple::VERSION;
162 '1.230000' => <<'---', # declared & defined on same line with 'our'
164 our $VERSION = '1.23_00_00';
166 '1.23' => <<'---', # package NAME VERSION
169 '1.23_01' => <<'---', # package NAME VERSION
170 package Simple 1.23_01;
172 'v1.2.3' => <<'---', # package NAME VERSION
173 package Simple v1.2.3;
175 'v1.2_3' => <<'---', # package NAME VERSION
176 package Simple v1.2_3;
178 '1.23' => <<'---', # trailing crud
181 $VERSION = '1.23-alpha';
183 '1.23' => <<'---', # trailing crud
188 '1.234' => <<'---', # multi_underscore
191 $VERSION = '1.2_3_4';
193 '0' => <<'---', # non-numeric
196 $VERSION = 'onetwothree';
198 $undef => <<'---', # package NAME BLOCK, undef $VERSION
203 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
205 our $VERSION = '1.23';
208 '1.23' => <<'---', # package NAME VERSION BLOCK
209 package Simple 1.23 {
213 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
214 package Simple v1.2.3_4 {
218 '0' => <<'---', # set from separately-initialised variable
220 our $CVSVERSION = '$Revision: 1.7 $';
221 our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
224 'v2.2.102.2' => <<'---', # our + bare v-string
226 our $VERSION = v2.2.102.2;
228 '0.0.9_1' => <<'---', # our + dev release
230 our $VERSION = "0.0.9_1";
232 '1.12' => <<'---', # our + crazy string and substitution code
234 our $VERSION = '1.12.B55J2qn'; our $WTF = $VERSION; $WTF =~ s/^\d+\.\d+\.//; # attempts to rationalize $WTF go here.
236 '1.12' => <<'---', # our in braces, as in Dist::Zilla::Plugin::PkgVersion with use_our = 1
238 { our $VERSION = '1.12'; }
240 sub { defined $_[0] and $_[0] =~ /^3\.14159/ } => <<'---', # calculated version - from Acme-Pi-3.14
242 my $version = atan2(1,1) * 4; $Simple::VERSION = "$version";
247 # format: expected package name => code snippet
249 [ 'Simple' ] => <<'---', # package NAME
252 [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
253 package Simple::Edward;
255 [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
256 package Simple::Edward::;
258 [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
259 package Simple'Edward;
261 [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
262 package Simple'Edward::;
264 [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
265 package Simple::::Edward;
267 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
268 package ::Simple::Edward;
270 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
271 package Simple:Edward;
273 [ 'main' ] => <<'---', # package NAME' (fail)
276 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
277 package Simple::Edward';
279 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
280 package Simple''Edward;
282 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
283 package Simple-Edward;
287 # 2 tests per each pair of @modules (plus 1 for defined keys), 2 per pair of @pkg_names
289 + ( @modules + grep { defined $modules[2*$_] } 0..$#modules/2 )
292 require_ok('Module::Metadata');
295 # class method C<find_module_by_name>
296 my $module = Module::Metadata->find_module_by_name(
297 'Module::Metadata' );
298 ok( -e $module, 'find_module_by_name() succeeds' );
301 #########################
304 my $cwd = File::Spec->rel2abs(Cwd::cwd);
305 sub original_cwd { return $cwd }
308 # Set up a temp directory
311 my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
312 return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
316 BEGIN { $tmp = tmpdir; note "using temp dir $tmp"; }
319 die "tests failed; leaving temp dir $tmp behind"
320 if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
321 note "removing temp dir $tmp";
323 File::Path::rmtree($tmp);
326 # generates a new distribution:
327 # files => { relative filename => $content ... }
328 # returns the name of the distribution (not including version),
329 # and the absolute path name to the dist.
335 my $distname = 'Simple' . $test_num++;
336 my $distdir = File::Spec->catdir($tmp, $distname);
337 note "using dist $distname in $distdir";
339 File::Path::mkpath($distdir) or die "failed to create '$distdir'";
341 foreach my $rel_filename (keys %{$opts{files}})
343 my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
344 my $dirname = File::Basename::dirname($abs_filename);
345 unless (-d $dirname) {
346 File::Path::mkpath($dirname) or die "Can't create '$dirname'";
349 note "creating $abs_filename";
350 my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
351 print $fh $opts{files}{$rel_filename};
356 return ($distname, $distdir);
361 # fail on invalid module name
362 my $pm_info = Module::Metadata->new_from_module(
363 'Foo::Bar', inc => [] );
364 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
368 # fail on invalid filename
369 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
370 my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
371 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
375 my $file = File::Spec->catfile('lib', 'Simple.pm');
376 my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
378 # construct from module filename
379 my $pm_info = Module::Metadata->new_from_file( $file );
380 ok( defined( $pm_info ), 'new_from_file() succeeds' );
382 # construct from filehandle
383 my $handle = IO::File->new($file);
384 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
385 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
386 $pm_info = Module::Metadata->new_from_handle( $handle );
387 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
392 # construct from module name, using custom include path
393 my $pm_info = Module::Metadata->new_from_module(
394 'Simple', inc => [ 'lib', @INC ] );
395 ok( defined( $pm_info ), 'new_from_module() succeeds' );
399 # iterate through @modules pairwise
401 while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
403 skip( "No our() support until perl 5.6", (defined $expected_version ? 3 : 2) )
404 if $] < 5.006 && $code =~ /\bour\b/;
405 skip( "No package NAME VERSION support until perl 5.11.1", (defined $expected_version ? 3 : 2) )
406 if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
408 my $file = File::Spec->catfile('lib', 'Simple.pm');
409 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
412 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
413 my $pm_info = Module::Metadata->new_from_file( $file );
416 my $got = $pm_info->version;
418 # note that in Test::More 0.94 and earlier, is() stringifies first before comparing;
419 # from 0.95_01 and later, it just lets the objects figure out how to handle 'eq'
420 # We want to ensure we preserve the original, as long as it's legal, so we
421 # explicitly check the stringified form.
422 isa_ok($got, 'version') if defined $expected_version;
424 if (ref($expected_version) eq 'CODE') {
426 $expected_version->($got),
427 "case $test_case: module version passes match sub"
433 (defined $got ? "$got" : $got),
435 "case $test_case: correct module version ("
436 . (defined $expected_version? "'$expected_version'" : 'undef')
442 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
443 diag Dumper({ got => $pm_info->version, module_contents => $code }) if $errs;
448 while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
449 my $file = File::Spec->catfile('lib', 'Simple.pm');
450 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
453 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
454 my $pm_info = Module::Metadata->new_from_file( $file );
456 # Test::Builder will prematurely numify objects, so use this form
458 my @got = $pm_info->packages_inside();
459 is_deeply( \@got, $expected_name,
460 "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
462 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
463 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
467 # Find each package only once
468 my $file = File::Spec->catfile('lib', 'Simple.pm');
469 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
472 package Error::Simple;
477 my $pm_info = Module::Metadata->new_from_file( $file );
479 my @packages = $pm_info->packages_inside;
480 is( @packages, 2, 'record only one occurence of each package' );
484 # Module 'Simple.pm' does not contain package 'Simple';
485 # constructor should not complain, no default module name or version
486 my $file = File::Spec->catfile('lib', 'Simple.pm');
487 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
492 my $pm_info = Module::Metadata->new_from_file( $file );
494 is( $pm_info->name, undef, 'no default package' );
495 is( $pm_info->version, undef, 'no version w/o default package' );
499 # Module 'Simple.pm' contains an alpha version
500 # constructor should report first $VERSION found
501 my $file = File::Spec->catfile('lib', 'Simple.pm');
502 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
504 $VERSION = '1.23_01';
505 $VERSION = eval $VERSION;
508 my $pm_info = Module::Metadata->new_from_file( $file );
510 is( $pm_info->version, '1.23_01', 'alpha version reported');
512 # NOTE the following test has be done this way because Test::Builder is
513 # too smart for our own good and tries to see if the version object is a
514 # dual-var, which breaks with alpha versions:
515 # Argument "1.23_0100" isn't numeric in addition (+) at
516 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
518 ok( $pm_info->version > 1.23, 'alpha version greater than non');
521 # parse $VERSION lines scripts for package main
523 <<'---', # package main declared
528 <<'---', # on first non-comment line, non declared package main
532 <<'---', # after non-comment line
537 <<'---', # 1st declared package
544 <<'---', # 2nd declared package
551 <<'---', # split package
559 <<'---', # define 'main' version from other package
564 <<'---', # define 'main' version from other package
571 my ( $i, $n ) = ( 1, scalar( @scripts ) );
572 foreach my $script ( @scripts ) {
573 my $file = File::Spec->catfile('bin', 'simple.plx');
574 my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
575 my $pm_info = Module::Metadata->new_from_file( $file );
577 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
582 # examine properties of a module: name, pod, etc
583 my $file = File::Spec->catfile('lib', 'Simple.pm');
584 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
598 You can find me on the IRC channel
599 #simon on irc.perl.org.
604 my $pm_info = Module::Metadata->new_from_module(
605 'Simple', inc => [ 'lib', @INC ] );
607 is( $pm_info->name, 'Simple', 'found default package' );
608 is( $pm_info->version, '0.01', 'version for default package' );
610 # got correct version for secondary package
611 is( $pm_info->version( 'Simple::Ex' ), '0.02',
612 'version for secondary package' );
614 my $filename = $pm_info->filename;
615 ok( defined( $filename ) && -e $filename,
616 'filename() returns valid path to module file' );
618 my @packages = $pm_info->packages_inside;
619 is( @packages, 2, 'found correct number of packages' );
620 is( $packages[0], 'Simple', 'packages stored in order found' );
622 # we can detect presence of pod regardless of whether we are collecting it
623 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
625 my @pod = $pm_info->pod_inside;
626 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
628 is( $pm_info->pod('NONE') , undef,
629 'return undef() if pod section not present' );
631 is( $pm_info->pod('NAME'), undef,
632 'return undef() if pod section not collected' );
636 $pm_info = Module::Metadata->new_from_module(
637 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
640 for my $section (qw(NAME AUTHOR)) {
641 my $content = $pm_info->pod( $section );
643 $content =~ s/^\s+//;
644 $content =~ s/\s+$//;
646 $pod{$section} = $content;
649 NAME => q|Simple - It's easy.|,
650 AUTHOR => <<'EXPECTED'
653 You can find me on the IRC channel
654 #simon on irc.perl.org.
657 for my $text (values %expected) {
661 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
662 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
666 # test things that look like POD, but aren't
667 my $file = File::Spec->catfile('lib', 'Simple.pm');
668 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
673 our $VERSION = '999';
677 our $VERSION = '666';
682 =*no_this_does_not_start_pod;
684 our $VERSION = '1.23';
687 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
688 is( $pm_info->name, 'Simple', 'found default package' );
689 is( $pm_info->version, '1.23', 'version for default package' );
693 # Make sure processing stops after __DATA__
694 my $file = File::Spec->catfile('lib', 'Simple.pm');
695 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
699 *UNIVERSAL::VERSION = sub {
704 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
705 is( $pm_info->name, 'Simple', 'found default package' );
706 is( $pm_info->version, '0.01', 'version for default package' );
707 my @packages = $pm_info->packages_inside;
708 is_deeply(\@packages, ['Simple'], 'packages inside');
712 # Make sure we handle version.pm $VERSIONs well
713 my $file = File::Spec->catfile('lib', 'Simple.pm');
714 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
716 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
717 package Simple::Simon;
718 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
721 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
722 is( $pm_info->name, 'Simple', 'found default package' );
723 is( $pm_info->version, '0.60.128', 'version for default package' );
724 my @packages = $pm_info->packages_inside;
725 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
726 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
729 # check that package_versions_from_directory works
732 my $file = File::Spec->catfile('lib', 'Simple.pm');
733 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
739 package main; # should ignore this
742 package DB; # should ignore this
745 package Simple::_private; # should ignore this
761 'file' => 'Simple.pm',
765 'file' => 'Simple.pm',
770 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
772 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
773 or diag explain $got_pvfd;
776 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
779 'file' => 'lib/Simple.pm',
783 'file' => 'lib/Simple.pm',
788 is_deeply( $got_provides, $exp_provides, "provides()" )
789 or diag explain $got_provides;
793 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
796 'file' => 'other/Simple.pm',
800 'file' => 'other/Simple.pm',
805 is_deeply( $got_provides, $exp_provides, "provides()" )
806 or diag explain $got_provides;
810 # Check package_versions_from_directory with regard to case-sensitivity
812 my $file = File::Spec->catfile('lib', 'Simple.pm');
813 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
818 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
819 is( $pm_info->name, undef, 'no default package' );
820 is( $pm_info->version, undef, 'version for default package' );
821 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
822 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
823 ok( $pm_info->is_indexable(), 'an indexable package is found' );
824 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
825 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
829 my $file = File::Spec->catfile('lib', 'Simple.pm');
830 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
839 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
840 is( $pm_info->name, 'Simple', 'found default package' );
841 is( $pm_info->version, '0.02', 'version for default package' );
842 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
843 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
844 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
845 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
846 ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
850 my $file = File::Spec->catfile('lib', 'Simple.pm');
851 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
852 package ## hide from PAUSE
857 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
858 is( $pm_info->name, undef, 'no package names found' );
859 ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
860 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
861 ok( !$pm_info->is_indexable(), 'no indexable package is found' );