Commit | Line | Data |
5aa08720 |
1 | #!./perl |
ed4a5f99 |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
78321866 |
5 | if ($ENV{PERL_CORE}) { |
39f7733f |
6 | @INC = '../lib'; |
78321866 |
7 | } |
ed4a5f99 |
8 | } |
78321866 |
9 | use Cwd; |
ed4a5f99 |
10 | |
11 | use Config; |
ed4a5f99 |
12 | use strict; |
13 | use warnings; |
e69a2255 |
14 | use File::Spec; |
1279e177 |
15 | use File::Path; |
ed4a5f99 |
16 | |
78321866 |
17 | use Test::More tests => 24; |
ca7ced35 |
18 | |
19 | my $IsVMS = $^O eq 'VMS'; |
e69a2255 |
20 | my $IsMacOS = $^O eq 'MacOS'; |
ed4a5f99 |
21 | |
22 | # check imports |
ca7ced35 |
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()'); |
27 | |
ed4a5f99 |
28 | |
0d2079fa |
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 |
31 | eval { fastcwd }; |
32 | |
da3f15f4 |
33 | # Must find an external pwd (or equivalent) command. |
34 | |
38f52085 |
35 | my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd"; |
da3f15f4 |
36 | my $pwd_cmd = |
38f52085 |
37 | ($^O eq "NetWare") ? |
023b4a43 |
38 | "cd" : |
e69a2255 |
39 | ($IsMacOS) ? |
40 | "pwd" : |
38f52085 |
41 | (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" } |
023b4a43 |
42 | split m/$Config{path_sep}/, $ENV{PATH})[0]; |
da3f15f4 |
43 | |
ca7ced35 |
44 | $pwd_cmd = 'SHOW DEFAULT' if $IsVMS; |
38f52085 |
45 | if ($^O eq 'MSWin32') { |
46 | $pwd_cmd =~ s,/,\\,g; |
47 | $pwd_cmd = "$pwd_cmd /c cd"; |
48 | } |
e8f7eed0 |
49 | $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos'); |
50 | |
ca7ced35 |
51 | SKIP: { |
52 | skip "No native pwd command found to test against", 4 unless $pwd_cmd; |
2390ecbc |
53 | |
d80cbc32 |
54 | print "# native pwd = '$pwd_cmd'\n"; |
55 | |
926cbafe |
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`); |
59 | |
14107c42 |
60 | # Win32's cd returns native C:\ style |
2986a63f |
61 | $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare"); |
2390ecbc |
62 | # DCL SHOW DEFAULT has leading spaces |
ca7ced35 |
63 | $start =~ s/^\s+// if $IsVMS; |
64 | SKIP: { |
12b7537a |
65 | skip("'$pwd_cmd' failed, nothing to test against", 4) if $?; |
66 | skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|; |
ca7ced35 |
67 | |
164336fe |
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'; |
82 | |
da3f15f4 |
83 | my $cwd = cwd; |
84 | my $getcwd = getcwd; |
85 | my $fastcwd = fastcwd; |
86 | my $fastgetcwd = fastgetcwd; |
12b7537a |
87 | |
1c26fec0 |
88 | is($cwd, $start, 'cwd()'); |
89 | is($getcwd, $start, 'getcwd()'); |
90 | is($fastcwd, $start, 'fastcwd()'); |
91 | is($fastgetcwd, $start, 'fastgetcwd()'); |
da3f15f4 |
92 | } |
93 | } |
94 | |
ea067225 |
95 | my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_}; |
96 | my $Test_Dir = File::Spec->catdir(@test_dirs); |
ca7ced35 |
97 | |
889f7a4f |
98 | mkpath([$Test_Dir], 0, 0777); |
99 | Cwd::chdir $Test_Dir; |
ca7ced35 |
100 | |
ad78113d |
101 | foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) { |
102 | my $result = eval "$func()"; |
103 | is $@, ''; |
ea067225 |
104 | dir_ends_with( $result, $Test_Dir, "$func()" ); |
ad78113d |
105 | } |
ed4a5f99 |
106 | |
107 | # Cwd::chdir should also update $ENV{PWD} |
ea067225 |
108 | dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' ); |
e69a2255 |
109 | my $updir = File::Spec->updir; |
110 | Cwd::chdir $updir; |
14107c42 |
111 | print "#$ENV{PWD}\n"; |
e69a2255 |
112 | Cwd::chdir $updir; |
14107c42 |
113 | print "#$ENV{PWD}\n"; |
e69a2255 |
114 | Cwd::chdir $updir; |
14107c42 |
115 | print "#$ENV{PWD}\n"; |
e69a2255 |
116 | Cwd::chdir $updir; |
14107c42 |
117 | print "#$ENV{PWD}\n"; |
e69a2255 |
118 | Cwd::chdir $updir; |
14107c42 |
119 | print "#$ENV{PWD}\n"; |
1279e177 |
120 | |
ea067225 |
121 | rmtree($test_dirs[0], 0, 0); |
1279e177 |
122 | |
889f7a4f |
123 | { |
124 | my $check = ($IsVMS ? qr|\b((?i)t)\]$| : |
125 | $IsMacOS ? qr|\bt:$| : |
126 | qr|\bt$| ); |
127 | |
128 | like($ENV{PWD}, $check); |
2390ecbc |
129 | } |
ed4a5f99 |
130 | |
ca7ced35 |
131 | SKIP: { |
132 | skip "no symlinks on this platform", 2 unless $Config{d_symlink}; |
133 | |
134 | mkpath([$Test_Dir], 0, 0777); |
78321866 |
135 | symlink $Test_Dir, "linktest"; |
7040f5d5 |
136 | |
137 | my $abs_path = Cwd::abs_path("linktest"); |
138 | my $fast_abs_path = Cwd::fast_abs_path("linktest"); |
16398b42 |
139 | my $want = File::Spec->catdir("t", $Test_Dir); |
7040f5d5 |
140 | |
ca7ced35 |
141 | like($abs_path, qr|$want$|); |
142 | like($fast_abs_path, qr|$want$|); |
7040f5d5 |
143 | |
ea067225 |
144 | rmtree($test_dirs[0], 0, 0); |
7040f5d5 |
145 | unlink "linktest"; |
ed4a5f99 |
146 | } |
ea067225 |
147 | |
78321866 |
148 | chdir '../ext/Cwd/t' if $ENV{PERL_CORE}; |
149 | |
150 | # Make sure we can run abs_path() on files, not just directories |
151 | my $path = 'cwd.t'; |
152 | dir_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file'); |
153 | dir_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file'); |
154 | |
155 | $path = File::Spec->catfile(File::Spec->updir, 't', $path); |
156 | dir_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file'); |
157 | dir_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file'); |
158 | |
159 | |
ea067225 |
160 | ############################################# |
161 | # These two routines give us sort of a poor-man's cross-platform |
162 | # directory comparison routine. |
163 | |
164 | sub bracketed_form { |
165 | return join '', map "[$_]", |
166 | grep length, File::Spec->splitdir(File::Spec->canonpath( shift() )); |
167 | } |
168 | |
169 | sub dir_ends_with { |
170 | my ($dir, $expect) = (shift, shift); |
171 | my $bracketed_expect = quotemeta bracketed_form($expect); |
172 | like( bracketed_form($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) ); |
173 | } |
174 | |