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