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