Fix a2p manpage (from Debian)
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Manifest.t
index 8c0246f..396789e 100644 (file)
@@ -13,47 +13,53 @@ chdir 't';
 
 use strict;
 
-# these files help the test run
-use Test::More tests => 33;
+use Test::More tests => 49;
 use Cwd;
 
-# these files are needed for the module itself
 use File::Spec;
 use File::Path;
-use Carp::Heavy;
+use File::Find;
+
+my $Is_VMS = $^O eq 'VMS';
+
+# We're going to be chdir'ing and modules are sometimes loaded on the
+# fly in this test, so we need an absolute @INC.
+@INC = map { File::Spec->rel2abs($_) } @INC;
 
 # keep track of everything added so it can all be deleted
-my %files;
+my %Files;
 sub add_file {
-       my ($file, $data) = @_;
-       $data ||= 'foo';
-    unlink $file;  # or else we'll get multiple versions on VMS
-       open( my $T, '>', $file) or return;
-       print $T $data;
-       ++$files{$file};
+    my ($file, $data) = @_;
+    $data ||= 'foo';
+    1 while unlink $file;  # or else we'll get multiple versions on VMS
+    open( T, '>'.$file) or return;
+    print T $data;
+    ++$Files{$file};
+    close T;
 }
 
 sub read_manifest {
-       open( my $M, 'MANIFEST' ) or return;
-       chomp( my @files = <$M> );
-       return @files;
+    open( M, 'MANIFEST' ) or return;
+    chomp( my @files = <M> );
+    close M;
+    return @files;
 }
 
 sub catch_warning {
-       my $warn;
-       local $SIG{__WARN__} = sub { $warn .= $_[0] };
-       return join('', $_[0]->() ), $warn;
+    my $warn;
+    local $SIG{__WARN__} = sub { $warn .= $_[0] };
+    return join('', $_[0]->() ), $warn;
 }
 
 sub remove_dir {
-       ok( rmdir( $_ ), "remove $_ directory" ) for @_;
+    ok( rmdir( $_ ), "remove $_ directory" ) for @_;
 }
 
 # use module, import functions
 BEGIN { 
     use_ok( 'ExtUtils::Manifest', 
             qw( mkmanifest manicheck filecheck fullcheck 
-                maniread manicopy skipcheck ) ); 
+                maniread manicopy skipcheck maniadd) ); 
 }
 
 my $cwd = Cwd::getcwd();
@@ -122,10 +128,33 @@ ok( exists( ExtUtils::Manifest::manifind()->{'moretest/quux'} ),
                                         "manifind found moretest/quux" );
 
 # only MANIFEST and foo are in the manifest
+$_ = 'foo';
 my $files = maniread();
 is( keys %$files, 2, 'two files found' );
 is( join(' ', sort { lc($a) cmp lc($b) } keys %$files), 'foo MANIFEST', 
                                         'both files found' );
+is( $_, 'foo', q{maniread() doesn't clobber $_} );
+
+ok( mkdir( 'copy', 0777 ), 'made copy directory' );
+
+# Check that manicopy copies files.
+manicopy( $files, 'copy', 'cp' );
+my @copies = ();
+find( sub { push @copies, $_ if -f }, 'copy' );
+@copies = map { s/\.$//; $_ } @copies if $Is_VMS;  # VMS likes to put dots on
+                                                   # the end of files.
+# Have to compare insensitively for non-case preserving VMS
+is_deeply( [sort map { lc } @copies], [sort map { lc } keys %$files] );
+
+# cp would leave files readonly, so check permissions.
+foreach my $orig (@copies) {
+    my $copy = "copy/$orig";
+    ok( -r $copy,               "$copy: must be readable" );
+    is( -w $copy, -w $orig,     "       writable if original was" );
+    is( -x $copy, -x $orig,     "       executable if original was" );
+}
+rmtree('copy');
+
 
 # poison the manifest, and add a comment that should be reported
 add_file( 'MANIFEST', 'none #none' );
@@ -133,12 +162,11 @@ is( ExtUtils::Manifest::maniread()->{none}, '#none',
                                         'maniread found comment' );
 
 ok( mkdir( 'copy', 0777 ), 'made copy directory' );
-
 $files = maniread();
 eval { (undef, $warn) = catch_warning( sub {
-               manicopy( $files, 'copy', 'cp' ) }) 
+               manicopy( $files, 'copy', 'cp' ) }) 
 };
-like( $@, qr/^Can't read none: /, 'carped about none' );
+like( $@, qr/^Can't read none: /, 'croaked about none' );
 
 # a newline comes through, so get rid of it
 chomp($warn);
@@ -153,7 +181,7 @@ like($warn, qr/^Skipping MANIFEST.SKIP/i, 'warned about MANIFEST.SKIP' );
        like( $warn, qr/Added to albatross: /, 'using a new manifest file' );
        
        # add the new file to the list of files to be deleted
-       $files{'albatross'}++;
+       $Files{'albatross'}++;
 }
 
 
@@ -168,21 +196,59 @@ like( $warn, qr{^Skipping moretest/quux$}i, 'got skipping warning again' );
 # There was a bug where entries in MANIFEST would be blotted out
 # by MANIFEST.SKIP rules.
 add_file( 'MANIFEST.SKIP' => 'foo' );
-add_file( 'MANIFEST'      => 'foobar'   );
+add_file( 'MANIFEST'      => "foobar\n"   );
 add_file( 'foobar'        => '123' );
 ($res, $warn) = catch_warning( \&manicheck );
 is( $res,  '',      'MANIFEST overrides MANIFEST.SKIP' );
 is( $warn, undef,   'MANIFEST overrides MANIFEST.SKIP, no warnings' );
 
+$files = maniread;
+ok( !$files->{wibble},     'MANIFEST in good state' );
+maniadd({ wibble => undef });
+maniadd({ yarrow => "hock" });
+$files = maniread;
+is( $files->{wibble}, '',    'maniadd() with undef comment' );
+is( $files->{yarrow}, 'hock','          with comment' );
+is( $files->{foobar}, '',    '          preserved old entries' );
+
+add_file('MANIFEST'   => 'Makefile.PL');
+maniadd({ foo  => 'bar' });
+$files = maniread;
+# VMS downcases the MANIFEST.  We normalize it here to match.
+%$files = map { (lc $_ => $files->{$_}) } keys %$files;
+my %expect = ( 'makefile.pl' => '',
+               'foo'    => 'bar'
+             );
+is_deeply( $files, \%expect, 'maniadd() vs MANIFEST without trailing newline');
+
+add_file('MANIFEST'   => 'Makefile.PL');
+maniadd({ foo => 'bar' });
+
+SKIP: {
+    chmod( 0400, 'MANIFEST' );
+    skip "Can't make MANIFEST read-only", 2 if -w 'MANIFEST';
+
+    eval {
+        maniadd({ 'foo' => 'bar' });
+    };
+    is( $@, '',  "maniadd() won't open MANIFEST if it doesn't need to" );
+
+    eval {
+        maniadd({ 'grrrwoof' => 'yippie' });
+    };
+    like( $@, qr/^\Qmaniadd() could not open MANIFEST:\E/,  
+                 "maniadd() dies if it can't open the MANIFEST" );
+
+    chmod( 0600, 'MANIFEST' );
+}
+
 
 END {
-       # the args are evaluated in scalar context
-       is( unlink( keys %files ), keys %files, 'remove all added files' );
+       is( unlink( keys %Files ), keys %Files, 'remove all added files' );
        remove_dir( 'moretest', 'copy' );
 
        # now get rid of the parent directory
        ok( chdir( $cwd ), 'return to parent directory' );
-       unlink('mantest/MANIFEST');
        remove_dir( 'mantest' );
 }