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