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 $_[0] };
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 shell false
57 @ARGV = ( $Testfile );
58 ok( test_f(), 'testing non-existent file' );
60 # these are destructive, have to keep setting @ARGV
61 @ARGV = ( $Testfile );
64 @ARGV = ( $Testfile );
65 ok( !test_f(), 'now creating that file' );
66 is_deeply( \@ARGV, [$Testfile], 'test_f preserves @ARGV' );
68 @ARGV = ( $Testfile );
69 ok( -e $ARGV[0], 'created!' );
72 utime ($now, $now, $ARGV[0]);
75 # Just checking modify time stamp, access time stamp is set
76 # to the beginning of the day in Win95.
77 # There's a small chance of a 1 second flutter here.
78 my $stamp = (stat($ARGV[0]))[9];
79 cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) ||
80 diag "mtime == $stamp, should be $now";
85 my $new_stamp = (stat('newfile'))[9];
86 cmp_ok( abs($new_stamp - $stamp), '>=', 2, 'newer file created' );
88 @ARGV = ('newfile', $Testfile);
91 $stamp = (stat($Testfile))[9];
92 cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' );
94 # eqtime use to clear the contents of the file being equalized!
95 open(FILE, ">>$Testfile") || die $!;
99 @ARGV = ('newfile', $Testfile);
101 ok( -s $Testfile, "eqtime doesn't clear the file being equalized" );
104 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
105 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
108 skip( "different file permission semantics on $^O", 4);
111 # change a file to execute-only
112 @ARGV = ( '0100', $Testfile );
113 ExtUtils::Command::chmod();
115 is( ((stat($Testfile))[2] & 07777) & 0700,
116 0100, 'change a file to execute-only' );
118 # change a file to read-only
119 @ARGV = ( '0400', $Testfile );
120 ExtUtils::Command::chmod();
122 is( ((stat($Testfile))[2] & 07777) & 0700,
123 ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
125 # change a file to write-only
126 @ARGV = ( '0200', $Testfile );
127 ExtUtils::Command::chmod();
129 is( ((stat($Testfile))[2] & 07777) & 0700,
130 ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
133 # change a file to read-write
134 @ARGV = ( '0600', $Testfile );
135 my @orig_argv = @ARGV;
136 ExtUtils::Command::chmod();
137 is_deeply( \@ARGV, \@orig_argv, 'chmod preserves @ARGV' );
139 is( ((stat($Testfile))[2] & 07777) & 0700,
140 ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
144 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
145 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
148 skip( "different file permission semantics on $^O", 4);
155 # change a dir to execute-only
156 @ARGV = ( '0100', 'testdir' );
157 ExtUtils::Command::chmod();
159 is( ((stat('testdir'))[2] & 07777) & 0700,
160 0100, 'change a dir to execute-only' );
162 # change a dir to write-only
163 @ARGV = ( '0200', 'testdir' );
164 ExtUtils::Command::chmod();
166 is( ((stat('testdir'))[2] & 07777) & 0700,
167 ($^O eq 'vos' ? 0700 : 0200), 'change a dir to write-only' );
169 # change a dir to read-only
170 @ARGV = ( '0400', 'testdir' );
171 ExtUtils::Command::chmod();
173 is( ((stat('testdir'))[2] & 07777) & 0700,
174 ($^O eq 'vos' ? 0500 : 0400), 'change a dir to read-only' );
178 ok( ! -e 'testdir', 'rm_rf can delete a read-only dir' );
183 @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
184 ok( ! -e $ARGV[0], 'temp directory not there yet' );
187 ok( -e $ARGV[0], 'temp directory created' );
189 # copy a file to a nested subdirectory
190 unshift @ARGV, $Testfile;
193 is_deeply( \@ARGV, \@orig_argv, 'cp preserves @ARGV' );
195 ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' );
197 # cp should croak if destination isn't directory (not a great warning)
198 @ARGV = ( $Testfile ) x 3;
201 like( $@, qr/Too many arguments/, 'cp croaks on error' );
203 # move a file to a subdirectory
204 @ARGV = ( $Testfile, 'ecmddir' );
207 is_deeply( \@ARGV, \@orig_argv, 'mv preserves @ARGV' );
209 ok( ! -e $Testfile, 'moved file away' );
210 ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' );
212 # mv should also croak with the same wacky warning
213 @ARGV = ( $Testfile ) x 3;
216 like( $@, qr/Too many arguments/, 'mv croaks on error' );
218 # Test expand_wildcards()
220 my $file = $Testfile;
224 # % means 'match one character' on VMS. Everything else is ?
225 my $match_char = $^O eq 'VMS' ? '%' : '?';
226 ($ARGV[0] = $file) =~ s/.\z/$match_char/;
228 # this should find the file
229 ExtUtils::Command::expand_wildcards();
231 is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' );
233 # try it with the asterisk now
234 ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
235 ExtUtils::Command::expand_wildcards();
237 is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' );
239 chdir File::Spec->updir;
243 my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ),
244 File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) );
247 ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
250 @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
252 ok( ! -e $dir, "removed $dir successfully" );
256 { local @ARGV = 'd2utest'; mkpath; }
257 open(FILE, '>d2utest/foo');
258 print FILE "stuff\015\012and thing\015\012";
261 open(FILE, '>d2utest/bar');
263 my $bin = "\c@\c@\c@\c@\c@\c@\cA\c@\c@\c@\015\012".
264 "\@\c@\cA\c@\c@\c@8__LIN\015\012";
268 local @ARGV = 'd2utest';
269 ExtUtils::Command::dos2unix();
271 open(FILE, 'd2utest/foo');
272 is( join('', <FILE>), "stuff\012and thing\012", 'dos2unix' );
275 open(FILE, 'd2utest/bar');
277 ok( -B 'd2utest/bar' );
278 is( join('', <FILE>), $bin, 'dos2unix preserves binaries');
283 1 while unlink $Testfile, 'newfile';
284 File::Path::rmtree( 'ecmddir' );
285 File::Path::rmtree( 'd2utest' );