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'; }
242 # format: expected package name => code snippet
244 [ 'Simple' ] => <<'---', # package NAME
247 [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
248 package Simple::Edward;
250 [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
251 package Simple::Edward::;
253 [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
254 package Simple'Edward;
256 [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
257 package Simple'Edward::;
259 [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
260 package Simple::::Edward;
262 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
263 package ::Simple::Edward;
265 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
266 package Simple:Edward;
268 [ 'main' ] => <<'---', # package NAME' (fail)
271 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
272 package Simple::Edward';
274 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
275 package Simple''Edward;
277 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
278 package Simple-Edward;
282 # 2 tests per each pair of @modules (plus 1 for defined keys), 2 per pair of @pkg_names
284 + ( @modules + grep { defined $modules[2*$_] } 0..$#modules/2 )
287 require_ok('Module::Metadata');
290 # class method C<find_module_by_name>
291 my $module = Module::Metadata->find_module_by_name(
292 'Module::Metadata' );
293 ok( -e $module, 'find_module_by_name() succeeds' );
296 #########################
299 my $cwd = File::Spec->rel2abs(Cwd::cwd);
300 sub original_cwd { return $cwd }
303 # Set up a temp directory
306 my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
307 return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
311 BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; }
314 die "tests failed; leaving temp dir $tmp behind"
315 if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
316 diag "removing temp dir $tmp";
318 File::Path::rmtree($tmp);
321 # generates a new distribution:
322 # files => { relative filename => $content ... }
323 # returns the name of the distribution (not including version),
324 # and the absolute path name to the dist.
330 my $distname = 'Simple' . $test_num++;
331 my $distdir = File::Spec->catdir($tmp, $distname);
332 note "using dist $distname in $distdir";
334 File::Path::mkpath($distdir) or die "failed to create '$distdir'";
336 foreach my $rel_filename (keys %{$opts{files}})
338 my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
339 my $dirname = File::Basename::dirname($abs_filename);
340 unless (-d $dirname) {
341 File::Path::mkpath($dirname) or die "Can't create '$dirname'";
344 note "creating $abs_filename";
345 my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
346 print $fh $opts{files}{$rel_filename};
351 return ($distname, $distdir);
356 # fail on invalid module name
357 my $pm_info = Module::Metadata->new_from_module(
358 'Foo::Bar', inc => [] );
359 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
363 # fail on invalid filename
364 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
365 my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
366 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
370 my $file = File::Spec->catfile('lib', 'Simple.pm');
371 my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
373 # construct from module filename
374 my $pm_info = Module::Metadata->new_from_file( $file );
375 ok( defined( $pm_info ), 'new_from_file() succeeds' );
377 # construct from filehandle
378 my $handle = IO::File->new($file);
379 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
380 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
381 $pm_info = Module::Metadata->new_from_handle( $handle );
382 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
387 # construct from module name, using custom include path
388 my $pm_info = Module::Metadata->new_from_module(
389 'Simple', inc => [ 'lib', @INC ] );
390 ok( defined( $pm_info ), 'new_from_module() succeeds' );
394 # iterate through @modules pairwise
396 while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
398 skip( "No our() support until perl 5.6", (defined $expected_version ? 3 : 2) )
399 if $] < 5.006 && $code =~ /\bour\b/;
400 skip( "No package NAME VERSION support until perl 5.11.1", (defined $expected_version ? 3 : 2) )
401 if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
403 my $file = File::Spec->catfile('lib', 'Simple.pm');
404 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
407 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
408 my $pm_info = Module::Metadata->new_from_file( $file );
411 my $got = $pm_info->version;
413 # note that in Test::More 0.94 and earlier, is() stringifies first before comparing;
414 # from 0.95_01 and later, it just lets the objects figure out how to handle 'eq'
415 # We want to ensure we preserve the original, as long as it's legal, so we
416 # explicitly check the stringified form.
417 isa_ok($got, 'version') if defined $expected_version;
419 (defined $got ? "$got" : $got),
421 "case $test_case: correct module version ("
422 . (defined $expected_version? "'$expected_version'" : 'undef')
427 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
428 diag Dumper({ got => $pm_info->version, module_contents => $code }) if $errs;
433 while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
434 my $file = File::Spec->catfile('lib', 'Simple.pm');
435 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
438 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
439 my $pm_info = Module::Metadata->new_from_file( $file );
441 # Test::Builder will prematurely numify objects, so use this form
443 my @got = $pm_info->packages_inside();
444 is_deeply( \@got, $expected_name,
445 "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
447 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
448 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
452 # Find each package only once
453 my $file = File::Spec->catfile('lib', 'Simple.pm');
454 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
457 package Error::Simple;
462 my $pm_info = Module::Metadata->new_from_file( $file );
464 my @packages = $pm_info->packages_inside;
465 is( @packages, 2, 'record only one occurence of each package' );
469 # Module 'Simple.pm' does not contain package 'Simple';
470 # constructor should not complain, no default module name or version
471 my $file = File::Spec->catfile('lib', 'Simple.pm');
472 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
477 my $pm_info = Module::Metadata->new_from_file( $file );
479 is( $pm_info->name, undef, 'no default package' );
480 is( $pm_info->version, undef, 'no version w/o default package' );
484 # Module 'Simple.pm' contains an alpha version
485 # constructor should report first $VERSION found
486 my $file = File::Spec->catfile('lib', 'Simple.pm');
487 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
489 $VERSION = '1.23_01';
490 $VERSION = eval $VERSION;
493 my $pm_info = Module::Metadata->new_from_file( $file );
495 is( $pm_info->version, '1.23_01', 'alpha version reported');
497 # NOTE the following test has be done this way because Test::Builder is
498 # too smart for our own good and tries to see if the version object is a
499 # dual-var, which breaks with alpha versions:
500 # Argument "1.23_0100" isn't numeric in addition (+) at
501 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
503 ok( $pm_info->version > 1.23, 'alpha version greater than non');
506 # parse $VERSION lines scripts for package main
508 <<'---', # package main declared
513 <<'---', # on first non-comment line, non declared package main
517 <<'---', # after non-comment line
522 <<'---', # 1st declared package
529 <<'---', # 2nd declared package
536 <<'---', # split package
544 <<'---', # define 'main' version from other package
549 <<'---', # define 'main' version from other package
556 my ( $i, $n ) = ( 1, scalar( @scripts ) );
557 foreach my $script ( @scripts ) {
558 my $file = File::Spec->catfile('bin', 'simple.plx');
559 my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
560 my $pm_info = Module::Metadata->new_from_file( $file );
562 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
567 # examine properties of a module: name, pod, etc
568 my $file = File::Spec->catfile('lib', 'Simple.pm');
569 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
583 You can find me on the IRC channel
584 #simon on irc.perl.org.
589 my $pm_info = Module::Metadata->new_from_module(
590 'Simple', inc => [ 'lib', @INC ] );
592 is( $pm_info->name, 'Simple', 'found default package' );
593 is( $pm_info->version, '0.01', 'version for default package' );
595 # got correct version for secondary package
596 is( $pm_info->version( 'Simple::Ex' ), '0.02',
597 'version for secondary package' );
599 my $filename = $pm_info->filename;
600 ok( defined( $filename ) && -e $filename,
601 'filename() returns valid path to module file' );
603 my @packages = $pm_info->packages_inside;
604 is( @packages, 2, 'found correct number of packages' );
605 is( $packages[0], 'Simple', 'packages stored in order found' );
607 # we can detect presence of pod regardless of whether we are collecting it
608 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
610 my @pod = $pm_info->pod_inside;
611 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
613 is( $pm_info->pod('NONE') , undef,
614 'return undef() if pod section not present' );
616 is( $pm_info->pod('NAME'), undef,
617 'return undef() if pod section not collected' );
621 $pm_info = Module::Metadata->new_from_module(
622 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
625 for my $section (qw(NAME AUTHOR)) {
626 my $content = $pm_info->pod( $section );
628 $content =~ s/^\s+//;
629 $content =~ s/\s+$//;
631 $pod{$section} = $content;
634 NAME => q|Simple - It's easy.|,
635 AUTHOR => <<'EXPECTED'
638 You can find me on the IRC channel
639 #simon on irc.perl.org.
642 for my $text (values %expected) {
646 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
647 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
651 # test things that look like POD, but aren't
652 my $file = File::Spec->catfile('lib', 'Simple.pm');
653 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
658 our $VERSION = '999';
662 our $VERSION = '666';
667 =*no_this_does_not_start_pod;
669 our $VERSION = '1.23';
672 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
673 is( $pm_info->name, 'Simple', 'found default package' );
674 is( $pm_info->version, '1.23', 'version for default package' );
678 # Make sure processing stops after __DATA__
679 my $file = File::Spec->catfile('lib', 'Simple.pm');
680 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
684 *UNIVERSAL::VERSION = sub {
689 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
690 is( $pm_info->name, 'Simple', 'found default package' );
691 is( $pm_info->version, '0.01', 'version for default package' );
692 my @packages = $pm_info->packages_inside;
693 is_deeply(\@packages, ['Simple'], 'packages inside');
697 # Make sure we handle version.pm $VERSIONs well
698 my $file = File::Spec->catfile('lib', 'Simple.pm');
699 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
701 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
702 package Simple::Simon;
703 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
706 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
707 is( $pm_info->name, 'Simple', 'found default package' );
708 is( $pm_info->version, '0.60.128', 'version for default package' );
709 my @packages = $pm_info->packages_inside;
710 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
711 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
714 # check that package_versions_from_directory works
717 my $file = File::Spec->catfile('lib', 'Simple.pm');
718 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
724 package main; # should ignore this
727 package DB; # should ignore this
730 package Simple::_private; # should ignore this
746 'file' => 'Simple.pm',
750 'file' => 'Simple.pm',
755 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
757 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
758 or diag explain $got_pvfd;
761 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
764 'file' => 'lib/Simple.pm',
768 'file' => 'lib/Simple.pm',
773 is_deeply( $got_provides, $exp_provides, "provides()" )
774 or diag explain $got_provides;
778 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
781 'file' => 'other/Simple.pm',
785 'file' => 'other/Simple.pm',
790 is_deeply( $got_provides, $exp_provides, "provides()" )
791 or diag explain $got_provides;
795 # Check package_versions_from_directory with regard to case-sensitivity
797 my $file = File::Spec->catfile('lib', 'Simple.pm');
798 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
803 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
804 is( $pm_info->name, undef, 'no default package' );
805 is( $pm_info->version, undef, 'version for default package' );
806 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
807 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
808 ok( $pm_info->is_indexable(), 'an indexable package is found' );
809 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
810 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
814 my $file = File::Spec->catfile('lib', 'Simple.pm');
815 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
824 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
825 is( $pm_info->name, 'Simple', 'found default package' );
826 is( $pm_info->version, '0.02', 'version for default package' );
827 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
828 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
829 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
830 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
831 ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
835 my $file = File::Spec->catfile('lib', 'Simple.pm');
836 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
837 package ## hide from PAUSE
842 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
843 is( $pm_info->name, undef, 'no package names found' );
844 ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
845 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
846 ok( !$pm_info->is_indexable(), 'no indexable package is found' );