17 use Test::More tests => 24;
19 my $IsVMS = $^O eq 'VMS';
20 my $IsMacOS = $^O eq 'MacOS';
23 can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
24 ok( !defined(&chdir), 'chdir() not exported by default' );
25 ok( !defined(&abs_path), ' nor abs_path()' );
26 ok( !defined(&fast_abs_path), ' nor fast_abs_path()');
29 # XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
30 # XXX and subsequent chdir()s can make them impossible to find
33 # Must find an external pwd (or equivalent) command.
35 my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
41 (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
42 split m/$Config{path_sep}/, $ENV{PATH})[0];
44 $pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
45 if ($^O eq 'MSWin32') {
47 $pwd_cmd = "$pwd_cmd /c cd";
49 $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
52 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
54 print "# native pwd = '$pwd_cmd'\n";
56 local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
57 my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
58 chomp(my $start = `$pwd_cmd_untainted`);
60 # Win32's cd returns native C:\ style
61 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
62 # DCL SHOW DEFAULT has leading spaces
63 $start =~ s/^\s+// if $IsVMS;
65 skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
66 skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
68 # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which
69 # Cwd.pm:getcwd uses) has some magic related to the PWD
70 # environment variable: if PWD is set to a directory that
71 # looks about right (guess: has the same (dev,ino) as the '.'?),
72 # the PWD is returned. However, if that path contains
73 # symlinks, the path will not be equal to the one returned by
74 # /bin/pwd (which probably uses the usual walking upwards in
75 # the path -trick). This situation is easy to reproduce since
76 # /tmp is a symlink to /private/tmp. Therefore we invalidate
77 # the PWD to force getcwd(3) to (re)compute the cwd in full.
78 # Admittedly fixing this in the Cwd module would be better
79 # long-term solution but deleting $ENV{PWD} should not be
80 # done light-heartedly. --jhi
81 delete $ENV{PWD} if $^O eq 'darwin';
85 my $fastcwd = fastcwd;
86 my $fastgetcwd = fastgetcwd;
88 is($cwd, $start, 'cwd()');
89 is($getcwd, $start, 'getcwd()');
90 is($fastcwd, $start, 'fastcwd()');
91 is($fastgetcwd, $start, 'fastgetcwd()');
95 my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
96 my $Test_Dir = File::Spec->catdir(@test_dirs);
98 mkpath([$Test_Dir], 0, 0777);
101 foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
102 my $result = eval "$func()";
104 dir_ends_with( $result, $Test_Dir, "$func()" );
107 # Cwd::chdir should also update $ENV{PWD}
108 dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
109 my $updir = File::Spec->updir;
111 print "#$ENV{PWD}\n";
113 print "#$ENV{PWD}\n";
115 print "#$ENV{PWD}\n";
117 print "#$ENV{PWD}\n";
119 print "#$ENV{PWD}\n";
121 rmtree($test_dirs[0], 0, 0);
124 my $check = ($IsVMS ? qr|\b((?i)t)\]$| :
125 $IsMacOS ? qr|\bt:$| :
128 like($ENV{PWD}, $check);
132 skip "no symlinks on this platform", 2 unless $Config{d_symlink};
134 mkpath([$Test_Dir], 0, 0777);
135 symlink $Test_Dir, "linktest";
137 my $abs_path = Cwd::abs_path("linktest");
138 my $fast_abs_path = Cwd::fast_abs_path("linktest");
139 my $want = File::Spec->catdir("t", $Test_Dir);
141 like($abs_path, qr|$want$|);
142 like($fast_abs_path, qr|$want$|);
144 rmtree($test_dirs[0], 0, 0);
148 if ($ENV{PERL_CORE}) {
149 chdir '../ext/Cwd/t';
150 unshift @INC, '../../../lib';
153 # Make sure we can run abs_path() on files, not just directories
155 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
156 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
158 $path = File::Spec->catfile(File::Spec->updir, 't', $path);
159 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
160 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
163 #############################################
164 # These routines give us sort of a poor-man's cross-platform
165 # directory or path comparison capability.
167 sub bracketed_form_dir {
168 return join '', map "[$_]",
169 grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
173 my ($dir, $expect) = (shift, shift);
174 my $bracketed_expect = quotemeta bracketed_form_dir($expect);
175 like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
178 sub bracketed_form_path {
179 return join '', map "[$_]",
180 grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
184 my ($dir, $expect) = (shift, shift);
185 my $bracketed_expect = quotemeta bracketed_form_path($expect);
186 like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );