4 if( $ENV{PERL_CORE} ) {
6 @INC = ('../lib', 'lib/');
9 unshift @INC, 't/lib/';
15 $Testfile = 'testfile.foo';
19 1 while unlink $Testfile, 'newfile';
20 # forcibly remove ecmddir/temp2, but don't import mkpath
22 File::Path::rmtree( 'ecmddir' );
26 use Test::More tests => 38;
31 # bad neighbor, but test_f() uses exit()
32 *CORE::GLOBAL::exit = ''; # quiet 'only once' warning.
33 *CORE::GLOBAL::exit = sub { return @_ };
34 use_ok( 'ExtUtils::Command' );
38 # concatenate this file with itself
39 # be extra careful the regex doesn't match itself
41 my $out = tie *STDOUT, 'TieOut';
44 my ($vol, $dirs, $file) = File::Spec->splitpath($self);
45 my @dirs = File::Spec->splitdir($dirs);
46 unshift(@dirs, File::Spec->updir);
47 $dirs = File::Spec->catdir(@dirs);
48 $self = File::Spec->catpath($vol, $dirs, $file);
50 @ARGV = ($self, $self);
53 is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2,
54 'concatenation worked' );
56 # the truth value here is reversed -- Perl true is C false
57 @ARGV = ( $Testfile );
58 ok( test_f(), 'testing non-existent file' );
60 @ARGV = ( $Testfile );
61 cmp_ok( ! test_f(), '==', (-f $Testfile), 'testing non-existent file' );
63 # these are destructive, have to keep setting @ARGV
64 @ARGV = ( $Testfile );
67 @ARGV = ( $Testfile );
68 ok( test_f(), 'now creating that file' );
69 is_deeply( \@ARGV, [$Testfile], 'test_f preserves @ARGV' );
71 @ARGV = ( $Testfile );
72 ok( -e $ARGV[0], 'created!' );
75 utime ($now, $now, $ARGV[0]);
78 # Just checking modify time stamp, access time stamp is set
79 # to the beginning of the day in Win95.
80 # There's a small chance of a 1 second flutter here.
81 my $stamp = (stat($ARGV[0]))[9];
82 cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) ||
83 diag "mtime == $stamp, should be $now";
88 my $new_stamp = (stat('newfile'))[9];
89 cmp_ok( abs($new_stamp - $stamp), '>=', 2, 'newer file created' );
91 @ARGV = ('newfile', $Testfile);
94 $stamp = (stat($Testfile))[9];
95 cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' );
97 # eqtime use to clear the contents of the file being equalized!
98 open(FILE, ">>$Testfile") || die $!;
102 @ARGV = ('newfile', $Testfile);
104 ok( -s $Testfile, "eqtime doesn't clear the file being equalized" );
107 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
108 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
111 skip( "different file permission semantics on $^O", 3);
114 # change a file to execute-only
115 @ARGV = ( '0100', $Testfile );
116 ExtUtils::Command::chmod();
118 is( ((stat($Testfile))[2] & 07777) & 0700,
119 0100, 'change a file to execute-only' );
121 # change a file to read-only
122 @ARGV = ( '0400', $Testfile );
123 ExtUtils::Command::chmod();
125 is( ((stat($Testfile))[2] & 07777) & 0700,
126 ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
128 # change a file to write-only
129 @ARGV = ( '0200', $Testfile );
130 ExtUtils::Command::chmod();
132 is( ((stat($Testfile))[2] & 07777) & 0700,
133 ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
136 # change a file to read-write
137 @ARGV = ( '0600', $Testfile );
138 my @orig_argv = @ARGV;
139 ExtUtils::Command::chmod();
140 is_deeply( \@ARGV, \@orig_argv, 'chmod preserves @ARGV' );
142 is( ((stat($Testfile))[2] & 07777) & 0700,
143 ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
147 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
148 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
151 skip( "different file permission semantics on $^O", 4);
158 # change a dir to execute-only
159 @ARGV = ( '0100', 'testdir' );
160 ExtUtils::Command::chmod();
162 is( ((stat('testdir'))[2] & 07777) & 0700,
163 0100, 'change a dir to execute-only' );
165 # change a dir to read-only
166 @ARGV = ( '0400', 'testdir' );
167 ExtUtils::Command::chmod();
169 is( ((stat('testdir'))[2] & 07777) & 0700,
170 ($^O eq 'vos' ? 0500 : 0400), 'change a dir to read-only' );
172 # change a dir to write-only
173 @ARGV = ( '0200', 'testdir' );
174 ExtUtils::Command::chmod();
176 is( ((stat('testdir'))[2] & 07777) & 0700,
177 ($^O eq 'vos' ? 0700 : 0200), 'change a dir to write-only' );
185 @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
186 ok( ! -e $ARGV[0], 'temp directory not there yet' );
189 ok( -e $ARGV[0], 'temp directory created' );
191 # copy a file to a nested subdirectory
192 unshift @ARGV, $Testfile;
195 is_deeply( \@ARGV, \@orig_argv, 'cp preserves @ARGV' );
197 ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' );
199 # cp should croak if destination isn't directory (not a great warning)
200 @ARGV = ( $Testfile ) x 3;
203 like( $@, qr/Too many arguments/, 'cp croaks on error' );
205 # move a file to a subdirectory
206 @ARGV = ( $Testfile, 'ecmddir' );
209 is_deeply( \@ARGV, \@orig_argv, 'mv preserves @ARGV' );
211 ok( ! -e $Testfile, 'moved file away' );
212 ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' );
214 # mv should also croak with the same wacky warning
215 @ARGV = ( $Testfile ) x 3;
218 like( $@, qr/Too many arguments/, 'mv croaks on error' );
220 # Test expand_wildcards()
222 my $file = $Testfile;
226 # % means 'match one character' on VMS. Everything else is ?
227 my $match_char = $^O eq 'VMS' ? '%' : '?';
228 ($ARGV[0] = $file) =~ s/.\z/$match_char/;
230 # this should find the file
231 ExtUtils::Command::expand_wildcards();
233 is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' );
235 # try it with the asterisk now
236 ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
237 ExtUtils::Command::expand_wildcards();
239 is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' );
241 chdir File::Spec->updir;
245 my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ),
246 File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) );
249 ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
252 @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
254 ok( ! -e $dir, "removed $dir successfully" );
258 { local @ARGV = 'd2utest'; mkpath; }
259 open(FILE, '>d2utest/foo');
260 print FILE "stuff\015\012and thing\015\012";
263 open(FILE, '>d2utest/bar');
265 my $bin = "\c@\c@\c@\c@\c@\c@\cA\c@\c@\c@\015\012".
266 "\@\c@\cA\c@\c@\c@8__LIN\015\012";
270 local @ARGV = 'd2utest';
271 ExtUtils::Command::dos2unix();
273 open(FILE, 'd2utest/foo');
274 is( join('', <FILE>), "stuff\012and thing\012", 'dos2unix' );
277 open(FILE, 'd2utest/bar');
279 ok( -B 'd2utest/bar' );
280 is( join('', <FILE>), $bin, 'dos2unix preserves binaries');
285 1 while unlink $Testfile, 'newfile';
286 File::Path::rmtree( 'ecmddir' );
287 File::Path::rmtree( 'd2utest' );