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