Silence new warning grep in void context warning in various modules and test files...
[p5sagit/p5-mst-13.2.git] / ext / Cwd / t / cwd.t
CommitLineData
99f36a73 1#!./perl -w
ed4a5f99 2
3BEGIN {
78321866 4 if ($ENV{PERL_CORE}) {
99f36a73 5 chdir 't';
7ada78df 6 @INC = '../lib';
78321866 7 }
ed4a5f99 8}
78321866 9use Cwd;
99f36a73 10chdir 't';
ed4a5f99 11
ed4a5f99 12use strict;
99f36a73 13use Config;
e69a2255 14use File::Spec;
1279e177 15use File::Path;
ed4a5f99 16
99f36a73 17use lib File::Spec->catdir('t', 'lib');
275e8705 18use Test::More;
99f36a73 19require VMS::Filespec if $^O eq 'VMS';
275e8705 20
23bb49fa 21my $tests = 30;
14815b0c 22# _perl_abs_path() currently only works when the directory separator
23# is '/', so don't test it when it won't work.
99f36a73 24my $EXTRA_ABSPATH_TESTS = ($Config{prefix} =~ m/\//) && $^O ne 'cygwin';
25$tests += 4 if $EXTRA_ABSPATH_TESTS;
275e8705 26plan tests => $tests;
ca7ced35 27
99f36a73 28SKIP: {
b04f6d36 29 skip "no need to check for blib/ in the core", 1 if $ENV{PERL_CORE};
30 like $INC{'Cwd.pm'}, qr{blib}i, "Cwd should be loaded from blib/ during testing";
99f36a73 31}
32
ca7ced35 33my $IsVMS = $^O eq 'VMS';
e69a2255 34my $IsMacOS = $^O eq 'MacOS';
ed4a5f99 35
36# check imports
ca7ced35 37can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
38ok( !defined(&chdir), 'chdir() not exported by default' );
39ok( !defined(&abs_path), ' nor abs_path()' );
40ok( !defined(&fast_abs_path), ' nor fast_abs_path()');
41
f6342b4b 42{
43 my @fields = qw(PATH IFS CDPATH ENV BASH_ENV);
44 my $before = grep exists $ENV{$_}, @fields;
45 cwd();
46 my $after = grep exists $ENV{$_}, @fields;
47 is($before, $after, "cwd() shouldn't create spurious entries in %ENV");
48}
ed4a5f99 49
0d2079fa 50# XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
51# XXX and subsequent chdir()s can make them impossible to find
52eval { fastcwd };
53
da3f15f4 54# Must find an external pwd (or equivalent) command.
55
38f52085 56my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
da3f15f4 57my $pwd_cmd =
38f52085 58 ($^O eq "NetWare") ?
023b4a43 59 "cd" :
e69a2255 60 ($IsMacOS) ?
61 "pwd" :
38f52085 62 (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
023b4a43 63 split m/$Config{path_sep}/, $ENV{PATH})[0];
da3f15f4 64
ca7ced35 65$pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
38f52085 66if ($^O eq 'MSWin32') {
67 $pwd_cmd =~ s,/,\\,g;
68 $pwd_cmd = "$pwd_cmd /c cd";
69}
e8f7eed0 70$pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
71
ca7ced35 72SKIP: {
73 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
2390ecbc 74
d80cbc32 75 print "# native pwd = '$pwd_cmd'\n";
76
926cbafe 77 local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
78 my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
79 chomp(my $start = `$pwd_cmd_untainted`);
80
14107c42 81 # Win32's cd returns native C:\ style
2986a63f 82 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
2390ecbc 83 # DCL SHOW DEFAULT has leading spaces
ca7ced35 84 $start =~ s/^\s+// if $IsVMS;
85 SKIP: {
12b7537a 86 skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
87 skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
ca7ced35 88
164336fe 89 # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which
90 # Cwd.pm:getcwd uses) has some magic related to the PWD
91 # environment variable: if PWD is set to a directory that
92 # looks about right (guess: has the same (dev,ino) as the '.'?),
93 # the PWD is returned. However, if that path contains
94 # symlinks, the path will not be equal to the one returned by
95 # /bin/pwd (which probably uses the usual walking upwards in
96 # the path -trick). This situation is easy to reproduce since
97 # /tmp is a symlink to /private/tmp. Therefore we invalidate
98 # the PWD to force getcwd(3) to (re)compute the cwd in full.
99 # Admittedly fixing this in the Cwd module would be better
100 # long-term solution but deleting $ENV{PWD} should not be
101 # done light-heartedly. --jhi
102 delete $ENV{PWD} if $^O eq 'darwin';
103
da3f15f4 104 my $cwd = cwd;
105 my $getcwd = getcwd;
106 my $fastcwd = fastcwd;
107 my $fastgetcwd = fastgetcwd;
12b7537a 108
1c26fec0 109 is($cwd, $start, 'cwd()');
110 is($getcwd, $start, 'getcwd()');
111 is($fastcwd, $start, 'fastcwd()');
112 is($fastgetcwd, $start, 'fastgetcwd()');
da3f15f4 113 }
114}
115
ea067225 116my @test_dirs = qw{_ptrslt_ _path_ _to_ _a_ _dir_};
117my $Test_Dir = File::Spec->catdir(@test_dirs);
ca7ced35 118
889f7a4f 119mkpath([$Test_Dir], 0, 0777);
120Cwd::chdir $Test_Dir;
ca7ced35 121
ad78113d 122foreach my $func (qw(cwd getcwd fastcwd fastgetcwd)) {
123 my $result = eval "$func()";
124 is $@, '';
ea067225 125 dir_ends_with( $result, $Test_Dir, "$func()" );
ad78113d 126}
ed4a5f99 127
23bb49fa 128{
129 # Some versions of File::Path (e.g. that shipped with perl 5.8.5)
130 # call getcwd() with an argument (perhaps by calling it as a
131 # method?), so make sure that doesn't die.
132 is getcwd(), getcwd('foo'), "Call getcwd() with an argument";
133}
134
ed4a5f99 135# Cwd::chdir should also update $ENV{PWD}
ea067225 136dir_ends_with( $ENV{PWD}, $Test_Dir, 'Cwd::chdir() updates $ENV{PWD}' );
e69a2255 137my $updir = File::Spec->updir;
138Cwd::chdir $updir;
14107c42 139print "#$ENV{PWD}\n";
e69a2255 140Cwd::chdir $updir;
14107c42 141print "#$ENV{PWD}\n";
e69a2255 142Cwd::chdir $updir;
14107c42 143print "#$ENV{PWD}\n";
e69a2255 144Cwd::chdir $updir;
14107c42 145print "#$ENV{PWD}\n";
e69a2255 146Cwd::chdir $updir;
14107c42 147print "#$ENV{PWD}\n";
1279e177 148
ea067225 149rmtree($test_dirs[0], 0, 0);
1279e177 150
889f7a4f 151{
152 my $check = ($IsVMS ? qr|\b((?i)t)\]$| :
153 $IsMacOS ? qr|\bt:$| :
154 qr|\bt$| );
155
156 like($ENV{PWD}, $check);
2390ecbc 157}
ed4a5f99 158
99f36a73 159{
160 # Make sure abs_path() doesn't trample $ENV{PWD}
161 my $start_pwd = $ENV{PWD};
162 mkpath([$Test_Dir], 0, 0777);
163 Cwd::abs_path($Test_Dir);
164 is $ENV{PWD}, $start_pwd;
165 rmtree($test_dirs[0], 0, 0);
166}
167
ca7ced35 168SKIP: {
275e8705 169 skip "no symlinks on this platform", 2+$EXTRA_ABSPATH_TESTS unless $Config{d_symlink};
ca7ced35 170
171 mkpath([$Test_Dir], 0, 0777);
78321866 172 symlink $Test_Dir, "linktest";
7040f5d5 173
174 my $abs_path = Cwd::abs_path("linktest");
175 my $fast_abs_path = Cwd::fast_abs_path("linktest");
61729915 176 my $want = quotemeta(
177 File::Spec->rel2abs(
178 $ENV{PERL_CORE} ? $Test_Dir : File::Spec->catdir('t', $Test_Dir)
179 )
180 );
7040f5d5 181
61729915 182 like($abs_path, qr|$want$|i);
183 like($fast_abs_path, qr|$want$|i);
184 like(Cwd::_perl_abs_path("linktest"), qr|$want$|i) if $EXTRA_ABSPATH_TESTS;
7040f5d5 185
ea067225 186 rmtree($test_dirs[0], 0, 0);
61729915 187 1 while unlink "linktest";
ed4a5f99 188}
ea067225 189
9d7d9729 190if ($ENV{PERL_CORE}) {
191 chdir '../ext/Cwd/t';
192 unshift @INC, '../../../lib';
193}
78321866 194
195# Make sure we can run abs_path() on files, not just directories
196my $path = 'cwd.t';
9d7d9729 197path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
198path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
275e8705 199path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
200 if $EXTRA_ABSPATH_TESTS;
78321866 201
202$path = File::Spec->catfile(File::Spec->updir, 't', $path);
9d7d9729 203path_ends_with(Cwd::abs_path($path), 'cwd.t', 'abs_path() can be invoked on a file');
204path_ends_with(Cwd::fast_abs_path($path), 'cwd.t', 'fast_abs_path() can be invoked on a file');
275e8705 205path_ends_with(Cwd::_perl_abs_path($path), 'cwd.t', '_perl_abs_path() can be invoked on a file')
206 if $EXTRA_ABSPATH_TESTS;
78321866 207
208
99f36a73 209
210SKIP: {
211 my $file;
212 {
f5f48b4d 213 my $root = Cwd::abs_path(File::Spec->rootdir); # Add drive letter?
99f36a73 214 local *FH;
215 opendir FH, $root or skip("Can't opendir($root): $!", 2+$EXTRA_ABSPATH_TESTS);
216 ($file) = grep {-f $_ and not -l $_} map File::Spec->catfile($root, $_), readdir FH;
217 closedir FH;
218 }
219 skip "No plain file in root directory to test with", 2+$EXTRA_ABSPATH_TESTS unless $file;
220
221 $file = VMS::Filespec::rmsexpand($file) if $^O eq 'VMS';
222 is Cwd::abs_path($file), $file, 'abs_path() works on files in the root directory';
223 is Cwd::fast_abs_path($file), $file, 'fast_abs_path() works on files in the root directory';
224 is Cwd::_perl_abs_path($file), $file, '_perl_abs_path() works on files in the root directory'
225 if $EXTRA_ABSPATH_TESTS;
226}
227
228
ea067225 229#############################################
9d7d9729 230# These routines give us sort of a poor-man's cross-platform
231# directory or path comparison capability.
ea067225 232
9d7d9729 233sub bracketed_form_dir {
ea067225 234 return join '', map "[$_]",
235 grep length, File::Spec->splitdir(File::Spec->canonpath( shift() ));
236}
237
238sub dir_ends_with {
239 my ($dir, $expect) = (shift, shift);
9d7d9729 240 my $bracketed_expect = quotemeta bracketed_form_dir($expect);
241 like( bracketed_form_dir($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
242}
243
244sub bracketed_form_path {
245 return join '', map "[$_]",
246 grep length, File::Spec->splitpath(File::Spec->canonpath( shift() ));
247}
248
249sub path_ends_with {
250 my ($dir, $expect) = (shift, shift);
251 my $bracketed_expect = quotemeta bracketed_form_path($expect);
252 like( bracketed_form_path($dir), qr|$bracketed_expect$|i, (@_ ? shift : ()) );
ea067225 253}