Commit | Line | Data |
bb4e9162 |
1 | #!/usr/bin/perl -w |
2 | |
3 | use strict; |
4 | use lib $ENV{PERL_CORE} ? '../lib/Module/Build/t/lib' : 't/lib'; |
77e96e88 |
5 | use MBTest tests => 47; |
bb4e9162 |
6 | |
7 | use Cwd (); |
8 | my $cwd = Cwd::cwd; |
9 | my $tmp = File::Spec->catdir( $cwd, 't', '_tmp' ); |
10 | |
11 | |
12 | use Module::Build; |
13 | use Module::Build::ConfigData; |
14 | my $has_YAML = Module::Build::ConfigData->feature('YAML_support'); |
15 | |
16 | |
17 | use DistGen; |
18 | my $dist = DistGen->new( dir => $tmp ); |
19 | $dist->change_file( 'Build.PL', <<"---" ); |
20 | |
21 | my \$builder = Module::Build->new( |
22 | module_name => '@{[$dist->name]}', |
23 | dist_version => '3.14159265', |
24 | dist_author => [ 'Simple Simon <ss\@somewhere.priv>' ], |
25 | dist_abstract => 'Something interesting', |
26 | license => 'perl', |
27 | ); |
28 | |
29 | \$builder->create_build_script(); |
30 | --- |
31 | $dist->regen; |
32 | |
33 | chdir( $dist->dirname ) or die "Can't chdir to '@{[$dist->dirname]}': $!"; |
34 | |
35 | use Module::Build; |
36 | my $mb = Module::Build->new_from_context; |
37 | |
38 | ################################################## |
39 | # |
40 | # Test for valid META.yml |
41 | |
42 | SKIP: { |
43 | skip( 'YAML_support feature is not enabled', 8 ) unless $has_YAML; |
44 | |
45 | require YAML; |
46 | require YAML::Node; |
47 | my $node = YAML::Node->new({}); |
48 | $node = $mb->prepare_metadata( $node ); |
49 | |
50 | # exists() doesn't seem to work here |
51 | ok defined( $node->{name} ), "'name' field present in META.yml"; |
52 | ok defined( $node->{version} ), "'version' field present in META.yml"; |
53 | ok defined( $node->{abstract} ), "'abstract' field present in META.yml"; |
54 | ok defined( $node->{author} ), "'author' field present in META.yml"; |
55 | ok defined( $node->{license} ), "'license' field present in META.yml"; |
56 | ok defined( $node->{generated_by} ), |
57 | "'generated_by' field present in META.yml"; |
58 | ok defined( $node->{'meta-spec'}{version} ), |
59 | "'meta-spec' -> 'version' field present in META.yml"; |
60 | ok defined( $node->{'meta-spec'}{url} ), |
61 | "'meta-spec' -> 'url' field present in META.yml"; |
62 | |
63 | # TODO : find a way to test for failure when above fields are not present |
64 | } |
65 | |
66 | $dist->clean; |
67 | |
68 | |
69 | ################################################## |
70 | # |
71 | # Tests to ensure that the correct packages and versions are |
72 | # recorded for the 'provides' field of META.yml |
73 | |
74 | my $provides; # Used a bunch of times below |
75 | |
76 | sub new_build { return Module::Build->new_from_context( quiet => 1, @_ ) } |
77 | |
78 | ############################## Single Module |
79 | |
80 | # File with corresponding package (w/ or w/o version) |
81 | # Simple.pm => Simple v1.23 |
82 | |
83 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
84 | package Simple; |
85 | $VERSION = '1.23'; |
86 | --- |
87 | $dist->regen( clean => 1 ); |
88 | $mb = new_build(); |
89 | is_deeply($mb->find_dist_packages, |
90 | {'Simple' => {file => 'lib/Simple.pm', |
91 | version => '1.23'}}); |
92 | |
93 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
94 | package Simple; |
95 | --- |
96 | $dist->regen( clean => 1 ); |
97 | $mb = new_build(); |
98 | is_deeply($mb->find_dist_packages, |
99 | {'Simple' => {file => 'lib/Simple.pm'}}); |
100 | |
101 | # File with no corresponding package (w/ or w/o version) |
102 | # Simple.pm => Foo::Bar v1.23 |
103 | |
104 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
105 | package Foo::Bar; |
106 | $VERSION = '1.23'; |
107 | --- |
108 | $dist->regen( clean => 1 ); |
109 | $mb = new_build(); |
110 | is_deeply($mb->find_dist_packages, |
111 | {'Foo::Bar' => { file => 'lib/Simple.pm', |
112 | version => '1.23' }}); |
113 | |
114 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
115 | package Foo::Bar; |
116 | --- |
117 | $dist->regen( clean => 1 ); |
118 | $mb = new_build(); |
119 | is_deeply($mb->find_dist_packages, |
120 | {'Foo::Bar' => { file => 'lib/Simple.pm'}}); |
121 | |
122 | |
123 | # Single file with multiple differing packages (w/ or w/o version) |
124 | # Simple.pm => Simple |
125 | # Simple.pm => Foo::Bar |
126 | |
127 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
128 | package Simple; |
129 | $VERSION = '1.23'; |
130 | package Foo::Bar; |
131 | $VERSION = '1.23'; |
132 | --- |
133 | $dist->regen( clean => 1 ); |
134 | $mb = new_build(); |
135 | is_deeply($mb->find_dist_packages, |
136 | {'Simple' => { file => 'lib/Simple.pm', |
137 | version => '1.23' }, |
138 | 'Foo::Bar' => { file => 'lib/Simple.pm', |
139 | version => '1.23' }}); |
140 | |
77e96e88 |
141 | { |
142 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
143 | package Simple; |
7253302f |
144 | $VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]); |
77e96e88 |
145 | package Simple::Simon; |
7253302f |
146 | $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]); |
77e96e88 |
147 | --- |
148 | $dist->regen; |
149 | my $provides = new_build()->prepare_metadata()->{provides}; |
8cb289bd |
150 | is $provides->{'Simple'}{version}, '0.60.128', "Check version"; |
151 | is $provides->{'Simple::Simon'}{version}, '0.61.129', "Check version"; |
77e96e88 |
152 | is ref($provides->{'Simple'}{version}), '', "Versions from prepare_metadata() aren't refs"; |
153 | is ref($provides->{'Simple::Simon'}{version}), '', "Versions from prepare_metadata() aren't refs"; |
154 | } |
155 | |
bb4e9162 |
156 | |
157 | # Single file with multiple differing packages, no corresponding package |
158 | # Simple.pm => Foo |
159 | # Simple.pm => Foo::Bar |
160 | |
161 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
162 | package Foo; |
163 | $VERSION = '1.23'; |
164 | package Foo::Bar; |
165 | $VERSION = '1.23'; |
166 | --- |
167 | $dist->regen( clean => 1 ); |
168 | $mb = new_build(); |
169 | is_deeply($mb->find_dist_packages, |
170 | {'Foo' => { file => 'lib/Simple.pm', |
171 | version => '1.23' }, |
172 | 'Foo::Bar' => { file => 'lib/Simple.pm', |
173 | version => '1.23' }}); |
174 | |
175 | |
176 | # Single file with same package appearing multiple times, no version |
177 | # only record a single instance |
178 | # Simple.pm => Simple |
179 | # Simple.pm => Simple |
180 | |
181 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
182 | package Simple; |
183 | package Simple; |
184 | --- |
185 | $dist->regen( clean => 1 ); |
186 | $mb = new_build(); |
187 | is_deeply($mb->find_dist_packages, |
188 | {'Simple' => { file => 'lib/Simple.pm' }}); |
189 | |
190 | |
191 | # Single file with same package appearing multiple times, single |
192 | # version 1st package: |
193 | # Simple.pm => Simple v1.23 |
194 | # Simple.pm => Simple |
195 | |
196 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
197 | package Simple; |
198 | $VERSION = '1.23'; |
199 | package Simple; |
200 | --- |
201 | $dist->regen( clean => 1 ); |
202 | $mb = new_build(); |
203 | is_deeply($mb->find_dist_packages, |
204 | {'Simple' => { file => 'lib/Simple.pm', |
205 | version => '1.23' }}); |
206 | |
207 | |
208 | # Single file with same package appearing multiple times, single |
209 | # version 2nd package |
210 | # Simple.pm => Simple |
211 | # Simple.pm => Simple v1.23 |
212 | |
213 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
214 | package Simple; |
215 | package Simple; |
216 | $VERSION = '1.23'; |
217 | --- |
218 | $dist->regen( clean => 1 ); |
219 | $mb = new_build(); |
220 | is_deeply($mb->find_dist_packages, |
221 | {'Simple' => { file => 'lib/Simple.pm', |
222 | version => '1.23' }}); |
223 | |
224 | |
225 | # Single file with same package appearing multiple times, conflicting versions |
226 | # Simple.pm => Simple v1.23 |
227 | # Simple.pm => Simple v2.34 |
228 | |
229 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
230 | package Simple; |
231 | $VERSION = '1.23'; |
232 | package Simple; |
233 | $VERSION = '2.34'; |
234 | --- |
235 | $dist->regen( clean => 1 ); |
236 | my $err = ''; |
237 | $err = stderr_of( sub { $mb = new_build() } ); |
238 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
239 | is_deeply($provides, |
240 | {'Simple' => { file => 'lib/Simple.pm', |
241 | version => '1.23' }}); # XXX should be 2.34? |
242 | like( $err, qr/already declared/, ' with conflicting versions reported' ); |
243 | |
244 | |
245 | # (Same as above three cases except with no corresponding package) |
246 | # Simple.pm => Foo v1.23 |
247 | # Simple.pm => Foo v2.34 |
248 | |
249 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
250 | package Foo; |
251 | $VERSION = '1.23'; |
252 | package Foo; |
253 | $VERSION = '2.34'; |
254 | --- |
255 | $dist->regen( clean => 1 ); |
256 | $err = stderr_of( sub { $mb = new_build() } ); |
257 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
258 | is_deeply($provides, |
259 | {'Foo' => { file => 'lib/Simple.pm', |
260 | version => '1.23' }}); # XXX should be 2.34? |
261 | like( $err, qr/already declared/, ' with conflicting versions reported' ); |
262 | |
263 | |
264 | |
265 | ############################## Multiple Modules |
266 | |
267 | # Multiple files with same package, no version |
268 | # Simple.pm => Simple |
269 | # Simple2.pm => Simple |
270 | |
271 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
272 | package Simple; |
273 | --- |
274 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
275 | package Simple; |
276 | --- |
277 | $dist->regen( clean => 1 ); |
278 | $mb = new_build(); |
279 | is_deeply($mb->find_dist_packages, |
280 | {'Simple' => { file => 'lib/Simple.pm' }}); |
281 | $dist->remove_file( 'lib/Simple2.pm' ); |
282 | |
283 | |
284 | # Multiple files with same package, single version in corresponding package |
285 | # Simple.pm => Simple v1.23 |
286 | # Simple2.pm => Simple |
287 | |
288 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
289 | package Simple; |
290 | $VERSION = '1.23'; |
291 | --- |
292 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
293 | package Simple; |
294 | --- |
295 | $dist->regen( clean => 1 ); |
296 | $mb = new_build(); |
297 | is_deeply($mb->find_dist_packages, |
298 | {'Simple' => { file => 'lib/Simple.pm', |
299 | version => '1.23' }}); |
300 | $dist->remove_file( 'lib/Simple2.pm' ); |
301 | |
302 | |
303 | # Multiple files with same package, |
304 | # single version in non-corresponding package |
305 | # Simple.pm => Simple |
306 | # Simple2.pm => Simple v1.23 |
307 | |
308 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
309 | package Simple; |
310 | --- |
311 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
312 | package Simple; |
313 | $VERSION = '1.23'; |
314 | --- |
315 | $dist->regen( clean => 1 ); |
316 | $mb = new_build(); |
317 | is_deeply($mb->find_dist_packages, |
318 | {'Simple' => { file => 'lib/Simple2.pm', |
319 | version => '1.23' }}); |
320 | $dist->remove_file( 'lib/Simple2.pm' ); |
321 | |
322 | |
323 | # Multiple files with same package, conflicting versions |
324 | # Simple.pm => Simple v1.23 |
325 | # Simple2.pm => Simple v2.34 |
326 | |
327 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
328 | package Simple; |
329 | $VERSION = '1.23'; |
330 | --- |
331 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
332 | package Simple; |
333 | $VERSION = '2.34'; |
334 | --- |
335 | $dist->regen( clean => 1 ); |
336 | $mb = new_build(); |
337 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
338 | is_deeply($provides, |
339 | {'Simple' => { file => 'lib/Simple.pm', |
340 | version => '1.23' }}); |
341 | like( $err, qr/Found conflicting versions for package/, |
342 | ' with conflicting versions reported' ); |
343 | $dist->remove_file( 'lib/Simple2.pm' ); |
344 | |
345 | |
346 | # Multiple files with same package, multiple agreeing versions |
347 | # Simple.pm => Simple v1.23 |
348 | # Simple2.pm => Simple v1.23 |
349 | |
350 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
351 | package Simple; |
352 | $VERSION = '1.23'; |
353 | --- |
354 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
355 | package Simple; |
356 | $VERSION = '1.23'; |
357 | --- |
358 | $dist->regen( clean => 1 ); |
359 | $mb = new_build(); |
360 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
361 | is_deeply($provides, |
362 | {'Simple' => { file => 'lib/Simple.pm', |
363 | version => '1.23' }}); |
364 | $dist->remove_file( 'lib/Simple2.pm' ); |
365 | |
366 | |
367 | ############################################################ |
368 | # |
369 | # (Same as above five cases except with non-corresponding package) |
370 | # |
371 | |
372 | # Multiple files with same package, no version |
373 | # Simple.pm => Foo |
374 | # Simple2.pm => Foo |
375 | |
376 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
377 | package Foo; |
378 | --- |
379 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
380 | package Foo; |
381 | --- |
382 | $dist->regen( clean => 1 ); |
383 | $mb = new_build(); |
384 | $provides = $mb->find_dist_packages; |
385 | ok( exists( $provides->{Foo} ) ); # it exist, can't predict which file |
386 | $dist->remove_file( 'lib/Simple2.pm' ); |
387 | |
388 | |
389 | # Multiple files with same package, version in first file |
390 | # Simple.pm => Foo v1.23 |
391 | # Simple2.pm => Foo |
392 | |
393 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
394 | package Foo; |
395 | $VERSION = '1.23'; |
396 | --- |
397 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
398 | package Foo; |
399 | --- |
400 | $dist->regen( clean => 1 ); |
401 | $mb = new_build(); |
402 | is_deeply($mb->find_dist_packages, |
403 | {'Foo' => { file => 'lib/Simple.pm', |
404 | version => '1.23' }}); |
405 | $dist->remove_file( 'lib/Simple2.pm' ); |
406 | |
407 | |
408 | # Multiple files with same package, version in second file |
409 | # Simple.pm => Foo |
410 | # Simple2.pm => Foo v1.23 |
411 | |
412 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
413 | package Foo; |
414 | --- |
415 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
416 | package Foo; |
417 | $VERSION = '1.23'; |
418 | --- |
419 | $dist->regen( clean => 1 ); |
420 | $mb = new_build(); |
421 | is_deeply($mb->find_dist_packages, |
422 | {'Foo' => { file => 'lib/Simple2.pm', |
423 | version => '1.23' }}); |
424 | $dist->remove_file( 'lib/Simple2.pm' ); |
425 | |
426 | |
427 | # Multiple files with same package, conflicting versions |
428 | # Simple.pm => Foo v1.23 |
429 | # Simple2.pm => Foo v2.34 |
430 | |
431 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
432 | package Foo; |
433 | $VERSION = '1.23'; |
434 | --- |
435 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
436 | package Foo; |
437 | $VERSION = '2.34'; |
438 | --- |
439 | $dist->regen( clean => 1 ); |
440 | $mb = new_build(); |
441 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
442 | # XXX Should 'Foo' exist ??? Can't predict values for file & version |
443 | ok( exists( $provides->{Foo} ) ); |
444 | like( $err, qr/Found conflicting versions for package/, |
445 | ' with conflicting versions reported' ); |
446 | $dist->remove_file( 'lib/Simple2.pm' ); |
447 | |
448 | |
449 | # Multiple files with same package, multiple agreeing versions |
450 | # Simple.pm => Foo v1.23 |
451 | # Simple2.pm => Foo v1.23 |
452 | |
453 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
454 | package Foo; |
455 | $VERSION = '1.23'; |
456 | --- |
457 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
458 | package Foo; |
459 | $VERSION = '1.23'; |
460 | --- |
461 | $dist->regen( clean => 1 ); |
462 | $mb = new_build(); |
463 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
464 | ok( exists( $provides->{Foo} ) ); |
465 | is( $provides->{Foo}{version}, '1.23' ); |
466 | ok( exists( $provides->{Foo}{file} ) ); # Can't predict which file |
467 | is( $err, '', ' no conflicts reported' ); |
468 | $dist->remove_file( 'lib/Simple2.pm' ); |
469 | |
470 | ############################################################ |
471 | # Conflicts among primary & multiple alternatives |
472 | |
473 | # multiple files, conflicting version in corresponding file |
474 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
475 | package Simple; |
476 | $VERSION = '1.23'; |
477 | --- |
478 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
479 | package Simple; |
480 | $VERSION = '2.34'; |
481 | --- |
482 | $dist->add_file( 'lib/Simple3.pm', <<'---' ); |
483 | package Simple; |
484 | $VERSION = '2.34'; |
485 | --- |
486 | $dist->regen( clean => 1 ); |
487 | $err = stderr_of( sub { |
488 | $mb = new_build(); |
489 | } ); |
490 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
491 | is_deeply($provides, |
492 | {'Simple' => { file => 'lib/Simple.pm', |
493 | version => '1.23' }}); |
494 | like( $err, qr/Found conflicting versions for package/, |
495 | ' corresponding package conflicts with multiple alternatives' ); |
496 | $dist->remove_file( 'lib/Simple2.pm' ); |
497 | $dist->remove_file( 'lib/Simple3.pm' ); |
498 | |
499 | # multiple files, conflicting version in non-corresponding file |
500 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
501 | package Simple; |
502 | $VERSION = '1.23'; |
503 | --- |
504 | $dist->add_file( 'lib/Simple2.pm', <<'---' ); |
505 | package Simple; |
506 | $VERSION = '1.23'; |
507 | --- |
508 | $dist->add_file( 'lib/Simple3.pm', <<'---' ); |
509 | package Simple; |
510 | $VERSION = '2.34'; |
511 | --- |
512 | $dist->regen( clean => 1 ); |
513 | $err = stderr_of( sub { |
514 | $mb = new_build(); |
515 | } ); |
516 | $err = stderr_of( sub { $provides = $mb->find_dist_packages } ); |
517 | is_deeply($provides, |
518 | {'Simple' => { file => 'lib/Simple.pm', |
519 | version => '1.23' }}); |
520 | like( $err, qr/Found conflicting versions for package/, |
521 | ' only one alternative conflicts with corresponding package' ); |
522 | $dist->remove_file( 'lib/Simple2.pm' ); |
523 | $dist->remove_file( 'lib/Simple3.pm' ); |
524 | |
525 | |
526 | ############################################################ |
527 | # Don't record private packages (beginning with underscore) |
528 | # Simple.pm => Simple::_private |
529 | # Simple.pm => Simple::_private::too |
530 | |
531 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
532 | package Simple; |
533 | $VERSION = '1.23'; |
534 | package Simple::_private; |
535 | $VERSION = '2.34'; |
536 | package Simple::_private::too; |
537 | $VERSION = '3.45'; |
538 | --- |
539 | $dist->regen( clean => 1 ); |
540 | $mb = new_build(); |
541 | is_deeply($mb->find_dist_packages, |
542 | {'Simple' => { file => 'lib/Simple.pm', |
543 | version => '1.23' }}); |
544 | |
545 | |
546 | ############################################################ |
547 | # Files with no packages? |
548 | |
549 | # Simple.pm => <empty> |
550 | |
551 | $dist->change_file( 'lib/Simple.pm', '' ); |
552 | $dist->regen( clean => 1 ); |
553 | $mb = new_build(); |
554 | is_deeply( $mb->find_dist_packages, {} ); |
555 | |
556 | # Simple.pm => =pod..=cut (no package declaration) |
557 | $dist->change_file( 'lib/Simple.pm', <<'---' ); |
558 | =pod |
559 | |
560 | =head1 NAME |
561 | |
562 | Simple - Pure Documentation |
563 | |
564 | =head1 DESCRIPTION |
565 | |
566 | Doesn't do anything. |
567 | |
568 | =cut |
569 | --- |
570 | $dist->regen( clean => 1 ); |
571 | $mb = new_build(); |
572 | is_deeply($mb->find_dist_packages, {}); |
573 | |
bb4e9162 |
574 | ############################################################ |
575 | # cleanup |
576 | chdir( $cwd ) or die "Can't chdir to '$cwd': $!"; |
577 | $dist->remove; |
578 | |
579 | use File::Path; |
580 | rmtree( $tmp ); |