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.
11 unshift @INC, qw(t . lib ../lib);
17 use Errno qw(ENOENT EBADF EINVAL);
19 my $IsVMS = $^O eq 'VMS';
21 # For an op regression test, I don't want to rely on "use constant" working.
22 my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
24 # Might be a little early in the testing process to start using these,
25 # but I can't think of a way to write this test without them.
26 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
28 # Can't use Cwd::abs_path() because it has different ideas about
29 # path separators than File::Spec.
31 my $d = rel2abs(curdir);
32 $d = lc($d) if $^O =~ /^uwin/;
38 # Let's get to a known position
40 my ($vol,$dir) = splitpath(abs_path,1);
42 my $compare_dir = (splitdir($dir))[-1];
44 # VMS is case insensitive but will preserve case in EFS mode.
45 # So we must normalize the case for the compare.
47 $compare_dir = lc($compare_dir) if $IsVMS;
48 skip("Already in t/", 2) if $compare_dir eq $test_dir;
50 ok( chdir($test_dir), 'chdir($test_dir)');
51 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
57 skip("no fchdir", 23) unless $has_fchdir;
58 my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
59 ok(opendir(my $dh, "."), "opendir .");
60 ok(open(my $fh, "<", "op"), "open op");
61 ok(chdir($fh), "fchdir op");
62 ok(-f "chdir.t", "verify that we are in op");
64 ok(chdir($dh), "fchdir back");
68 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
72 # same with bareword file handles
76 ok(chdir FH, "fchdir op bareword");
77 ok(-f "chdir.t", "verify that we are in op");
79 ok(chdir DH, "fchdir back bareword");
83 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
86 ok(-d "op", "verify that we are back");
88 # And now the ambiguous case
90 no warnings qw<io deprecated>;
91 ok(opendir(H, "op"), "opendir op") or diag $!;
92 ok(open(H, "<", "base"), "open base") or diag $!;
95 ok(chdir(H), "fchdir to op");
96 ok(-f "chdir.t", "verify that we are in 'op'");
101 like($@, qr/^The dirfd function is unimplemented at/,
102 "dirfd is unimplemented");
104 skip("dirfd is unimplemented");
107 ok(closedir(H), "closedir");
108 ok(chdir(H), "fchdir to base");
109 ok(-f "cond.t", "verify that we are in 'base'");
110 ok(close(H), "close");
114 local $SIG{__WARN__} = sub { $warn = shift };
115 ok(!chdir(H), "check we can't chdir to closed handle");
116 is(0+$!, EBADF, 'check $! set appropriately');
117 like($warn, qr/on closed filehandle H/, 'like closed');
122 local $SIG{__WARN__} = sub { $warn = shift };
123 ok(!chdir(NEVEROPENED), "check we can't chdir to never opened handle");
124 is(0+$!, EBADF, 'check $! set appropriately');
125 like($warn, qr/on unopened filehandle NEVEROPENED/, 'like never opened');
126 chdir ".." or die $!;
131 skip("has fchdir", 1) if $has_fchdir;
132 opendir(my $dh, "op");
133 eval { chdir($dh); };
134 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
137 # The environment variables chdir() pays attention to.
138 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
143 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
144 if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
145 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
146 is( abs_path, $Cwd, ' abs_path() did not change' );
147 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..4;
150 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
151 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
153 is( abs_path, $Cwd, ' and back again' );
156 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
158 ok(!chdir(''), "chdir('') no longer implied chdir()");
159 is($!+0, ENOENT, 'check $! set appropriately');
160 is($warning, '', 'should no longer warn about deprecation');
166 foreach my $env (@magic_envs) {
167 $Saved_Env{$env} = $ENV{$env};
169 # Can't actually delete SYS$ stuff on VMS.
170 next if $IsVMS && $env eq 'SYS$LOGIN';
172 # On VMS, %ENV is many layered.
173 delete $ENV{$env} while exists $ENV{$env};
176 # The following means we won't really be testing for non-existence,
177 # but in Perl we can only delete from the process table, not the job
179 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
183 no warnings 'uninitialized';
185 # Restore the environment for VMS (and doesn't hurt for anyone else)
186 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
188 # On VMS this must be deleted or process table is wrong on exit
189 # when this script is run interactively.
190 delete $ENV{'SYS$LOGIN'} if $IsVMS;
194 foreach my $key (@magic_envs) {
195 # We're going to be using undefs a lot here.
196 no warnings 'uninitialized';
199 $ENV{$key} = catdir $Cwd, 'op';
209 and skip "Can't delete SYS\$LOGIN, so chdir() test meaningless", 2;
211 ok( !chdir(), 'chdir() w/o any ENV set' );
212 is( $!+0, EINVAL, 'check $! set to EINVAL');
214 is( abs_path, $Cwd, ' abs_path() agrees' );