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;
#########################
-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
}
{
+ 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' );
# 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' );
}
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
}
}
-# 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;
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;
{
# 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' );
{
# 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');
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
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++;
{
# 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;
=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' );
# 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)) {
{
# 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
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' );
{
# 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__
foo();
};
---
- $dist->regen;
my $pm_info = Module::Metadata->new_from_file('lib/Simple.pm');
is( $pm_info->name, 'Simple', 'found default package' );
{
# 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' );
# 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;
=cut
---
- $dist->regen;
my $exp_pvfd = {
'Simple' => {
# 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' );
}
{
- $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;
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' );
}
{
- $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' );