20 my $EXTRA_ABSPATH_TESTS = $ENV{PERL_CORE} || $ENV{TEST_PERL_CWD_CODE};
21 # _perl_abs_path() currently only works when the directory separator
22 # is '/', so don't test it when it won't work.
23 $EXTRA_ABSPATH_TESTS &&= $Config{prefix} =~ m/\//;
24 $tests += 3 if $EXTRA_ABSPATH_TESTS;
27 my $IsVMS = $^O eq 'VMS';
28 my $IsMacOS = $^O eq 'MacOS';
31 can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
32 ok( !defined(&chdir), 'chdir() not exported by default' );
33 ok( !defined(&abs_path), ' nor abs_path()' );
34 ok( !defined(&fast_abs_path), ' nor fast_abs_path()');
37 # XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
38 # XXX and subsequent chdir()s can make them impossible to find
41 # Must find an external pwd (or equivalent) command.
43 my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
49 (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
50 split m/$Config{path_sep}/, $ENV{PATH})[0];
52 $pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
53 if ($^O eq 'MSWin32') {
55 $pwd_cmd = "$pwd_cmd /c cd";
57 $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
60 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
62 print "# native pwd = '$pwd_cmd'\n";
64 local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
65 my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
66 chomp(my $start = `$pwd_cmd_untainted`);
68 # Win32's cd returns native C:\ style
69 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
70 # DCL SHOW DEFAULT has leading spaces
71 $start =~ s/^\s+// if $IsVMS;
73 skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
74 skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
76 # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which
77 # Cwd.pm:getcwd uses) has some magic related to the PWD
78 # environment variable: if PWD is set to a directory that
79 # looks about right (guess: has the same (dev,ino) as the '.'?),
80 # the PWD is returned. However, if that path contains
81 # symlinks, the path will not be equal to the one returned by
82 # /bin/pwd (which probably uses the usual walking upwards in
83 # the path -trick). This situation is easy to reproduce since
84 # /tmp is a symlink to /private/tmp. Therefore we invalidate
85 # the PWD to force getcwd(3) to (re)compute the cwd in full.
86 # Admittedly fixing this in the Cwd module would be better
87 # long-term solution but deleting $ENV{PWD} should not be
88 # done light-heartedly. --jhi
89 delete $ENV{PWD} if $^O eq 'darwin';
93 my $fastcwd = fastcwd;
94 my $fastgetcwd = fastgetcwd;
96 is($cwd, $start, 'cwd()');
97 is($getcwd, $start, 'getcwd()');
98 is($fastcwd, $start, 'fastcwd()');
99 is($fastgetcwd, $start, 'fastgetcwd()');
103 my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
104 my $Test_Dir = File::Spec->catdir(@test_dirs);
106 mkpath([$Test_Dir], 0, 0777);
107 Cwd::chdir $Test_Dir;
109 foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
110 my $result = eval "$func()";
112 dir_ends_with( $result, $Test_Dir, "$func()" );
115 # Cwd::chdir should also update $ENV{PWD}
116 dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
117 my $updir = File::Spec->updir;
119 print "#$ENV{PWD}\n";
121 print "#$ENV{PWD}\n";
123 print "#$ENV{PWD}\n";
125 print "#$ENV{PWD}\n";
127 print "#$ENV{PWD}\n";
129 rmtree($test_dirs[0], 0, 0);
132 my $check = ($IsVMS ? qr|\b((?i)t)\]$| :
133 $IsMacOS ? qr|\bt:$| :
136 like($ENV{PWD}, $check);
140 skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
142 mkpath([$Test_Dir], 0, 0777);
143 symlink $Test_Dir, "linktest";
145 my $abs_path = Cwd::abs_path("linktest");
146 my $fast_abs_path = Cwd::fast_abs_path("linktest");
147 my $want = File::Spec->catdir("t", $Test_Dir);
149 like($abs_path, qr|$want$|);
150 like($fast_abs_path, qr|$want$|);
151 like(Cwd::_perl_abs_path("linktest"), qr|$want$|) if $EXTRA_ABSPATH_TESTS;
153 rmtree($test_dirs[0], 0, 0);
157 if ($ENV{PERL_CORE}) {
158 chdir '../ext/Cwd/t';
159 unshift @INC, '../../../lib';
162 # Make sure we can run abs_path() on files, not just directories
164 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
165 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
166 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
167 if $EXTRA_ABSPATH_TESTS;
169 $path = File::Spec->catfile(File::Spec->updir, 't', $path);
170 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
171 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
172 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
173 if $EXTRA_ABSPATH_TESTS;
176 #############################################
177 # These routines give us sort of a poor-man's cross-platform
178 # directory or path comparison capability.
180 sub bracketed_form_dir {
181 return join '', map "[$_]",
182 grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
186 my ($dir, $expect) = (shift, shift);
187 my $bracketed_expect = quotemeta bracketed_form_dir($expect);
188 like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
191 sub bracketed_form_path {
192 return join '', map "[$_]",
193 grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
197 my ($dir, $expect) = (shift, shift);
198 my $bracketed_expect = quotemeta bracketed_form_path($expect);
199 like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );