13 use lib File::Spec->catdir('t', 'lib');
16 my $IsVMS = $^O eq 'VMS';
17 my $IsMacOS = $^O eq 'MacOS';
24 require VMS::Filespec;
28 if (eval 'require VMS::Feature') {
29 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
30 $vms_efs = VMS::Feature::current("efs_charset");
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;
37 $vms_mode = 0 if ($vms_unix_rpt);
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;
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";
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()');
60 my @fields = qw(PATH IFS CDPATH ENV BASH_ENV);
61 my $before = grep exists $ENV{$_}, @fields;
63 my $after = grep exists $ENV{$_}, @fields;
64 is($before, $after, "cwd() shouldn't create spurious entries in %ENV");
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
71 # Must find an external pwd (or equivalent) command.
73 my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
79 (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
80 split m/$Config{path_sep}/, $ENV{PATH})[0];
82 $pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
83 if ($^O eq 'MSWin32') {
85 $pwd_cmd = "$pwd_cmd /c cd";
87 $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
90 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
92 print "# native pwd = '$pwd_cmd'\n";
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`);
98 # Win32's cd returns native C:\ style
99 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
101 # DCL SHOW DEFAULT has leading spaces
104 # When in UNIX report mode, need to convert to compare it.
106 $start = VMS::Filespec::unixpath($start);
107 # Remove trailing slash.
112 skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
113 skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
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';
132 my $fastcwd = fastcwd;
133 my $fastgetcwd = fastgetcwd;
135 is($cwd, $start, 'cwd()');
136 is($getcwd, $start, 'getcwd()');
137 is($fastcwd, $start, 'fastcwd()');
138 is($fastgetcwd, $start, 'fastgetcwd()');
142 my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
143 my $Test_Dir = File::Spec->catdir(@test_dirs);
145 mkpath([$Test_Dir], 0, 0777);
146 Cwd::chdir $Test_Dir;
148 foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
149 my $result = eval "$func()";
151 dir_ends_with( $result, $Test_Dir, "$func()" );
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";
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;
165 for (1..@test_dirs) {
167 print "#$ENV{PWD}\n";
170 rmtree($test_dirs[0], 0, 0);
173 my $check = ($vms_mode ? qr|\b((?i)t)\]$| :
174 $IsMacOS ? qr|\bt:$| :
177 like($ENV{PWD}, $check);
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);
190 skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
192 my $file = "linktest";
193 mkpath([$Test_Dir], 0, 0777);
194 symlink $Test_Dir, $file;
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 )
202 # Not easy to predict the physical volume name
203 $want = $ENV{PERL_CORE} ? $Test_Dir : File::Spec->catdir('t', $Test_Dir);
205 # So just use the relative volume name
208 $want = quotemeta($want);
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;
215 rmtree($test_dirs[0], 0, 0);
216 1 while unlink $file;
219 # Make sure we can run abs_path() on files, not just directories
221 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
222 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
223 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
224 if $EXTRA_ABSPATH_TESTS;
226 $path = File::Spec->catfile(File::Spec->updir, 't', $path);
227 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
228 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
229 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
230 if $EXTRA_ABSPATH_TESTS;
237 my $root = Cwd::abs_path(File::Spec->rootdir); # Add drive letter?
239 opendir FH, $root or skip("Can't opendir($root): $!", 2+$EXTRA_ABSPATH_TESTS);
240 ($file) = grep {-f $_ and not -l $_} map File::Spec->catfile($root, $_), readdir FH;
243 skip "No plain file in root directory to test with", 2+$EXTRA_ABSPATH_TESTS unless $file;
245 $file = VMS::Filespec::rmsexpand($file) if $^O eq 'VMS';
246 is Cwd::abs_path($file), $file, 'abs_path() works on files in the root directory';
247 is Cwd::fast_abs_path($file), $file, 'fast_abs_path() works on files in the root directory';
248 is Cwd::_perl_abs_path($file), $file, '_perl_abs_path() works on files in the root directory'
249 if $EXTRA_ABSPATH_TESTS;
253 #############################################
254 # These routines give us sort of a poor-man's cross-platform
255 # directory or path comparison capability.
257 sub bracketed_form_dir {
258 return join '', map "[$_]",
259 grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
263 my ($dir, $expect) = (shift, shift);
264 my $bracketed_expect = quotemeta bracketed_form_dir($expect);
265 like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
268 sub bracketed_form_path {
269 return join '', map "[$_]",
270 grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
274 my ($dir, $expect) = (shift, shift);
275 my $bracketed_expect = quotemeta bracketed_form_path($expect);
276 like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );