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