2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
16 # parse various module $VERSION lines
17 # these will be reversed later to create %modules
19 $undef => <<'---', # no $VERSION line
22 $undef => <<'---', # undefined $VERSION
26 '1.23' => <<'---', # declared & defined on same line with 'our'
28 our $VERSION = '1.23';
30 '1.23' => <<'---', # declared & defined on separate lines with 'our'
35 '1.23' => <<'---', # commented & defined on same line
37 our $VERSION = '1.23'; # our $VERSION = '4.56';
39 '1.23' => <<'---', # commented & defined on separate lines
41 # our $VERSION = '4.56';
42 our $VERSION = '1.23';
44 '1.23' => <<'---', # use vars
46 use vars qw( $VERSION );
49 '1.23' => <<'---', # choose the right default package based on package/file name
50 package Simple::_private;
53 $VERSION = '1.23'; # this should be chosen for version
55 '1.23' => <<'---', # just read the first $VERSION line
57 $VERSION = '1.23'; # we should see this line
58 $VERSION = eval $VERSION; # and ignore this one
60 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
63 package Error::Simple;
67 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
69 package Error::Simple;
74 '1.23' => <<'---', # mentions another module's $VERSION
77 if ( $Other::VERSION ) {
81 '1.23' => <<'---', # mentions another module's $VERSION in a different package
85 if ( $Simple::VERSION ) {
89 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
92 if ( $VERSION =~ /1\.23/ ) {
96 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
99 if ( $VERSION == 3.45 ) {
103 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
107 if ( $Simple::VERSION == 3.45 ) {
111 '1.23' => <<'---', # Fully qualified $VERSION declared in package
113 $Simple::VERSION = 1.23;
115 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
117 $Simple2::VERSION = '999';
118 $Simple::VERSION = 1.23;
120 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
122 $Simple2::VERSION = '999';
125 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
126 $Simple::VERSION = '1.23';
129 $x = $y, $cats = $dogs;
132 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
134 ( $VERSION ) = '1.23';
136 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
140 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
142 __PACKAGE__->mk_accessors(qw(
144 package filename line codeline subroutine finished));
146 our $VERSION = "1.23";
148 '1.23' => <<'---', # $VERSION using version.pm
150 use version; our $VERSION = version->new('1.23');
152 '1.23' => <<'---', # $VERSION using version.pm and qv()
154 use version; our $VERSION = qv('1.230');
156 '1.23' => <<'---', # Two version assignments, should ignore second one
157 $Simple::VERSION = '1.230';
158 $Simple::VERSION = eval $Simple::VERSION;
160 '1.23' => <<'---', # declared & defined on same line with 'our'
162 our $VERSION = '1.23_00_00';
164 '1.23' => <<'---', # package NAME VERSION
167 '1.23_01' => <<'---', # package NAME VERSION
168 package Simple 1.23_01;
170 'v1.2.3' => <<'---', # package NAME VERSION
171 package Simple v1.2.3;
173 'v1.2_3' => <<'---', # package NAME VERSION
174 package Simple v1.2_3;
176 '1.23' => <<'---', # trailing crud
179 $VERSION = '1.23-alpha';
181 '1.23' => <<'---', # trailing crud
186 '1.234' => <<'---', # multi_underscore
189 $VERSION = '1.2_3_4';
191 '0' => <<'---', # non-numeric
194 $VERSION = 'onetwothree';
196 $undef => <<'---', # package NAME BLOCK, undef $VERSION
201 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
203 our $VERSION = '1.23';
206 '1.23' => <<'---', # package NAME VERSION BLOCK
207 package Simple 1.23 {
211 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
212 package Simple v1.2.3_4 {
216 '0' => <<'---', # set from separately-initialised variable
218 our $CVSVERSION = '$Revision: 1.7 $';
219 our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
223 my %modules = reverse @modules;
226 [ 'Simple' ] => <<'---', # package NAME
229 [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
230 package Simple::Edward;
232 [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
233 package Simple::Edward::;
235 [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
236 package Simple'Edward;
238 [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
239 package Simple'Edward::;
241 [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
242 package Simple::::Edward;
244 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
245 package ::Simple::Edward;
247 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
248 package Simple:Edward;
250 [ 'main' ] => <<'---', # package NAME' (fail)
253 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
254 package Simple::Edward';
256 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
257 package Simple''Edward;
259 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
260 package Simple-Edward;
263 my %pkg_names = reverse @pkg_names;
265 plan tests => 64 + (2 * keys( %modules )) + (2 * keys( %pkg_names ));
267 require_ok('Module::Metadata');
270 # class method C<find_module_by_name>
271 my $module = Module::Metadata->find_module_by_name(
272 'Module::Metadata' );
273 ok( -e $module, 'find_module_by_name() succeeds' );
276 #########################
279 my $cwd = File::Spec->rel2abs(Cwd::cwd);
280 sub original_cwd { return $cwd }
283 # Setup a temp directory
286 my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
287 return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
291 BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; }
294 die "tests failed; leaving temp dir $tmp behind"
295 if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
296 diag "removing temp dir $tmp";
298 File::Path::remove_tree($tmp);
301 # generates a new distribution:
302 # files => { relative filename => $content ... }
303 # returns the name of the distribution (not including version),
304 # and the absolute path name to the dist.
310 my $distname = 'Simple' . $test_num++;
311 my $distdir = File::Spec->catdir($tmp, $distname);
312 note "using dist $distname in $distdir";
314 File::Path::mkpath($distdir) or die "failed to create '$distdir'";
316 foreach my $rel_filename (keys %{$opts{files}})
318 my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
319 my $dirname = File::Basename::dirname($abs_filename);
320 unless (-d $dirname) {
321 File::Path::mkpath($dirname) or die "Can't create '$dirname'";
324 note "creating $abs_filename";
325 my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
326 print $fh $opts{files}{$rel_filename};
331 return ($distname, $distdir);
336 # fail on invalid module name
337 my $pm_info = Module::Metadata->new_from_module(
338 'Foo::Bar', inc => [] );
339 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
343 # fail on invalid filename
344 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
345 my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
346 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
350 my $file = File::Spec->catfile('lib', 'Simple.pm');
351 my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
353 # construct from module filename
354 my $pm_info = Module::Metadata->new_from_file( $file );
355 ok( defined( $pm_info ), 'new_from_file() succeeds' );
357 # construct from filehandle
358 my $handle = IO::File->new($file);
359 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
360 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
361 $pm_info = Module::Metadata->new_from_handle( $handle );
362 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
365 # construct from module name, using custom include path
366 $pm_info = Module::Metadata->new_from_module(
367 'Simple', inc => [ 'lib', @INC ] );
368 ok( defined( $pm_info ), 'new_from_module() succeeds' );
372 foreach my $module ( sort keys %modules ) {
373 my $expected = $modules{$module};
375 skip( "No our() support until perl 5.6", 2 )
376 if $] < 5.006 && $module =~ /\bour\b/;
377 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
378 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
380 my $file = File::Spec->catfile('lib', 'Simple.pm');
381 my ($dist_name, $dist_dir) = new_dist(files => { $file => $module });
384 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
385 my $pm_info = Module::Metadata->new_from_file( $file );
387 # Test::Builder will prematurely numify objects, so use this form
389 my $got = $pm_info->version;
390 if ( defined $expected ) {
391 ok( $got eq $expected,
392 "correct module version (expected '$expected')" )
396 "correct module version (expected undef)" )
399 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
400 diag "Got: '$got'\nModule contents:\n$module" if $errs;
404 foreach my $pkg_contents ( sort keys %pkg_names ) {
405 my $expected = $pkg_names{$pkg_contents};
407 my $file = File::Spec->catfile('lib', 'Simple.pm');
408 my ($dist_name, $dist_dir) = new_dist(files => { $file => $pkg_contents });
411 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
412 my $pm_info = Module::Metadata->new_from_file( $file );
414 # Test::Builder will prematurely numify objects, so use this form
416 my @got = $pm_info->packages_inside();
417 is_deeply( \@got, $expected,
418 "correct package names (expected '" . join(', ', @$expected) . "')" )
420 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
421 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_contents" if $errs;
425 # Find each package only once
426 my $file = File::Spec->catfile('lib', 'Simple.pm');
427 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
430 package Error::Simple;
435 my $pm_info = Module::Metadata->new_from_file( $file );
437 my @packages = $pm_info->packages_inside;
438 is( @packages, 2, 'record only one occurence of each package' );
442 # Module 'Simple.pm' does not contain package 'Simple';
443 # constructor should not complain, no default module name or version
444 my $file = File::Spec->catfile('lib', 'Simple.pm');
445 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
450 my $pm_info = Module::Metadata->new_from_file( $file );
452 is( $pm_info->name, undef, 'no default package' );
453 is( $pm_info->version, undef, 'no version w/o default package' );
457 # Module 'Simple.pm' contains an alpha version
458 # constructor should report first $VERSION found
459 my $file = File::Spec->catfile('lib', 'Simple.pm');
460 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
462 $VERSION = '1.23_01';
463 $VERSION = eval $VERSION;
466 my $pm_info = Module::Metadata->new_from_file( $file );
468 is( $pm_info->version, '1.23_01', 'alpha version reported');
470 # NOTE the following test has be done this way because Test::Builder is
471 # too smart for our own good and tries to see if the version object is a
472 # dual-var, which breaks with alpha versions:
473 # Argument "1.23_0100" isn't numeric in addition (+) at
474 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
476 ok( $pm_info->version > 1.23, 'alpha version greater than non');
479 # parse $VERSION lines scripts for package main
481 <<'---', # package main declared
486 <<'---', # on first non-comment line, non declared package main
490 <<'---', # after non-comment line
495 <<'---', # 1st declared package
502 <<'---', # 2nd declared package
509 <<'---', # split package
517 <<'---', # define 'main' version from other package
522 <<'---', # define 'main' version from other package
529 my ( $i, $n ) = ( 1, scalar( @scripts ) );
530 foreach my $script ( @scripts ) {
531 my $file = File::Spec->catfile('bin', 'simple.plx');
532 my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
533 my $pm_info = Module::Metadata->new_from_file( $file );
535 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
540 # examine properties of a module: name, pod, etc
541 my $file = File::Spec->catfile('lib', 'Simple.pm');
542 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
556 You can find me on the IRC channel
557 #simon on irc.perl.org.
562 my $pm_info = Module::Metadata->new_from_module(
563 'Simple', inc => [ 'lib', @INC ] );
565 is( $pm_info->name, 'Simple', 'found default package' );
566 is( $pm_info->version, '0.01', 'version for default package' );
568 # got correct version for secondary package
569 is( $pm_info->version( 'Simple::Ex' ), '0.02',
570 'version for secondary package' );
572 my $filename = $pm_info->filename;
573 ok( defined( $filename ) && -e $filename,
574 'filename() returns valid path to module file' );
576 my @packages = $pm_info->packages_inside;
577 is( @packages, 2, 'found correct number of packages' );
578 is( $packages[0], 'Simple', 'packages stored in order found' );
580 # we can detect presence of pod regardless of whether we are collecting it
581 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
583 my @pod = $pm_info->pod_inside;
584 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
586 is( $pm_info->pod('NONE') , undef,
587 'return undef() if pod section not present' );
589 is( $pm_info->pod('NAME'), undef,
590 'return undef() if pod section not collected' );
594 $pm_info = Module::Metadata->new_from_module(
595 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
598 for my $section (qw(NAME AUTHOR)) {
599 my $content = $pm_info->pod( $section );
601 $content =~ s/^\s+//;
602 $content =~ s/\s+$//;
604 $pod{$section} = $content;
607 NAME => q|Simple - It's easy.|,
608 AUTHOR => <<'EXPECTED'
611 You can find me on the IRC channel
612 #simon on irc.perl.org.
615 for my $text (values %expected) {
619 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
620 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
624 # test things that look like POD, but aren't
625 my $file = File::Spec->catfile('lib', 'Simple.pm');
626 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
631 our $VERSION = '999';
635 our $VERSION = '666';
640 =*no_this_does_not_start_pod;
642 our $VERSION = '1.23';
645 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
646 is( $pm_info->name, 'Simple', 'found default package' );
647 is( $pm_info->version, '1.23', 'version for default package' );
651 # Make sure processing stops after __DATA__
652 my $file = File::Spec->catfile('lib', 'Simple.pm');
653 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
657 *UNIVERSAL::VERSION = sub {
662 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
663 is( $pm_info->name, 'Simple', 'found default package' );
664 is( $pm_info->version, '0.01', 'version for default package' );
665 my @packages = $pm_info->packages_inside;
666 is_deeply(\@packages, ['Simple'], 'packages inside');
670 # Make sure we handle version.pm $VERSIONs well
671 my $file = File::Spec->catfile('lib', 'Simple.pm');
672 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
674 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
675 package Simple::Simon;
676 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
679 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
680 is( $pm_info->name, 'Simple', 'found default package' );
681 is( $pm_info->version, '0.60.128', 'version for default package' );
682 my @packages = $pm_info->packages_inside;
683 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
684 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
687 # check that package_versions_from_directory works
690 my $file = File::Spec->catfile('lib', 'Simple.pm');
691 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
697 package main; # should ignore this
700 package DB; # should ignore this
703 package Simple::_private; # should ignore this
719 'file' => 'Simple.pm',
723 'file' => 'Simple.pm',
728 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
730 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
731 or diag explain $got_pvfd;
734 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
737 'file' => 'lib/Simple.pm',
741 'file' => 'lib/Simple.pm',
746 is_deeply( $got_provides, $exp_provides, "provides(dir => ...)" )
747 or diag explain $got_provides;
749 $got_provides = Module::Metadata->provides(files => ['lib/Simple.pm'], version => 2);
750 is_deeply( $got_provides, $exp_provides, "provides(files => ...)" )
751 or diag explain $got_provides;
755 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
758 'file' => 'other/Simple.pm',
762 'file' => 'other/Simple.pm',
767 is_deeply( $got_provides, $exp_provides, "provides(dir => ..., prefix => ...)" )
768 or diag explain $got_provides;
772 # Check package_versions_from_directory with regard to case-sensitivity
774 my $file = File::Spec->catfile('lib', 'Simple.pm');
775 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
780 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
781 is( $pm_info->name, undef, 'no default package' );
782 is( $pm_info->version, undef, 'version for default package' );
783 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
784 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
785 ok( $pm_info->is_indexable(), 'an indexable package is found' );
786 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
787 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
791 my $file = File::Spec->catfile('lib', 'Simple.pm');
792 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
801 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
802 is( $pm_info->name, 'Simple', 'found default package' );
803 is( $pm_info->version, '0.02', 'version for default package' );
804 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
805 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
806 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
807 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
808 ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
812 my $file = File::Spec->catfile('lib', 'Simple.pm');
813 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
814 package ## hide from PAUSE
819 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
820 is( $pm_info->name, undef, 'no package names found' );
821 ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
822 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
823 ok( !$pm_info->is_indexable(), 'no indexable package is found' );