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' );
25 use Test::More tests => 40;
29 # bad neighbor, but test_f() uses exit()
30 *CORE::GLOBAL::exit = ''; # quiet 'only once' warning.
31 *CORE::GLOBAL::exit = sub (;$) { return $_[0] };
32 use_ok( 'ExtUtils::Command' );
36 # concatenate this file with itself
37 # be extra careful the regex doesn't match itself
39 my $out = tie *STDOUT, 'TieOut';
42 my ($vol, $dirs, $file) = File::Spec->splitpath($self);
43 my @dirs = File::Spec->splitdir($dirs);
44 unshift(@dirs, File::Spec->updir);
45 $dirs = File::Spec->catdir(@dirs);
46 $self = File::Spec->catpath($vol, $dirs, $file);
48 @ARGV = ($self, $self);
51 is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2,
52 'concatenation worked' );
54 # the truth value here is reversed -- Perl true is shell false
55 @ARGV = ( $Testfile );
56 is( test_f(), 1, 'testing non-existent file' );
58 # these are destructive, have to keep setting @ARGV
59 @ARGV = ( $Testfile );
62 @ARGV = ( $Testfile );
63 is( test_f(), 0, 'testing touch() and test_f()' );
64 is_deeply( \@ARGV, [$Testfile], 'test_f preserves @ARGV' );
66 @ARGV = ( $Testfile );
67 ok( -e $ARGV[0], 'created!' );
70 utime ($now, $now, $ARGV[0]);
73 # Just checking modify time stamp, access time stamp is set
74 # to the beginning of the day in Win95.
75 # There's a small chance of a 1 second flutter here.
76 my $stamp = (stat($ARGV[0]))[9];
77 cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) ||
78 diag "mtime == $stamp, should be $now";
83 my $new_stamp = (stat('newfile'))[9];
84 cmp_ok( abs($new_stamp - $stamp), '>=', 2, 'newer file created' );
86 @ARGV = ('newfile', $Testfile);
89 $stamp = (stat($Testfile))[9];
90 cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' );
92 # eqtime use to clear the contents of the file being equalized!
93 open(FILE, ">>$Testfile") || die $!;
97 @ARGV = ('newfile', $Testfile);
99 ok( -s $Testfile, "eqtime doesn't clear the file being equalized" );
102 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
103 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
106 skip( "different file permission semantics on $^O", 3);
109 # change a file to execute-only
110 @ARGV = ( '0100', $Testfile );
111 ExtUtils::Command::chmod();
113 is( ((stat($Testfile))[2] & 07777) & 0700,
114 0100, 'change a file to execute-only' );
116 # change a file to read-only
117 @ARGV = ( '0400', $Testfile );
118 ExtUtils::Command::chmod();
120 is( ((stat($Testfile))[2] & 07777) & 0700,
121 ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
123 # change a file to write-only
124 @ARGV = ( '0200', $Testfile );
125 ExtUtils::Command::chmod();
127 is( ((stat($Testfile))[2] & 07777) & 0700,
128 ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
131 # change a file to read-write
132 @ARGV = ( '0600', $Testfile );
133 my @orig_argv = @ARGV;
134 ExtUtils::Command::chmod();
135 is_deeply( \@ARGV, \@orig_argv, 'chmod preserves @ARGV' );
137 is( ((stat($Testfile))[2] & 07777) & 0700,
138 ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
142 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
143 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
146 skip( "different file permission semantics on $^O", 5);
153 # change a dir to execute-only
154 @ARGV = ( '0100', 'testdir' );
155 ExtUtils::Command::chmod();
157 is( ((stat('testdir'))[2] & 07777) & 0700,
158 0100, 'change a dir to execute-only' );
160 # change a dir to read-only
161 @ARGV = ( '0400', 'testdir' );
162 ExtUtils::Command::chmod();
164 is( ((stat('testdir'))[2] & 07777) & 0700,
165 ($^O eq 'vos' ? 0500 : 0400), 'change a dir to read-only' );
167 # change a dir to write-only
168 @ARGV = ( '0200', 'testdir' );
169 ExtUtils::Command::chmod();
171 is( ((stat('testdir'))[2] & 07777) & 0700,
172 ($^O eq 'vos' ? 0700 : 0200), 'change a dir to write-only' );
176 ok( ! -e 'testdir', 'rm_rf can delete a read-only dir' );
181 my $test_dir = File::Spec->join( 'ecmddir', 'temp2' );
182 @ARGV = ( $test_dir );
183 ok( ! -e $ARGV[0], 'temp directory not there yet' );
184 is( test_d(), 1, 'testing non-existent directory' );
186 @ARGV = ( $test_dir );
188 ok( -e $ARGV[0], 'temp directory created' );
189 is( test_d(), 0, 'testing existing dir' );
191 @ARGV = ( $test_dir );
192 # copy a file to a nested subdirectory
193 unshift @ARGV, $Testfile;
196 is_deeply( \@ARGV, \@orig_argv, 'cp preserves @ARGV' );
198 ok( -e File::Spec->join( 'ecmddir', 'temp2', $Testfile ), 'copied okay' );
200 # cp should croak if destination isn't directory (not a great warning)
201 @ARGV = ( $Testfile ) x 3;
204 like( $@, qr/Too many arguments/, 'cp croaks on error' );
206 # move a file to a subdirectory
207 @ARGV = ( $Testfile, 'ecmddir' );
210 is_deeply( \@ARGV, \@orig_argv, 'mv preserves @ARGV' );
212 ok( ! -e $Testfile, 'moved file away' );
213 ok( -e File::Spec->join( 'ecmddir', $Testfile ), 'file in new location' );
215 # mv should also croak with the same wacky warning
216 @ARGV = ( $Testfile ) x 3;
219 like( $@, qr/Too many arguments/, 'mv croaks on error' );
221 # Test expand_wildcards()
223 my $file = $Testfile;
227 # % means 'match one character' on VMS. Everything else is ?
228 my $match_char = $^O eq 'VMS' ? '%' : '?';
229 ($ARGV[0] = $file) =~ s/.\z/$match_char/;
231 # this should find the file
232 ExtUtils::Command::expand_wildcards();
234 is_deeply( \@ARGV, [$file], 'expanded wildcard ? successfully' );
236 # try it with the asterisk now
237 ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
238 ExtUtils::Command::expand_wildcards();
240 is_deeply( \@ARGV, [$file], 'expanded wildcard * successfully' );
242 chdir File::Spec->updir;
246 my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', $Testfile ),
247 File::Spec->catfile( 'ecmddir', 'temp2', $Testfile ) );
250 ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
253 @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
255 ok( ! -e $dir, "removed $dir successfully" );
259 { local @ARGV = 'd2utest'; mkpath; }
260 open(FILE, '>d2utest/foo');
262 print FILE "stuff\015\012and thing\015\012";
265 open(FILE, '>d2utest/bar');
267 my $bin = "\c@\c@\c@\c@\c@\c@\cA\c@\c@\c@\015\012".
268 "\@\c@\cA\c@\c@\c@8__LIN\015\012";
272 local @ARGV = 'd2utest';
273 ExtUtils::Command::dos2unix();
275 open(FILE, 'd2utest/foo');
276 is( join('', <FILE>), "stuff\012and thing\012", 'dos2unix' );
279 open(FILE, 'd2utest/bar');
281 ok( -B 'd2utest/bar' );
282 is( join('', <FILE>), $bin, 'dos2unix preserves binaries');
287 1 while unlink $Testfile, 'newfile';
288 File::Path::rmtree( 'ecmddir' );
289 File::Path::rmtree( 'd2utest' );