ESUCCESS = 0 is not true, but exists.
[p5sagit/p5-mst-13.2.git] / ext / Cwd / t / cwd.t
CommitLineData
5aa08720 1#!./perl
ed4a5f99 2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use Config;
9use Cwd;
10use strict;
11use warnings;
e69a2255 12use File::Spec;
1279e177 13use File::Path;
ed4a5f99 14
ca7ced35 15use Test::More tests => 16;
16
17my $IsVMS = $^O eq 'VMS';
e69a2255 18my $IsMacOS = $^O eq 'MacOS';
ed4a5f99 19
20# check imports
ca7ced35 21can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
22ok( !defined(&chdir), 'chdir() not exported by default' );
23ok( !defined(&abs_path), ' nor abs_path()' );
24ok( !defined(&fast_abs_path), ' nor fast_abs_path()');
25
ed4a5f99 26
0d2079fa 27# XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
28# XXX and subsequent chdir()s can make them impossible to find
29eval { fastcwd };
30
da3f15f4 31# Must find an external pwd (or equivalent) command.
32
38f52085 33my $pwd = $^O eq 'MSWin32' ? "cmd" : "pwd";
da3f15f4 34my $pwd_cmd =
38f52085 35 ($^O eq "NetWare") ?
023b4a43 36 "cd" :
e69a2255 37 ($IsMacOS) ?
38 "pwd" :
38f52085 39 (grep { -x && -f } map { "$_/$pwd$Config{exe_ext}" }
023b4a43 40 split m/$Config{path_sep}/, $ENV{PATH})[0];
da3f15f4 41
ca7ced35 42$pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
38f52085 43if ($^O eq 'MSWin32') {
44 $pwd_cmd =~ s,/,\\,g;
45 $pwd_cmd = "$pwd_cmd /c cd";
46}
e8f7eed0 47$pwd_cmd =~ s=\\=/=g if ($^O eq 'dos');
48
ca7ced35 49SKIP: {
50 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
2390ecbc 51
d80cbc32 52 print "# native pwd = '$pwd_cmd'\n";
53
926cbafe 54 local @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
55 my ($pwd_cmd_untainted) = $pwd_cmd =~ /^(.+)$/; # Untaint.
56 chomp(my $start = `$pwd_cmd_untainted`);
57
14107c42 58 # Win32's cd returns native C:\ style
2986a63f 59 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
2390ecbc 60 # DCL SHOW DEFAULT has leading spaces
ca7ced35 61 $start =~ s/^\s+// if $IsVMS;
62 SKIP: {
12b7537a 63 skip("'$pwd_cmd' failed, nothing to test against", 4) if $?;
64 skip("/afs seen, paths unlikely to match", 4) if $start =~ m|/afs/|;
ca7ced35 65
164336fe 66 # Darwin's getcwd(3) (which Cwd.xs:bsd_realpath() uses which
67 # Cwd.pm:getcwd uses) has some magic related to the PWD
68 # environment variable: if PWD is set to a directory that
69 # looks about right (guess: has the same (dev,ino) as the '.'?),
70 # the PWD is returned. However, if that path contains
71 # symlinks, the path will not be equal to the one returned by
72 # /bin/pwd (which probably uses the usual walking upwards in
73 # the path -trick). This situation is easy to reproduce since
74 # /tmp is a symlink to /private/tmp. Therefore we invalidate
75 # the PWD to force getcwd(3) to (re)compute the cwd in full.
76 # Admittedly fixing this in the Cwd module would be better
77 # long-term solution but deleting $ENV{PWD} should not be
78 # done light-heartedly. --jhi
79 delete $ENV{PWD} if $^O eq 'darwin';
80
da3f15f4 81 my $cwd = cwd;
82 my $getcwd = getcwd;
83 my $fastcwd = fastcwd;
84 my $fastgetcwd = fastgetcwd;
12b7537a 85
1c26fec0 86 is($cwd, $start, 'cwd()');
87 is($getcwd, $start, 'getcwd()');
88 is($fastcwd, $start, 'fastcwd()');
89 is($fastgetcwd, $start, 'fastgetcwd()');
da3f15f4 90 }
91}
92
ca7ced35 93my $Top_Test_Dir = '_ptrslt_';
94my $Test_Dir = "$Top_Test_Dir/_path_/_to_/_a_/_dir_";
95my $want = "t/$Test_Dir";
96if( $IsVMS ) {
97 # translate the unixy path to VMSish
ca7ced35 98 $want =~ s|/|\.|g;
99 $want .= '\]';
b4558e59 100 $want = '((?i)' . $want . ')'; # might be ODS-2 or ODS-5
e69a2255 101} elsif ( $IsMacOS ) {
102 $_ = ":$_" for ($Top_Test_Dir, $Test_Dir);
103 s|/|:|g, s|$|:| for ($want, $Test_Dir);
ca7ced35 104}
105
106mkpath(["$Test_Dir"], 0, 0777);
107Cwd::chdir "$Test_Dir";
108
109like(cwd(), qr|$want$|, 'chdir() + cwd()');
110like(getcwd(), qr|$want$|, ' + getcwd()');
111like(fastcwd(), qr|$want$|, ' + fastcwd()');
112like(fastgetcwd(), qr|$want$|, ' + fastgetcwd()');
ed4a5f99 113
114# Cwd::chdir should also update $ENV{PWD}
ca7ced35 115like($ENV{PWD}, qr|$want$|, 'Cwd::chdir() updates $ENV{PWD}');
e69a2255 116my $updir = File::Spec->updir;
117Cwd::chdir $updir;
14107c42 118print "#$ENV{PWD}\n";
e69a2255 119Cwd::chdir $updir;
14107c42 120print "#$ENV{PWD}\n";
e69a2255 121Cwd::chdir $updir;
14107c42 122print "#$ENV{PWD}\n";
e69a2255 123Cwd::chdir $updir;
14107c42 124print "#$ENV{PWD}\n";
e69a2255 125Cwd::chdir $updir;
14107c42 126print "#$ENV{PWD}\n";
1279e177 127
ca7ced35 128rmtree([$Top_Test_Dir], 0, 0);
1279e177 129
ca7ced35 130if ($IsVMS) {
b4558e59 131 like($ENV{PWD}, qr|\b((?i)t)\]$|);
2390ecbc 132}
e69a2255 133elsif ($IsMacOS) {
134 like($ENV{PWD}, qr|\bt:$|);
135}
2390ecbc 136else {
ca7ced35 137 like($ENV{PWD}, qr|\bt$|);
2390ecbc 138}
ed4a5f99 139
ca7ced35 140SKIP: {
141 skip "no symlinks on this platform", 2 unless $Config{d_symlink};
142
143 mkpath([$Test_Dir], 0, 0777);
144 symlink $Test_Dir => "linktest";
7040f5d5 145
146 my $abs_path = Cwd::abs_path("linktest");
147 my $fast_abs_path = Cwd::fast_abs_path("linktest");
16398b42 148 my $want = File::Spec->catdir("t", $Test_Dir);
7040f5d5 149
ca7ced35 150 like($abs_path, qr|$want$|);
151 like($fast_abs_path, qr|$want$|);
7040f5d5 152
ca7ced35 153 rmtree([$Top_Test_Dir], 0, 0);
7040f5d5 154 unlink "linktest";
ed4a5f99 155}