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