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