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);
14 my $IsVMS = $^O eq 'VMS';
15 my $IsMacOS = $^O eq 'MacOS';
17 # For an op regression test, I don't want to rely on "use constant" working.
18 my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
20 # Might be a little early in the testing process to start using these,
21 # but I can't think of a way to write this test without them.
22 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
24 # Can't use Cwd::abs_path() because it has different ideas about
25 # path separators than File::Spec.
27 my $d = rel2abs(curdir);
29 $d = uc($d) if $IsVMS;
30 $d = lc($d) if $^O =~ /^uwin/;
36 # Let's get to a known position
38 my ($vol,$dir) = splitpath(abs_path,1);
39 my $test_dir = $IsVMS ? 'T' : 't';
40 skip("Already in t/", 2) if (splitdir($dir))[-1] eq $test_dir;
42 ok( chdir($test_dir), 'chdir($test_dir)');
43 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
49 skip("no fchdir", 16) unless $has_fchdir;
50 my $has_dirfd = ($Config{d_dirfd} || "") eq "define";
51 ok(opendir(my $dh, "."), "opendir .");
52 ok(open(my $fh, "<", "op"), "open op");
53 ok(chdir($fh), "fchdir op");
54 ok(-f "chdir.t", "verify that we are in op");
56 ok(chdir($dh), "fchdir back");
60 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
64 # same with bareword file handles
68 ok(chdir FH, "fchdir op bareword");
69 ok(-f "chdir.t", "verify that we are in op");
71 ok(chdir DH, "fchdir back bareword");
75 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
78 ok(-d "op", "verify that we are back");
80 # And now the ambiguous case
81 ok(opendir(H, "op"), "opendir op") or diag $!;
82 ok(open(H, "<", "base"), "open base") or diag $!;
83 if (($Config{d_dirfd} || "") eq "define") {
84 ok(chdir(H), "fchdir to op");
85 ok(-f "chdir.t", "verify that we are in 'op'");
90 like($@, qr/^The dirfd function is unimplemented at/,
91 "dirfd is unimplemented");
93 skip("dirfd is unimplemented");
96 ok(closedir(H), "closedir");
97 ok(chdir(H), "fchdir to base");
98 ok(-f "cond.t", "verify that we are in 'base'");
103 skip("has fchdir", 1) if $has_fchdir;
104 opendir(my $dh, "op");
105 eval { chdir($dh); };
106 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
109 # The environment variables chdir() pays attention to.
110 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
115 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
116 if( $key eq 'SYS$LOGIN' && !$IsVMS && !$IsMacOS ) {
117 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
118 is( abs_path, $Cwd, ' abs_path() did not change' );
119 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
122 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
123 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
125 is( abs_path, $Cwd, ' and back again' );
128 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
131 # Check the deprecated chdir(undef) feature.
133 ok( chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
134 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
135 is( $warning, <<WARNING, ' got uninit & deprecation warning' );
136 Use of uninitialized value in chdir at $0 line 64.
137 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
145 ok( chdir(''), "chdir('') w/ only \$ENV{$key} set" );
146 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
147 is( $warning, <<WARNING, ' got deprecation warning' );
148 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
157 foreach my $env (@magic_envs) {
158 $Saved_Env{$env} = $ENV{$env};
160 # Can't actually delete SYS$ stuff on VMS.
161 next if $IsVMS && $env eq 'SYS$LOGIN';
162 next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
164 unless ($IsMacOS) { # ENV on MacOS is "special" :-)
165 # On VMS, %ENV is many layered.
166 delete $ENV{$env} while exists $ENV{$env};
170 # The following means we won't really be testing for non-existence,
171 # but in Perl we can only delete from the process table, not the job
173 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
177 no warnings 'uninitialized';
179 # Restore the environment for VMS (and doesn't hurt for anyone else)
180 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
182 # On VMS this must be deleted or process table is wrong on exit
183 # when this script is run interactively.
184 delete $ENV{'SYS$LOGIN'} if $IsVMS;
188 foreach my $key (@magic_envs) {
189 # We're going to be using undefs a lot here.
190 no warnings 'uninitialized';
193 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
200 if (($IsVMS || $IsMacOS) && !$Config{'d_setenv'}) {
201 pass("Can't reset HOME, so chdir() test meaningless");
203 ok( !chdir(), 'chdir() w/o any ENV set' );
205 is( abs_path, $Cwd, ' abs_path() agrees' );