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