2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
12 # parse various module $VERSION lines
13 # these will be reversed later to create %modules
15 $undef => <<'---', # no $VERSION line
18 $undef => <<'---', # undefined $VERSION
22 '1.23' => <<'---', # declared & defined on same line with 'our'
24 our $VERSION = '1.23';
26 '1.23' => <<'---', # declared & defined on separate lines with 'our'
31 '1.23' => <<'---', # use vars
33 use vars qw( $VERSION );
36 '1.23' => <<'---', # choose the right default package based on package/file name
37 package Simple::_private;
40 $VERSION = '1.23'; # this should be chosen for version
42 '1.23' => <<'---', # just read the first $VERSION line
44 $VERSION = '1.23'; # we should see this line
45 $VERSION = eval $VERSION; # and ignore this one
47 '1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
50 package Error::Simple;
54 '1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
56 package Error::Simple;
61 '1.23' => <<'---', # mentions another module's $VERSION
64 if ( $Other::VERSION ) {
68 '1.23' => <<'---', # mentions another module's $VERSION in a different package
72 if ( $Simple::VERSION ) {
76 '1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
79 if ( $VERSION =~ /1\.23/ ) {
83 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
86 if ( $VERSION == 3.45 ) {
90 '1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
94 if ( $Simple::VERSION == 3.45 ) {
98 '1.23' => <<'---', # Fully qualified $VERSION declared in package
100 $Simple::VERSION = 1.23;
102 '1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
104 $Simple2::VERSION = '999';
105 $Simple::VERSION = 1.23;
107 '1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
109 $Simple2::VERSION = '999';
112 '1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
113 $Simple::VERSION = '1.23';
116 $x = $y, $cats = $dogs;
119 '1.23' => <<'---', # $VERSION wrapped in parens - space inside
121 ( $VERSION ) = '1.23';
123 '1.23' => <<'---', # $VERSION wrapped in parens - no space inside
127 '1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
129 __PACKAGE__->mk_accessors(qw(
131 package filename line codeline subroutine finished));
133 our $VERSION = "1.23";
135 '1.23' => <<'---', # $VERSION using version.pm
137 use version; our $VERSION = version->new('1.23');
139 '1.23' => <<'---', # $VERSION using version.pm and qv()
141 use version; our $VERSION = qv('1.230');
143 '1.23' => <<'---', # Two version assignments, should ignore second one
144 $Simple::VERSION = '1.230';
145 $Simple::VERSION = eval $Simple::VERSION;
147 '1.23' => <<'---', # declared & defined on same line with 'our'
149 our $VERSION = '1.23_00_00';
151 '1.23' => <<'---', # package NAME VERSION
154 '1.23_01' => <<'---', # package NAME VERSION
155 package Simple 1.23_01;
157 'v1.2.3' => <<'---', # package NAME VERSION
158 package Simple v1.2.3;
160 'v1.2_3' => <<'---', # package NAME VERSION
161 package Simple v1.2_3;
163 '1.23' => <<'---', # trailing crud
166 $VERSION = '1.23-alpha';
168 '1.23' => <<'---', # trailing crud
173 '1.234' => <<'---', # multi_underscore
176 $VERSION = '1.2_3_4';
178 '0' => <<'---', # non-numeric
181 $VERSION = 'onetwothree';
183 $undef => <<'---', # package NAME BLOCK, undef $VERSION
188 '1.23' => <<'---', # package NAME BLOCK, with $VERSION
190 our $VERSION = '1.23';
193 '1.23' => <<'---', # package NAME VERSION BLOCK
194 package Simple 1.23 {
198 'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
199 package Simple v1.2.3_4 {
204 my %modules = reverse @modules;
206 plan tests => 40 + 2 * keys( %modules );
208 require_ok('Module::Metadata');
210 # class method C<find_module_by_name>
211 my $module = Module::Metadata->find_module_by_name(
212 'Module::Metadata' );
213 ok( -e $module, 'find_module_by_name() succeeds' );
215 #########################
217 my $tmp = MBTest->tmpdir;
220 my $dist = DistGen->new( dir => $tmp );
226 # fail on invalid module name
227 my $pm_info = Module::Metadata->new_from_module(
228 'Foo::Bar', inc => [] );
229 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
232 # fail on invalid filename
233 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
234 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
235 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
238 # construct from module filename
239 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
240 $pm_info = Module::Metadata->new_from_file( $file );
241 ok( defined( $pm_info ), 'new_from_file() succeeds' );
243 # construct from filehandle
244 my $handle = IO::File->new($file);
245 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
246 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
247 $pm_info = Module::Metadata->new_from_handle( $handle );
248 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
251 # construct from module name, using custom include path
252 $pm_info = Module::Metadata->new_from_module(
253 $dist->name, inc => [ 'lib', @INC ] );
254 ok( defined( $pm_info ), 'new_from_module() succeeds' );
257 foreach my $module ( sort keys %modules ) {
258 my $expected = $modules{$module};
260 skip( "No our() support until perl 5.6", 2 )
261 if $] < 5.006 && $module =~ /\bour\b/;
262 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
263 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
265 $dist->change_file( 'lib/Simple.pm', $module );
269 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
270 my $pm_info = Module::Metadata->new_from_file( $file );
272 # Test::Builder will prematurely numify objects, so use this form
274 my $got = $pm_info->version;
275 if ( defined $expected ) {
276 ok( $got eq $expected,
277 "correct module version (expected '$expected')" )
281 "correct module version (expected undef)" )
284 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
285 diag "Got: '$got'\nModule contents:\n$module" if $errs;
289 # revert to pristine state
290 $dist->regen( clean => 1 );
292 # Find each package only once
293 $dist->change_file( 'lib/Simple.pm', <<'---' );
296 package Error::Simple;
303 $pm_info = Module::Metadata->new_from_file( $file );
305 my @packages = $pm_info->packages_inside;
306 is( @packages, 2, 'record only one occurence of each package' );
309 # Module 'Simple.pm' does not contain package 'Simple';
310 # constructor should not complain, no default module name or version
311 $dist->change_file( 'lib/Simple.pm', <<'---' );
317 $pm_info = Module::Metadata->new_from_file( $file );
319 is( $pm_info->name, undef, 'no default package' );
320 is( $pm_info->version, undef, 'no version w/o default package' );
322 # Module 'Simple.pm' contains an alpha version
323 # constructor should report first $VERSION found
324 $dist->change_file( 'lib/Simple.pm', <<'---' );
326 $VERSION = '1.23_01';
327 $VERSION = eval $VERSION;
331 $pm_info = Module::Metadata->new_from_file( $file );
333 is( $pm_info->version, '1.23_01', 'alpha version reported');
335 # NOTE the following test has be done this way because Test::Builder is
336 # too smart for our own good and tries to see if the version object is a
337 # dual-var, which breaks with alpha versions:
338 # Argument "1.23_0100" isn't numeric in addition (+) at
339 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
341 ok( $pm_info->version > 1.23, 'alpha version greater than non');
343 # revert to pristine state
344 $dist->regen( clean => 1 );
346 # parse $VERSION lines scripts for package main
348 <<'---', # package main declared
353 <<'---', # on first non-comment line, non declared package main
357 <<'---', # after non-comment line
362 <<'---', # 1st declared package
369 <<'---', # 2nd declared package
376 <<'---', # split package
384 <<'---', # define 'main' version from other package
389 <<'---', # define 'main' version from other package
396 my ( $i, $n ) = ( 1, scalar( @scripts ) );
397 foreach my $script ( @scripts ) {
398 $dist->change_file( 'bin/simple.plx', $script );
400 $pm_info = Module::Metadata->new_from_file(
401 File::Spec->catfile( 'bin', 'simple.plx' ) );
403 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
408 # examine properties of a module: name, pod, etc
409 $dist->change_file( 'lib/Simple.pm', <<'---' );
427 $pm_info = Module::Metadata->new_from_module(
428 $dist->name, inc => [ 'lib', @INC ] );
430 is( $pm_info->name, 'Simple', 'found default package' );
431 is( $pm_info->version, '0.01', 'version for default package' );
433 # got correct version for secondary package
434 is( $pm_info->version( 'Simple::Ex' ), '0.02',
435 'version for secondary package' );
437 my $filename = $pm_info->filename;
438 ok( defined( $filename ) && -e $filename,
439 'filename() returns valid path to module file' );
441 @packages = $pm_info->packages_inside;
442 is( @packages, 2, 'found correct number of packages' );
443 is( $packages[0], 'Simple', 'packages stored in order found' );
445 # we can detect presence of pod regardless of whether we are collecting it
446 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
448 my @pod = $pm_info->pod_inside;
449 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
451 is( $pm_info->pod('NONE') , undef,
452 'return undef() if pod section not present' );
454 is( $pm_info->pod('NAME'), undef,
455 'return undef() if pod section not collected' );
459 $pm_info = Module::Metadata->new_from_module(
460 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
462 my $name = $pm_info->pod('NAME');
467 is( $name, q|Simple - It's easy.|, 'collected pod section' );
471 # Make sure processing stops after __DATA__
472 $dist->change_file( 'lib/Simple.pm', <<'---' );
476 *UNIVERSAL::VERSION = sub {
482 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
483 is( $pm_info->name, 'Simple', 'found default package' );
484 is( $pm_info->version, '0.01', 'version for default package' );
485 my @packages = $pm_info->packages_inside;
486 is_deeply(\@packages, ['Simple'], 'packages inside');
490 # Make sure we handle version.pm $VERSIONs well
491 $dist->change_file( 'lib/Simple.pm', <<'---' );
493 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
494 package Simple::Simon;
495 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
499 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
500 is( $pm_info->name, 'Simple', 'found default package' );
501 is( $pm_info->version, '0.60.128', 'version for default package' );
502 my @packages = $pm_info->packages_inside;
503 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
504 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
507 # check that package_versions_from_directory works
509 $dist->change_file( 'lib/Simple.pm', <<'---' );
515 package main; # should ignore this
518 package DB; # should ignore this
521 package Simple::_private; # should ignore this
538 'file' => 'Simple.pm',
542 'file' => 'Simple.pm',
547 my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
549 is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory" )
550 or diag explain $got_pvfd;