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