OS2 patches
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Command.t
CommitLineData
39234879 1#!/usr/bin/perl -w
e38fdfdb 2
3BEGIN {
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 12chdir 't';
8f78c13d 13
14BEGIN {
479d2113 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' );
e38fdfdb 19}
20
8f78c13d 21BEGIN {
479d2113 22 use Test::More tests => 27;
23 use File::Spec;
8f78c13d 24}
e38fdfdb 25
479d2113 26BEGIN {
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}
e38fdfdb 32
479d2113 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 }
e38fdfdb 45
57b1a898 46
47 # % means 'match one character' on VMS. Everything else is ?
48 my $match_char = $^O eq 'VMS' ? '%' : '?';
479d2113 49 ($ARGV[0] = $file) =~ s/.\z/$match_char/;
57b1a898 50
479d2113 51 # this should find the file
52 ExtUtils::Command::expand_wildcards();
e38fdfdb 53
479d2113 54 is( scalar @ARGV, 1, 'found one file' );
55 like( $ARGV[0], qr/$file/, 'expanded wildcard ? successfully' );
e38fdfdb 56
479d2113 57 # try it with the asterisk now
58 ($ARGV[0] = $file) =~ s/.{3}\z/\*/;
59 ExtUtils::Command::expand_wildcards();
e38fdfdb 60
479d2113 61 ok( (grep { qr/$file/ } @ARGV), 'expanded wildcard * successfully' );
e38fdfdb 62
479d2113 63 # concatenate this file with itself
64 # be extra careful the regex doesn't match itself
39234879 65 use TieOut;
479d2113 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]);
5cff3c2c 100 sleep 2;
851f5327 101
479d2113 102 # Just checking modify time stamp, access time stamp is set
103 # to the beginning of the day in Win95.
5cff3c2c 104 # There's a small chance of a 1 second flutter here.
105 my $stamp = (stat($ARGV[0]))[9];
479d2113 106 cmp_ok( abs($now - $stamp), '<=', 1, 'checking modify time stamp' ) ||
57b1a898 107 diag "mtime == $stamp, should be $now";
e38fdfdb 108
479d2113 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
f6d6199c 130 SKIP: {
131 if ($^O eq 'amigaos' || $^O eq 'os2' || $^O eq 'MSWin32' ||
d5201bd2 132 $^O eq 'NetWare' || $^O eq 'dos' || $^O eq 'cygwin' ||
d5d4ec93 133 $^O eq 'MacOS'
134 ) {
f6d6199c 135 skip( "different file permission semantics on $^O", 3);
136 }
388296f8 137
f6d6199c 138 # change a file to execute-only
479d2113 139 @ARGV = ( '0100', 'ecmdfile' );
f6d6199c 140 ExtUtils::Command::chmod();
388296f8 141
f6d6199c 142 is( ((stat('ecmdfile'))[2] & 07777) & 0700,
143 0100, 'change a file to execute-only' );
388296f8 144
f6d6199c 145 # change a file to read-only
479d2113 146 @ARGV = ( '0400', 'ecmdfile' );
f6d6199c 147 ExtUtils::Command::chmod();
388296f8 148
f6d6199c 149 is( ((stat('ecmdfile'))[2] & 07777) & 0700,
150 ($^O eq 'vos' ? 0500 : 0400), 'change a file to read-only' );
388296f8 151
f6d6199c 152 # change a file to write-only
479d2113 153 @ARGV = ( '0200', 'ecmdfile' );
f6d6199c 154 ExtUtils::Command::chmod();
388296f8 155
f6d6199c 156 is( ((stat('ecmdfile'))[2] & 07777) & 0700,
157 ($^O eq 'vos' ? 0700 : 0200), 'change a file to write-only' );
158 }
388296f8 159
f6d6199c 160 # change a file to read-write
479d2113 161 @ARGV = ( '0600', 'ecmdfile' );
162 ExtUtils::Command::chmod();
e38fdfdb 163
f6d6199c 164 is( ((stat('ecmdfile'))[2] & 07777) & 0700,
165 ($^O eq 'vos' ? 0700 : 0600), 'change a file to read-write' );
e38fdfdb 166
479d2113 167 # mkpath
168 @ARGV = ( File::Spec->join( 'ecmddir', 'temp2' ) );
169 ok( ! -e $ARGV[0], 'temp directory not there yet' );
e38fdfdb 170
479d2113 171 mkpath();
172 ok( -e $ARGV[0], 'temp directory created' );
e38fdfdb 173
479d2113 174 # copy a file to a nested subdirectory
175 unshift @ARGV, 'ecmdfile';
176 cp();
e38fdfdb 177
479d2113 178 ok( -e File::Spec->join( 'ecmddir', 'temp2', 'ecmdfile' ), 'copied okay' );
e38fdfdb 179
479d2113 180 # cp should croak if destination isn't directory (not a great warning)
181 @ARGV = ( 'ecmdfile' ) x 3;
182 eval { cp() };
e38fdfdb 183
479d2113 184 like( $@, qr/Too many arguments/, 'cp croaks on error' );
e38fdfdb 185
479d2113 186 # move a file to a subdirectory
187 @ARGV = ( 'ecmdfile', 'ecmddir' );
188 mv();
e38fdfdb 189
479d2113 190 ok( ! -e 'ecmdfile', 'moved file away' );
191 ok( -e File::Spec->join( 'ecmddir', 'ecmdfile' ), 'file in new location' );
e38fdfdb 192
479d2113 193 # mv should also croak with the same wacky warning
194 @ARGV = ( 'ecmdfile' ) x 3;
e38fdfdb 195
479d2113 196 eval { mv() };
197 like( $@, qr/Too many arguments/, 'mv croaks on error' );
e38fdfdb 198
479d2113 199 # remove some files
200 my @files = @ARGV = ( File::Spec->catfile( 'ecmddir', 'ecmdfile' ),
201 File::Spec->catfile( 'ecmddir', 'temp2', 'ecmdfile' ) );
202 rm_f();
e38fdfdb 203
479d2113 204 ok( ! -e $_, "removed $_ successfully" ) for (@ARGV);
e38fdfdb 205
479d2113 206 # rm_f dir
207 @ARGV = my $dir = File::Spec->catfile( 'ecmddir' );
208 rm_rf();
209 ok( ! -e $dir, "removed $dir successfully" );
e38fdfdb 210}
211
212END {
479d2113 213 1 while unlink 'ecmdfile', 'newfile';
214 File::Path::rmtree( 'ecmddir' );
e38fdfdb 215}