4 # We're not going to chdir() into 't' because we don't know if
5 # chdir() works! Instead, we'll hedge our bets and put both
6 # possibilities into @INC.
7 @INC = qw(t . lib ../lib);
9 # Really want to know if chdir is working, as the build process will all go
11 if (is_miniperl() && !eval {require File::Spec::Functions; 1}) {
12 push @INC, qw(dist/PathTools/lib dist/PathTools
13 ../dist/PathTools/lib ../dist/PathTools);
20 my $IsVMS = $^O eq 'VMS';
25 if (eval 'require VMS::Feature') {
26 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
27 $vms_efs = VMS::Feature::current("efs_charset");
29 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
30 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
31 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
32 $vms_efs = $efs_charset =~ /^[ET1]/i;
36 # For an op regression test, I don't want to rely on "use constant" working.
37 my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
39 # Might be a little early in the testing process to start using these,
40 # but I can't think of a way to write this test without them.
41 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
43 # Can't use Cwd::abs_path() because it has different ideas about
44 # path separators than File::Spec.
46 my $d = rel2abs(curdir);
47 $d = lc($d) if $^O =~ /^uwin/;
53 # Let's get to a known position
55 my ($vol,$dir) = splitpath(abs_path,1);
57 my $compare_dir = (splitdir($dir))[-1];
59 # VMS is case insensitive but will preserve case in EFS mode.
60 # So we must normalize the case for the compare.
62 $compare_dir = lc($compare_dir) if $IsVMS;
63 skip("Already in t/", 2) if $compare_dir eq $test_dir;
65 ok( chdir($test_dir), 'chdir($test_dir)');
66 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
72 skip("no fchdir", 16) unless $has_fchdir;
73 my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
74 ok(opendir(my $dh, "."), "opendir .");
75 ok(open(my $fh, "<", "op"), "open op");
76 ok(chdir($fh), "fchdir op");
77 ok(-f "chdir.t", "verify that we are in op");
79 ok(chdir($dh), "fchdir back");
83 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
87 # same with bareword file handles
91 ok(chdir FH, "fchdir op bareword");
92 ok(-f "chdir.t", "verify that we are in op");
94 ok(chdir DH, "fchdir back bareword");
98 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
101 ok(-d "op", "verify that we are back");
103 # And now the ambiguous case
105 no warnings qw<io deprecated>;
106 ok(opendir(H, "op"), "opendir op") or diag $!;
107 ok(open(H, "<", "base"), "open base") or diag $!;
110 ok(chdir(H), "fchdir to op");
111 ok(-f "chdir.t", "verify that we are in 'op'");
112 chdir ".." or die $!;
116 like($@, qr/^The dirfd function is unimplemented at/,
117 "dirfd is unimplemented");
119 skip("dirfd is unimplemented");
122 ok(closedir(H), "closedir");
123 ok(chdir(H), "fchdir to base");
124 ok(-f "cond.t", "verify that we are in 'base'");
125 chdir ".." or die $!;
129 skip("has fchdir", 1) if $has_fchdir;
130 opendir(my $dh, "op");
131 eval { chdir($dh); };
132 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
135 # The environment variables chdir() pays attention to.
136 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
141 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
142 if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
143 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
144 is( abs_path, $Cwd, ' abs_path() did not change' );
145 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
148 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
149 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
151 is( abs_path, $Cwd, ' and back again' );
154 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
157 # Check the deprecated chdir(undef) feature.
159 ok( chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
160 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
161 is( $warning, <<WARNING, ' got uninit & deprecation warning' );
162 Use of uninitialized value in chdir at $0 line 64.
163 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
171 ok( chdir(''), "chdir('') w/ only \$ENV{$key} set" );
172 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
173 is( $warning, <<WARNING, ' got deprecation warning' );
174 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
183 foreach my $env (@magic_envs) {
184 $Saved_Env{$env} = $ENV{$env};
186 # Can't actually delete SYS$ stuff on VMS.
187 next if $IsVMS && $env eq 'SYS$LOGIN';
188 next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
190 # On VMS, %ENV is many layered.
191 delete $ENV{$env} while exists $ENV{$env};
194 # The following means we won't really be testing for non-existence,
195 # but in Perl we can only delete from the process table, not the job
197 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
201 no warnings 'uninitialized';
203 # Restore the environment for VMS (and doesn't hurt for anyone else)
204 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
206 # On VMS this must be deleted or process table is wrong on exit
207 # when this script is run interactively.
208 delete $ENV{'SYS$LOGIN'} if $IsVMS;
212 foreach my $key (@magic_envs) {
213 # We're going to be using undefs a lot here.
214 no warnings 'uninitialized';
217 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
224 if ($IsVMS && !$Config{'d_setenv'}) {
225 pass("Can't reset HOME, so chdir() test meaningless");
227 ok( !chdir(), 'chdir() w/o any ENV set' );
229 is( abs_path, $Cwd, ' abs_path() agrees' );