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';
184 my %modules = reverse @modules;
186 plan tests => 39 + 2 * keys( %modules );
188 require_ok('Module::Metadata');
190 # class method C<find_module_by_name>
191 my $module = Module::Metadata->find_module_by_name(
192 'Module::Metadata' );
193 ok( -e $module, 'find_module_by_name() succeeds' );
195 #########################
197 my $tmp = MBTest->tmpdir;
200 my $dist = DistGen->new( dir => $tmp );
206 # fail on invalid module name
207 my $pm_info = Module::Metadata->new_from_module(
208 'Foo::Bar', inc => [] );
209 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
212 # fail on invalid filename
213 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
214 $pm_info = Module::Metadata->new_from_file( $file, inc => [] );
215 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
218 # construct from module filename
219 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
220 $pm_info = Module::Metadata->new_from_file( $file );
221 ok( defined( $pm_info ), 'new_from_file() succeeds' );
223 # construct from filehandle
224 my $handle = IO::File->new($file);
225 $pm_info = Module::Metadata->new_from_handle( $handle, $file );
226 ok( defined( $pm_info ), 'new_from_handle() succeeds' );
227 $pm_info = Module::Metadata->new_from_handle( $handle );
228 is( $pm_info, undef, "new_from_handle() without filename returns undef" );
231 # construct from module name, using custom include path
232 $pm_info = Module::Metadata->new_from_module(
233 $dist->name, inc => [ 'lib', @INC ] );
234 ok( defined( $pm_info ), 'new_from_module() succeeds' );
237 foreach my $module ( sort keys %modules ) {
238 my $expected = $modules{$module};
240 skip( "No our() support until perl 5.6", 2 )
241 if $] < 5.006 && $module =~ /\bour\b/;
242 skip( "No package NAME VERSION support until perl 5.11.1", 2 )
243 if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
245 $dist->change_file( 'lib/Simple.pm', $module );
249 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
250 my $pm_info = Module::Metadata->new_from_file( $file );
252 # Test::Builder will prematurely numify objects, so use this form
254 my $got = $pm_info->version;
255 if ( defined $expected ) {
256 ok( $got eq $expected,
257 "correct module version (expected '$expected')" )
261 "correct module version (expected undef)" )
264 is( $warnings, '', 'no warnings from parsing' ) or $errs++;
265 diag "Got: '$got'\nModule contents:\n$module" if $errs;
269 # revert to pristine state
270 $dist->regen( clean => 1 );
272 # Find each package only once
273 $dist->change_file( 'lib/Simple.pm', <<'---' );
276 package Error::Simple;
283 $pm_info = Module::Metadata->new_from_file( $file );
285 my @packages = $pm_info->packages_inside;
286 is( @packages, 2, 'record only one occurence of each package' );
289 # Module 'Simple.pm' does not contain package 'Simple';
290 # constructor should not complain, no default module name or version
291 $dist->change_file( 'lib/Simple.pm', <<'---' );
297 $pm_info = Module::Metadata->new_from_file( $file );
299 is( $pm_info->name, undef, 'no default package' );
300 is( $pm_info->version, undef, 'no version w/o default package' );
302 # Module 'Simple.pm' contains an alpha version
303 # constructor should report first $VERSION found
304 $dist->change_file( 'lib/Simple.pm', <<'---' );
306 $VERSION = '1.23_01';
307 $VERSION = eval $VERSION;
311 $pm_info = Module::Metadata->new_from_file( $file );
313 is( $pm_info->version, '1.23_01', 'alpha version reported');
315 # NOTE the following test has be done this way because Test::Builder is
316 # too smart for our own good and tries to see if the version object is a
317 # dual-var, which breaks with alpha versions:
318 # Argument "1.23_0100" isn't numeric in addition (+) at
319 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
321 ok( $pm_info->version > 1.23, 'alpha version greater than non');
323 # revert to pristine state
324 $dist->regen( clean => 1 );
326 # parse $VERSION lines scripts for package main
328 <<'---', # package main declared
333 <<'---', # on first non-comment line, non declared package main
337 <<'---', # after non-comment line
342 <<'---', # 1st declared package
349 <<'---', # 2nd declared package
356 <<'---', # split package
364 <<'---', # define 'main' version from other package
369 <<'---', # define 'main' version from other package
376 my ( $i, $n ) = ( 1, scalar( @scripts ) );
377 foreach my $script ( @scripts ) {
378 $dist->change_file( 'bin/simple.plx', $script );
380 $pm_info = Module::Metadata->new_from_file(
381 File::Spec->catfile( 'bin', 'simple.plx' ) );
383 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
388 # examine properties of a module: name, pod, etc
389 $dist->change_file( 'lib/Simple.pm', <<'---' );
406 $pm_info = Module::Metadata->new_from_module(
407 $dist->name, inc => [ 'lib', @INC ] );
409 is( $pm_info->name, 'Simple', 'found default package' );
410 is( $pm_info->version, '0.01', 'version for default package' );
412 # got correct version for secondary package
413 is( $pm_info->version( 'Simple::Ex' ), '0.02',
414 'version for secondary package' );
416 my $filename = $pm_info->filename;
417 ok( defined( $filename ) && -e $filename,
418 'filename() returns valid path to module file' );
420 @packages = $pm_info->packages_inside;
421 is( @packages, 2, 'found correct number of packages' );
422 is( $packages[0], 'Simple', 'packages stored in order found' );
424 # we can detect presence of pod regardless of whether we are collecting it
425 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
427 my @pod = $pm_info->pod_inside;
428 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
430 is( $pm_info->pod('NONE') , undef,
431 'return undef() if pod section not present' );
433 is( $pm_info->pod('NAME'), undef,
434 'return undef() if pod section not collected' );
438 $pm_info = Module::Metadata->new_from_module(
439 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
441 my $name = $pm_info->pod('NAME');
446 is( $name, q|Simple - It's easy.|, 'collected pod section' );
450 # Make sure processing stops after __DATA__
451 $dist->change_file( 'lib/Simple.pm', <<'---' );
455 *UNIVERSAL::VERSION = sub {
461 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
462 is( $pm_info->name, 'Simple', 'found default package' );
463 is( $pm_info->version, '0.01', 'version for default package' );
464 my @packages = $pm_info->packages_inside;
465 is_deeply(\@packages, ['Simple'], 'packages inside');
469 # Make sure we handle version.pm $VERSIONs well
470 $dist->change_file( 'lib/Simple.pm', <<'---' );
472 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
473 package Simple::Simon;
474 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
478 $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
479 is( $pm_info->name, 'Simple', 'found default package' );
480 is( $pm_info->version, '0.60.128', 'version for default package' );
481 my @packages = $pm_info->packages_inside;
482 is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
483 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );