2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
17 # parse various module $VERSION lines
18 # format: expected version => code snippet
20 $undef => <<'---', # no $VERSION line
23 $undef => <<'---', # undefined $VERSION
27 '1.23' => <<'---', # declared & defined on same line with 'our'
29 our $VERSION = '1.23';
31 '1.23' => <<'---', # declared & defined on separate lines with 'our'
36 '1.23' => <<'---', # commented & defined on same line
38 our $VERSION = '1.23'; # our $VERSION = '4.56';
40 '1.23' => <<'---', # commented & defined on separate lines
42 # our $VERSION = '4.56';
43 our $VERSION = '1.23';
45 '1.23' => <<'---', # use vars
47 use vars qw( $VERSION );
50 '1.23' => <<'---', # choose the right default package based on package/file name
51 package Simple::_private;
54 $VERSION = '1.23'; # this should be chosen for version
56 '1.23' => <<'---', # just read the first $VERSION line
58 $VERSION = '1.23'; # we should see this line
59 $VERSION = eval $VERSION; # and ignore this one
61 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
64 package Error::Simple;
68 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
70 package Error::Simple;
75 '1.23' => <<'---', # mentions another module's $VERSION
78 if ( $Other::VERSION ) {
82 '1.23' => <<'---', # mentions another module's $VERSION in a different package
86 if ( $Simple::VERSION ) {
90 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
93 if ( $VERSION =~ /1\.23/ ) {
97 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
100 if ( $VERSION == 3.45 ) {
104 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
108 if ( $Simple::VERSION == 3.45 ) {
112 '1.23' => <<'---', # Fully qualified $VERSION declared in package
114 $Simple::VERSION = 1.23;
116 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
118 $Simple2::VERSION = '999';
119 $Simple::VERSION = 1.23;
121 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
123 $Simple2::VERSION = '999';
126 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
127 $Simple::VERSION = '1.23';
130 $x = $y, $cats = $dogs;
133 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
135 ( $VERSION ) = '1.23';
137 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
141 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
143 __PACKAGE__->mk_accessors(qw(
145 package filename line codeline subroutine finished));
147 our $VERSION = "1.23";
149 '1.23' => <<'---', # $VERSION using version.pm
151 use version; our $VERSION = version->new('1.23');
153 '1.23' => <<'---', # $VERSION using version.pm and qv()
155 use version; our $VERSION = qv('1.230');
157 '1.23' => <<'---', # Two version assignments, should ignore second one
158 $Simple::VERSION = '1.230';
159 $Simple::VERSION = eval $Simple::VERSION;
161 '1.23' => <<'---', # declared & defined on same line with 'our'
163 our $VERSION = '1.23_00_00';
165 '1.23' => <<'---', # package NAME VERSION
168 '1.23_01' => <<'---', # package NAME VERSION
169 package Simple 1.23_01;
171 'v1.2.3' => <<'---', # package NAME VERSION
172 package Simple v1.2.3;
174 'v1.2_3' => <<'---', # package NAME VERSION
175 package Simple v1.2_3;
177 '1.23' => <<'---', # trailing crud
180 $VERSION = '1.23-alpha';
182 '1.23' => <<'---', # trailing crud
187 '1.234' => <<'---', # multi_underscore
190 $VERSION = '1.2_3_4';
192 '0' => <<'---', # non-numeric
195 $VERSION = 'onetwothree';
197 $undef => <<'---', # package NAME BLOCK, undef $VERSION
202 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
204 our $VERSION = '1.23';
207 '1.23' => <<'---', # package NAME VERSION BLOCK
208 package Simple 1.23 {
212 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
213 package Simple v1.2.3_4 {
217 '0' => <<'---', # set from separately-initialised variable
219 our $CVSVERSION = '$Revision: 1.7 $';
220 our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
225 # format: expected package name => code snippet
227 [ 'Simple' ] => <<'---', # package NAME
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 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
246 package ::Simple::Edward;
248 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
249 package Simple:Edward;
251 [ 'main' ] => <<'---', # package NAME' (fail)
254 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
255 package Simple::Edward';
257 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
258 package Simple''Edward;
260 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
261 package Simple-Edward;
265 # 2 tests per each pair of @modules, @pkg_names entry
266 plan tests => 63 + ( @modules ) + ( @pkg_names );
268 require_ok('Module::Metadata');
271 # class method C<find_module_by_name>
272 my $module = Module::Metadata->find_module_by_name(
273 'Module::Metadata' );
274 ok( -e $module, 'find_module_by_name() succeeds' );
277 #########################
280 my $cwd = File::Spec->rel2abs(Cwd::cwd);
281 sub original_cwd { return $cwd }
284 # Setup a temp directory
287 my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
288 return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
292 BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; }
295 die "tests failed; leaving temp dir $tmp behind"
296 if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
297 diag "removing temp dir $tmp";
299 File::Path::remove_tree($tmp);
302 # generates a new distribution:
303 # files => { relative filename => $content ... }
304 # returns the name of the distribution (not including version),
305 # and the absolute path name to the dist.
311 my $distname = 'Simple' . $test_num++;
312 my $distdir = File::Spec->catdir($tmp, $distname);
313 note "using dist $distname in $distdir";
315 File::Path::mkpath($distdir) or die "failed to create '$distdir'";
317 foreach my $rel_filename (keys %{$opts{files}})
319 my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
320 my $dirname = File::Basename::dirname($abs_filename);
321 unless (-d $dirname) {
322 File::Path::mkpath($dirname) or die "Can't create '$dirname'";
325 note "creating $abs_filename";
326 my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
327 print $fh $opts{files}{$rel_filename};
332 return ($distname, $distdir);
337 # fail on invalid module name
338 my $pm_info = Module::Metadata->new_from_module(
339 'Foo::Bar', inc => [] );
340 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
344 # fail on invalid filename
345 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
346 my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
347 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
351 my $file = File::Spec->catfile('lib', 'Simple.pm');
352 my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
354 # construct from module filename
355 my $pm_info = Module::Metadata->new_from_file( $file );
356 ok( defined( $pm_info ), 'new_from_file() succeeds' );
358 # construct from filehandle
359 my $handle = IO::File->new($file);
360 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
361 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
362 $pm_info = Module::Metadata->new_from_handle( $handle );
363 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
368 # construct from module name, using custom include path
369 my $pm_info = Module::Metadata->new_from_module(
370 'Simple', inc => [ 'lib', @INC ] );
371 ok( defined( $pm_info ), 'new_from_module() succeeds' );
375 # iterate through @modules pairwise
377 while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
379 skip( "No our() support until perl 5.6", 2 )
380 if $] < 5.006 && $code =~ /\bour\b/;
381 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
382 if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
384 my $file = File::Spec->catfile('lib', 'Simple.pm');
385 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
388 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
389 my $pm_info = Module::Metadata->new_from_file( $file );
391 # Test::Builder will prematurely numify objects, so use this form
393 my $got = $pm_info->version;
398 "case $test_case: correct module version ("
399 . (defined $expected_version? "'$expected_version'" : 'undef')
404 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
405 diag "Got: '$got'\nModule contents:\n$code" if $errs;
410 while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
411 my $file = File::Spec->catfile('lib', 'Simple.pm');
412 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
415 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
416 my $pm_info = Module::Metadata->new_from_file( $file );
418 # Test::Builder will prematurely numify objects, so use this form
420 my @got = $pm_info->packages_inside();
421 is_deeply( \@got, $expected_name,
422 "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
424 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
425 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
429 # Find each package only once
430 my $file = File::Spec->catfile('lib', 'Simple.pm');
431 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
434 package Error::Simple;
439 my $pm_info = Module::Metadata->new_from_file( $file );
441 my @packages = $pm_info->packages_inside;
442 is( @packages, 2, 'record only one occurence of each package' );
446 # Module 'Simple.pm' does not contain package 'Simple';
447 # constructor should not complain, no default module name or version
448 my $file = File::Spec->catfile('lib', 'Simple.pm');
449 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
454 my $pm_info = Module::Metadata->new_from_file( $file );
456 is( $pm_info->name, undef, 'no default package' );
457 is( $pm_info->version, undef, 'no version w/o default package' );
461 # Module 'Simple.pm' contains an alpha version
462 # constructor should report first $VERSION found
463 my $file = File::Spec->catfile('lib', 'Simple.pm');
464 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
466 $VERSION = '1.23_01';
467 $VERSION = eval $VERSION;
470 my $pm_info = Module::Metadata->new_from_file( $file );
472 is( $pm_info->version, '1.23_01', 'alpha version reported');
474 # NOTE the following test has be done this way because Test::Builder is
475 # too smart for our own good and tries to see if the version object is a
476 # dual-var, which breaks with alpha versions:
477 # Argument "1.23_0100" isn't numeric in addition (+) at
478 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
480 ok( $pm_info->version > 1.23, 'alpha version greater than non');
483 # parse $VERSION lines scripts for package main
485 <<'---', # package main declared
490 <<'---', # on first non-comment line, non declared package main
494 <<'---', # after non-comment line
499 <<'---', # 1st declared package
506 <<'---', # 2nd declared package
513 <<'---', # split package
521 <<'---', # define 'main' version from other package
526 <<'---', # define 'main' version from other package
533 my ( $i, $n ) = ( 1, scalar( @scripts ) );
534 foreach my $script ( @scripts ) {
535 my $file = File::Spec->catfile('bin', 'simple.plx');
536 my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
537 my $pm_info = Module::Metadata->new_from_file( $file );
539 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
544 # examine properties of a module: name, pod, etc
545 my $file = File::Spec->catfile('lib', 'Simple.pm');
546 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
560 You can find me on the IRC channel
561 #simon on irc.perl.org.
566 my $pm_info = Module::Metadata->new_from_module(
567 'Simple', inc => [ 'lib', @INC ] );
569 is( $pm_info->name, 'Simple', 'found default package' );
570 is( $pm_info->version, '0.01', 'version for default package' );
572 # got correct version for secondary package
573 is( $pm_info->version( 'Simple::Ex' ), '0.02',
574 'version for secondary package' );
576 my $filename = $pm_info->filename;
577 ok( defined( $filename ) && -e $filename,
578 'filename() returns valid path to module file' );
580 my @packages = $pm_info->packages_inside;
581 is( @packages, 2, 'found correct number of packages' );
582 is( $packages[0], 'Simple', 'packages stored in order found' );
584 # we can detect presence of pod regardless of whether we are collecting it
585 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
587 my @pod = $pm_info->pod_inside;
588 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
590 is( $pm_info->pod('NONE') , undef,
591 'return undef() if pod section not present' );
593 is( $pm_info->pod('NAME'), undef,
594 'return undef() if pod section not collected' );
598 $pm_info = Module::Metadata->new_from_module(
599 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
602 for my $section (qw(NAME AUTHOR)) {
603 my $content = $pm_info->pod( $section );
605 $content =~ s/^\s+//;
606 $content =~ s/\s+$//;
608 $pod{$section} = $content;
611 NAME => q|Simple - It's easy.|,
612 AUTHOR => <<'EXPECTED'
615 You can find me on the IRC channel
616 #simon on irc.perl.org.
619 for my $text (values %expected) {
623 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
624 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
628 # test things that look like POD, but aren't
629 my $file = File::Spec->catfile('lib', 'Simple.pm');
630 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
635 our $VERSION = '999';
639 our $VERSION = '666';
644 =*no_this_does_not_start_pod;
646 our $VERSION = '1.23';
649 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
650 is( $pm_info->name, 'Simple', 'found default package' );
651 is( $pm_info->version, '1.23', 'version for default package' );
655 # Make sure processing stops after __DATA__
656 my $file = File::Spec->catfile('lib', 'Simple.pm');
657 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
661 *UNIVERSAL::VERSION = sub {
666 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
667 is( $pm_info->name, 'Simple', 'found default package' );
668 is( $pm_info->version, '0.01', 'version for default package' );
669 my @packages = $pm_info->packages_inside;
670 is_deeply(\@packages, ['Simple'], 'packages inside');
674 # Make sure we handle version.pm $VERSIONs well
675 my $file = File::Spec->catfile('lib', 'Simple.pm');
676 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
678 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
679 package Simple::Simon;
680 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
683 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
684 is( $pm_info->name, 'Simple', 'found default package' );
685 is( $pm_info->version, '0.60.128', 'version for default package' );
686 my @packages = $pm_info->packages_inside;
687 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
688 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
691 # check that package_versions_from_directory works
694 my $file = File::Spec->catfile('lib', 'Simple.pm');
695 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
701 package main; # should ignore this
704 package DB; # should ignore this
707 package Simple::_private; # should ignore this
723 'file' => 'Simple.pm',
727 'file' => 'Simple.pm',
732 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
734 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
735 or diag explain $got_pvfd;
738 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
741 'file' => 'lib/Simple.pm',
745 'file' => 'lib/Simple.pm',
750 is_deeply( $got_provides, $exp_provides, "provides()" )
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()" )
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' );