2 # -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
3 # vim:ts=8:sw=2:et:sta:sts=2
6 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
7 use MBTest tests => 82;
9 use_ok 'Module::Build::ModuleInfo';
10 ensure_blib('Module::Build::ModuleInfo');
12 my $tmp = MBTest->tmpdir;
15 my $dist = DistGen->new( dir => $tmp );
20 #########################
22 # class method C<find_module_by_name>
23 my $module = Module::Build::ModuleInfo->find_module_by_name(
24 'Module::Build::ModuleInfo' );
25 ok( -e $module, 'find_module_by_name() succeeds' );
28 # fail on invalid module name
29 my $pm_info = Module::Build::ModuleInfo->new_from_module(
30 'Foo::Bar', inc => [] );
31 ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
34 # fail on invalid filename
35 my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
36 $pm_info = Module::Build::ModuleInfo->new_from_file( $file, inc => [] );
37 ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
40 # construct from module filename
41 $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
42 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
43 ok( defined( $pm_info ), 'new_from_file() succeeds' );
45 # construct from module name, using custom include path
46 $pm_info = Module::Build::ModuleInfo->new_from_module(
47 $dist->name, inc => [ 'lib', @INC ] );
48 ok( defined( $pm_info ), 'new_from_module() succeeds' );
51 # parse various module $VERSION lines
53 <<'---', # declared & defined on same line with 'our'
55 our $VERSION = '1.23';
57 <<'---', # declared & defined on seperate lines with 'our'
64 use vars qw( $VERSION );
67 <<'---', # choose the right default package based on package/file name
68 package Simple::_private;
71 $VERSION = '1.23'; # this should be chosen for version
73 <<'---', # just read the first $VERSION line
75 $VERSION = '1.23'; # we should see this line
76 $VERSION = eval $VERSION; # and ignore this one
78 <<'---', # just read the first $VERSION line in reopened package (1)
81 package Error::Simple;
85 <<'---', # just read the first $VERSION line in reopened package (2)
87 package Error::Simple;
92 <<'---', # mentions another module's $VERSION
95 if ( $Other::VERSION ) {
99 <<'---', # mentions another module's $VERSION in a different package
103 if ( $Simple::VERSION ) {
107 <<'---', # $VERSION checked only in assignments, not regexp ops
110 if ( $VERSION =~ /1\.23/ ) {
114 <<'---', # $VERSION checked only in assignments, not relational ops
117 if ( $VERSION == 3.45 ) {
121 <<'---', # $VERSION checked only in assignments, not relational ops
125 if ( $Simple::VERSION == 3.45 ) {
129 <<'---', # Fully qualified $VERSION declared in package
131 $Simple::VERSION = 1.23;
133 <<'---', # Differentiate fully qualified $VERSION in a package
135 $Simple2::VERSION = '999';
136 $Simple::VERSION = 1.23;
138 <<'---', # Differentiate fully qualified $VERSION and unqualified
140 $Simple2::VERSION = '999';
143 <<'---', # $VERSION declared as package variable from within 'main' package
144 $Simple::VERSION = '1.23';
147 $x = $y, $cats = $dogs;
150 <<'---', # $VERSION wrapped in parens - space inside
152 ( $VERSION ) = '1.23';
154 <<'---', # $VERSION wrapped in parens - no space inside
158 <<'---', # $VERSION follows a spurious 'package' in a quoted construct
160 __PACKAGE__->mk_accessors(qw(
162 package filename line codeline subroutine finished));
164 our $VERSION = "1.23";
166 <<'---', # $VERSION using version.pm
168 use version; our $VERSION = version->new('1.23');
170 <<'---', # $VERSION using version.pm and qv()
172 use version; our $VERSION = qv('1.230');
174 <<'---', # Two version assignments, should ignore second one
175 $Simple::VERSION = '1.230';
176 $Simple::VERSION = eval $Simple::VERSION;
180 my( $i, $n ) = ( 1, scalar( @modules ) );
181 foreach my $module ( @modules ) {
183 skip( "No our() support until perl 5.6", 2 )
184 if $] < 5.006 && $module =~ /\bour\b/;
186 $dist->change_file( 'lib/Simple.pm', $module );
190 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
191 my $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
193 # Test::Builder will prematurely numify objects, so use this form
194 ok( $pm_info->version eq '1.23',
195 "correct module version ($i of $n)" );
196 is( $warnings, '', 'no warnings from parsing' );
201 # revert to pristine state
203 $dist = DistGen->new( dir => $tmp );
208 # Find each package only once
209 $dist->change_file( 'lib/Simple.pm', <<'---' );
212 package Error::Simple;
219 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
221 my @packages = $pm_info->packages_inside;
222 is( @packages, 2, 'record only one occurence of each package' );
225 # Module 'Simple.pm' does not contain package 'Simple';
226 # constructor should not complain, no default module name or version
227 $dist->change_file( 'lib/Simple.pm', <<'---' );
233 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
235 is( $pm_info->name, undef, 'no default package' );
236 is( $pm_info->version, undef, 'no version w/o default package' );
238 # Module 'Simple.pm' contains an alpha version
239 # constructor should report first $VERSION found
240 $dist->change_file( 'lib/Simple.pm', <<'---' );
242 $VERSION = '1.23_01';
243 $VERSION = eval $VERSION;
247 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
249 is( $pm_info->version, '1.23_01', 'alpha version reported');
251 # NOTE the following test has be done this way because Test::Builder is
252 # too smart for our own good and tries to see if the version object is a
253 # dual-var, which breaks with alpha versions:
254 # Argument "1.23_0100" isn't numeric in addition (+) at
255 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
257 ok( $pm_info->version > 1.23, 'alpha version greater than non');
259 # revert to pristine state
261 $dist = DistGen->new( dir => $tmp );
266 # parse $VERSION lines scripts for package main
268 <<'---', # package main declared
273 <<'---', # on first non-comment line, non declared package main
277 <<'---', # after non-comment line
282 <<'---', # 1st declared package
289 <<'---', # 2nd declared package
296 <<'---', # split package
304 <<'---', # define 'main' version from other package
309 <<'---', # define 'main' version from other package
316 ( $i, $n ) = ( 1, scalar( @scripts ) );
317 foreach my $script ( @scripts ) {
318 $dist->change_file( 'bin/simple.plx', $script );
320 $pm_info = Module::Build::ModuleInfo->new_from_file(
321 File::Spec->catfile( 'bin', 'simple.plx' ) );
323 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
328 # examine properties of a module: name, pod, etc
329 $dist->change_file( 'lib/Simple.pm', <<'---' );
346 $pm_info = Module::Build::ModuleInfo->new_from_module(
347 $dist->name, inc => [ 'lib', @INC ] );
349 is( $pm_info->name, 'Simple', 'found default package' );
350 is( $pm_info->version, '0.01', 'version for default package' );
352 # got correct version for secondary package
353 is( $pm_info->version( 'Simple::Ex' ), '0.02',
354 'version for secondary package' );
356 my $filename = $pm_info->filename;
357 ok( defined( $filename ) && -e $filename,
358 'filename() returns valid path to module file' );
360 @packages = $pm_info->packages_inside;
361 is( @packages, 2, 'found correct number of packages' );
362 is( $packages[0], 'Simple', 'packages stored in order found' );
364 # we can detect presence of pod regardless of whether we are collecting it
365 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
367 my @pod = $pm_info->pod_inside;
368 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
370 is( $pm_info->pod('NONE') , undef,
371 'return undef() if pod section not present' );
373 is( $pm_info->pod('NAME'), undef,
374 'return undef() if pod section not collected' );
378 $pm_info = Module::Build::ModuleInfo->new_from_module(
379 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
381 my $name = $pm_info->pod('NAME');
386 is( $name, q|Simple - It's easy.|, 'collected pod section' );
390 # Make sure processing stops after __DATA__
391 $dist->change_file( 'lib/Simple.pm', <<'---' );
395 *UNIVERSAL::VERSION = sub {
401 $pm_info = Module::Build::ModuleInfo->new_from_file('lib/Simple.pm');
402 is( $pm_info->name, 'Simple', 'found default package' );
403 is( $pm_info->version, '0.01', 'version for default package' );
404 my @packages = $pm_info->packages_inside;
405 is_deeply(\@packages, ['Simple']);
409 # Make sure we handle version.pm $VERSIONs well
410 $dist->change_file( 'lib/Simple.pm', <<'---' );
412 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
413 package Simple::Simon;
414 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
418 $pm_info = Module::Build::ModuleInfo->new_from_file('lib/Simple.pm');
419 is( $pm_info->name, 'Simple', 'found default package' );
420 is( $pm_info->version, '0.60.128', 'version for default package' );
421 my @packages = $pm_info->packages_inside;
422 is_deeply([sort @packages], ['Simple', 'Simple::Simon']);
423 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );