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
1 #!./perl -w
2
3 BEGIN {
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);
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}) {
12         push @INC, qw(dist/PathTools/lib    dist/PathTools
13                    ../dist/PathTools/lib ../dist/PathTools);
14     }
15     plan(tests => 48);
16 }
17
18 use Config;
19
20 my $IsVMS   = $^O eq 'VMS';
21
22 my $vms_unix_rpt = 0;
23 my $vms_efs = 0;
24 if ($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
36 # For an op regression test, I don't want to rely on "use constant" working.
37 my $has_fchdir = ($Config{d_fchdir} || "") eq "define";
38
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.
41 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
42
43 # Can't use Cwd::abs_path() because it has different ideas about
44 # path separators than File::Spec.
45 sub abs_path {
46     my $d = rel2abs(curdir);
47     $d = lc($d) if $^O =~ /^uwin/;
48     $d;
49 }
50
51 my $Cwd = abs_path;
52
53 # Let's get to a known position
54 SKIP: {
55     my ($vol,$dir) = splitpath(abs_path,1);
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;
64
65     ok( chdir($test_dir),     'chdir($test_dir)');
66     is( abs_path, catdir($Cwd, $test_dir),    '  abs_path() agrees' );
67 }
68
69 $Cwd = abs_path;
70
71 SKIP: {
72     skip("no fchdir", 16) unless $has_fchdir;
73     my $has_dirfd = ($Config{d_dirfd} || $Config{d_dir_dd_fd} || "") eq "define";
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");
78     if ($has_dirfd) {
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");
84        chdir ".." or die $!;
85     }
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");
93     if ($has_dirfd) {
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");
99        chdir ".." or die $!;
100     }
101     ok(-d "op", "verify that we are back");
102
103     # And now the ambiguous case
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     }
109     if ($has_dirfd) {
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 $!;
126 }
127
128 SKIP: {
129     skip("has fchdir", 1) if $has_fchdir;
130     opendir(my $dh, "op");
131     eval { chdir($dh); };
132     like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
133 }
134
135 # The environment variables chdir() pays attention to.
136 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
137
138 sub check_env {
139     my($key) = @_;
140
141     # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
142     if( $key eq 'SYS$LOGIN' && !$IsVMS ) {
143         ok( !chdir(),         "chdir() on $^O ignores only \$ENV{$key} set" );
144         is( abs_path, $Cwd,   '  abs_path() did not change' );
145         pass( "  no need to test SYS\$LOGIN on $^O" ) for 1..7;
146     }
147     else {
148         ok( chdir(),              "chdir() w/ only \$ENV{$key} set" );
149         is( abs_path, $ENV{$key}, '  abs_path() agrees' );
150         chdir($Cwd);
151         is( abs_path, $Cwd,       '  and back again' );
152
153         my $warning = '';
154         local $SIG{__WARN__} = sub { $warning .= join '', @_ };
155
156
157         # Check the deprecated chdir(undef) feature.
158 #line 64
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' );
162 Use of uninitialized value in chdir at $0 line 64.
163 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
164 WARNING
165
166         chdir($Cwd);
167
168         # Ditto chdir('').
169         $warning = '';
170 #line 76
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' );
174 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
175 WARNING
176
177         chdir($Cwd);
178     }
179 }
180
181 my %Saved_Env = ();
182 sub clean_env {
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
190         # On VMS, %ENV is many layered.
191         delete $ENV{$env} while exists $ENV{$env};
192     }
193
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
200 END {
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};
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;
209 }
210
211
212 foreach my $key (@magic_envs) {
213     # We're going to be using undefs a lot here.
214     no warnings 'uninitialized';
215
216     clean_env;
217     $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
218
219     check_env($key);
220 }
221
222 {
223     clean_env;
224     if ($IsVMS && !$Config{'d_setenv'}) {
225         pass("Can't reset HOME, so chdir() test meaningless");
226     } else {
227         ok( !chdir(),                   'chdir() w/o any ENV set' );
228     }
229     is( abs_path, $Cwd,             '  abs_path() agrees' );
230 }