X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fmetadata.t;fp=t%2Fmetadata.t;h=55d06c68d6672580b5869dc2c8b6eae71c9630a7;hb=62849b9aac1248b26061ba3dfc9fc07162a4b9c6;hp=31112912e674ec5fa6405f471191c61aff3dff01;hpb=2235a0d7a71bb7d5edda0901a5f5d8bb40158e72;p=p5sagit%2FModule-Metadata.git diff --git a/t/metadata.t b/t/metadata.t index 3111291..55d06c6 100644 --- a/t/metadata.t +++ b/t/metadata.t @@ -4,10 +4,12 @@ use strict; use warnings; -use lib 't/lib'; use Test::More; use IO::File; -use MBTest; +use File::Spec; +use File::Temp; +use File::Basename; +use Cwd (); my $undef; @@ -273,13 +275,62 @@ require_ok('Module::Metadata'); ######################### -my $tmp = MBTest->tmpdir; +BEGIN { + my $cwd = File::Spec->rel2abs(Cwd::cwd); + sub original_cwd { return $cwd } +} + +# Setup a temp directory +sub tmpdir { + my (@args) = @_; + my $dir = $ENV{PERL_CORE} ? original_cwd : File::Spec->tmpdir; + return File::Temp::tempdir('MMD-XXXXXXXX', CLEANUP => 0, DIR => $dir, @args); +} -use DistGen; -my $dist = DistGen->new( dir => $tmp ); -$dist->regen; +my $tmp; +BEGIN { $tmp = tmpdir; diag "using temp dir $tmp"; } -$dist->chdir_in; +END { + die "tests failed; leaving temp dir $tmp behind" + if $ENV{AUTHOR_TESTING} and not Test::Builder->new->is_passing; + diag "removing temp dir $tmp"; + chdir original_cwd; + File::Path::remove_tree($tmp); +} + +# generates a new distribution: +# files => { relative filename => $content ... } +# returns the name of the distribution (not including version), +# and the absolute path name to the dist. +{ + my $test_num = 0; + sub new_dist { + my %opts = @_; + + my $distname = 'Simple' . $test_num++; + my $distdir = File::Spec->catdir($tmp, $distname); + note "using dist $distname in $distdir"; + + File::Path::mkpath($distdir) or die "failed to create '$distdir'"; + + foreach my $rel_filename (keys %{$opts{files}}) + { + my $abs_filename = File::Spec->catfile($distdir, $rel_filename); + my $dirname = File::Basename::dirname($abs_filename); + unless (-d $dirname) { + File::Path::mkpath($dirname) or die "Can't create '$dirname'"; + } + + note "creating $abs_filename"; + my $fh = IO::File->new(">$abs_filename") or die "Can't write '$abs_filename'\n"; + print $fh $opts{files}{$rel_filename}; + close $fh; + } + + chdir $distdir; + return ($distname, $distdir); + } +} { # fail on invalid module name @@ -296,8 +347,10 @@ $dist->chdir_in; } { + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => "package Simple;\n" }); + # construct from module filename - my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; my $pm_info = Module::Metadata->new_from_file( $file ); ok( defined( $pm_info ), 'new_from_file() succeeds' ); @@ -311,7 +364,7 @@ $dist->chdir_in; # construct from module name, using custom include path $pm_info = Module::Metadata->new_from_module( - $dist->name, inc => [ 'lib', @INC ] ); + 'Simple', inc => [ 'lib', @INC ] ); ok( defined( $pm_info ), 'new_from_module() succeeds' ); } @@ -324,12 +377,11 @@ foreach my $module ( sort keys %modules ) { 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 $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => $module }); my $warnings = ''; local $SIG{__WARN__} = sub { $warnings .= $_ for @_ }; - my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; my $pm_info = Module::Metadata->new_from_file( $file ); # Test::Builder will prematurely numify objects, so use this form @@ -349,36 +401,30 @@ foreach my $module ( sort keys %modules ) { } } -# revert to pristine state -$dist->regen( clean => 1 ); - -foreach my $pkg_name ( sort keys %pkg_names ) { - my $expected = $pkg_names{$pkg_name}; +foreach my $pkg_contents ( sort keys %pkg_names ) { + my $expected = $pkg_names{$pkg_contents}; - $dist->change_file( 'lib/Simple.pm', $pkg_name ); - $dist->regen; + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => $pkg_contents }); - my $warnings = ''; - local $SIG{__WARN__} = sub { $warnings .= $_ for @_ }; - my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; - my $pm_info = Module::Metadata->new_from_file( $file ); + 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; + # 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_contents" if $errs; } -# revert to pristine state -$dist->regen( clean => 1 ); - { # Find each package only once - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple; $VERSION = '1.23'; package Error::Simple; @@ -386,9 +432,6 @@ $VERSION = '2.34'; package Simple; --- - $dist->regen; - - my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; my $pm_info = Module::Metadata->new_from_file( $file ); my @packages = $pm_info->packages_inside; @@ -398,13 +441,12 @@ package Simple; { # Module 'Simple.pm' does not contain package 'Simple'; # constructor should not complain, no default module name or version - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple::Not; $VERSION = '1.23'; --- - $dist->regen; - my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; my $pm_info = Module::Metadata->new_from_file( $file ); is( $pm_info->name, undef, 'no default package' ); @@ -414,14 +456,13 @@ $VERSION = '1.23'; { # Module 'Simple.pm' contains an alpha version # constructor should report first $VERSION found - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple; $VERSION = '1.23_01'; $VERSION = eval $VERSION; --- - $dist->regen; - my $file = File::Spec->catfile( 'lib', split( /::/, $dist->name ) ) . '.pm'; my $pm_info = Module::Metadata->new_from_file( $file ); is( $pm_info->version, '1.23_01', 'alpha version reported'); @@ -435,9 +476,6 @@ $VERSION = eval $VERSION; 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 @@ -490,10 +528,9 @@ $::VERSION = 0.01; my ( $i, $n ) = ( 1, scalar( @scripts ) ); foreach my $script ( @scripts ) { - $dist->change_file( 'bin/simple.plx', $script ); - $dist->regen; - my $pm_info = Module::Metadata->new_from_file( - File::Spec->catfile( 'bin', 'simple.plx' ) ); + my $file = File::Spec->catfile('bin', 'simple.plx'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => $script } ); + my $pm_info = Module::Metadata->new_from_file( $file ); is( $pm_info->version, '0.01', "correct script version ($i of $n)" ); $i++; @@ -501,7 +538,8 @@ foreach my $script ( @scripts ) { { # examine properties of a module: name, pod, etc - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple; $VERSION = '0.01'; package Simple::Ex; @@ -520,10 +558,9 @@ You can find me on the IRC channel =cut --- - $dist->regen; my $pm_info = Module::Metadata->new_from_module( - $dist->name, inc => [ 'lib', @INC ] ); + 'Simple', inc => [ 'lib', @INC ] ); is( $pm_info->name, 'Simple', 'found default package' ); is( $pm_info->version, '0.01', 'version for default package' ); @@ -555,7 +592,7 @@ You can find me on the IRC channel # collect_pod $pm_info = Module::Metadata->new_from_module( - $dist->name, inc => [ 'lib', @INC ], collect_pod => 1 ); + 'Simple', inc => [ 'lib', @INC ], collect_pod => 1 ); my %pod; for my $section (qw(NAME AUTHOR)) { @@ -585,7 +622,8 @@ EXPECTED { # test things that look like POD, but aren't -$dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple; =YES THIS STARTS POD @@ -604,7 +642,6 @@ our $VERSION = '666'; our $VERSION = '1.23'; --- - $dist->regen; my $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' ); @@ -612,7 +649,8 @@ our $VERSION = '1.23'; { # Make sure processing stops after __DATA__ - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple; $VERSION = '0.01'; __DATA__ @@ -620,7 +658,6 @@ __DATA__ foo(); }; --- - $dist->regen; my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, 'Simple', 'found default package' ); @@ -631,13 +668,13 @@ __DATA__ { # Make sure we handle version.pm $VERSIONs well - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); 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; my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, 'Simple', 'found default package' ); @@ -650,7 +687,8 @@ $VERSION = version->new('0.61.' . (qw$Revision: 129 $)[1]); # check that package_versions_from_directory works { - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package Simple; $VERSION = '0.01'; package Simple::Ex; @@ -675,7 +713,6 @@ Simple Simon =cut --- - $dist->regen; my $exp_pvfd = { 'Simple' => { @@ -730,11 +767,11 @@ Simple Simon # Check package_versions_from_directory with regard to case-sensitivity { - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package simple; $VERSION = '0.01'; --- - $dist->regen; my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, undef, 'no default package' ); @@ -747,7 +784,8 @@ $VERSION = '0.01'; } { - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package simple; $VERSION = '0.01'; package Simple; @@ -755,7 +793,6 @@ $VERSION = '0.02'; package SiMpLe; $VERSION = '0.03'; --- - $dist->regen; my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, 'Simple', 'found default package' ); @@ -768,14 +805,13 @@ $VERSION = '0.03'; } { - $dist->change_file( 'lib/Simple.pm', <<'---' ); + my $file = File::Spec->catfile('lib', 'Simple.pm'); + my ($dist_name, $dist_dir) = new_dist(files => { $file => <<'---' } ); package ## hide from PAUSE simple; $VERSION = '0.01'; --- - $dist->regen; - my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm'); is( $pm_info->name, undef, 'no package names found' ); ok( !$pm_info->is_indexable('simple'), 'the simple package would not be indexed' );