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