hv_fetchs() support
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Command.t
index 4e54189..22eabe5 100644 (file)
@@ -12,14 +12,18 @@ BEGIN {
 chdir 't';
 
 BEGIN {
-    1 while unlink 'ecmdfile', 'newfile';
+    $Testfile = 'testfile.foo';
+}
+
+BEGIN {
+    1 while unlink $Testfile, 'newfile';
     # forcibly remove ecmddir/temp2, but don't import mkpath
     use File::Path ();
     File::Path::rmtree( 'ecmddir' );
 }
 
 BEGIN {
-    use Test::More tests => 27;
+    use Test::More tests => 38;
     use File::Spec;
 }
 
@@ -31,38 +35,6 @@ BEGIN {
 }
 
 {
-    # get a file in the MM test directory, replace last char with wildcard 
-    my $file;
-    {
-        local *DIR;
-        my $mmtestdir = $ENV{PERL_CORE}
-          ? File::Spec->catdir(File::Spec->updir, 'lib', 'ExtUtils', 't')
-          : File::Spec->curdir;
-        opendir(DIR, $mmtestdir);
-        while ($file = readdir(DIR)) {
-            $file =~ s/\.\z// if $^O eq 'VMS';
-            last if $file =~ /^\w/;
-        }
-        closedir DIR;
-    }
-
-
-    # % means 'match one character' on VMS.  Everything else is ?
-    my $match_char = $^O eq 'VMS' ? '%' : '?';
-    ($ARGV[0] = $file) =~ s/.\z/$match_char/;
-
-    # this should find the file
-    ExtUtils::Command::expand_wildcards();
-
-    is( scalar @ARGV, 1, 'found one file' );
-    like( $ARGV[0], qr/$file/, 'expanded wildcard ? successfully' );
-
-    # try it with the asterisk now
-    ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
-    ExtUtils::Command::expand_wildcards();
-
-    ok( (grep { qr/$file/ } @ARGV), 'expanded wildcard * successfully' );
-
     # concatenate this file with itself
     # be extra careful the regex doesn't match itself
     use TieOut;
@@ -82,20 +54,21 @@ BEGIN {
         'concatenation worked' );
 
     # the truth value here is reversed -- Perl true is C false
-    @ARGV = ( 'ecmdfile' );
+    @ARGV = ( $Testfile );
     ok( test_f(), 'testing non-existent file' );
 
-    @ARGV = ( 'ecmdfile' );
-    cmp_ok( ! test_f(), '==', (-f 'ecmdfile'), 'testing non-existent file' );
+    @ARGV = ( $Testfile );
+    cmp_ok( ! test_f(), '==', defined (-f $Testfile), 'testing non-existent file' );
 
     # these are destructive, have to keep setting @ARGV
-    @ARGV = ( 'ecmdfile' );
+    @ARGV = ( $Testfile );
     touch();
 
-    @ARGV = ( 'ecmdfile' );
+    @ARGV = ( $Testfile );
     ok( test_f(), 'now creating that file' );
+    is_deeply( \@ARGV, [$Testfile], 'test_f preserves @ARGV' );
 
-    @ARGV = ( 'ecmdfile' );
+    @ARGV = ( $Testfile );
     ok( -e $ARGV[0], 'created!' );
 
     my ($now) = time;
@@ -115,20 +88,20 @@ BEGIN {
     my $new_stamp = (stat('newfile'))[9];
     cmp_ok( abs($new_stamp - $stamp), '>=', 2,  'newer file created' );
 
-    @ARGV = qw(newfile ecmdfile);
+    @ARGV = ('newfile', $Testfile);
     eqtime();
 
-    $stamp = (stat('ecmdfile'))[9];
+    $stamp = (stat($Testfile))[9];
     cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' );
 
     # eqtime use to clear the contents of the file being equalized!
-    open(FILE, '>>ecmdfile') || die $!;
+    open(FILE, ">>$Testfile") || die $!;
     print FILE "Foo";
     close FILE;
 
-    @ARGV = qw(newfile ecmdfile);
+    @ARGV = ('newfile', $Testfile);
     eqtime();
-    ok( -s 'ecmdfile', "eqtime doesn't clear the file being equalized" );
+    ok( -s $Testfile, "eqtime doesn't clear the file being equalized" );
 
     SKIP: {
         if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
@@ -139,34 +112,75 @@ BEGIN {
         }
 
         # change a file to execute-only
-        @ARGV = ( '0100', 'ecmdfile' );
+        @ARGV = ( '0100', $Testfile );
         ExtUtils::Command::chmod();
 
-        is( ((stat('ecmdfile'))[2] & 07777) & 0700,
+        is( ((stat($Testfile))[2] & 07777) & 0700,
             0100, 'change a file to execute-only' );
 
         # change a file to read-only
-        @ARGV = ( '0400', 'ecmdfile' );
+        @ARGV = ( '0400', $Testfile );
         ExtUtils::Command::chmod();
 
-        is( ((stat('ecmdfile'))[2] & 07777) & 0700,
+        is( ((stat($Testfile))[2] & 07777) & 0700,
             ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
 
         # change a file to write-only
-        @ARGV = ( '0200', 'ecmdfile' );
+        @ARGV = ( '0200', $Testfile );
         ExtUtils::Command::chmod();
 
-        is( ((stat('ecmdfile'))[2] & 07777) & 0700,
+        is( ((stat($Testfile))[2] & 07777) & 0700,
             ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
     }
 
     # change a file to read-write
-    @ARGV = ( '0600', 'ecmdfile' );
+    @ARGV = ( '0600', $Testfile );
+    my @orig_argv = @ARGV;
     ExtUtils::Command::chmod();
+    is_deeply( \@ARGV, \@orig_argv, 'chmod preserves @ARGV' );
 
-    is( ((stat('ecmdfile'))[2] & 07777) & 0700,
+    is( ((stat($Testfile))[2] & 07777) & 0700,
         ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
 
+
+    SKIP: {
+        if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
+            $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin'  ||
+            $^O eq 'MacOS'
+           ) {
+            skip( "different file permission semantics on $^O", 4);
+        }
+
+        @ARGV = ('testdir');
+        mkpath;
+        ok( -e 'testdir' );
+
+        # change a dir to execute-only
+        @ARGV = ( '0100', 'testdir' );
+        ExtUtils::Command::chmod();
+
+        is( ((stat('testdir'))[2] & 07777) & 0700,
+            0100, 'change a dir to execute-only' );
+
+        # change a dir to read-only
+        @ARGV = ( '0400', 'testdir' );
+        ExtUtils::Command::chmod();
+
+        is( ((stat('testdir'))[2] & 07777) & 0700,
+            ($^O eq 'vos' ? 0500 : 0400), 'change a dir to read-only' );
+
+        # change a dir to write-only
+        @ARGV = ( '0200', 'testdir' );
+        ExtUtils::Command::chmod();
+
+        is( ((stat('testdir'))[2] & 07777) & 0700,
+            ($^O eq 'vos' ? 0700 : 0200), 'change a dir to write-only' );
+
+        @ARGV = ('testdir');
+        rm_rf;
+    }
+
+
     # mkpath
     @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
     ok( ! -e $ARGV[0], 'temp directory not there yet' );
@@ -175,33 +189,61 @@ BEGIN {
     ok( -e $ARGV[0], 'temp directory created' );
 
     # copy a file to a nested subdirectory
-    unshift @ARGV, 'ecmdfile';
+    unshift @ARGV, $Testfile;
+    @orig_argv = @ARGV;
     cp();
+    is_deeply( \@ARGV, \@orig_argv, 'cp preserves @ARGV' );
 
-    ok( -e File::Spec->join( 'ecmddir', 'temp2', 'ecmdfile' ), 'copied okay' );
+    ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' );
 
     # cp should croak if destination isn't directory (not a great warning)
-    @ARGV = ( 'ecmdfile' ) x 3;
+    @ARGV = ( $Testfile ) x 3;
     eval { cp() };
 
     like( $@, qr/Too many arguments/, 'cp croaks on error' );
 
     # move a file to a subdirectory
-    @ARGV = ( 'ecmdfile', 'ecmddir' );
-    mv();
+    @ARGV = ( $Testfile, 'ecmddir' );
+    @orig_argv = @ARGV;
+    ok( mv() );
+    is_deeply( \@ARGV, \@orig_argv, 'mv preserves @ARGV' );
 
-    ok( ! -e 'ecmdfile', 'moved file away' );
-    ok( -e File::Spec->join( 'ecmddir', 'ecmdfile' ), 'file in new location' );
+    ok( ! -e $Testfile, 'moved file away' );
+    ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' );
 
     # mv should also croak with the same wacky warning
-    @ARGV = ( 'ecmdfile' ) x 3;
+    @ARGV = ( $Testfile ) x 3;
 
     eval { mv() };
     like( $@, qr/Too many arguments/, 'mv croaks on error' );
 
+    # Test expand_wildcards()
+    {
+        my $file = $Testfile;
+        @ARGV = ();
+        chdir 'ecmddir';
+
+        # % means 'match one character' on VMS.  Everything else is ?
+        my $match_char = $^O eq 'VMS' ? '%' : '?';
+        ($ARGV[0] = $file) =~ s/.\z/$match_char/;
+
+        # this should find the file
+        ExtUtils::Command::expand_wildcards();
+
+        is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' );
+
+        # try it with the asterisk now
+        ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
+        ExtUtils::Command::expand_wildcards();
+
+        is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' );
+
+        chdir File::Spec->updir;
+    }
+
     # remove some files
-    my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', 'ecmdfile' ),
-    File::Spec->catfile( 'ecmddir', 'temp2', 'ecmdfile' ) );
+    my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ),
+    File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) );
     rm_f();
 
     ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
@@ -212,7 +254,35 @@ BEGIN {
     ok( ! -e $dir, "removed $dir successfully" );
 }
 
+{
+    { local @ARGV = 'd2utest'; mkpath; }
+    open(FILE, '>d2utest/foo');
+    print FILE "stuff\015\012and thing\015\012";
+    close FILE;
+
+    open(FILE, '>d2utest/bar');
+    binmode(FILE);
+    my $bin = "\c@\c@\c@\c@\c@\c@\cA\c@\c@\c@\015\012".
+              "\@\c@\cA\c@\c@\c@8__LIN\015\012";
+    print FILE $bin;
+    close FILE;
+
+    local @ARGV = 'd2utest';
+    ExtUtils::Command::dos2unix();
+
+    open(FILE, 'd2utest/foo');
+    is( join('', <FILE>), "stuff\012and thing\012", 'dos2unix' );
+    close FILE;
+
+    open(FILE, 'd2utest/bar');
+    binmode(FILE);
+    ok( -B 'd2utest/bar' );
+    is( join('', <FILE>), $bin, 'dos2unix preserves binaries');
+    close FILE;
+}
+
 END {
-    1 while unlink 'ecmdfile', 'newfile';
+    1 while unlink $Testfile, 'newfile';
     File::Path::rmtree( 'ecmddir' );
+    File::Path::rmtree( 'd2utest' );
 }