1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
|
#!/usr/bin/perl -w
# -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-
# vim:ts=8:sw=2:et:sta:sts=2
use strict;
use warnings;
use lib 't/lib';
use IO::File;
use MBTest;
my $undef;
# parse various module $VERSION lines
# these will be reversed later to create %modules
my @modules = (
$undef => <<'---', # no $VERSION line
package Simple;
---
$undef => <<'---', # undefined $VERSION
package Simple;
our $VERSION;
---
'1.23' => <<'---', # declared & defined on same line with 'our'
package Simple;
our $VERSION = '1.23';
---
'1.23' => <<'---', # declared & defined on separate lines with 'our'
package Simple;
our $VERSION;
$VERSION = '1.23';
---
'1.23' => <<'---', # commented & defined on same line
package Simple;
our $VERSION = '1.23'; # our $VERSION = '4.56';
---
'1.23' => <<'---', # commented & defined on separate lines
package Simple;
# our $VERSION = '4.56';
our $VERSION = '1.23';
---
'1.23' => <<'---', # use vars
package Simple;
use vars qw( $VERSION );
$VERSION = '1.23';
---
'1.23' => <<'---', # choose the right default package based on package/file name
package Simple::_private;
$VERSION = '0';
package Simple;
$VERSION = '1.23'; # this should be chosen for version
---
'1.23' => <<'---', # just read the first $VERSION line
package Simple;
$VERSION = '1.23'; # we should see this line
$VERSION = eval $VERSION; # and ignore this one
---
'1.23' => <<'---', # just read the first $VERSION line in reopened package (1)
package Simple;
$VERSION = '1.23';
package Error::Simple;
$VERSION = '2.34';
package Simple;
---
'1.23' => <<'---', # just read the first $VERSION line in reopened package (2)
package Simple;
package Error::Simple;
$VERSION = '2.34';
package Simple;
$VERSION = '1.23';
---
'1.23' => <<'---', # mentions another module's $VERSION
package Simple;
$VERSION = '1.23';
if ( $Other::VERSION ) {
# whatever
}
---
'1.23' => <<'---', # mentions another module's $VERSION in a different package
package Simple;
$VERSION = '1.23';
package Simple2;
if ( $Simple::VERSION ) {
# whatever
}
---
'1.23' => <<'---', # $VERSION checked only in assignments, not regexp ops
package Simple;
$VERSION = '1.23';
if ( $VERSION =~ /1\.23/ ) {
# whatever
}
---
'1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
package Simple;
$VERSION = '1.23';
if ( $VERSION == 3.45 ) {
# whatever
}
---
'1.23' => <<'---', # $VERSION checked only in assignments, not relational ops
package Simple;
$VERSION = '1.23';
package Simple2;
if ( $Simple::VERSION == 3.45 ) {
# whatever
}
---
'1.23' => <<'---', # Fully qualified $VERSION declared in package
package Simple;
$Simple::VERSION = 1.23;
---
'1.23' => <<'---', # Differentiate fully qualified $VERSION in a package
package Simple;
$Simple2::VERSION = '999';
$Simple::VERSION = 1.23;
---
'1.23' => <<'---', # Differentiate fully qualified $VERSION and unqualified
package Simple;
$Simple2::VERSION = '999';
$VERSION = 1.23;
---
'1.23' => <<'---', # $VERSION declared as package variable from within 'main' package
$Simple::VERSION = '1.23';
{
package Simple;
$x = $y, $cats = $dogs;
}
---
'1.23' => <<'---', # $VERSION wrapped in parens - space inside
package Simple;
( $VERSION ) = '1.23';
---
'1.23' => <<'---', # $VERSION wrapped in parens - no space inside
package Simple;
($VERSION) = '1.23';
---
'1.23' => <<'---', # $VERSION follows a spurious 'package' in a quoted construct
package Simple;
__PACKAGE__->mk_accessors(qw(
program socket proc
package filename line codeline subroutine finished));
our $VERSION = "1.23";
---
'1.23' => <<'---', # $VERSION using version.pm
package Simple;
use version; our $VERSION = version->new('1.23');
---
'1.23' => <<'---', # $VERSION using version.pm and qv()
package Simple;
use version; our $VERSION = qv('1.230');
---
'1.23' => <<'---', # Two version assignments, should ignore second one
$Simple::VERSION = '1.230';
$Simple::VERSION = eval $Simple::VERSION;
---
'1.23' => <<'---', # declared & defined on same line with 'our'
package Simple;
our $VERSION = '1.23_00_00';
---
'1.23' => <<'---', # package NAME VERSION
package Simple 1.23;
---
'1.23_01' => <<'---', # package NAME VERSION
package Simple 1.23_01;
---
'v1.2.3' => <<'---', # package NAME VERSION
package Simple v1.2.3;
---
'v1.2_3' => <<'---', # package NAME VERSION
package Simple v1.2_3;
---
'1.23' => <<'---', # trailing crud
package Simple;
our $VERSION;
$VERSION = '1.23-alpha';
---
'1.23' => <<'---', # trailing crud
package Simple;
our $VERSION;
$VERSION = '1.23b';
---
'1.234' => <<'---', # multi_underscore
package Simple;
our $VERSION;
$VERSION = '1.2_3_4';
---
'0' => <<'---', # non-numeric
package Simple;
our $VERSION;
$VERSION = 'onetwothree';
---
$undef => <<'---', # package NAME BLOCK, undef $VERSION
package Simple {
our $VERSION;
}
---
'1.23' => <<'---', # package NAME BLOCK, with $VERSION
package Simple {
our $VERSION = '1.23';
}
---
'1.23' => <<'---', # package NAME VERSION BLOCK
package Simple 1.23 {
1;
}
---
'v1.2.3_4' => <<'---', # package NAME VERSION BLOCK
package Simple v1.2.3_4 {
1;
}
---
'0' => <<'---', # set from separately-initialised variable
package Simple;
our $CVSVERSION = '$Revision: 1.7 $';
our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/);
}
---
);
my %modules = reverse @modules;
my @pkg_names = (
[ 'Simple' ] => <<'---', # package NAME
package Simple;
---
[ 'Simple::Edward' ] => <<'---', # package NAME::SUBNAME
package Simple::Edward;
---
[ 'Simple::Edward::' ] => <<'---', # package NAME::SUBNAME::
package Simple::Edward::;
---
[ "Simple'Edward" ] => <<'---', # package NAME'SUBNAME
package Simple'Edward;
---
[ "Simple'Edward::" ] => <<'---', # package NAME'SUBNAME::
package Simple'Edward::;
---
[ 'Simple::::Edward' ] => <<'---', # package NAME::::SUBNAME
package Simple::::Edward;
---
[ '::Simple::Edward' ] => <<'---', # package ::NAME::SUBNAME
package ::Simple::Edward;
---
[ 'main' ] => <<'---', # package NAME:SUBNAME (fail)
package Simple:Edward;
---
[ 'main' ] => <<'---', # package NAME' (fail)
package Simple';
---
[ 'main' ] => <<'---', # package NAME::SUBNAME' (fail)
package Simple::Edward';
---
[ 'main' ] => <<'---', # package NAME''SUBNAME (fail)
package Simple''Edward;
---
[ 'main' ] => <<'---', # package NAME-SUBNAME (fail)
package Simple-Edward;
---
);
my %pkg_names = reverse @pkg_names;
plan tests => 54 + (2 * keys( %modules )) + (2 * keys( %pkg_names ));
require_ok('Module::Metadata');
# class method C<find_module_by_name>
my $module = Module::Metadata->find_module_by_name(
'Module::Metadata' );
ok( -e $module, 'find_module_by_name() succeeds' );
#########################
my $tmp = MBTest->tmpdir;
use DistGen;
my $dist = DistGen->new( dir => $tmp );
$dist->regen;
$dist->chdir_in;
# fail on invalid module name
my $pm_info = Module::Metadata->new_from_module(
'Foo::Bar', inc => [] );
ok( !defined( $pm_info ), 'fail if can\'t find module by module name' );
# fail on invalid filename
my $file = File::Spec->catfile( 'Foo', 'Bar.pm' );
$pm_info = Module::Metadata->new_from_file( $file, inc => [] );
ok( !defined( $pm_info ), 'fail if can\'t find module by file name' );
# construct from module filename
$file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm';
$pm_info = Module::Metadata->new_from_file( $file );
ok( defined( $pm_info ), 'new_from_file() succeeds' );
# construct from filehandle
my $handle = IO::File->new($file);
$pm_info = Module::Metadata->new_from_handle( $handle, $file );
ok( defined( $pm_info ), 'new_from_handle() succeeds' );
$pm_info = Module::Metadata->new_from_handle( $handle );
is( $pm_info, undef, "new_from_handle() without filename returns undef" );
close($handle);
# construct from module name, using custom include path
$pm_info = Module::Metadata->new_from_module(
$dist->name, inc => [ 'lib', @INC ] );
ok( defined( $pm_info ), 'new_from_module() succeeds' );
foreach my $module ( sort keys %modules ) {
my $expected = $modules{$module};
SKIP: {
skip( "No our() support until perl 5.6", 2 )
if $] < 5.006 && $module =~ /\bour\b/;
skip( "No package NAME VERSION support until perl 5.11.1", 2 )
if $] < 5.011001 && $module =~ /package\s+[\w\:\']+\s+v?[0-9._]+/;
$dist->change_file( 'lib/Simple.pm', $module );
$dist->regen;
my $warnings = '';
local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
my $pm_info = Module::Metadata->new_from_file( $file );
# Test::Builder will prematurely numify objects, so use this form
my $errs;
my $got = $pm_info->version;
if ( defined $expected ) {
ok( $got eq $expected,
"correct module version (expected '$expected')" )
or $errs++;
} else {
ok( !defined($got),
"correct module version (expected undef)" )
or $errs++;
}
is( $warnings, '', 'no warnings from parsing' ) or $errs++;
diag "Got: '$got'\nModule contents:\n$module" if $errs;
}
}
# revert to pristine state
$dist->regen( clean => 1 );
foreach my $pkg_name ( sort keys %pkg_names ) {
my $expected = $pkg_names{$pkg_name};
$dist->change_file( 'lib/Simple.pm', $pkg_name );
$dist->regen;
my $warnings = '';
local $SIG{__WARN__} = sub { $warnings .= $_ for @_ };
my $pm_info = Module::Metadata->new_from_file( $file );
# Test::Builder will prematurely numify objects, so use this form
my $errs;
my @got = $pm_info->packages_inside();
is_deeply( \@got, $expected,
"correct package names (expected '" . join(', ', @$expected) . "')" )
or $errs++;
is( $warnings, '', 'no warnings from parsing' ) or $errs++;
diag "Got: '" . join(', ', @got) . "'\nModule contents:\n$pkg_name" if $errs;
}
# revert to pristine state
$dist->regen( clean => 1 );
# Find each package only once
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
$VERSION = '1.23';
package Error::Simple;
$VERSION = '2.34';
package Simple;
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file( $file );
my @packages = $pm_info->packages_inside;
is( @packages, 2, 'record only one occurence of each package' );
# Module 'Simple.pm' does not contain package 'Simple';
# constructor should not complain, no default module name or version
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple::Not;
$VERSION = '1.23';
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file( $file );
is( $pm_info->name, undef, 'no default package' );
is( $pm_info->version, undef, 'no version w/o default package' );
# Module 'Simple.pm' contains an alpha version
# constructor should report first $VERSION found
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
$VERSION = '1.23_01';
$VERSION = eval $VERSION;
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file( $file );
is( $pm_info->version, '1.23_01', 'alpha version reported');
# NOTE the following test has be done this way because Test::Builder is
# too smart for our own good and tries to see if the version object is a
# dual-var, which breaks with alpha versions:
# Argument "1.23_0100" isn't numeric in addition (+) at
# /usr/lib/perl5/5.8.7/Test/Builder.pm line 505.
ok( $pm_info->version > 1.23, 'alpha version greater than non');
# revert to pristine state
$dist->regen( clean => 1 );
# parse $VERSION lines scripts for package main
my @scripts = (
<<'---', # package main declared
#!perl -w
package main;
$VERSION = '0.01';
---
<<'---', # on first non-comment line, non declared package main
#!perl -w
$VERSION = '0.01';
---
<<'---', # after non-comment line
#!perl -w
use strict;
$VERSION = '0.01';
---
<<'---', # 1st declared package
#!perl -w
package main;
$VERSION = '0.01';
package _private;
$VERSION = '999';
---
<<'---', # 2nd declared package
#!perl -w
package _private;
$VERSION = '999';
package main;
$VERSION = '0.01';
---
<<'---', # split package
#!perl -w
package main;
package _private;
$VERSION = '999';
package main;
$VERSION = '0.01';
---
<<'---', # define 'main' version from other package
package _private;
$::VERSION = 0.01;
$VERSION = '999';
---
<<'---', # define 'main' version from other package
package _private;
$VERSION = '999';
$::VERSION = 0.01;
---
);
my ( $i, $n ) = ( 1, scalar( @scripts ) );
foreach my $script ( @scripts ) {
$dist->change_file( 'bin/simple.plx', $script );
$dist->regen;
$pm_info = Module::Metadata->new_from_file(
File::Spec->catfile( 'bin', 'simple.plx' ) );
is( $pm_info->version, '0.01', "correct script version ($i of $n)" );
$i++;
}
# examine properties of a module: name, pod, etc
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
$VERSION = '0.01';
package Simple::Ex;
$VERSION = '0.02';
=head1 NAME
Simple - It's easy.
=head1 AUTHOR
Simple Simon
You can find me on the IRC channel
#simon on irc.perl.org.
=cut
---
$dist->regen;
$pm_info = Module::Metadata->new_from_module(
$dist->name, inc => [ 'lib', @INC ] );
is( $pm_info->name, 'Simple', 'found default package' );
is( $pm_info->version, '0.01', 'version for default package' );
# got correct version for secondary package
is( $pm_info->version( 'Simple::Ex' ), '0.02',
'version for secondary package' );
my $filename = $pm_info->filename;
ok( defined( $filename ) && -e $filename,
'filename() returns valid path to module file' );
@packages = $pm_info->packages_inside;
is( @packages, 2, 'found correct number of packages' );
is( $packages[0], 'Simple', 'packages stored in order found' );
# we can detect presence of pod regardless of whether we are collecting it
ok( $pm_info->contains_pod, 'contains_pod() succeeds' );
my @pod = $pm_info->pod_inside;
is_deeply( \@pod, [qw(NAME AUTHOR)], 'found all pod sections' );
is( $pm_info->pod('NONE') , undef,
'return undef() if pod section not present' );
is( $pm_info->pod('NAME'), undef,
'return undef() if pod section not collected' );
# collect_pod
$pm_info = Module::Metadata->new_from_module(
$dist->name, inc => [ 'lib', @INC ], collect_pod => 1 );
{
my %pod;
for my $section (qw(NAME AUTHOR)) {
my $content = $pm_info->pod( $section );
if ( $content ) {
$content =~ s/^\s+//;
$content =~ s/\s+$//;
}
$pod{$section} = $content;
}
my %expected = (
NAME => q|Simple - It's easy.|,
AUTHOR => <<'EXPECTED'
Simple Simon
You can find me on the IRC channel
#simon on irc.perl.org.
EXPECTED
);
for my $text (values %expected) {
$text =~ s/^\s+//;
$text =~ s/\s+$//;
}
is( $pod{NAME}, $expected{NAME}, 'collected NAME pod section' );
is( $pod{AUTHOR}, $expected{AUTHOR}, 'collected AUTHOR pod section' );
}
{
# test things that look like POD, but aren't
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
=YES THIS STARTS POD
our $VERSION = '999';
=cute
our $VERSION = '666';
=cut
*foo
=*no_this_does_not_start_pod;
our $VERSION = '1.23';
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
is( $pm_info->name, 'Simple', 'found default package' );
is( $pm_info->version, '1.23', 'version for default package' );
}
{
# Make sure processing stops after __DATA__
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
$VERSION = '0.01';
__DATA__
*UNIVERSAL::VERSION = sub {
foo();
};
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
is( $pm_info->name, 'Simple', 'found default package' );
is( $pm_info->version, '0.01', 'version for default package' );
my @packages = $pm_info->packages_inside;
is_deeply(\@packages, ['Simple'], 'packages inside');
}
{
# Make sure we handle version.pm $VERSIONs well
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
$VERSION = version->new('0.60.' . (qw$Revision: 128 $)[1]);
package Simple::Simon;
$VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]);
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
is( $pm_info->name, 'Simple', 'found default package' );
is( $pm_info->version, '0.60.128', 'version for default package' );
my @packages = $pm_info->packages_inside;
is_deeply([sort @packages], ['Simple', 'Simple::Simon'], 'packages inside');
is( $pm_info->version('Simple::Simon'), '0.61.129', 'version for embedded package' );
}
# check that package_versions_from_directory works
$dist->change_file( 'lib/Simple.pm', <<'---' );
package Simple;
$VERSION = '0.01';
package Simple::Ex;
$VERSION = '0.02';
{
package main; # should ignore this
}
{
package DB; # should ignore this
}
{
package Simple::_private; # should ignore this
}
=head1 NAME
Simple - It's easy.
=head1 AUTHOR
Simple Simon
=cut
---
$dist->regen;
my $exp_pvfd = {
'Simple' => {
'file' => 'Simple.pm',
'version' => '0.01'
},
'Simple::Ex' => {
'file' => 'Simple.pm',
'version' => '0.02'
}
};
my $got_pvfd = Module::Metadata->package_versions_from_directory('lib');
is_deeply( $got_pvfd, $exp_pvfd, "package_version_from_directory()" )
or diag explain $got_pvfd;
{
my $got_provides = Module::Metadata->provides(dir => 'lib', version => 2);
my $exp_provides = {
'Simple' => {
'file' => 'lib/Simple.pm',
'version' => '0.01'
},
'Simple::Ex' => {
'file' => 'lib/Simple.pm',
'version' => '0.02'
}
};
is_deeply( $got_provides, $exp_provides, "provides()" )
or diag explain $got_provides;
}
{
my $got_provides = Module::Metadata->provides(dir => 'lib', prefix => 'other', version => 1.4);
my $exp_provides = {
'Simple' => {
'file' => 'other/Simple.pm',
'version' => '0.01'
},
'Simple::Ex' => {
'file' => 'other/Simple.pm',
'version' => '0.02'
}
};
is_deeply( $got_provides, $exp_provides, "provides()" )
or diag explain $got_provides;
}
# Check package_versions_from_directory with regard to case-sensitivity
{
$dist->change_file( 'lib/Simple.pm', <<'---' );
package simple;
$VERSION = '0.01';
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
is( $pm_info->name, undef, 'no default package' );
is( $pm_info->version, undef, 'version for default package' );
is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
is( $pm_info->version('Simple'), undef, 'version for capitalized package' );
$dist->change_file( 'lib/Simple.pm', <<'---' );
package simple;
$VERSION = '0.01';
package Simple;
$VERSION = '0.02';
package SiMpLe;
$VERSION = '0.03';
---
$dist->regen;
$pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
is( $pm_info->name, 'Simple', 'found default package' );
is( $pm_info->version, '0.02', 'version for default package' );
is( $pm_info->version('simple'), '0.01', 'version for lower-case package' );
is( $pm_info->version('Simple'), '0.02', 'version for capitalized package' );
is( $pm_info->version('SiMpLe'), '0.03', 'version for mixed-case package' );
}
|