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