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