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