Fix when( scalar ... ) bug
[p5sagit/p5-mst-13.2.git] / cpan / Cwd / t / cwd.t
1 #!./perl -w
2
3 use strict;
4
5 use Cwd;
6
7 chdir 't';
8
9 use Config;
10 use File::Spec;
11 use File::Path;
12
13 use lib File::Spec->catdir('t', 'lib');
14 use Test::More;
15
16 my $IsVMS = $^O eq 'VMS';
17 my $IsMacOS = $^O eq 'MacOS';
18
19 my $vms_unix_rpt = 0;
20 my $vms_efs = 0;
21 my $vms_mode = 0;
22
23 if ($IsVMS) {
24     require VMS::Filespec;
25     use Carp;
26     use Carp::Heavy;
27     $vms_mode = 1;
28     if (eval 'require VMS::Feature') {
29         $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
30         $vms_efs = VMS::Feature::current("efs_charset");
31     } else {
32         my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
33         my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
34         $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i; 
35         $vms_efs = $efs_charset =~ /^[ET1]/i; 
36     }
37     $vms_mode = 0 if ($vms_unix_rpt);
38 }
39
40 my $tests = 30;
41 # _perl_abs_path() currently only works when the directory separator
42 # is '/', so don't test it when it won't work.
43 my $EXTRA_ABSPATH_TESTS = ($Config{prefix} =~ m/\//) && $^O ne 'cygwin';
44 $tests += 4 if $EXTRA_ABSPATH_TESTS;
45 plan tests => $tests;
46
47 SKIP: {
48   skip "no need to check for blib/ in the core", 1 if $ENV{PERL_CORE};
49   like $INC{'Cwd.pm'}, qr{blib}i, "Cwd should be loaded from blib/ during testing";
50 }
51
52
53 # check imports
54 can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
55 ok( !defined(&chdir),           'chdir() not exported by default' );
56 ok( !defined(&abs_path),        '  nor abs_path()' );
57 ok( !defined(&fast_abs_path),   '  nor fast_abs_path()');
58
59 {
60   my @fields = qw(PATH IFS CDPATH ENV BASH_ENV);
61   my $before = grep exists $ENV{$_}, @fields;
62   cwd();
63   my $after = grep exists $ENV{$_}, @fields;
64   is($before, $after, "cwd() shouldn't create spurious entries in %ENV");
65 }
66
67 # XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
68 # XXX and subsequent chdir()s can make them impossible to find
69 eval { fastcwd };
70
71 # Must find an external pwd (or equivalent) command.
72
73 my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
74 my $pwd_cmd =
75     ($^O eq "NetWare") ?
76         "cd" :
77     ($IsMacOS) ?
78         "pwd" :
79         (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
80                            split m/$Config{path_sep}/, $ENV{PATH})[0];
81
82 $pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
83 if ($^O eq 'MSWin32') {
84     $pwd_cmd =~ s,/,\\,g;
85     $pwd_cmd = "$pwd_cmd /c cd";
86 }
87 $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
88
89 SKIP: {
90     skip "No native pwd command found to test against", 4 unless $pwd_cmd;
91
92     print "# native pwd = '$pwd_cmd'\n";
93
94     local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
95     my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
96     chomp(my $start = `$pwd_cmd_untainted`);
97
98     # Win32's cd returns native C:\ style
99     $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
100     if ($IsVMS) {
101         # DCL SHOW DEFAULT has leading spaces
102         $start =~ s/^\s+//;
103
104         # When in UNIX report mode, need to convert to compare it.
105         if ($vms_unix_rpt) {
106             $start = VMS::Filespec::unixpath($start);
107             # Remove trailing slash.
108             $start =~ s#/$##;
109         }
110     }
111     SKIP: {
112         skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
113         skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
114
115         # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which
116         # Cwd.pm:getcwd uses) has some magic related to the PWD
117         # environment variable: if PWD is set to a directory that
118         # looks about right (guess: has the same (dev,ino) as the '.'?),
119         # the PWD is returned.  However, if that path contains
120         # symlinks, the path will not be equal to the one returned by
121         # /bin/pwd (which probably uses the usual walking upwards in
122         # the path -trick).  This situation is easy to reproduce since
123         # /tmp is a symlink to /private/tmp.  Therefore we invalidate
124         # the PWD to force getcwd(3) to (re)compute the cwd in full.
125         # Admittedly fixing this in the Cwd module would be better
126         # long-term solution but deleting $ENV{PWD} should not be
127         # done light-heartedly. --jhi
128         delete $ENV{PWD} if $^O eq 'darwin';
129
130         my $cwd        = cwd;
131         my $getcwd     = getcwd;
132         my $fastcwd    = fastcwd;
133         my $fastgetcwd = fastgetcwd;
134
135         is($cwd,        $start, 'cwd()');
136         is($getcwd,     $start, 'getcwd()');
137         is($fastcwd,    $start, 'fastcwd()');
138         is($fastgetcwd, $start, 'fastgetcwd()');
139     }
140 }
141
142 my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
143 my $Test_Dir     = File::Spec->catdir(@test_dirs);
144
145 mkpath([$Test_Dir], 0, 0777);
146 Cwd::chdir $Test_Dir;
147
148 foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
149   my $result = eval "$func()";
150   is $@, '';
151   dir_ends_with( $result, $Test_Dir, "$func()" );
152 }
153
154 {
155   # Some versions of File::Path (e.g. that shipped with perl 5.8.5)
156   # call getcwd() with an argument (perhaps by calling it as a
157   # method?), so make sure that doesn't die.
158   is getcwd(), getcwd('foo'), "Call getcwd() with an argument";
159 }
160
161 # Cwd::chdir should also update $ENV{PWD}
162 dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
163 my $updir = File::Spec->updir;
164
165 for (1..@test_dirs) {
166   Cwd::chdir $updir;
167   print "#$ENV{PWD}\n";
168 }
169
170 rmtree($test_dirs[0], 0, 0);
171
172 {
173   my $check = ($vms_mode ? qr|\b((?i)t)\]$| :
174                $IsMacOS  ? qr|\bt:$| :
175                            qr|\bt$| );
176   
177   like($ENV{PWD}, $check);
178 }
179
180 {
181   # Make sure abs_path() doesn't trample $ENV{PWD}
182   my $start_pwd = $ENV{PWD};
183   mkpath([$Test_Dir], 0, 0777);
184   Cwd::abs_path($Test_Dir);
185   is $ENV{PWD}, $start_pwd;
186   rmtree($test_dirs[0], 0, 0);
187 }
188
189 SKIP: {
190     skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
191
192     my $file = "linktest";
193     mkpath([$Test_Dir], 0, 0777);
194     symlink $Test_Dir, $file;
195
196     my $abs_path      =  Cwd::abs_path($file);
197     my $fast_abs_path =  Cwd::fast_abs_path($file);
198     my $want          =  quotemeta(
199                            File::Spec->rel2abs( $Test_Dir )
200                          );
201     if ($^O eq 'VMS') {
202        # Not easy to predict the physical volume name
203        $want = $ENV{PERL_CORE} ? $Test_Dir : File::Spec->catdir('t', $Test_Dir);
204
205        # So just use the relative volume name
206        $want =~ s/^\[//;
207
208        $want = quotemeta($want);
209     }
210
211     like($abs_path,      qr|$want$|i);
212     like($fast_abs_path, qr|$want$|i);
213     like(Cwd::_perl_abs_path($file), qr|$want$|i) if $EXTRA_ABSPATH_TESTS;
214
215     rmtree($test_dirs[0], 0, 0);
216     1 while unlink $file;
217 }
218
219 if ($ENV{PERL_CORE}) {
220     chdir '../ext/Cwd/t';
221     unshift @INC, '../../../lib';
222 }
223
224 # Make sure we can run abs_path() on files, not just directories
225 my $path = 'cwd.t';
226 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
227 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
228 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
229   if $EXTRA_ABSPATH_TESTS;
230
231 $path = File::Spec->catfile(File::Spec->updir, 't', $path);
232 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
233 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
234 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
235   if $EXTRA_ABSPATH_TESTS;
236
237
238   
239 SKIP: {
240   my $file;
241   {
242     my $root = Cwd::abs_path(File::Spec->rootdir);      # Add drive letter?
243     local *FH;
244     opendir FH, $root or skip("Can't opendir($root): $!", 2+$EXTRA_ABSPATH_TESTS);
245     ($file) = grep {-f $_ and not -l $_} map File::Spec->catfile($root, $_), readdir FH;
246     closedir FH;
247   }
248   skip "No plain file in root directory to test with", 2+$EXTRA_ABSPATH_TESTS unless $file;
249   
250   $file = VMS::Filespec::rmsexpand($file) if $^O eq 'VMS';
251   is Cwd::abs_path($file), $file, 'abs_path() works on files in the root directory';
252   is Cwd::fast_abs_path($file), $file, 'fast_abs_path() works on files in the root directory';
253   is Cwd::_perl_abs_path($file), $file, '_perl_abs_path() works on files in the root directory'
254     if $EXTRA_ABSPATH_TESTS;
255 }
256
257
258 #############################################
259 # These routines give us sort of a poor-man's cross-platform
260 # directory or path comparison capability.
261
262 sub bracketed_form_dir {
263   return join '', map "[$_]", 
264     grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
265 }
266
267 sub dir_ends_with {
268   my ($dir, $expect) = (shift, shift);
269   my $bracketed_expect = quotemeta bracketed_form_dir($expect);
270   like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
271 }
272
273 sub bracketed_form_path {
274   return join '', map "[$_]", 
275     grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
276 }
277
278 sub path_ends_with {
279   my ($dir, $expect) = (shift, shift);
280   my $bracketed_expect = quotemeta bracketed_form_path($expect);
281   like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
282 }