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