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