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