1#!/usr/bin/perl -w 2 3BEGIN { 4 unshift @INC, 't/lib/'; 5} 6chdir 't'; 7 8BEGIN { 9 $Testfile = 'testfile.foo'; 10} 11 12BEGIN { 13 1 while unlink $Testfile, 'newfile'; 14 # forcibly remove ecmddir/temp2, but don't import mkpath 15 use File::Path (); 16 File::Path::rmtree( 'ecmddir' ) if -e 'ecmddir'; 17} 18 19use Test::More tests => 40; 20use File::Spec; 21 22BEGIN { 23 # bad neighbor, but test_f() uses exit() 24 *CORE::GLOBAL::exit = ''; # quiet 'only once' warning. 25 *CORE::GLOBAL::exit = sub (;$) { return $_[0] }; 26 use_ok( 'ExtUtils::Command' ); 27} 28 29{ 30 # concatenate this file with itself 31 # be extra careful the regex doesn't match itself 32 use TieOut; 33 my $out = tie *STDOUT, 'TieOut'; 34 my $self = $0; 35 unless (-f $self) { 36 my ($vol, $dirs, $file) = File::Spec->splitpath($self); 37 my @dirs = File::Spec->splitdir($dirs); 38 unshift(@dirs, File::Spec->updir); 39 $dirs = File::Spec->catdir(@dirs); 40 $self = File::Spec->catpath($vol, $dirs, $file); 41 } 42 @ARGV = ($self, $self); 43 44 cat(); 45 is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2, 46 'concatenation worked' ); 47 48 # the truth value here is reversed -- Perl true is shell false 49 @ARGV = ( $Testfile ); 50 is( test_f(), 1, 'testing non-existent file' ); 51 52 # these are destructive, have to keep setting @ARGV 53 @ARGV = ( $Testfile ); 54 touch(); 55 56 @ARGV = ( $Testfile ); 57 is( test_f(), 0, 'testing touch() and test_f()' ); 58 is_deeply( \@ARGV, [$Testfile], 'test_f preserves @ARGV' ); 59 60 @ARGV = ( $Testfile ); 61 ok( -e $ARGV[0], 'created!' ); 62 63 my ($now) = time; 64 utime ($now, $now, $ARGV[0]); 65 sleep 2; 66 67 # Just checking modify time stamp, access time stamp is set 68 # to the beginning of the day in Win95. 69 # There's a small chance of a 1 second flutter here. 70 my $stamp = (stat($ARGV[0]))[9]; 71 cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) || 72 diag "mtime == $stamp, should be $now"; 73 74 @ARGV = qw(newfile); 75 touch(); 76 77 my $new_stamp = (stat('newfile'))[9]; 78 cmp_ok( abs($new_stamp - $stamp), '>=', 2, 'newer file created' ); 79 80 @ARGV = ('newfile', $Testfile); 81 eqtime(); 82 83 $stamp = (stat($Testfile))[9]; 84 cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' ); 85 86 # eqtime use to clear the contents of the file being equalized! 87 open(FILE, ">>$Testfile") || die $!; 88 print FILE "Foo"; 89 close FILE; 90 91 @ARGV = ('newfile', $Testfile); 92 eqtime(); 93 ok( -s $Testfile, "eqtime doesn't clear the file being equalized" ); 94 95 SKIP: { 96 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || 97 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' || 98 $^O eq 'MacOS' 99 ) { 100 skip( "different file permission semantics on $^O", 3); 101 } 102 103 # change a file to execute-only 104 @ARGV = ( '0100', $Testfile ); 105 ExtUtils::Command::chmod(); 106 107 is( ((stat($Testfile))[2] & 07777) & 0700, 108 0100, 'change a file to execute-only' ); 109 110 # change a file to read-only 111 @ARGV = ( '0400', $Testfile ); 112 ExtUtils::Command::chmod(); 113 114 is( ((stat($Testfile))[2] & 07777) & 0700, 115 0400, 'change a file to read-only' ); 116 117 # change a file to write-only 118 @ARGV = ( '0200', $Testfile ); 119 ExtUtils::Command::chmod(); 120 121 is( ((stat($Testfile))[2] & 07777) & 0700, 122 0200, 'change a file to write-only' ); 123 } 124 125 # change a file to read-write 126 @ARGV = ( '0600', $Testfile ); 127 my @orig_argv = @ARGV; 128 ExtUtils::Command::chmod(); 129 is_deeply( \@ARGV, \@orig_argv, 'chmod preserves @ARGV' ); 130 131 is( ((stat($Testfile))[2] & 07777) & 0700, 132 0600, 'change a file to read-write' ); 133 134 135 SKIP: { 136 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' || 137 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' || 138 $^O eq 'MacOS' || $^O eq 'haiku' 139 ) { 140 skip( "different file permission semantics on $^O", 5); 141 } 142 143 @ARGV = ('testdir'); 144 mkpath; 145 ok( -e 'testdir' ); 146 147 # change a dir to execute-only 148 @ARGV = ( '0100', 'testdir' ); 149 ExtUtils::Command::chmod(); 150 151 is( ((stat('testdir'))[2] & 07777) & 0700, 152 0100, 'change a dir to execute-only' ); 153 154 # change a dir to write-only 155 @ARGV = ( '0200', 'testdir' ); 156 ExtUtils::Command::chmod(); 157 158 is( ((stat('testdir'))[2] & 07777) & 0700, 159 0200, 'change a dir to write-only' ); 160 161 # change a dir to read-only 162 @ARGV = ( '0400', 'testdir' ); 163 ExtUtils::Command::chmod(); 164 165 is( ((stat('testdir'))[2] & 07777) & 0700, 166 0400, 'change a dir to read-only' ); 167 168 # remove the dir we've been playing with 169 @ARGV = ('testdir'); 170 rm_rf; 171 ok( ! -e 'testdir', 'rm_rf can delete a read-only dir' ); 172 } 173 174 175 # mkpath 176 my $test_dir = File::Spec->join( 'ecmddir', 'temp2' ); 177 @ARGV = ( $test_dir ); 178 ok( ! -e $ARGV[0], 'temp directory not there yet' ); 179 is( test_d(), 1, 'testing non-existent directory' ); 180 181 @ARGV = ( $test_dir ); 182 mkpath(); 183 ok( -e $ARGV[0], 'temp directory created' ); 184 is( test_d(), 0, 'testing existing dir' ); 185 186 @ARGV = ( $test_dir ); 187 # copy a file to a nested subdirectory 188 unshift @ARGV, $Testfile; 189 @orig_argv = @ARGV; 190 cp(); 191 is_deeply( \@ARGV, \@orig_argv, 'cp preserves @ARGV' ); 192 193 ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' ); 194 195 # cp should croak if destination isn't directory (not a great warning) 196 @ARGV = ( $Testfile ) x 3; 197 eval { cp() }; 198 199 like( $@, qr/Too many arguments/, 'cp croaks on error' ); 200 201 # move a file to a subdirectory 202 @ARGV = ( $Testfile, 'ecmddir' ); 203 @orig_argv = @ARGV; 204 ok( mv() ); 205 is_deeply( \@ARGV, \@orig_argv, 'mv preserves @ARGV' ); 206 207 ok( ! -e $Testfile, 'moved file away' ); 208 ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' ); 209 210 # mv should also croak with the same wacky warning 211 @ARGV = ( $Testfile ) x 3; 212 213 eval { mv() }; 214 like( $@, qr/Too many arguments/, 'mv croaks on error' ); 215 216 # Test expand_wildcards() 217 { 218 my $file = $Testfile; 219 @ARGV = (); 220 chdir 'ecmddir'; 221 222 # % means 'match one character' on VMS. Everything else is ? 223 my $match_char = $^O eq 'VMS' ? '%' : '?'; 224 ($ARGV[0] = $file) =~ s/.\z/$match_char/; 225 226 # this should find the file 227 ExtUtils::Command::expand_wildcards(); 228 229 is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' ); 230 231 # try it with the asterisk now 232 ($ARGV[0] = $file) =~ s/.{3}\z/\*/; 233 ExtUtils::Command::expand_wildcards(); 234 235 is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' ); 236 237 chdir File::Spec->updir; 238 } 239 240 # remove some files 241 my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ), 242 File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) ); 243 rm_f(); 244 245 ok( ! -e $_, "removed $_ successfully" ) for (@ARGV); 246 247 # rm_f dir 248 @ARGV = my $dir = File::Spec->catfile( 'ecmddir' ); 249 rm_rf(); 250 ok( ! -e $dir, "removed $dir successfully" ); 251} 252 253{ 254 { local @ARGV = 'd2utest'; mkpath; } 255 open(FILE, '>d2utest/foo'); 256 binmode(FILE); 257 print FILE "stuff\015\012and thing\015\012"; 258 close FILE; 259 260 open(FILE, '>d2utest/bar'); 261 binmode(FILE); 262 my $bin = "\c@\c@\c@\c@\c@\c@\cA\c@\c@\c@\015\012". 263 "\@\c@\cA\c@\c@\c@8__LIN\015\012"; 264 print FILE $bin; 265 close FILE; 266 267 local @ARGV = 'd2utest'; 268 ExtUtils::Command::dos2unix(); 269 270 open(FILE, 'd2utest/foo'); 271 is( join('', <FILE>), "stuff\012and thing\012", 'dos2unix' ); 272 close FILE; 273 274 open(FILE, 'd2utest/bar'); 275 binmode(FILE); 276 ok( -B 'd2utest/bar' ); 277 is( join('', <FILE>), $bin, 'dos2unix preserves binaries'); 278 close FILE; 279} 280 281END { 282 1 while unlink $Testfile, 'newfile'; 283 File::Path::rmtree( 'ecmddir' ) if -e 'ecmddir'; 284 File::Path::rmtree( 'd2utest' ) if -e 'd2utest'; 285} 286