This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Encode to CPAN version 2.78
[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);
03c5a851 12 require "test.pl";
201e9e2a 13 plan(tests => 47);
8ea155d1
MS
14}
15
58277c14 16use Config;
201e9e2a 17use Errno qw(ENOENT EBADF EINVAL);
8ea155d1 18
dc459aad 19my $IsVMS = $^O eq 'VMS';
2d6b1654 20
55adfc3e
NC
21# For an op regression test, I don't want to rely on "use constant" working.
22my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
23
8ea155d1
MS
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.
58277c14 26use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
d9c93211
MS
27
28# Can't use Cwd::abs_path() because it has different ideas about
d6cdadd4 29# path separators than File::Spec.
d9c93211 30sub abs_path {
1b76c3e4 31 my $d = rel2abs(curdir);
1b76c3e4
JH
32 $d = lc($d) if $^O =~ /^uwin/;
33 $d;
d9c93211 34}
8ea155d1 35
35ae6b54 36my $Cwd = abs_path;
8ea155d1
MS
37
38# Let's get to a known position
39SKIP: {
58277c14 40 my ($vol,$dir) = splitpath(abs_path,1);
6d74d930
JM
41 my $test_dir = 't';
42 my $compare_dir = (splitdir($dir))[-1];
43
44 # VMS is case insensitive but will preserve case in EFS mode.
45 # So we must normalize the case for the compare.
46
47 $compare_dir = lc($compare_dir) if $IsVMS;
48 skip("Already in t/", 2) if $compare_dir eq $test_dir;
8ea155d1 49
fb7a80d6
MS
50 ok( chdir($test_dir), 'chdir($test_dir)');
51 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
8ea155d1
MS
52}
53
35ae6b54 54$Cwd = abs_path;
8ea155d1 55
c4aca7d0 56SKIP: {
c1424f07 57 skip("no fchdir", 23) unless $has_fchdir;
4964fccb 58 my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
c4aca7d0
GA
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");
55adfc3e 63 if ($has_dirfd) {
ac49b025
MB
64 ok(chdir($dh), "fchdir back");
65 }
66 else {
67 eval { chdir($dh); };
68 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
73bf7cf9 69 chdir ".." or die $!;
ac49b025 70 }
d4ac975e
GA
71
72 # same with bareword file handles
73 no warnings 'once';
74 *DH = $dh;
75 *FH = $fh;
76 ok(chdir FH, "fchdir op bareword");
77 ok(-f "chdir.t", "verify that we are in op");
55adfc3e 78 if ($has_dirfd) {
d4ac975e
GA
79 ok(chdir DH, "fchdir back bareword");
80 }
81 else {
82 eval { chdir(DH); };
83 like($@, qr/^The dirfd function is unimplemented at/, "dirfd is unimplemented");
73bf7cf9 84 chdir ".." or die $!;
d4ac975e 85 }
c4aca7d0 86 ok(-d "op", "verify that we are back");
73bf7cf9
NC
87
88 # And now the ambiguous case
abc718f2
RGS
89 {
90 no warnings qw<io deprecated>;
91 ok(opendir(H, "op"), "opendir op") or diag $!;
92 ok(open(H, "<", "base"), "open base") or diag $!;
93 }
4964fccb 94 if ($has_dirfd) {
73bf7cf9
NC
95 ok(chdir(H), "fchdir to op");
96 ok(-f "chdir.t", "verify that we are in 'op'");
97 chdir ".." or die $!;
98 }
99 else {
100 eval { chdir(H); };
101 like($@, qr/^The dirfd function is unimplemented at/,
102 "dirfd is unimplemented");
103 SKIP: {
104 skip("dirfd is unimplemented");
105 }
106 }
107 ok(closedir(H), "closedir");
108 ok(chdir(H), "fchdir to base");
109 ok(-f "cond.t", "verify that we are in 'base'");
60121127
TC
110 ok(close(H), "close");
111 $! = 0;
03c5a851
JH
112 {
113 my $warn;
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');
118 $! = 0;
119 }
120 {
121 my $warn;
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 $!;
127 }
c4aca7d0
GA
128}
129
130SKIP: {
55adfc3e 131 skip("has fchdir", 1) if $has_fchdir;
c4aca7d0
GA
132 opendir(my $dh, "op");
133 eval { chdir($dh); };
134 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
135}
136
8ea155d1
MS
137# The environment variables chdir() pays attention to.
138my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
139
35ae6b54
MS
140sub check_env {
141 my($key) = @_;
8ea155d1 142
89eee1ed 143 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
7b903762 144 if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
35ae6b54
MS
145 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
146 is( abs_path, $Cwd, ' abs_path() did not change' );
634b12fe 147 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..4;
8ea155d1
MS
148 }
149 else {
89eee1ed 150 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
8ea155d1 151 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
35ae6b54
MS
152 chdir($Cwd);
153 is( abs_path, $Cwd, ' and back again' );
154
155 my $warning = '';
156 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
b4929cb4
TC
157 $! = 0;
158 ok(!chdir(''), "chdir('') no longer implied chdir()");
159 is($!+0, ENOENT, 'check $! set appropriately');
160 is($warning, '', 'should no longer warn about deprecation');
35ae6b54 161 }
8ea155d1
MS
162}
163
fb7a80d6 164my %Saved_Env = ();
d6cdadd4 165sub clean_env {
fb7a80d6
MS
166 foreach my $env (@magic_envs) {
167 $Saved_Env{$env} = $ENV{$env};
168
169 # Can't actually delete SYS$ stuff on VMS.
170 next if $IsVMS && $env eq 'SYS$LOGIN';
fb7a80d6 171
7b903762
RGS
172 # On VMS, %ENV is many layered.
173 delete $ENV{$env} while exists $ENV{$env};
58277c14 174 }
fb7a80d6 175
d6cdadd4
CB
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
178 # table.
179 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
180}
181
fb7a80d6
MS
182END {
183 no warnings 'uninitialized';
184
185 # Restore the environment for VMS (and doesn't hurt for anyone else)
186 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
16ed4686
JM
187
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;
fb7a80d6
MS
191}
192
193
35ae6b54 194foreach my $key (@magic_envs) {
8ea155d1
MS
195 # We're going to be using undefs a lot here.
196 no warnings 'uninitialized';
197
d6cdadd4 198 clean_env;
41349288 199 $ENV{$key} = catdir $Cwd, 'op';
58277c14 200
35ae6b54
MS
201 check_env($key);
202}
203
204{
d6cdadd4 205 clean_env;
201e9e2a
TC
206 SKIP:
207 {
41349288
CB
208 $IsVMS
209 and skip "Can't delete SYS\$LOGIN, so chdir() test meaningless", 2;
201e9e2a 210 $! = 0;
58277c14 211 ok( !chdir(), 'chdir() w/o any ENV set' );
201e9e2a 212 is( $!+0, EINVAL, 'check $! set to EINVAL');
58277c14 213 }
35ae6b54 214 is( abs_path, $Cwd, ' abs_path() agrees' );
8ea155d1 215}