Commit | Line | Data |
5aa08720 |
1 | #!./perl |
ed4a5f99 |
2 | |
a9939470 |
3 | use Cwd; |
ed4a5f99 |
4 | BEGIN { |
5 | chdir 't' if -d 't'; |
ed4a5f99 |
6 | } |
7 | |
8 | use Config; |
ed4a5f99 |
9 | use strict; |
10 | use warnings; |
e69a2255 |
11 | use File::Spec; |
1279e177 |
12 | use File::Path; |
ed4a5f99 |
13 | |
ca7ced35 |
14 | use Test::More tests => 16; |
15 | |
16 | my $IsVMS = $^O eq 'VMS'; |
e69a2255 |
17 | my $IsMacOS = $^O eq 'MacOS'; |
ed4a5f99 |
18 | |
19 | # check imports |
ca7ced35 |
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 | |
ed4a5f99 |
25 | |
0d2079fa |
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 | |
da3f15f4 |
30 | # Must find an external pwd (or equivalent) command. |
31 | |
38f52085 |
32 | my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd"; |
da3f15f4 |
33 | my $pwd_cmd = |
38f52085 |
34 | ($^O eq "NetWare") ? |
023b4a43 |
35 | "cd" : |
e69a2255 |
36 | ($IsMacOS) ? |
37 | "pwd" : |
38f52085 |
38 | (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" } |
023b4a43 |
39 | split m/$Config{path_sep}/, $ENV{PATH})[0]; |
da3f15f4 |
40 | |
ca7ced35 |
41 | $pwd_cmd = 'SHOW DEFAULT' if $IsVMS; |
38f52085 |
42 | if ($^O eq 'MSWin32') { |
43 | $pwd_cmd =~ s,/,\\,g; |
44 | $pwd_cmd = "$pwd_cmd /c cd"; |
45 | } |
e8f7eed0 |
46 | $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos'); |
47 | |
ca7ced35 |
48 | SKIP: { |
49 | skip "No native pwd command found to test against", 4 unless $pwd_cmd; |
2390ecbc |
50 | |
d80cbc32 |
51 | print "# native pwd = '$pwd_cmd'\n"; |
52 | |
926cbafe |
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 | |
14107c42 |
57 | # Win32's cd returns native C:\ style |
2986a63f |
58 | $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare"); |
2390ecbc |
59 | # DCL SHOW DEFAULT has leading spaces |
ca7ced35 |
60 | $start =~ s/^\s+// if $IsVMS; |
61 | SKIP: { |
12b7537a |
62 | skip("'$pwd_cmd' failed, nothing to test against", 4) if $?; |
63 | skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|; |
ca7ced35 |
64 | |
164336fe |
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 | |
da3f15f4 |
80 | my $cwd = cwd; |
81 | my $getcwd = getcwd; |
82 | my $fastcwd = fastcwd; |
83 | my $fastgetcwd = fastgetcwd; |
12b7537a |
84 | |
1c26fec0 |
85 | is($cwd, $start, 'cwd()'); |
86 | is($getcwd, $start, 'getcwd()'); |
87 | is($fastcwd, $start, 'fastcwd()'); |
88 | is($fastgetcwd, $start, 'fastgetcwd()'); |
da3f15f4 |
89 | } |
90 | } |
91 | |
ca7ced35 |
92 | my $Top_Test_Dir = '_ptrslt_'; |
93 | my $Test_Dir = "$Top_Test_Dir/_path_/_to_/_a_/_dir_"; |
94 | my $want = "t/$Test_Dir"; |
95 | if( $IsVMS ) { |
96 | # translate the unixy path to VMSish |
ca7ced35 |
97 | $want =~ s|/|\.|g; |
98 | $want .= '\]'; |
b4558e59 |
99 | $want = '((?i)' . $want . ')'; # might be ODS-2 or ODS-5 |
e69a2255 |
100 | } elsif ( $IsMacOS ) { |
101 | $_ = ":$_" for ($Top_Test_Dir, $Test_Dir); |
102 | s|/|:|g, s|$|:| for ($want, $Test_Dir); |
ca7ced35 |
103 | } |
104 | |
105 | mkpath(["$Test_Dir"], 0, 0777); |
106 | Cwd::chdir "$Test_Dir"; |
107 | |
108 | like(cwd(), qr|$want$|, 'chdir() + cwd()'); |
109 | like(getcwd(), qr|$want$|, ' + getcwd()'); |
110 | like(fastcwd(), qr|$want$|, ' + fastcwd()'); |
111 | like(fastgetcwd(), qr|$want$|, ' + fastgetcwd()'); |
ed4a5f99 |
112 | |
113 | # Cwd::chdir should also update $ENV{PWD} |
ca7ced35 |
114 | like($ENV{PWD}, qr|$want$|, 'Cwd::chdir() updates $ENV{PWD}'); |
e69a2255 |
115 | my $updir = File::Spec->updir; |
116 | Cwd::chdir $updir; |
14107c42 |
117 | print "#$ENV{PWD}\n"; |
e69a2255 |
118 | Cwd::chdir $updir; |
14107c42 |
119 | print "#$ENV{PWD}\n"; |
e69a2255 |
120 | Cwd::chdir $updir; |
14107c42 |
121 | print "#$ENV{PWD}\n"; |
e69a2255 |
122 | Cwd::chdir $updir; |
14107c42 |
123 | print "#$ENV{PWD}\n"; |
e69a2255 |
124 | Cwd::chdir $updir; |
14107c42 |
125 | print "#$ENV{PWD}\n"; |
1279e177 |
126 | |
ca7ced35 |
127 | rmtree([$Top_Test_Dir], 0, 0); |
1279e177 |
128 | |
ca7ced35 |
129 | if ($IsVMS) { |
b4558e59 |
130 | like($ENV{PWD}, qr|\b((?i)t)\]$|); |
2390ecbc |
131 | } |
e69a2255 |
132 | elsif ($IsMacOS) { |
133 | like($ENV{PWD}, qr|\bt:$|); |
134 | } |
2390ecbc |
135 | else { |
ca7ced35 |
136 | like($ENV{PWD}, qr|\bt$|); |
2390ecbc |
137 | } |
ed4a5f99 |
138 | |
ca7ced35 |
139 | SKIP: { |
140 | skip "no symlinks on this platform", 2 unless $Config{d_symlink}; |
141 | |
142 | mkpath([$Test_Dir], 0, 0777); |
143 | symlink $Test_Dir => "linktest"; |
7040f5d5 |
144 | |
145 | my $abs_path = Cwd::abs_path("linktest"); |
146 | my $fast_abs_path = Cwd::fast_abs_path("linktest"); |
16398b42 |
147 | my $want = File::Spec->catdir("t", $Test_Dir); |
7040f5d5 |
148 | |
ca7ced35 |
149 | like($abs_path, qr|$want$|); |
150 | like($fast_abs_path, qr|$want$|); |
7040f5d5 |
151 | |
ca7ced35 |
152 | rmtree([$Top_Test_Dir], 0, 0); |
7040f5d5 |
153 | unlink "linktest"; |
ed4a5f99 |
154 | } |