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