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