4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest tests => 81;
9 my $tmp = MBTest->tmpdir;
12 my $dist = DistGen->new( dir => $tmp );
15 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
17 #########################
20 use_ok( 'Module::Build::ModuleInfo' );
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
202 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
204 $dist = DistGen->new( dir => $tmp );
206 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
209 # Find each package only once
210 $dist->change_file( 'lib/Simple.pm', <<'---' );
213 package Error::Simple;
220 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
222 my @packages = $pm_info->packages_inside;
223 is( @packages, 2, 'record only one occurence of each package' );
226 # Module 'Simple.pm' does not contain package 'Simple';
227 # constructor should not complain, no default module name or version
228 $dist->change_file( 'lib/Simple.pm', <<'---' );
234 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
236 is( $pm_info->name, undef, 'no default package' );
237 is( $pm_info->version, undef, 'no version w/o default package' );
239 # Module 'Simple.pm' contains an alpha version
240 # constructor should report first $VERSION found
241 $dist->change_file( 'lib/Simple.pm', <<'---' );
243 $VERSION = '1.23_01';
244 $VERSION = eval $VERSION;
248 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
250 is( $pm_info->version, '1.23_01', 'alpha version reported');
252 # NOTE the following test has be done this way because Test::Builder is
253 # too smart for our own good and tries to see if the version object is a
254 # dual-var, which breaks with alpha versions:
255 # Argument "1.23_0100" isn't numeric in addition (+) at
256 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
258 ok( $pm_info->version > 1.23, 'alpha version greater than non');
260 # revert to pristine state
261 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
263 $dist = DistGen->new( dir => $tmp );
265 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
268 # parse $VERSION lines scripts for package main
270 <<'---', # package main declared
275 <<'---', # on first non-comment line, non declared package main
279 <<'---', # after non-comment line
284 <<'---', # 1st declared package
291 <<'---', # 2nd declared package
298 <<'---', # split package
306 <<'---', # define 'main' version from other package
311 <<'---', # define 'main' version from other package
318 ( $i, $n ) = ( 1, scalar( @scripts ) );
319 foreach my $script ( @scripts ) {
320 $dist->change_file( 'bin/simple.plx', $script );
322 $pm_info = Module::Build::ModuleInfo->new_from_file(
323 File::Spec->catfile( 'bin', 'simple.plx' ) );
325 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
330 # examine properties of a module: name, pod, etc
331 $dist->change_file( 'lib/Simple.pm', <<'---' );
348 $pm_info = Module::Build::ModuleInfo->new_from_module(
349 $dist->name, inc => [ 'lib', @INC ] );
351 is( $pm_info->name, 'Simple', 'found default package' );
352 is( $pm_info->version, '0.01', 'version for default package' );
354 # got correct version for secondary package
355 is( $pm_info->version( 'Simple::Ex' ), '0.02',
356 'version for secondary package' );
358 my $filename = $pm_info->filename;
359 ok( defined( $filename ) && -e $filename,
360 'filename() returns valid path to module file' );
362 @packages = $pm_info->packages_inside;
363 is( @packages, 2, 'found correct number of packages' );
364 is( $packages[0], 'Simple', 'packages stored in order found' );
366 # we can detect presence of pod regardless of whether we are collecting it
367 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
369 my @pod = $pm_info->pod_inside;
370 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
372 is( $pm_info->pod('NONE') , undef,
373 'return undef() if pod section not present' );
375 is( $pm_info->pod('NAME'), undef,
376 'return undef() if pod section not collected' );
380 $pm_info = Module::Build::ModuleInfo->new_from_module(
381 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
383 my $name = $pm_info->pod('NAME');
388 is( $name, q|Simple - It's easy.|, 'collected pod section' );
392 # Make sure processing stops after __DATA__
393 $dist->change_file( 'lib/Simple.pm', <<'---' );
397 *UNIVERSAL::VERSION = sub {
403 $pm_info = Module::Build::ModuleInfo->new_from_file('lib/Simple.pm');
404 is( $pm_info->name, 'Simple', 'found default package' );
405 is( $pm_info->version, '0.01', 'version for default package' );
406 my @packages = $pm_info->packages_inside;
407 is_deeply(\@packages, ['Simple']);
411 # Make sure we handle version.pm $VERSIONs well
412 $dist->change_file( 'lib/Simple.pm', <<'---' );
414 $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
415 package Simple::Simon;
416 $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
420 $pm_info = Module::Build::ModuleInfo->new_from_file('lib/Simple.pm');
421 is( $pm_info->name, 'Simple', 'found default package' );
422 is( $pm_info->version, '0.60.128', 'version for default package' );
423 my @packages = $pm_info->packages_inside;
424 is_deeply([sort @packages], ['Simple', 'Simple::Simon']);
425 is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
430 chdir( $cwd ) or die "Can't chdir to '$cwd': $!";