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