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 {
213 '0' => <<'---', # set from separately-initialised variable
215 our $CVSVERSION = '$Revision: 1.7 $';
216 our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
220 my %modules = reverse @modules;
223 [ 'Simple' ] => <<'---', # package NAME
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 [ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
239 package Simple::::Edward;
241 [ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
242 package ::Simple::Edward;
244 [ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
245 package Simple:Edward;
247 [ 'main' ] => <<'---', # package NAME' (fail)
250 [ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
251 package Simple::Edward';
253 [ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
254 package Simple''Edward;
256 [ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
257 package Simple-Edward;
260 my %pkg_names = reverse @pkg_names;
262 plan tests => 54 + (2 * keys( %modules )) + (2 * keys( %pkg_names ));
264 require_ok('Module::Metadata');
266 # class method C<find_module_by_name>
267 my $module = Module::Metadata->find_module_by_name(
268 'Module::Metadata' );
269 ok( -e $module, 'find_module_by_name() succeeds' );
271 #########################
273 my $tmp = MBTest->tmpdir;
276 my $dist = DistGen->new( dir => $tmp );
282 # fail on invalid module name
283 my $pm_info = Module::Metadata->new_from_module(
284 'Foo::Bar', inc => [] );
285 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
288 # fail on invalid filename
289 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
290 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
291 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
294 # construct from module filename
295 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
296 $pm_info = Module::Metadata->new_from_file( $file );
297 ok( defined( $pm_info ), 'new_from_file() succeeds' );
299 # construct from filehandle
300 my $handle = IO::File->new($file);
301 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
302 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
303 $pm_info = Module::Metadata->new_from_handle( $handle );
304 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
307 # construct from module name, using custom include path
308 $pm_info = Module::Metadata->new_from_module(
309 $dist->name, inc => [ 'lib', @INC ] );
310 ok( defined( $pm_info ), 'new_from_module() succeeds' );
313 foreach my $module ( sort keys %modules ) {
314 my $expected = $modules{$module};
316 skip( "No our() support until perl 5.6", 2 )
317 if $] < 5.006 && $module =~ /\bour\b/;
318 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
319 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
321 $dist->change_file( 'lib/Simple.pm', $module );
325 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
326 my $pm_info = Module::Metadata->new_from_file( $file );
328 # Test::Builder will prematurely numify objects, so use this form
330 my $got = $pm_info->version;
331 if ( defined $expected ) {
332 ok( $got eq $expected,
333 "correct module version (expected '$expected')" )
337 "correct module version (expected undef)" )
340 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
341 diag "Got: '$got'\nModule contents:\n$module" if $errs;
345 # revert to pristine state
346 $dist->regen( clean => 1 );
348 foreach my $pkg_name ( sort keys %pkg_names ) {
349 my $expected = $pkg_names{$pkg_name};
351 $dist->change_file( 'lib/Simple.pm', $pkg_name );
355 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
356 my $pm_info = Module::Metadata->new_from_file( $file );
358 # Test::Builder will prematurely numify objects, so use this form
360 my @got = $pm_info->packages_inside();
361 is_deeply( \@got, $expected,
362 "correct package names (expected '" . join(', ', @$expected) . "')" )
364 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
365 diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_name" if $errs;
368 # revert to pristine state
369 $dist->regen( clean => 1 );
371 # Find each package only once
372 $dist->change_file( 'lib/Simple.pm', <<'---' );
375 package Error::Simple;
382 $pm_info = Module::Metadata->new_from_file( $file );
384 my @packages = $pm_info->packages_inside;
385 is( @packages, 2, 'record only one occurence of each package' );
388 # Module 'Simple.pm' does not contain package 'Simple';
389 # constructor should not complain, no default module name or version
390 $dist->change_file( 'lib/Simple.pm', <<'---' );
396 $pm_info = Module::Metadata->new_from_file( $file );
398 is( $pm_info->name, undef, 'no default package' );
399 is( $pm_info->version, undef, 'no version w/o default package' );
401 # Module 'Simple.pm' contains an alpha version
402 # constructor should report first $VERSION found
403 $dist->change_file( 'lib/Simple.pm', <<'---' );
405 $VERSION = '1.23_01';
406 $VERSION = eval $VERSION;
410 $pm_info = Module::Metadata->new_from_file( $file );
412 is( $pm_info->version, '1.23_01', 'alpha version reported');
414 # NOTE the following test has be done this way because Test::Builder is
415 # too smart for our own good and tries to see if the version object is a
416 # dual-var, which breaks with alpha versions:
417 # Argument "1.23_0100" isn't numeric in addition (+) at
418 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
420 ok( $pm_info->version > 1.23, 'alpha version greater than non');
422 # revert to pristine state
423 $dist->regen( clean => 1 );
425 # parse $VERSION lines scripts for package main
427 <<'---', # package main declared
432 <<'---', # on first non-comment line, non declared package main
436 <<'---', # after non-comment line
441 <<'---', # 1st declared package
448 <<'---', # 2nd declared package
455 <<'---', # split package
463 <<'---', # define 'main' version from other package
468 <<'---', # define 'main' version from other package
475 my ( $i, $n ) = ( 1, scalar( @scripts ) );
476 foreach my $script ( @scripts ) {
477 $dist->change_file( 'bin/simple.plx', $script );
479 $pm_info = Module::Metadata->new_from_file(
480 File::Spec->catfile( 'bin', 'simple.plx' ) );
482 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
487 # examine properties of a module: name, pod, etc
488 $dist->change_file( 'lib/Simple.pm', <<'---' );
502 You can find me on the IRC channel
503 #simon on irc.perl.org.
509 $pm_info = Module::Metadata->new_from_module(
510 $dist->name, inc => [ 'lib', @INC ] );
512 is( $pm_info->name, 'Simple', 'found default package' );
513 is( $pm_info->version, '0.01', 'version for default package' );
515 # got correct version for secondary package
516 is( $pm_info->version( 'Simple::Ex' ), '0.02',
517 'version for secondary package' );
519 my $filename = $pm_info->filename;
520 ok( defined( $filename ) && -e $filename,
521 'filename() returns valid path to module file' );
523 @packages = $pm_info->packages_inside;
524 is( @packages, 2, 'found correct number of packages' );
525 is( $packages[0], 'Simple', 'packages stored in order found' );
527 # we can detect presence of pod regardless of whether we are collecting it
528 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
530 my @pod = $pm_info->pod_inside;
531 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
533 is( $pm_info->pod('NONE') , undef,
534 'return undef() if pod section not present' );
536 is( $pm_info->pod('NAME'), undef,
537 'return undef() if pod section not collected' );
541 $pm_info = Module::Metadata->new_from_module(
542 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
546 for my $section (qw(NAME AUTHOR)) {
547 my $content = $pm_info->pod( $section );
549 $content =~ s/^\s+//;
550 $content =~ s/\s+$//;
552 $pod{$section} = $content;
555 NAME => q|Simple - It's easy.|,
556 AUTHOR => <<'EXPECTED'
559 You can find me on the IRC channel
560 #simon on irc.perl.org.
563 for my $text (values %expected) {
567 is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
568 is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
572 # test things that look like POD, but aren't
573 $dist->change_file( 'lib/Simple.pm', <<'---' );
578 our $VERSION = '999';
582 our $VERSION = '666';
587 =*no_this_does_not_start_pod;
589 our $VERSION = '1.23';
593 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
594 is( $pm_info->name, 'Simple', 'found default package' );
595 is( $pm_info->version, '1.23', 'version for default package' );
599 # Make sure processing stops after __DATA__
600 $dist->change_file( 'lib/Simple.pm', <<'---' );
604 *UNIVERSAL::VERSION = sub {
610 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
611 is( $pm_info->name, 'Simple', 'found default package' );
612 is( $pm_info->version, '0.01', 'version for default package' );
613 my @packages = $pm_info->packages_inside;
614 is_deeply(\@packages, ['Simple'], 'packages inside');
618 # Make sure we handle version.pm $VERSIONs well
619 $dist->change_file( 'lib/Simple.pm', <<'---' );
621 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
622 package Simple::Simon;
623 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
627 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
628 is( $pm_info->name, 'Simple', 'found default package' );
629 is( $pm_info->version, '0.60.128', 'version for default package' );
630 my @packages = $pm_info->packages_inside;
631 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
632 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
635 # check that package_versions_from_directory works
637 $dist->change_file( 'lib/Simple.pm', <<'---' );
643 package main; # should ignore this
646 package DB; # should ignore this
649 package Simple::_private; # should ignore this
666 'file' => 'Simple.pm',
670 'file' => 'Simple.pm',
675 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
677 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
678 or diag explain $got_pvfd;
681 my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
684 'file' => 'lib/Simple.pm',
688 'file' => 'lib/Simple.pm',
693 is_deeply( $got_provides, $exp_provides, "provides()" )
694 or diag explain $got_provides;
698 my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
701 'file' => 'other/Simple.pm',
705 'file' => 'other/Simple.pm',
710 is_deeply( $got_provides, $exp_provides, "provides()" )
711 or diag explain $got_provides;
714 # Check package_versions_from_directory with regard to case-sensitivity
716 $dist->change_file( 'lib/Simple.pm', <<'---' );
722 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
723 is( $pm_info->name, undef, 'no default package' );
724 is( $pm_info->version, undef, 'version for default package' );
725 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
726 is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
728 $dist->change_file( 'lib/Simple.pm', <<'---' );
738 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
739 is( $pm_info->name, 'Simple', 'found default package' );
740 is( $pm_info->version, '0.02', 'version for default package' );
741 is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
742 is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
743 is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );