20 my $EXTRA_ABSPATH_TESTS = $ENV{PERL_CORE} || $ENV{TEST_PERL_CWD_CODE};
21 $tests += 3 if $EXTRA_ABSPATH_TESTS;
24 my $IsVMS = $^O eq 'VMS';
25 my $IsMacOS = $^O eq 'MacOS';
28 can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
29 ok( !defined(&chdir), 'chdir() not exported by default' );
30 ok( !defined(&abs_path), ' nor abs_path()' );
31 ok( !defined(&fast_abs_path), ' nor fast_abs_path()');
34 # XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
35 # XXX and subsequent chdir()s can make them impossible to find
38 # Must find an external pwd (or equivalent) command.
40 my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
46 (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
47 split m/$Config{path_sep}/, $ENV{PATH})[0];
49 $pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
50 if ($^O eq 'MSWin32') {
52 $pwd_cmd = "$pwd_cmd /c cd";
54 $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
57 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
59 print "# native pwd = '$pwd_cmd'\n";
61 local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
62 my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
63 chomp(my $start = `$pwd_cmd_untainted`);
65 # Win32's cd returns native C:\ style
66 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
67 # DCL SHOW DEFAULT has leading spaces
68 $start =~ s/^\s+// if $IsVMS;
70 skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
71 skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
73 # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which
74 # Cwd.pm:getcwd uses) has some magic related to the PWD
75 # environment variable: if PWD is set to a directory that
76 # looks about right (guess: has the same (dev,ino) as the '.'?),
77 # the PWD is returned. However, if that path contains
78 # symlinks, the path will not be equal to the one returned by
79 # /bin/pwd (which probably uses the usual walking upwards in
80 # the path -trick). This situation is easy to reproduce since
81 # /tmp is a symlink to /private/tmp. Therefore we invalidate
82 # the PWD to force getcwd(3) to (re)compute the cwd in full.
83 # Admittedly fixing this in the Cwd module would be better
84 # long-term solution but deleting $ENV{PWD} should not be
85 # done light-heartedly. --jhi
86 delete $ENV{PWD} if $^O eq 'darwin';
90 my $fastcwd = fastcwd;
91 my $fastgetcwd = fastgetcwd;
93 is($cwd, $start, 'cwd()');
94 is($getcwd, $start, 'getcwd()');
95 is($fastcwd, $start, 'fastcwd()');
96 is($fastgetcwd, $start, 'fastgetcwd()');
100 my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
101 my $Test_Dir = File::Spec->catdir(@test_dirs);
103 mkpath([$Test_Dir], 0, 0777);
104 Cwd::chdir $Test_Dir;
106 foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
107 my $result = eval "$func()";
109 dir_ends_with( $result, $Test_Dir, "$func()" );
112 # Cwd::chdir should also update $ENV{PWD}
113 dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
114 my $updir = File::Spec->updir;
116 print "#$ENV{PWD}\n";
118 print "#$ENV{PWD}\n";
120 print "#$ENV{PWD}\n";
122 print "#$ENV{PWD}\n";
124 print "#$ENV{PWD}\n";
126 rmtree($test_dirs[0], 0, 0);
129 my $check = ($IsVMS ? qr|\b((?i)t)\]$| :
130 $IsMacOS ? qr|\bt:$| :
133 like($ENV{PWD}, $check);
137 skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
139 mkpath([$Test_Dir], 0, 0777);
140 symlink $Test_Dir, "linktest";
142 my $abs_path = Cwd::abs_path("linktest");
143 my $fast_abs_path = Cwd::fast_abs_path("linktest");
144 my $want = File::Spec->catdir("t", $Test_Dir);
146 like($abs_path, qr|$want$|);
147 like($fast_abs_path, qr|$want$|);
148 like(Cwd::_perl_abs_path("linktest"), qr|$want$|) if $EXTRA_ABSPATH_TESTS;
150 rmtree($test_dirs[0], 0, 0);
154 if ($ENV{PERL_CORE}) {
155 chdir '../ext/Cwd/t';
156 unshift @INC, '../../../lib';
159 # Make sure we can run abs_path() on files, not just directories
161 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
162 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
163 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
164 if $EXTRA_ABSPATH_TESTS;
166 $path = File::Spec->catfile(File::Spec->updir, 't', $path);
167 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
168 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
169 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
170 if $EXTRA_ABSPATH_TESTS;
173 #############################################
174 # These routines give us sort of a poor-man's cross-platform
175 # directory or path comparison capability.
177 sub bracketed_form_dir {
178 return join '', map "[$_]",
179 grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
183 my ($dir, $expect) = (shift, shift);
184 my $bracketed_expect = quotemeta bracketed_form_dir($expect);
185 like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
188 sub bracketed_form_path {
189 return join '', map "[$_]",
190 grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
194 my ($dir, $expect) = (shift, shift);
195 my $bracketed_expect = quotemeta bracketed_form_path($expect);
196 like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );