This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Really get chdir.t working under miniperl
[perl5.git] / t / op / chdir.t
CommitLineData
35ae6b54
MS
1#!./perl -w
2
8ea155d1 3BEGIN {
63a9daf3
FC
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;
7
8ea155d1
MS
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.
63a9daf3 11 unshift @INC, qw(t . lib ../lib);
79c67e33 12 require "test.pl";
79c67e33 13 plan(tests => 48);
8ea155d1
MS
14}
15
58277c14 16use Config;
8ea155d1 17
dc459aad 18my $IsVMS = $^O eq 'VMS';
2d6b1654 19
6d74d930
JM
20my $vms_unix_rpt = 0;
21my $vms_efs = 0;
22if ($IsVMS) {
23 if (eval 'require VMS::Feature') {
24 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
25 $vms_efs = VMS::Feature::current("efs_charset");
26 } else {
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;
31 }
32}
33
55adfc3e
NC
34# For an op regression test, I don't want to rely on "use constant" working.
35my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
36
8ea155d1
MS
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.
58277c14 39use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
d9c93211
MS
40
41# Can't use Cwd::abs_path() because it has different ideas about
d6cdadd4 42# path separators than File::Spec.
d9c93211 43sub abs_path {
1b76c3e4 44 my $d = rel2abs(curdir);
1b76c3e4
JH
45 $d = lc($d) if $^O =~ /^uwin/;
46 $d;
d9c93211 47}
8ea155d1 48
35ae6b54 49my $Cwd = abs_path;
8ea155d1
MS
50
51# Let's get to a known position
52SKIP: {
58277c14 53 my ($vol,$dir) = splitpath(abs_path,1);
6d74d930
JM
54 my $test_dir = 't';
55 my $compare_dir = (splitdir($dir))[-1];
56
57 # VMS is case insensitive but will preserve case in EFS mode.
58 # So we must normalize the case for the compare.
59
60 $compare_dir = lc($compare_dir) if $IsVMS;
61 skip("Already in t/", 2) if $compare_dir eq $test_dir;
8ea155d1 62
fb7a80d6
MS
63 ok( chdir($test_dir), 'chdir($test_dir)');
64 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
8ea155d1
MS
65}
66
35ae6b54 67$Cwd = abs_path;
8ea155d1 68
c4aca7d0 69SKIP: {
73bf7cf9 70 skip("no fchdir", 16) unless $has_fchdir;
4964fccb 71 my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
c4aca7d0
GA
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");
55adfc3e 76 if ($has_dirfd) {
ac49b025
MB
77 ok(chdir($dh), "fchdir back");
78 }
79 else {
80 eval { chdir($dh); };
81 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
73bf7cf9 82 chdir ".." or die $!;
ac49b025 83 }
d4ac975e
GA
84
85 # same with bareword file handles
86 no warnings 'once';
87 *DH = $dh;
88 *FH = $fh;
89 ok(chdir FH, "fchdir op bareword");
90 ok(-f "chdir.t", "verify that we are in op");
55adfc3e 91 if ($has_dirfd) {
d4ac975e
GA
92 ok(chdir DH, "fchdir back bareword");
93 }
94 else {
95 eval { chdir(DH); };
96 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
73bf7cf9 97 chdir ".." or die $!;
d4ac975e 98 }
c4aca7d0 99 ok(-d "op", "verify that we are back");
73bf7cf9
NC
100
101 # And now the ambiguous case
abc718f2
RGS
102 {
103 no warnings qw<io deprecated>;
104 ok(opendir(H, "op"), "opendir op") or diag $!;
105 ok(open(H, "<", "base"), "open base") or diag $!;
106 }
4964fccb 107 if ($has_dirfd) {
73bf7cf9
NC
108 ok(chdir(H), "fchdir to op");
109 ok(-f "chdir.t", "verify that we are in 'op'");
110 chdir ".." or die $!;
111 }
112 else {
113 eval { chdir(H); };
114 like($@, qr/^The dirfd function is unimplemented at/,
115 "dirfd is unimplemented");
116 SKIP: {
117 skip("dirfd is unimplemented");
118 }
119 }
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 $!;
c4aca7d0
GA
124}
125
126SKIP: {
55adfc3e 127 skip("has fchdir", 1) if $has_fchdir;
c4aca7d0
GA
128 opendir(my $dh, "op");
129 eval { chdir($dh); };
130 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
131}
132
8ea155d1
MS
133# The environment variables chdir() pays attention to.
134my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
135
35ae6b54
MS
136sub check_env {
137 my($key) = @_;
8ea155d1 138
89eee1ed 139 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
7b903762 140 if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
35ae6b54
MS
141 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
142 is( abs_path, $Cwd, ' abs_path() did not change' );
d6cdadd4 143 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
8ea155d1
MS
144 }
145 else {
89eee1ed 146 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
8ea155d1 147 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
35ae6b54
MS
148 chdir($Cwd);
149 is( abs_path, $Cwd, ' and back again' );
150
151 my $warning = '';
152 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
153
8ea155d1 154
35ae6b54 155 # Check the deprecated chdir(undef) feature.
fb7a80d6 156#line 64
35ae6b54
MS
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' );
fb7a80d6
MS
160Use of uninitialized value in chdir at $0 line 64.
161Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
35ae6b54 162WARNING
8ea155d1 163
35ae6b54
MS
164 chdir($Cwd);
165
166 # Ditto chdir('').
167 $warning = '';
fb7a80d6 168#line 76
35ae6b54
MS
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' );
fb7a80d6 172Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
35ae6b54
MS
173WARNING
174
175 chdir($Cwd);
176 }
8ea155d1
MS
177}
178
fb7a80d6 179my %Saved_Env = ();
d6cdadd4 180sub clean_env {
fb7a80d6
MS
181 foreach my $env (@magic_envs) {
182 $Saved_Env{$env} = $ENV{$env};
183
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'};
187
7b903762
RGS
188 # On VMS, %ENV is many layered.
189 delete $ENV{$env} while exists $ENV{$env};
58277c14 190 }
fb7a80d6 191
d6cdadd4
CB
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
194 # table.
195 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
196}
197
fb7a80d6
MS
198END {
199 no warnings 'uninitialized';
200
201 # Restore the environment for VMS (and doesn't hurt for anyone else)
202 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
16ed4686
JM
203
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;
fb7a80d6
MS
207}
208
209
35ae6b54 210foreach my $key (@magic_envs) {
8ea155d1
MS
211 # We're going to be using undefs a lot here.
212 no warnings 'uninitialized';
213
d6cdadd4 214 clean_env;
9d4c144b 215 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
58277c14 216
35ae6b54
MS
217 check_env($key);
218}
219
220{
d6cdadd4 221 clean_env;
7b903762 222 if ($IsVMS && !$Config{'d_setenv'}) {
58277c14
JH
223 pass("Can't reset HOME, so chdir() test meaningless");
224 } else {
225 ok( !chdir(), 'chdir() w/o any ENV set' );
226 }
35ae6b54 227 is( abs_path, $Cwd, ' abs_path() agrees' );
8ea155d1 228}