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