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