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);
18 my $IsVMS = $^O eq 'VMS';
23 if (eval 'require VMS::Feature') {
24 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
25 $vms_efs = VMS::Feature::current("efs_charset");
27 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
28 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
29 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
30 $vms_efs = $efs_charset =~ /^[ET1]/i;
34 # For an op regression test, I don't want to rely on "use constant" working.
35 my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
37 # Might be a little early in the testing process to start using these,
38 # but I can't think of a way to write this test without them.
39 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
41 # Can't use Cwd::abs_path() because it has different ideas about
42 # path separators than File::Spec.
44 my $d = rel2abs(curdir);
45 $d = lc($d) if $^O =~ /^uwin/;
51 # Let's get to a known position
53 my ($vol,$dir) = splitpath(abs_path,1);
55 my $compare_dir = (splitdir($dir))[-1];
57 # VMS is case insensitive but will preserve case in EFS mode.
58 # So we must normalize the case for the compare.
60 $compare_dir = lc($compare_dir) if $IsVMS;
61 skip("Already in t/", 2) if $compare_dir eq $test_dir;
63 ok( chdir($test_dir), 'chdir($test_dir)');
64 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
70 skip("no fchdir", 16) unless $has_fchdir;
71 my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
72 ok(opendir(my $dh, "."), "opendir .");
73 ok(open(my $fh, "<", "op"), "open op");
74 ok(chdir($fh), "fchdir op");
75 ok(-f "chdir.t", "verify that we are in op");
77 ok(chdir($dh), "fchdir back");
81 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
85 # same with bareword file handles
89 ok(chdir FH, "fchdir op bareword");
90 ok(-f "chdir.t", "verify that we are in op");
92 ok(chdir DH, "fchdir back bareword");
96 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
99 ok(-d "op", "verify that we are back");
101 # And now the ambiguous case
103 no warnings qw<io deprecated>;
104 ok(opendir(H, "op"), "opendir op") or diag $!;
105 ok(open(H, "<", "base"), "open base") or diag $!;
108 ok(chdir(H), "fchdir to op");
109 ok(-f "chdir.t", "verify that we are in 'op'");
110 chdir ".." or die $!;
114 like($@, qr/^The dirfd function is unimplemented at/,
115 "dirfd is unimplemented");
117 skip("dirfd is unimplemented");
120 ok(closedir(H), "closedir");
121 ok(chdir(H), "fchdir to base");
122 ok(-f "cond.t", "verify that we are in 'base'");
123 chdir ".." or die $!;
127 skip("has fchdir", 1) if $has_fchdir;
128 opendir(my $dh, "op");
129 eval { chdir($dh); };
130 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
133 # The environment variables chdir() pays attention to.
134 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
139 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
140 if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
141 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
142 is( abs_path, $Cwd, ' abs_path() did not change' );
143 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
146 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
147 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
149 is( abs_path, $Cwd, ' and back again' );
152 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
155 # Check the deprecated chdir(undef) feature.
157 ok( chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
158 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
159 is( $warning, <<WARNING, ' got uninit & deprecation warning' );
160 Use of uninitialized value in chdir at $0 line 64.
161 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
169 ok( chdir(''), "chdir('') w/ only \$ENV{$key} set" );
170 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
171 is( $warning, <<WARNING, ' got deprecation warning' );
172 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
181 foreach my $env (@magic_envs) {
182 $Saved_Env{$env} = $ENV{$env};
184 # Can't actually delete SYS$ stuff on VMS.
185 next if $IsVMS && $env eq 'SYS$LOGIN';
186 next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
188 # On VMS, %ENV is many layered.
189 delete $ENV{$env} while exists $ENV{$env};
192 # The following means we won't really be testing for non-existence,
193 # but in Perl we can only delete from the process table, not the job
195 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
199 no warnings 'uninitialized';
201 # Restore the environment for VMS (and doesn't hurt for anyone else)
202 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
204 # On VMS this must be deleted or process table is wrong on exit
205 # when this script is run interactively.
206 delete $ENV{'SYS$LOGIN'} if $IsVMS;
210 foreach my $key (@magic_envs) {
211 # We're going to be using undefs a lot here.
212 no warnings 'uninitialized';
215 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
222 if ($IsVMS && !$Config{'d_setenv'}) {
223 pass("Can't reset HOME, so chdir() test meaningless");
225 ok( !chdir(), 'chdir() w/o any ENV set' );
227 is( abs_path, $Cwd, ' abs_path() agrees' );