4 use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
5 use MBTest tests => 75;
9 my $tmp = File::Spec->catdir( $cwd, 't', '_tmp' );
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');
177 my( $i, $n ) = ( 1, scalar( @modules ) );
178 foreach my $module ( @modules ) {
180 skip( "No our() support until perl 5.6", 2 )
181 if $] < 5.006 && $module =~ /\bour\b/;
183 $dist->change_file( 'lib/Simple.pm', $module );
187 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
188 my $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
190 # Test::Builder will prematurely numify objects, so use this form
191 ok( $pm_info->version eq '1.23',
192 "correct module version ($i of $n)" );
193 is( $warnings, '', 'no warnings from parsing' );
198 # revert to pristine state
199 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
201 $dist = DistGen->new( dir => $tmp );
203 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
206 # Find each package only once
207 $dist->change_file( 'lib/Simple.pm', <<'---' );
210 package Error::Simple;
217 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
219 my @packages = $pm_info->packages_inside;
220 is( @packages, 2, 'record only one occurence of each package' );
223 # Module 'Simple.pm' does not contain package 'Simple';
224 # constructor should not complain, no default module name or version
225 $dist->change_file( 'lib/Simple.pm', <<'---' );
231 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
233 is( $pm_info->name, undef, 'no default package' );
234 is( $pm_info->version, undef, 'no version w/o default package' );
236 # Module 'Simple.pm' contains an alpha version
237 # constructor should report first $VERSION found
238 $dist->change_file( 'lib/Simple.pm', <<'---' );
240 $VERSION = '1.23_01';
241 $VERSION = eval $VERSION;
245 $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
247 is( $pm_info->version, '1.23_01', 'alpha version reported');
249 # NOTE the following test has be done this way because Test::Builder is
250 # too smart for our own good and tries to see if the version object is a
251 # dual-var, which breaks with alpha versions:
252 # Argument "1.23_0100" isn't numeric in addition (+) at
253 # /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
255 ok( $pm_info->version > 1.23, 'alpha version greater than non');
257 # revert to pristine state
258 chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
260 $dist = DistGen->new( dir => $tmp );
262 chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
265 # parse $VERSION lines scripts for package main
267 <<'---', # package main declared
272 <<'---', # on first non-comment line, non declared package main
276 <<'---', # after non-comment line
281 <<'---', # 1st declared package
288 <<'---', # 2nd declared package
295 <<'---', # split package
303 <<'---', # define 'main' version from other package
308 <<'---', # define 'main' version from other package
315 ( $i, $n ) = ( 1, scalar( @scripts ) );
316 foreach my $script ( @scripts ) {
317 $dist->change_file( 'bin/simple.plx', $script );
319 $pm_info = Module::Build::ModuleInfo->new_from_file(
320 File::Spec->catfile( 'bin', 'simple.plx' ) );
322 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
327 # examine properties of a module: name, pod, etc
328 $dist->change_file( 'lib/Simple.pm', <<'---' );
345 $pm_info = Module::Build::ModuleInfo->new_from_module(
346 $dist->name, inc => [ 'lib', @INC ] );
348 is( $pm_info->name, 'Simple', 'found default package' );
349 is( $pm_info->version, '0.01', 'version for default package' );
351 # got correct version for secondary package
352 is( $pm_info->version( 'Simple::Ex' ), '0.02',
353 'version for secondary package' );
355 my $filename = $pm_info->filename;
356 ok( defined( $filename ) && -e $filename,
357 'filename() returns valid path to module file' );
359 @packages = $pm_info->packages_inside;
360 is( @packages, 2, 'found correct number of packages' );
361 is( $packages[0], 'Simple', 'packages stored in order found' );
363 # we can detect presence of pod regardless of whether we are collecting it
364 ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
366 my @pod = $pm_info->pod_inside;
367 is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
369 is( $pm_info->pod('NONE') , undef,
370 'return undef() if pod section not present' );
372 is( $pm_info->pod('NAME'), undef,
373 'return undef() if pod section not collected' );
377 $pm_info = Module::Build::ModuleInfo->new_from_module(
378 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
380 my $name = $pm_info->pod('NAME');
385 is( $name, q|Simple - It's easy.|, 'collected pod section' );
389 # examine properties of a module: name, pod, etc
390 $dist->change_file( 'lib/Simple.pm', <<'---' );
394 *UNIVERSAL::VERSION = sub {
400 $pm_info = Module::Build::ModuleInfo->new_from_file('lib/Simple.pm');
401 is( $pm_info->name, 'Simple', 'found default package' );
402 is( $pm_info->version, '0.01', 'version for default package' );
403 my @packages = $pm_info->packages_inside;
404 is_deeply(\@packages, ['Simple']);
409 chdir( $cwd ) or die "Can't chdir to '$cwd': $!";