2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
13 # parse various module $VERSION lines
14 # these will be reversed later to create %modules
16 $undef => <<'---', # no $VERSION line
19 $undef => <<'---', # undefined $VERSION
23 '1.23' => <<'---', # declared & defined on same line with 'our'
25 our $VERSION = '1.23';
27 '1.23' => <<'---', # declared & defined on separate lines with 'our'
32 '1.23' => <<'---', # commented & defined on same line
34 our $VERSION = '1.23'; # our $VERSION = '4.56';
36 '1.23' => <<'---', # commented & defined on separate lines
38 # our $VERSION = '4.56';
39 our $VERSION = '1.23';
41 '1.23' => <<'---', # use vars
43 use vars qw( $VERSION );
46 '1.23' => <<'---', # choose the right default package based on package/file name
47 package Simple::_private;
50 $VERSION = '1.23'; # this should be chosen for version
52 '1.23' => <<'---', # just read the first $VERSION line
54 $VERSION = '1.23'; # we should see this line
55 $VERSION = eval $VERSION; # and ignore this one
57 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
60 package Error::Simple;
64 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
66 package Error::Simple;
71 '1.23' => <<'---', # mentions another module's $VERSION
74 if ( $Other::VERSION ) {
78 '1.23' => <<'---', # mentions another module's $VERSION in a different package
82 if ( $Simple::VERSION ) {
86 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
89 if ( $VERSION =~ /1\.23/ ) {
93 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
96 if ( $VERSION == 3.45 ) {
100 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
104 if ( $Simple::VERSION == 3.45 ) {
108 '1.23' => <<'---', # Fully qualified $VERSION declared in package
110 $Simple::VERSION = 1.23;
112 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
114 $Simple2::VERSION = '999';
115 $Simple::VERSION = 1.23;
117 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
119 $Simple2::VERSION = '999';
122 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
123 $Simple::VERSION = '1.23';
126 $x = $y, $cats = $dogs;
129 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
131 ( $VERSION ) = '1.23';
133 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
137 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
139 __PACKAGE__->mk_accessors(qw(
141 package filename line codeline subroutine finished));
143 our $VERSION = "1.23";
145 '1.23' => <<'---', # $VERSION using version.pm
147 use version; our $VERSION = version->new('1.23');
149 '1.23' => <<'---', # $VERSION using version.pm and qv()
151 use version; our $VERSION = qv('1.230');
153 '1.23' => <<'---', # Two version assignments, should ignore second one
154 $Simple::VERSION = '1.230';
155 $Simple::VERSION = eval $Simple::VERSION;
157 '1.23' => <<'---', # declared & defined on same line with 'our'
159 our $VERSION = '1.23_00_00';
161 '1.23' => <<'---', # package NAME VERSION
164 '1.23_01' => <<'---', # package NAME VERSION
165 package Simple 1.23_01;
167 'v1.2.3' => <<'---', # package NAME VERSION
168 package Simple v1.2.3;
170 'v1.2_3' => <<'---', # package NAME VERSION
171 package Simple v1.2_3;
173 '1.23' => <<'---', # trailing crud
176 $VERSION = '1.23-alpha';
178 '1.23' => <<'---', # trailing crud
183 '1.234' => <<'---', # multi_underscore
186 $VERSION = '1.2_3_4';
188 '0' => <<'---', # non-numeric
191 $VERSION = 'onetwothree';
193 $undef => <<'---', # package NAME BLOCK, undef $VERSION
198 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
200 our $VERSION = '1.23';
203 '1.23' => <<'---', # package NAME VERSION BLOCK
204 package Simple 1.23 {
208 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
209 package Simple v1.2.3_4 {
214 my %modules = reverse @modules;
217 [ 'Simple' ] => <<'---', # package NAME
220 [ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
221 package Simple::Edward;
223 [ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
224 package Simple::Edward::;
226 [ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
227 package Simple'Edward;
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 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
239 package Simple:Edward;
241 [ 'main' ] => <<'---', # package NAME' (fail)
244 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
245 package Simple::Edward';
247 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
248 package Simple''Edward;
250 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
251 package Simple-Edward;
254 my %pkg_names = reverse @pkg_names;
256 plan tests => 54 + (2 * keys( %modules )) + (2 * keys( %pkg_names ));
258 require_ok('Module::Metadata');
260 # class method C<find_module_by_name>
261 my $module = Module::Metadata->find_module_by_name(
262 'Module::Metadata' );
263 ok( -e $module, 'find_module_by_name() succeeds' );
265 #########################
267 my $tmp = MBTest->tmpdir;
270 my $dist = DistGen->new( dir => $tmp );
276 # fail on invalid module name
277 my $pm_info = Module::Metadata->new_from_module(
278 'Foo::Bar', inc => [] );
279 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
282 # fail on invalid filename
283 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
284 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
285 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
288 # construct from module filename
289 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
290 $pm_info = Module::Metadata->new_from_file( $file );
291 ok( defined( $pm_info ), 'new_from_file() succeeds' );
293 # construct from filehandle
294 my $handle = IO::File->new($file);
295 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
296 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
297 $pm_info = Module::Metadata->new_from_handle( $handle );
298 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
301 # construct from module name, using custom include path
302 $pm_info = Module::Metadata->new_from_module(
303 $dist->name, inc => [ 'lib', @INC ] );
304 ok( defined( $pm_info ), 'new_from_module() succeeds' );
307 foreach my $module ( sort keys %modules ) {
308 my $expected = $modules{$module};
310 skip( "No our() support until perl 5.6", 2 )
311 if $] < 5.006 && $module =~ /\bour\b/;
312 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
313 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
315 $dist->change_file( 'lib/Simple.pm', $module );
319 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
320 my $pm_info = Module::Metadata->new_from_file( $file );
322 # Test::Builder will prematurely numify objects, so use this form
324 my $got = $pm_info->version;
325 if ( defined $expected ) {
326 ok( $got eq $expected,
327 "correct module version (expected '$expected')" )
331 "correct module version (expected undef)" )
334 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
335 diag "Got: '$got'\nModule contents:\n$module" if $errs;
339 # revert to pristine state
340 $dist->regen( clean => 1 );
342 foreach my $pkg_name ( sort keys %pkg_names ) {
343 my $expected = $pkg_names{$pkg_name};
345 $dist->change_file( 'lib/Simple.pm', $pkg_name );
349 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
350 my $pm_info = Module::Metadata->new_from_file( $file );
352 # Test::Builder will prematurely numify objects, so use this form
354 my @got = $pm_info->packages_inside();
355 is_deeply( \@got, $expected,
356 "correct package names (expected '" . join(', ', @$expected) . "')" )
358 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
359 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_name" if $errs;
362 # revert to pristine state
363 $dist->regen( clean => 1 );
365 # Find each package only once
366 $dist->change_file( 'lib/Simple.pm', <<'---' );
369 package Error::Simple;
376 $pm_info = Module::Metadata->new_from_file( $file );
378 my @packages = $pm_info->packages_inside;
379 is( @packages, 2, 'record only one occurence of each package' );
382 # Module 'Simple.pm' does not contain package 'Simple';
383 # constructor should not complain, no default module name or version
384 $dist->change_file( 'lib/Simple.pm', <<'---' );
390 $pm_info = Module::Metadata->new_from_file( $file );
392 is( $pm_info->name, undef, 'no default package' );
393 is( $pm_info->version, undef, 'no version w/o default package' );
395 # Module 'Simple.pm' contains an alpha version
396 # constructor should report first $VERSION found
397 $dist->change_file( 'lib/Simple.pm', <<'---' );
399 $VERSION = '1.23_01';
400 $VERSION = eval $VERSION;
404 $pm_info = Module::Metadata->new_from_file( $file );
406 is( $pm_info->version, '1.23_01', 'alpha version reported');
408 # NOTE the following test has be done this way because Test::Builder is
409 # too smart for our own good and tries to see if the version object is a
410 # dual-var, which breaks with alpha versions:
411 # Argument "1.23_0100" isn't numeric in addition (+) at
412 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
414 ok( $pm_info->version > 1.23, 'alpha version greater than non');
416 # revert to pristine state
417 $dist->regen( clean => 1 );
419 # parse $VERSION lines scripts for package main
421 <<'---', # package main declared
426 <<'---', # on first non-comment line, non declared package main
430 <<'---', # after non-comment line
435 <<'---', # 1st declared package
442 <<'---', # 2nd declared package
449 <<'---', # split package
457 <<'---', # define 'main' version from other package
462 <<'---', # define 'main' version from other package
469 my ( $i, $n ) = ( 1, scalar( @scripts ) );
470 foreach my $script ( @scripts ) {
471 $dist->change_file( 'bin/simple.plx', $script );
473 $pm_info = Module::Metadata->new_from_file(
474 File::Spec->catfile( 'bin', 'simple.plx' ) );
476 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
481 # examine properties of a module: name, pod, etc
482 $dist->change_file( 'lib/Simple.pm', <<'---' );
496 You can find me on the IRC channel
497 #simon on irc.perl.org.
503 $pm_info = Module::Metadata->new_from_module(
504 $dist->name, inc => [ 'lib', @INC ] );
506 is( $pm_info->name, 'Simple', 'found default package' );
507 is( $pm_info->version, '0.01', 'version for default package' );
509 # got correct version for secondary package
510 is( $pm_info->version( 'Simple::Ex' ), '0.02',
511 'version for secondary package' );
513 my $filename = $pm_info->filename;
514 ok( defined( $filename ) && -e $filename,
515 'filename() returns valid path to module file' );
517 @packages = $pm_info->packages_inside;
518 is( @packages, 2, 'found correct number of packages' );
519 is( $packages[0], 'Simple', 'packages stored in order found' );
521 # we can detect presence of pod regardless of whether we are collecting it
522 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
524 my @pod = $pm_info->pod_inside;
525 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
527 is( $pm_info->pod('NONE') , undef,
528 'return undef() if pod section not present' );
530 is( $pm_info->pod('NAME'), undef,
531 'return undef() if pod section not collected' );
535 $pm_info = Module::Metadata->new_from_module(
536 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
540 for my $section (qw(NAME AUTHOR)) {
541 my $content = $pm_info->pod( $section );
543 $content =~ s/^\s+//;
544 $content =~ s/\s+$//;
546 $pod{$section} = $content;
549 NAME => q|Simple - It's easy.|,
550 AUTHOR => <<'EXPECTED'
553 You can find me on the IRC channel
554 #simon on irc.perl.org.
557 for my $text (values %expected) {
561 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
562 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
566 # test things that look like POD, but aren't
567 $dist->change_file( 'lib/Simple.pm', <<'---' );
572 our $VERSION = '999';
576 our $VERSION = '666';
581 =*no_this_does_not_start_pod;
583 our $VERSION = '1.23';
587 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
588 is( $pm_info->name, 'Simple', 'found default package' );
589 is( $pm_info->version, '1.23', 'version for default package' );
593 # Make sure processing stops after __DATA__
594 $dist->change_file( 'lib/Simple.pm', <<'---' );
598 *UNIVERSAL::VERSION = sub {
604 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
605 is( $pm_info->name, 'Simple', 'found default package' );
606 is( $pm_info->version, '0.01', 'version for default package' );
607 my @packages = $pm_info->packages_inside;
608 is_deeply(\@packages, ['Simple'], 'packages inside');
612 # Make sure we handle version.pm $VERSIONs well
613 $dist->change_file( 'lib/Simple.pm', <<'---' );
615 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
616 package Simple::Simon;
617 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
621 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
622 is( $pm_info->name, 'Simple', 'found default package' );
623 is( $pm_info->version, '0.60.128', 'version for default package' );
624 my @packages = $pm_info->packages_inside;
625 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
626 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
629 # check that package_versions_from_directory works
631 $dist->change_file( 'lib/Simple.pm', <<'---' );
637 package main; # should ignore this
640 package DB; # should ignore this
643 package Simple::_private; # should ignore this
660 'file' => 'Simple.pm',
664 'file' => 'Simple.pm',
669 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
671 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
672 or diag explain $got_pvfd;
675 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
678 'file' => 'lib/Simple.pm',
682 'file' => 'lib/Simple.pm',
687 is_deeply( $got_provides, $exp_provides, "provides()" )
688 or diag explain $got_provides;
692 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
695 'file' => 'other/Simple.pm',
699 'file' => 'other/Simple.pm',
704 is_deeply( $got_provides, $exp_provides, "provides()" )
705 or diag explain $got_provides;
708 # Check package_versions_from_directory with regard to case-sensitivity
710 $dist->change_file( 'lib/Simple.pm', <<'---' );
716 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
717 is( $pm_info->name, undef, 'no default package' );
718 is( $pm_info->version, undef, 'version for default package' );
719 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
720 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
722 $dist->change_file( 'lib/Simple.pm', <<'---' );
732 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
733 is( $pm_info->name, 'Simple', 'found default package' );
734 is( $pm_info->version, '0.02', 'version for default package' );
735 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
736 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
737 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );