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