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+)/);
226 # format: expected package name => code snippet
228 [ 'Simple' ] => <<'---', # package NAME
231 [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
232 package Simple::Edward;
234 [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
235 package Simple::Edward::;
237 [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
238 package Simple'Edward;
240 [ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
241 package Simple'Edward::;
243 [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
244 package Simple::::Edward;
246 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
247 package ::Simple::Edward;
249 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
250 package Simple:Edward;
252 [ 'main' ] => <<'---', # package NAME' (fail)
255 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
256 package Simple::Edward';
258 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
259 package Simple''Edward;
261 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
262 package Simple-Edward;
266 # 2 tests per each pair of @modules (plus 1 for defined keys), 2 per pair of @pkg_names
268 + ( @modules + grep { defined $modules[2*$_] } 0..$#modules/2 )
271 require_ok('Module::Metadata');
274 # class method C<find_module_by_name>
275 my $module = Module::Metadata->find_module_by_name(
276 'Module::Metadata' );
277 ok( -e $module, 'find_module_by_name() succeeds' );
280 #########################
283 my $cwd = File::Spec->rel2abs(Cwd::cwd);
284 sub original_cwd { return $cwd }
287 # Setup a temp directory
290 my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir;
291 return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args);
295 BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; }
298 die "tests failed; leaving temp dir $tmp behind"
299 if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing;
300 diag "removing temp dir $tmp";
302 File::Path::rmtree($tmp);
305 # generates a new distribution:
306 # files => { relative filename => $content ... }
307 # returns the name of the distribution (not including version),
308 # and the absolute path name to the dist.
314 my $distname = 'Simple' . $test_num++;
315 my $distdir = File::Spec->catdir($tmp, $distname);
316 note "using dist $distname in $distdir";
318 File::Path::mkpath($distdir) or die "failed to create '$distdir'";
320 foreach my $rel_filename (keys %{$opts{files}})
322 my $abs_filename = File::Spec->catfile($distdir, $rel_filename);
323 my $dirname = File::Basename::dirname($abs_filename);
324 unless (-d $dirname) {
325 File::Path::mkpath($dirname) or die "Can't create '$dirname'";
328 note "creating $abs_filename";
329 my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n";
330 print $fh $opts{files}{$rel_filename};
335 return ($distname, $distdir);
340 # fail on invalid module name
341 my $pm_info = Module::Metadata->new_from_module(
342 'Foo::Bar', inc => [] );
343 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
347 # fail on invalid filename
348 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
349 my $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
350 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
354 my $file = File::Spec->catfile('lib', 'Simple.pm');
355 my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" });
357 # construct from module filename
358 my $pm_info = Module::Metadata->new_from_file( $file );
359 ok( defined( $pm_info ), 'new_from_file() succeeds' );
361 # construct from filehandle
362 my $handle = IO::File->new($file);
363 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
364 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
365 $pm_info = Module::Metadata->new_from_handle( $handle );
366 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
371 # construct from module name, using custom include path
372 my $pm_info = Module::Metadata->new_from_module(
373 'Simple', inc => [ 'lib', @INC ] );
374 ok( defined( $pm_info ), 'new_from_module() succeeds' );
378 # iterate through @modules pairwise
380 while (++$test_case and my ($expected_version, $code) = splice @modules, 0, 2 ) {
382 skip( "No our() support until perl 5.6", (defined $expected_version ? 3 : 2) )
383 if $] < 5.006 && $code =~ /\bour\b/;
384 skip( "No package NAME VERSION support until perl 5.11.1", (defined $expected_version ? 3 : 2) )
385 if $] < 5.011001 && $code =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
387 my $file = File::Spec->catfile('lib', 'Simple.pm');
388 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
391 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
392 my $pm_info = Module::Metadata->new_from_file( $file );
395 my $got = $pm_info->version;
397 # note that in Test::More 0.94 and earlier, is() stringifies first before comparing;
398 # from 0.95_01 and later, it just lets the objects figure out how to handle 'eq'
399 # We want to ensure we preserve the original, as long as it's legal, so we
400 # explicitly check the stringified form.
401 isa_ok($got, 'version') if defined $expected_version;
403 (defined $got ? "$got" : $got),
405 "case $test_case: correct module version ("
406 . (defined $expected_version? "'$expected_version'" : 'undef')
411 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
412 diag Dumper({ got => $pm_info->version, module_contents => $code }) if $errs;
417 while (++$test_case and my ($expected_name, $code) = splice @pkg_names, 0, 2) {
418 my $file = File::Spec->catfile('lib', 'Simple.pm');
419 my ($dist_name, $dist_dir) = new_dist(files => { $file => $code });
422 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
423 my $pm_info = Module::Metadata->new_from_file( $file );
425 # Test::Builder will prematurely numify objects, so use this form
427 my @got = $pm_info->packages_inside();
428 is_deeply( \@got, $expected_name,
429 "case $test_case: correct package names (expected '" . join(', ', @$expected_name) . "')" )
431 is( $warnings, '', "case $test_case: no warnings from parsing" ) or $errs++;
432 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$code" if $errs;
436 # Find each package only once
437 my $file = File::Spec->catfile('lib', 'Simple.pm');
438 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
441 package Error::Simple;
446 my $pm_info = Module::Metadata->new_from_file( $file );
448 my @packages = $pm_info->packages_inside;
449 is( @packages, 2, 'record only one occurence of each package' );
453 # Module 'Simple.pm' does not contain package 'Simple';
454 # constructor should not complain, no default module name or version
455 my $file = File::Spec->catfile('lib', 'Simple.pm');
456 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
461 my $pm_info = Module::Metadata->new_from_file( $file );
463 is( $pm_info->name, undef, 'no default package' );
464 is( $pm_info->version, undef, 'no version w/o default package' );
468 # Module 'Simple.pm' contains an alpha version
469 # constructor should report first $VERSION found
470 my $file = File::Spec->catfile('lib', 'Simple.pm');
471 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
473 $VERSION = '1.23_01';
474 $VERSION = eval $VERSION;
477 my $pm_info = Module::Metadata->new_from_file( $file );
479 is( $pm_info->version, '1.23_01', 'alpha version reported');
481 # NOTE the following test has be done this way because Test::Builder is
482 # too smart for our own good and tries to see if the version object is a
483 # dual-var, which breaks with alpha versions:
484 # Argument "1.23_0100" isn't numeric in addition (+) at
485 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
487 ok( $pm_info->version > 1.23, 'alpha version greater than non');
490 # parse $VERSION lines scripts for package main
492 <<'---', # package main declared
497 <<'---', # on first non-comment line, non declared package main
501 <<'---', # after non-comment line
506 <<'---', # 1st declared package
513 <<'---', # 2nd declared package
520 <<'---', # split package
528 <<'---', # define 'main' version from other package
533 <<'---', # define 'main' version from other package
540 my ( $i, $n ) = ( 1, scalar( @scripts ) );
541 foreach my $script ( @scripts ) {
542 my $file = File::Spec->catfile('bin', 'simple.plx');
543 my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } );
544 my $pm_info = Module::Metadata->new_from_file( $file );
546 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
551 # examine properties of a module: name, pod, etc
552 my $file = File::Spec->catfile('lib', 'Simple.pm');
553 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
567 You can find me on the IRC channel
568 #simon on irc.perl.org.
573 my $pm_info = Module::Metadata->new_from_module(
574 'Simple', inc => [ 'lib', @INC ] );
576 is( $pm_info->name, 'Simple', 'found default package' );
577 is( $pm_info->version, '0.01', 'version for default package' );
579 # got correct version for secondary package
580 is( $pm_info->version( 'Simple::Ex' ), '0.02',
581 'version for secondary package' );
583 my $filename = $pm_info->filename;
584 ok( defined( $filename ) && -e $filename,
585 'filename() returns valid path to module file' );
587 my @packages = $pm_info->packages_inside;
588 is( @packages, 2, 'found correct number of packages' );
589 is( $packages[0], 'Simple', 'packages stored in order found' );
591 # we can detect presence of pod regardless of whether we are collecting it
592 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
594 my @pod = $pm_info->pod_inside;
595 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
597 is( $pm_info->pod('NONE') , undef,
598 'return undef() if pod section not present' );
600 is( $pm_info->pod('NAME'), undef,
601 'return undef() if pod section not collected' );
605 $pm_info = Module::Metadata->new_from_module(
606 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 );
609 for my $section (qw(NAME AUTHOR)) {
610 my $content = $pm_info->pod( $section );
612 $content =~ s/^\s+//;
613 $content =~ s/\s+$//;
615 $pod{$section} = $content;
618 NAME => q|Simple - It's easy.|,
619 AUTHOR => <<'EXPECTED'
622 You can find me on the IRC channel
623 #simon on irc.perl.org.
626 for my $text (values %expected) {
630 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
631 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
635 # test things that look like POD, but aren't
636 my $file = File::Spec->catfile('lib', 'Simple.pm');
637 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
642 our $VERSION = '999';
646 our $VERSION = '666';
651 =*no_this_does_not_start_pod;
653 our $VERSION = '1.23';
656 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
657 is( $pm_info->name, 'Simple', 'found default package' );
658 is( $pm_info->version, '1.23', 'version for default package' );
662 # Make sure processing stops after __DATA__
663 my $file = File::Spec->catfile('lib', 'Simple.pm');
664 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
668 *UNIVERSAL::VERSION = sub {
673 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
674 is( $pm_info->name, 'Simple', 'found default package' );
675 is( $pm_info->version, '0.01', 'version for default package' );
676 my @packages = $pm_info->packages_inside;
677 is_deeply(\@packages, ['Simple'], 'packages inside');
681 # Make sure we handle version.pm $VERSIONs well
682 my $file = File::Spec->catfile('lib', 'Simple.pm');
683 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
685 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
686 package Simple::Simon;
687 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
690 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
691 is( $pm_info->name, 'Simple', 'found default package' );
692 is( $pm_info->version, '0.60.128', 'version for default package' );
693 my @packages = $pm_info->packages_inside;
694 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
695 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
698 # check that package_versions_from_directory works
701 my $file = File::Spec->catfile('lib', 'Simple.pm');
702 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
708 package main; # should ignore this
711 package DB; # should ignore this
714 package Simple::_private; # should ignore this
730 'file' => 'Simple.pm',
734 'file' => 'Simple.pm',
739 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
741 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
742 or diag explain $got_pvfd;
745 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
748 'file' => 'lib/Simple.pm',
752 'file' => 'lib/Simple.pm',
757 is_deeply( $got_provides, $exp_provides, "provides()" )
758 or diag explain $got_provides;
762 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
765 'file' => 'other/Simple.pm',
769 'file' => 'other/Simple.pm',
774 is_deeply( $got_provides, $exp_provides, "provides()" )
775 or diag explain $got_provides;
779 # Check package_versions_from_directory with regard to case-sensitivity
781 my $file = File::Spec->catfile('lib', 'Simple.pm');
782 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
787 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
788 is( $pm_info->name, undef, 'no default package' );
789 is( $pm_info->version, undef, 'version for default package' );
790 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
791 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
792 ok( $pm_info->is_indexable(), 'an indexable package is found' );
793 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
794 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
798 my $file = File::Spec->catfile('lib', 'Simple.pm');
799 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
808 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
809 is( $pm_info->name, 'Simple', 'found default package' );
810 is( $pm_info->version, '0.02', 'version for default package' );
811 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
812 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
813 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
814 ok( $pm_info->is_indexable('simple'), 'the simple package is indexable' );
815 ok( $pm_info->is_indexable('Simple'), 'the Simple package is indexable' );
819 my $file = File::Spec->catfile('lib', 'Simple.pm');
820 my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } );
821 package ## hide from PAUSE
826 my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
827 is( $pm_info->name, undef, 'no package names found' );
828 ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );
829 ok( !$pm_info->is_indexable('Simple'), 'the Simple package would not be indexed' );
830 ok( !$pm_info->is_indexable(), 'no indexable package is found' );