4 # We really want to know if chdir is working, as the build process will
5 # all go wrong if it is not. So avoid clearing @INC under miniperl.
6 @INC = () if defined &DynaLoader::boot_DynaLoader;
8 # We're not going to chdir() into 't' because we don't know if
9 # chdir() works! Instead, we'll hedge our bets and put both
10 # possibilities into @INC.
12 set_up_inc(qw(t . lib ../lib));
18 use Errno qw(ENOENT EBADF EINVAL);
20 my $IsVMS = $^O eq 'VMS';
22 # For an op regression test, I don't want to rely on "use constant" working.
23 my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
25 # Might be a little early in the testing process to start using these,
26 # but I can't think of a way to write this test without them.
27 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
29 # Can't use Cwd::abs_path() because it has different ideas about
30 # path separators than File::Spec.
32 my $d = rel2abs(curdir);
33 $d = lc($d) if $^O =~ /^uwin/;
39 # Let's get to a known position
41 my ($vol,$dir) = splitpath(abs_path,1);
43 my $compare_dir = (splitdir($dir))[-1];
45 # VMS is case insensitive but will preserve case in EFS mode.
46 # So we must normalize the case for the compare.
48 $compare_dir = lc($compare_dir) if $IsVMS;
49 skip("Already in t/", 2) if $compare_dir eq $test_dir;
51 ok( chdir($test_dir), 'chdir($test_dir)');
52 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
58 skip("no fchdir", 19) unless $has_fchdir;
59 my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
60 ok(opendir(my $dh, "."), "opendir .");
61 ok(open(my $fh, "<", "op"), "open op");
62 ok(chdir($fh), "fchdir op");
63 ok(-f "chdir.t", "verify that we are in op");
65 ok(chdir($dh), "fchdir back");
69 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
73 # same with bareword file handles
77 ok(chdir FH, "fchdir op bareword");
78 ok(-f "chdir.t", "verify that we are in op");
80 ok(chdir DH, "fchdir back bareword");
84 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
87 ok(-d "op", "verify that we are back");
89 ok(open(H, "<", "base"), "open base") or diag $!;
90 ok(chdir(H), "fchdir to base");
91 ok(-f "cond.t", "verify that we are in 'base'");
92 ok(close(H), "close");
96 local $SIG{__WARN__} = sub { $warn = shift };
97 ok(!chdir(H), "check we can't chdir to closed handle");
98 is(0+$!, EBADF, 'check $! set appropriately');
99 like($warn, qr/on closed filehandle H/, 'like closed');
104 local $SIG{__WARN__} = sub { $warn = shift };
105 ok(!chdir(NEVEROPENED), "check we can't chdir to never opened handle");
106 is(0+$!, EBADF, 'check $! set appropriately');
107 like($warn, qr/on unopened filehandle NEVEROPENED/, 'like never opened');
108 chdir ".." or die $!;
113 skip("has fchdir", 1) if $has_fchdir;
114 opendir(my $dh, "op");
115 eval { chdir($dh); };
116 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
119 # The environment variables chdir() pays attention to.
120 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
125 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
126 if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
127 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
128 is( abs_path, $Cwd, ' abs_path() did not change' );
129 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..4;
132 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
133 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
135 is( abs_path, $Cwd, ' and back again' );
138 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
140 ok(!chdir(''), "chdir('') no longer implied chdir()");
141 is($!+0, ENOENT, 'check $! set appropriately');
142 is($warning, '', 'should no longer warn about deprecation');
146 fresh_perl_is(<<'EOP', '', { stderr => 1 }, "check stack handling");
147 for $x (map $_+1, 1 .. 100) {
154 foreach my $env (@magic_envs) {
155 $Saved_Env{$env} = $ENV{$env};
157 # Can't actually delete SYS$ stuff on VMS.
158 next if $IsVMS && $env eq 'SYS$LOGIN';
160 # On VMS, %ENV is many layered.
161 delete $ENV{$env} while exists $ENV{$env};
164 # The following means we won't really be testing for non-existence,
165 # but in Perl we can only delete from the process table, not the job
167 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
171 no warnings 'uninitialized';
173 # Restore the environment for VMS (and doesn't hurt for anyone else)
174 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
176 # On VMS this must be deleted or process table is wrong on exit
177 # when this script is run interactively.
178 delete $ENV{'SYS$LOGIN'} if $IsVMS;
182 foreach my $key (@magic_envs) {
183 # We're going to be using undefs a lot here.
184 no warnings 'uninitialized';
187 $ENV{$key} = catdir $Cwd, 'op';
197 and skip "Can't delete SYS\$LOGIN, so chdir() test meaningless", 2;
199 ok( !chdir(), 'chdir() w/o any ENV set' );
200 is( $!+0, EINVAL, 'check $! set to EINVAL');
202 is( abs_path, $Cwd, ' abs_path() agrees' );