| 1 | #!./perl |
| 2 | |
| 3 | use Cwd; |
| 4 | BEGIN { |
| 5 | chdir 't' if -d 't'; |
| 6 | } |
| 7 | |
| 8 | use Config; |
| 9 | use strict; |
| 10 | use warnings; |
| 11 | use File::Spec; |
| 12 | use File::Path; |
| 13 | |
| 14 | use Test::More tests => 20; |
| 15 | |
| 16 | my $IsVMS = $^O eq 'VMS'; |
| 17 | my $IsMacOS = $^O eq 'MacOS'; |
| 18 | |
| 19 | # check imports |
| 20 | can_ok('main', qw(cwd getcwd fastcwd fastgetcwd)); |
| 21 | ok( !defined(&chdir), 'chdir() not exported by default' ); |
| 22 | ok( !defined(&abs_path), ' nor abs_path()' ); |
| 23 | ok( !defined(&fast_abs_path), ' nor fast_abs_path()'); |
| 24 | |
| 25 | |
| 26 | # XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib" |
| 27 | # XXX and subsequent chdir()s can make them impossible to find |
| 28 | eval { fastcwd }; |
| 29 | |
| 30 | # Must find an external pwd (or equivalent) command. |
| 31 | |
| 32 | my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd"; |
| 33 | my $pwd_cmd = |
| 34 | ($^O eq "NetWare") ? |
| 35 | "cd" : |
| 36 | ($IsMacOS) ? |
| 37 | "pwd" : |
| 38 | (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" } |
| 39 | split m/$Config{path_sep}/, $ENV{PATH})[0]; |
| 40 | |
| 41 | $pwd_cmd = 'SHOW DEFAULT' if $IsVMS; |
| 42 | if ($^O eq 'MSWin32') { |
| 43 | $pwd_cmd =~ s,/,\\,g; |
| 44 | $pwd_cmd = "$pwd_cmd /c cd"; |
| 45 | } |
| 46 | $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos'); |
| 47 | |
| 48 | SKIP: { |
| 49 | skip "No native pwd command found to test against", 4 unless $pwd_cmd; |
| 50 | |
| 51 | print "# native pwd = '$pwd_cmd'\n"; |
| 52 | |
| 53 | local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; |
| 54 | my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint. |
| 55 | chomp(my $start = `$pwd_cmd_untainted`); |
| 56 | |
| 57 | # Win32's cd returns native C:\ style |
| 58 | $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare"); |
| 59 | # DCL SHOW DEFAULT has leading spaces |
| 60 | $start =~ s/^\s+// if $IsVMS; |
| 61 | SKIP: { |
| 62 | skip("'$pwd_cmd' failed, nothing to test against", 4) if $?; |
| 63 | skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|; |
| 64 | |
| 65 | # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which |
| 66 | # Cwd.pm:getcwd uses) has some magic related to the PWD |
| 67 | # environment variable: if PWD is set to a directory that |
| 68 | # looks about right (guess: has the same (dev,ino) as the '.'?), |
| 69 | # the PWD is returned. However, if that path contains |
| 70 | # symlinks, the path will not be equal to the one returned by |
| 71 | # /bin/pwd (which probably uses the usual walking upwards in |
| 72 | # the path -trick). This situation is easy to reproduce since |
| 73 | # /tmp is a symlink to /private/tmp. Therefore we invalidate |
| 74 | # the PWD to force getcwd(3) to (re)compute the cwd in full. |
| 75 | # Admittedly fixing this in the Cwd module would be better |
| 76 | # long-term solution but deleting $ENV{PWD} should not be |
| 77 | # done light-heartedly. --jhi |
| 78 | delete $ENV{PWD} if $^O eq 'darwin'; |
| 79 | |
| 80 | my $cwd = cwd; |
| 81 | my $getcwd = getcwd; |
| 82 | my $fastcwd = fastcwd; |
| 83 | my $fastgetcwd = fastgetcwd; |
| 84 | |
| 85 | is($cwd, $start, 'cwd()'); |
| 86 | is($getcwd, $start, 'getcwd()'); |
| 87 | is($fastcwd, $start, 'fastcwd()'); |
| 88 | is($fastgetcwd, $start, 'fastgetcwd()'); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | my $Top_Test_Dir = '_ptrslt_'; |
| 93 | my $Test_Dir = File::Spec->catdir($Top_Test_Dir, qw/_path_ _to_ _a_ _dir_/); |
| 94 | my $want = quotemeta File::Spec->rel2abs($Test_Dir); |
| 95 | |
| 96 | mkpath([$Test_Dir], 0, 0777); |
| 97 | Cwd::chdir $Test_Dir; |
| 98 | |
| 99 | foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) { |
| 100 | my $result = eval "$func()"; |
| 101 | is $@, ''; |
| 102 | like( File::Spec->canonpath($result), qr|$want$|i, "$func()" ); |
| 103 | } |
| 104 | |
| 105 | # Cwd::chdir should also update $ENV{PWD} |
| 106 | like(File::Spec->canonpath($ENV{PWD}), qr|$want$|i, 'Cwd::chdir() updates $ENV{PWD}'); |
| 107 | my $updir = File::Spec->updir; |
| 108 | Cwd::chdir $updir; |
| 109 | print "#$ENV{PWD}\n"; |
| 110 | Cwd::chdir $updir; |
| 111 | print "#$ENV{PWD}\n"; |
| 112 | Cwd::chdir $updir; |
| 113 | print "#$ENV{PWD}\n"; |
| 114 | Cwd::chdir $updir; |
| 115 | print "#$ENV{PWD}\n"; |
| 116 | Cwd::chdir $updir; |
| 117 | print "#$ENV{PWD}\n"; |
| 118 | |
| 119 | rmtree([$Top_Test_Dir], 0, 0); |
| 120 | |
| 121 | { |
| 122 | my $check = ($IsVMS ? qr|\b((?i)t)\]$| : |
| 123 | $IsMacOS ? qr|\bt:$| : |
| 124 | qr|\bt$| ); |
| 125 | |
| 126 | like($ENV{PWD}, $check); |
| 127 | } |
| 128 | |
| 129 | SKIP: { |
| 130 | skip "no symlinks on this platform", 2 unless $Config{d_symlink}; |
| 131 | |
| 132 | mkpath([$Test_Dir], 0, 0777); |
| 133 | symlink $Test_Dir => "linktest"; |
| 134 | |
| 135 | my $abs_path = Cwd::abs_path("linktest"); |
| 136 | my $fast_abs_path = Cwd::fast_abs_path("linktest"); |
| 137 | my $want = File::Spec->catdir("t", $Test_Dir); |
| 138 | |
| 139 | like($abs_path, qr|$want$|); |
| 140 | like($fast_abs_path, qr|$want$|); |
| 141 | |
| 142 | rmtree([$Top_Test_Dir], 0, 0); |
| 143 | unlink "linktest"; |
| 144 | } |