Refactor the "has fchdir" and "has dirfd" logic to one place.
[p5sagit/p5-mst-13.2.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 }
9
10 use Config;
11 require "test.pl";
12 plan(tests => 41);
13
14 my $IsVMS   = $^O eq 'VMS';
15 my $IsMacOS = $^O eq 'MacOS';
16
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
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.
22 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
23
24 # Can't use Cwd::abs_path() because it has different ideas about
25 # path separators than File::Spec.
26 sub abs_path {
27     my $d = rel2abs(curdir);
28
29     $d = uc($d) if $IsVMS;
30     $d = lc($d) if $^O =~ /^uwin/;
31     $d;
32 }
33
34 my $Cwd = abs_path;
35
36 # Let's get to a known position
37 SKIP: {
38     my ($vol,$dir) = splitpath(abs_path,1);
39     my $test_dir = $IsVMS ? 'T' : 't';
40     skip("Already in t/", 2) if (splitdir($dir))[-1] eq $test_dir;
41
42     ok( chdir($test_dir),     'chdir($test_dir)');
43     is( abs_path, catdir($Cwd, $test_dir),    '  abs_path() agrees' );
44 }
45
46 $Cwd = abs_path;
47
48 SKIP: {
49     skip("no fchdir", 9) unless $has_fchdir;
50     my $has_dirfd = ($Config{d_dirfd} || "") eq "define";
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");
55     if ($has_dirfd) {
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");
61        chdir "..";
62     }
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");
70     if ($has_dirfd) {
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");
76        chdir "..";
77     }
78     ok(-d "op", "verify that we are back");
79 }
80
81 SKIP: {
82     skip("has fchdir", 1) if $has_fchdir;
83     opendir(my $dh, "op");
84     eval { chdir($dh); };
85     like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
86 }
87
88 # The environment variables chdir() pays attention to.
89 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
90
91 sub check_env {
92     my($key) = @_;
93
94     # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
95     if( $key eq 'SYS$LOGIN' && !$IsVMS && !$IsMacOS ) {
96         ok( !chdir(),         "chdir() on $^O ignores only \$ENV{$key} set" );
97         is( abs_path, $Cwd,   '  abs_path() did not change' );
98         pass( "  no need to test SYS\$LOGIN on $^O" ) for 1..7;
99     }
100     else {
101         ok( chdir(),              "chdir() w/ only \$ENV{$key} set" );
102         is( abs_path, $ENV{$key}, '  abs_path() agrees' );
103         chdir($Cwd);
104         is( abs_path, $Cwd,       '  and back again' );
105
106         my $warning = '';
107         local $SIG{__WARN__} = sub { $warning .= join '', @_ };
108
109
110         # Check the deprecated chdir(undef) feature.
111 #line 64
112         ok( chdir(undef),           "chdir(undef) w/ only \$ENV{$key} set" );
113         is( abs_path, $ENV{$key},   '  abs_path() agrees' );
114         is( $warning,  <<WARNING,   '  got uninit & deprecation warning' );
115 Use of uninitialized value in chdir at $0 line 64.
116 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
117 WARNING
118
119         chdir($Cwd);
120
121         # Ditto chdir('').
122         $warning = '';
123 #line 76
124         ok( chdir(''),              "chdir('') w/ only \$ENV{$key} set" );
125         is( abs_path, $ENV{$key},   '  abs_path() agrees' );
126         is( $warning,  <<WARNING,   '  got deprecation warning' );
127 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
128 WARNING
129
130         chdir($Cwd);
131     }
132 }
133
134 my %Saved_Env = ();
135 sub clean_env {
136     foreach my $env (@magic_envs) {
137         $Saved_Env{$env} = $ENV{$env};
138
139         # Can't actually delete SYS$ stuff on VMS.
140         next if $IsVMS && $env eq 'SYS$LOGIN';
141         next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
142
143         unless ($IsMacOS) { # ENV on MacOS is "special" :-)
144             # On VMS, %ENV is many layered.
145             delete $ENV{$env} while exists $ENV{$env};
146         }
147     }
148
149     # The following means we won't really be testing for non-existence,
150     # but in Perl we can only delete from the process table, not the job 
151     # table.
152     $ENV{'SYS$LOGIN'} = '' if $IsVMS;
153 }
154
155 END {
156     no warnings 'uninitialized';
157
158     # Restore the environment for VMS (and doesn't hurt for anyone else)
159     @ENV{@magic_envs} = @Saved_Env{@magic_envs};
160
161     # On VMS this must be deleted or process table is wrong on exit
162     # when this script is run interactively.
163     delete $ENV{'SYS$LOGIN'} if $IsVMS;
164 }
165
166
167 foreach my $key (@magic_envs) {
168     # We're going to be using undefs a lot here.
169     no warnings 'uninitialized';
170
171     clean_env;
172     $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
173
174     check_env($key);
175 }
176
177 {
178     clean_env;
179     if (($IsVMS || $IsMacOS) && !$Config{'d_setenv'}) {
180         pass("Can't reset HOME, so chdir() test meaningless");
181     } else {
182         ok( !chdir(),                   'chdir() w/o any ENV set' );
183     }
184     is( abs_path, $Cwd,             '  abs_path() agrees' );
185 }