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