2c7d6c5598cec1eea7814b95e7390e7d01939c2c
[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 $tests += 3 if $EXTRA_ABSPATH_TESTS;
22 plan tests => $tests;
23
24 my $IsVMS = $^O eq 'VMS';
25 my $IsMacOS = $^O eq 'MacOS';
26
27 # check imports
28 can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
29 ok( !defined(&chdir),           'chdir() not exported by default' );
30 ok( !defined(&abs_path),        '  nor abs_path()' );
31 ok( !defined(&fast_abs_path),   '  nor fast_abs_path()');
32
33
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
36 eval { fastcwd };
37
38 # Must find an external pwd (or equivalent) command.
39
40 my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
41 my $pwd_cmd =
42     ($^O eq "NetWare") ?
43         "cd" :
44     ($IsMacOS) ?
45         "pwd" :
46         (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
47                            split m/$Config{path_sep}/, $ENV{PATH})[0];
48
49 $pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
50 if ($^O eq 'MSWin32') {
51     $pwd_cmd =~ s,/,\\,g;
52     $pwd_cmd = "$pwd_cmd /c cd";
53 }
54 $pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
55
56 SKIP: {
57     skip "No native pwd command found to test against", 4 unless $pwd_cmd;
58
59     print "# native pwd = '$pwd_cmd'\n";
60
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
65     # Win32's cd returns native C:\ style
66     $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
67     # DCL SHOW DEFAULT has leading spaces
68     $start =~ s/^\s+// if $IsVMS;
69     SKIP: {
70         skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
71         skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
72
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
88         my $cwd        = cwd;
89         my $getcwd     = getcwd;
90         my $fastcwd    = fastcwd;
91         my $fastgetcwd = fastgetcwd;
92
93         is($cwd,        $start, 'cwd()');
94         is($getcwd,     $start, 'getcwd()');
95         is($fastcwd,    $start, 'fastcwd()');
96         is($fastgetcwd, $start, 'fastgetcwd()');
97     }
98 }
99
100 my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
101 my $Test_Dir     = File::Spec->catdir(@test_dirs);
102
103 mkpath([$Test_Dir], 0, 0777);
104 Cwd::chdir $Test_Dir;
105
106 foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
107   my $result = eval "$func()";
108   is $@, '';
109   dir_ends_with( $result, $Test_Dir, "$func()" );
110 }
111
112 # Cwd::chdir should also update $ENV{PWD}
113 dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
114 my $updir = File::Spec->updir;
115 Cwd::chdir $updir;
116 print "#$ENV{PWD}\n";
117 Cwd::chdir $updir;
118 print "#$ENV{PWD}\n";
119 Cwd::chdir $updir;
120 print "#$ENV{PWD}\n";
121 Cwd::chdir $updir;
122 print "#$ENV{PWD}\n";
123 Cwd::chdir $updir;
124 print "#$ENV{PWD}\n";
125
126 rmtree($test_dirs[0], 0, 0);
127
128 {
129   my $check = ($IsVMS   ? qr|\b((?i)t)\]$| :
130                $IsMacOS ? qr|\bt:$| :
131                           qr|\bt$| );
132   
133   like($ENV{PWD}, $check);
134 }
135
136 SKIP: {
137     skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
138
139     mkpath([$Test_Dir], 0, 0777);
140     symlink $Test_Dir, "linktest";
141
142     my $abs_path      =  Cwd::abs_path("linktest");
143     my $fast_abs_path =  Cwd::fast_abs_path("linktest");
144     my $want          =  File::Spec->catdir("t", $Test_Dir);
145
146     like($abs_path,      qr|$want$|);
147     like($fast_abs_path, qr|$want$|);
148     like(Cwd::_perl_abs_path("linktest"), qr|$want$|) if $EXTRA_ABSPATH_TESTS;
149
150     rmtree($test_dirs[0], 0, 0);
151     unlink "linktest";
152 }
153
154 if ($ENV{PERL_CORE}) {
155     chdir '../ext/Cwd/t';
156     unshift @INC, '../../../lib';
157 }
158
159 # Make sure we can run abs_path() on files, not just directories
160 my $path = 'cwd.t';
161 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
162 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
163 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
164   if $EXTRA_ABSPATH_TESTS;
165
166 $path = File::Spec->catfile(File::Spec->updir, 't', $path);
167 path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
168 path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
169 path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
170   if $EXTRA_ABSPATH_TESTS;
171
172
173 #############################################
174 # These routines give us sort of a poor-man's cross-platform
175 # directory or path comparison capability.
176
177 sub bracketed_form_dir {
178   return join '', map "[$_]", 
179     grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
180 }
181
182 sub dir_ends_with {
183   my ($dir, $expect) = (shift, shift);
184   my $bracketed_expect = quotemeta bracketed_form_dir($expect);
185   like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
186 }
187
188 sub bracketed_form_path {
189   return join '', map "[$_]", 
190     grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
191 }
192
193 sub 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 : ()) );
197 }