Upgrade to Module-Build-0.2805
[p5sagit/p5-mst-13.2.git] / lib / Module / Build / t / moduleinfo.t
CommitLineData
bb4e9162 1#!/usr/bin/perl -w
2
3use strict;
4use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib';
0ec9ad96 5use MBTest tests => 75;
bb4e9162 6
7use Cwd ();
8my $cwd = Cwd::cwd;
9my $tmp = File::Spec->catdir( $cwd, 't', '_tmp' );
10
11use DistGen;
12my $dist = DistGen->new( dir => $tmp );
13$dist->regen;
14
15chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
16
17#########################
18
19
20use_ok( 'Module::Build::ModuleInfo' );
21
22# class method C<find_module_by_name>
23my $module = Module::Build::ModuleInfo->find_module_by_name(
24 'Module::Build::ModuleInfo' );
25ok( -e $module, 'find_module_by_name() succeeds' );
26
27
28# fail on invalid module name
29my $pm_info = Module::Build::ModuleInfo->new_from_module(
30 'Foo::Bar', inc => [] );
31ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
32
33
34# fail on invalid filename
35my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
36$pm_info = Module::Build::ModuleInfo->new_from_file( $file, inc => [] );
37ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
38
39
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 );
43ok( defined( $pm_info ), 'new_from_file() succeeds' );
44
45# construct from module name, using custom include path
46$pm_info = Module::Build::ModuleInfo->new_from_module(
47 $dist->name, inc => [ 'lib', @INC ] );
48ok( defined( $pm_info ), 'new_from_module() succeeds' );
49
50
51# parse various module $VERSION lines
52my @modules = (
53 <<'---', # declared & defined on same line with 'our'
54package Simple;
55our $VERSION = '1.23';
56---
57 <<'---', # declared & defined on seperate lines with 'our'
58package Simple;
59our $VERSION;
60$VERSION = '1.23';
61---
62 <<'---', # use vars
63package Simple;
64use vars qw( $VERSION );
65$VERSION = '1.23';
66---
67 <<'---', # choose the right default package based on package/file name
68package Simple::_private;
69$VERSION = '0';
70package Simple;
71$VERSION = '1.23'; # this should be chosen for version
72---
73 <<'---', # just read the first $VERSION line
74package Simple;
75$VERSION = '1.23'; # we should see this line
76$VERSION = eval $VERSION; # and ignore this one
77---
78 <<'---', # just read the first $VERSION line in reopened package (1)
79package Simple;
80$VERSION = '1.23';
81package Error::Simple;
82$VERSION = '2.34';
83package Simple;
84---
85 <<'---', # just read the first $VERSION line in reopened package (2)
86package Simple;
87package Error::Simple;
88$VERSION = '2.34';
89package Simple;
90$VERSION = '1.23';
91---
92 <<'---', # mentions another module's $VERSION
93package Simple;
94$VERSION = '1.23';
95if ( $Other::VERSION ) {
96 # whatever
97}
98---
99 <<'---', # mentions another module's $VERSION in a different package
100package Simple;
101$VERSION = '1.23';
102package Simple2;
103if ( $Simple::VERSION ) {
104 # whatever
105}
106---
107 <<'---', # $VERSION checked only in assignments, not regexp ops
108package Simple;
109$VERSION = '1.23';
110if ( $VERSION =~ /1\.23/ ) {
111 # whatever
112}
113---
114 <<'---', # $VERSION checked only in assignments, not relational ops
115package Simple;
116$VERSION = '1.23';
117if ( $VERSION == 3.45 ) {
118 # whatever
119}
120---
121 <<'---', # $VERSION checked only in assignments, not relational ops
122package Simple;
123$VERSION = '1.23';
124package Simple2;
125if ( $Simple::VERSION == 3.45 ) {
126 # whatever
127}
128---
129 <<'---', # Fully qualified $VERSION declared in package
130package Simple;
131$Simple::VERSION = 1.23;
132---
133 <<'---', # Differentiate fully qualified $VERSION in a package
134package Simple;
135$Simple2::VERSION = '999';
136$Simple::VERSION = 1.23;
137---
138 <<'---', # Differentiate fully qualified $VERSION and unqualified
139package Simple;
140$Simple2::VERSION = '999';
141$VERSION = 1.23;
142---
143 <<'---', # $VERSION declared as package variable from within 'main' package
144$Simple::VERSION = '1.23';
145{
146 package Simple;
147 $x = $y, $cats = $dogs;
148}
149---
150 <<'---', # $VERSION wrapped in parens - space inside
151package Simple;
152( $VERSION ) = '1.23';
153---
154 <<'---', # $VERSION wrapped in parens - no space inside
155package Simple;
156($VERSION) = '1.23';
157---
158 <<'---', # $VERSION follows a spurious 'package' in a quoted construct
159package Simple;
160__PACKAGE__->mk_accessors(qw(
161 program socket proc
162 package filename line codeline subroutine finished));
163
164our $VERSION = "1.23";
165---
b3dfda33 166 <<'---', # $VERSION using version.pm
167 package Simple;
168 use version; our $VERSION = version->new('1.23');
169---
170 <<'---', # $VERSION using version.pm and qv()
171 package Simple;
172 use version; our $VERSION = qv('1.230');
173---
174
bb4e9162 175);
176
177my( $i, $n ) = ( 1, scalar( @modules ) );
178foreach my $module ( @modules ) {
179 SKIP: {
180 skip( "No our() support until perl 5.6", 2 )
181 if $] < 5.006 && $module =~ /\bour\b/;
182
183 $dist->change_file( 'lib/Simple.pm', $module );
184 $dist->regen;
185
186 my $warnings = '';
187 local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
188 my $pm_info = Module::Build::ModuleInfo->new_from_file( $file );
189
0ec9ad96 190 # Test::Builder will prematurely numify objects, so use this form
191 ok( $pm_info->version eq '1.23',
bb4e9162 192 "correct module version ($i of $n)" );
193 is( $warnings, '', 'no warnings from parsing' );
194 $i++;
195 }
196}
197
198# revert to pristine state
199chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
200$dist->remove;
201$dist = DistGen->new( dir => $tmp );
202$dist->regen;
203chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
204
205
206# Find each package only once
207$dist->change_file( 'lib/Simple.pm', <<'---' );
208package Simple;
209$VERSION = '1.23';
210package Error::Simple;
211$VERSION = '2.34';
212package Simple;
213---
214
215$dist->regen;
216
217$pm_info = Module::Build::ModuleInfo->new_from_file( $file );
218
219my @packages = $pm_info->packages_inside;
220is( @packages, 2, 'record only one occurence of each package' );
221
222
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', <<'---' );
226package Simple::Not;
227$VERSION = '1.23';
228---
229
230$dist->regen;
231$pm_info = Module::Build::ModuleInfo->new_from_file( $file );
232
233is( $pm_info->name, undef, 'no default package' );
234is( $pm_info->version, undef, 'no version w/o default package' );
235
b3dfda33 236# Module 'Simple.pm' contains an alpha version
237# constructor should report first $VERSION found
238$dist->change_file( 'lib/Simple.pm', <<'---' );
239package Simple;
240$VERSION = '1.23_01';
241$VERSION = eval $VERSION;
242---
243
244$dist->regen;
245$pm_info = Module::Build::ModuleInfo->new_from_file( $file );
246
247is( $pm_info->version, '1.23_01', 'alpha version reported');
248
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.
254
255ok( $pm_info->version > 1.23, 'alpha version greater than non');
bb4e9162 256
257# revert to pristine state
258chdir( $cwd ) or die "Can''t chdir to '$cwd': $!";
259$dist->remove;
260$dist = DistGen->new( dir => $tmp );
261$dist->regen;
262chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!";
263
264
265# parse $VERSION lines scripts for package main
266my @scripts = (
267 <<'---', # package main declared
268#!perl -w
269package main;
270$VERSION = '0.01';
271---
272 <<'---', # on first non-comment line, non declared package main
273#!perl -w
274$VERSION = '0.01';
275---
276 <<'---', # after non-comment line
277#!perl -w
278use strict;
279$VERSION = '0.01';
280---
281 <<'---', # 1st declared package
282#!perl -w
283package main;
284$VERSION = '0.01';
285package _private;
286$VERSION = '999';
287---
288 <<'---', # 2nd declared package
289#!perl -w
290package _private;
291$VERSION = '999';
292package main;
293$VERSION = '0.01';
294---
295 <<'---', # split package
296#!perl -w
297package main;
298package _private;
299$VERSION = '999';
300package main;
301$VERSION = '0.01';
302---
303 <<'---', # define 'main' version from other package
304package _private;
305$::VERSION = 0.01;
306$VERSION = '999';
307---
308 <<'---', # define 'main' version from other package
309package _private;
310$VERSION = '999';
311$::VERSION = 0.01;
312---
313);
314
315( $i, $n ) = ( 1, scalar( @scripts ) );
316foreach my $script ( @scripts ) {
317 $dist->change_file( 'bin/simple.plx', $script );
318 $dist->regen;
319 $pm_info = Module::Build::ModuleInfo->new_from_file(
320 File::Spec->catfile( 'bin', 'simple.plx' ) );
321
322 is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
323 $i++;
324}
325
326
327# examine properties of a module: name, pod, etc
328$dist->change_file( 'lib/Simple.pm', <<'---' );
329package Simple;
330$VERSION = '0.01';
331package Simple::Ex;
332$VERSION = '0.02';
333=head1 NAME
334
335Simple - It's easy.
336
337=head1 AUTHOR
338
339Simple Simon
340
341=cut
342---
343$dist->regen;
344
345$pm_info = Module::Build::ModuleInfo->new_from_module(
346 $dist->name, inc => [ 'lib', @INC ] );
347
348is( $pm_info->name, 'Simple', 'found default package' );
bb4e9162 349is( $pm_info->version, '0.01', 'version for default package' );
350
351# got correct version for secondary package
352is( $pm_info->version( 'Simple::Ex' ), '0.02',
353 'version for secondary package' );
354
355my $filename = $pm_info->filename;
356ok( defined( $filename ) && -e $filename,
357 'filename() returns valid path to module file' );
358
359@packages = $pm_info->packages_inside;
360is( @packages, 2, 'found correct number of packages' );
361is( $packages[0], 'Simple', 'packages stored in order found' );
362
363# we can detect presence of pod regardless of whether we are collecting it
364ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
365
366my @pod = $pm_info->pod_inside;
367is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
368
369is( $pm_info->pod('NONE') , undef,
370 'return undef() if pod section not present' );
371
372is( $pm_info->pod('NAME'), undef,
373 'return undef() if pod section not collected' );
374
375
376# collect_pod
377$pm_info = Module::Build::ModuleInfo->new_from_module(
378 $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
379
380my $name = $pm_info->pod('NAME');
381if ( $name ) {
382 $name =~ s/^\s+//;
383 $name =~ s/\s+$//;
384}
385is( $name, q|Simple - It's easy.|, 'collected pod section' );
386
387
0ec9ad96 388{
389 # examine properties of a module: name, pod, etc
390 $dist->change_file( 'lib/Simple.pm', <<'---' );
391package Simple;
392$VERSION = '0.01';
393__DATA__
394*UNIVERSAL::VERSION = sub {
395 foo();
396};
397---
398 $dist->regen;
399
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']);
405}
406
407
bb4e9162 408# cleanup
0ec9ad96 409chdir( $cwd ) or die "Can't chdir to '$cwd': $!";
bb4e9162 410$dist->remove;
411
412use File::Path;
413rmtree( $tmp );