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