The change #20022 didn't work for Command.t.
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / 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     1 while unlink 'ecmdfile', 'newfile';
16     # forcibly remove ecmddir/temp2, but don't import mkpath
17     use File::Path ();
18     File::Path::rmtree( 'ecmddir' );
19 }
20
21 BEGIN {
22     use Test::More tests => 27;
23     use File::Spec;
24 }
25
26 BEGIN {
27     # bad neighbor, but test_f() uses exit()
28         *CORE::GLOBAL::exit = '';   # quiet 'only once' warning.
29     *CORE::GLOBAL::exit = sub { return @_ };
30     use_ok( 'ExtUtils::Command' );
31 }
32
33 {
34     # get a file in the current directory, replace last char with wildcard 
35     my $file;
36     {
37         local *DIR;
38         opendir(DIR, File::Spec->curdir());
39         while ($file = readdir(DIR)) {
40             $file =~ s/\.\z// if $^O eq 'VMS';
41             last if $file =~ /^\w/;
42         }
43         closedir DIR;
44     }
45
46
47     # % means 'match one character' on VMS.  Everything else is ?
48     my $match_char = $^O eq 'VMS' ? '%' : '?';
49     ($ARGV[0] = $file) =~ s/.\z/$match_char/;
50
51     # this should find the file
52     ExtUtils::Command::expand_wildcards();
53
54     is( scalar @ARGV, 1, 'found one file' );
55     like( $ARGV[0], qr/$file/, 'expanded wildcard ? successfully' );
56
57     # try it with the asterisk now
58     ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
59     ExtUtils::Command::expand_wildcards();
60
61     ok( (grep { qr/$file/ } @ARGV), 'expanded wildcard * successfully' );
62
63     # concatenate this file with itself
64     # be extra careful the regex doesn't match itself
65     use TieOut;
66     my $out = tie *STDOUT, 'TieOut';
67     my $self = $0;
68     unless (-f $self) {
69         my ($vol, $dirs, $file) = File::Spec->splitpath($self);
70         my @dirs = File::Spec->splitdir($dirs);
71         unshift(@dirs, File::Spec->updir);
72         $dirs = File::Spec->catdir(@dirs);
73         $self = File::Spec->catpath($vol, $dirs, $file);
74     }
75     @ARGV = ($self, $self);
76
77     cat();
78     is( scalar( $$out =~ s/use_ok\( 'ExtUtils::Command'//g), 2, 
79         'concatenation worked' );
80
81     # the truth value here is reversed -- Perl true is C false
82     @ARGV = ( 'ecmdfile' );
83     ok( test_f(), 'testing non-existent file' );
84
85     @ARGV = ( 'ecmdfile' );
86     cmp_ok( ! test_f(), '==', (-f 'ecmdfile'), 'testing non-existent file' );
87
88     # these are destructive, have to keep setting @ARGV
89     @ARGV = ( 'ecmdfile' );
90     touch();
91
92     @ARGV = ( 'ecmdfile' );
93     ok( test_f(), 'now creating that file' );
94
95     @ARGV = ( 'ecmdfile' );
96     ok( -e $ARGV[0], 'created!' );
97
98     my ($now) = time;
99     utime ($now, $now, $ARGV[0]);
100     sleep 2;
101
102     # Just checking modify time stamp, access time stamp is set
103     # to the beginning of the day in Win95.
104     # There's a small chance of a 1 second flutter here.
105     my $stamp = (stat($ARGV[0]))[9];
106     cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) ||
107       diag "mtime == $stamp, should be $now";
108
109     @ARGV = qw(newfile);
110     touch();
111
112     my $new_stamp = (stat('newfile'))[9];
113     cmp_ok( abs($new_stamp - $stamp), '>=', 2,  'newer file created' );
114
115     @ARGV = qw(newfile ecmdfile);
116     eqtime();
117
118     $stamp = (stat('ecmdfile'))[9];
119     cmp_ok( abs($new_stamp - $stamp), '<=', 1, 'eqtime' );
120
121     # eqtime use to clear the contents of the file being equalized!
122     open(FILE, '>>ecmdfile') || die $!;
123     print FILE "Foo";
124     close FILE;
125
126     @ARGV = qw(newfile ecmdfile);
127     eqtime();
128     ok( -s 'ecmdfile', "eqtime doesn't clear the file being equalized" );
129
130     SKIP: {
131         if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
132             $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin'  ||
133             $^O eq 'MacOS'
134            ) {
135             skip( "different file permission semantics on $^O", 3);
136         }
137
138         # change a file to execute-only
139         @ARGV = ( '0100', 'ecmdfile' );
140         ExtUtils::Command::chmod();
141
142         is( ((stat('ecmdfile'))[2] & 07777) & 0700,
143             0100, 'change a file to execute-only' );
144
145         # change a file to read-only
146         @ARGV = ( '0400', 'ecmdfile' );
147         ExtUtils::Command::chmod();
148
149         is( ((stat('ecmdfile'))[2] & 07777) & 0700,
150             ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
151
152         # change a file to write-only
153         @ARGV = ( '0200', 'ecmdfile' );
154         ExtUtils::Command::chmod();
155
156         is( ((stat('ecmdfile'))[2] & 07777) & 0700,
157             ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
158     }
159
160     # change a file to read-write
161     @ARGV = ( '0600', 'ecmdfile' );
162     ExtUtils::Command::chmod();
163
164     is( ((stat('ecmdfile'))[2] & 07777) & 0700,
165         ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
166
167     # mkpath
168     @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
169     ok( ! -e $ARGV[0], 'temp directory not there yet' );
170
171     mkpath();
172     ok( -e $ARGV[0], 'temp directory created' );
173
174     # copy a file to a nested subdirectory
175     unshift @ARGV, 'ecmdfile';
176     cp();
177
178     ok( -e File::Spec->join( 'ecmddir', 'temp2', 'ecmdfile' ), 'copied okay' );
179
180     # cp should croak if destination isn't directory (not a great warning)
181     @ARGV = ( 'ecmdfile' ) x 3;
182     eval { cp() };
183
184     like( $@, qr/Too many arguments/, 'cp croaks on error' );
185
186     # move a file to a subdirectory
187     @ARGV = ( 'ecmdfile', 'ecmddir' );
188     mv();
189
190     ok( ! -e 'ecmdfile', 'moved file away' );
191     ok( -e File::Spec->join( 'ecmddir', 'ecmdfile' ), 'file in new location' );
192
193     # mv should also croak with the same wacky warning
194     @ARGV = ( 'ecmdfile' ) x 3;
195
196     eval { mv() };
197     like( $@, qr/Too many arguments/, 'mv croaks on error' );
198
199     # remove some files
200     my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', 'ecmdfile' ),
201     File::Spec->catfile( 'ecmddir', 'temp2', 'ecmdfile' ) );
202     rm_f();
203
204     ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
205
206     # rm_f dir
207     @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
208     rm_rf();
209     ok( ! -e $dir, "removed $dir successfully" );
210 }
211
212 END {
213     1 while unlink 'ecmdfile', 'newfile';
214     File::Path::rmtree( 'ecmddir' );
215 }