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