Re: [perl #36622] y/// at end of file
[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";
c4aca7d0 12plan(tests => 38);
8ea155d1 13
dc459aad 14my $IsVMS = $^O eq 'VMS';
15my $IsMacOS = $^O eq 'MacOS';
2d6b1654 16
8ea155d1 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.
58277c14 19use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
d9c93211 20
21# Can't use Cwd::abs_path() because it has different ideas about
d6cdadd4 22# path separators than File::Spec.
d9c93211 23sub abs_path {
1b76c3e4 24 my $d = rel2abs(curdir);
25
26 $d = uc($d) if $IsVMS;
27 $d = lc($d) if $^O =~ /^uwin/;
28 $d;
d9c93211 29}
8ea155d1 30
35ae6b54 31my $Cwd = abs_path;
8ea155d1 32
33# Let's get to a known position
34SKIP: {
58277c14 35 my ($vol,$dir) = splitpath(abs_path,1);
fb7a80d6 36 my $test_dir = $IsVMS ? 'T' : 't';
37 skip("Already in t/", 2) if (splitdir($dir))[-1] eq $test_dir;
8ea155d1 38
fb7a80d6 39 ok( chdir($test_dir), 'chdir($test_dir)');
40 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
8ea155d1 41}
42
35ae6b54 43$Cwd = abs_path;
8ea155d1 44
c4aca7d0 45SKIP: {
46 skip("no fchdir", 6) 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");
ac49b025 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 }
c4aca7d0 59 ok(-d "op", "verify that we are back");
60}
61
62SKIP: {
63 skip("has fchdir", 1) if ($Config{d_fchdir} || "") eq "define";
64 opendir(my $dh, "op");
65 eval { chdir($dh); };
66 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
67}
68
8ea155d1 69# The environment variables chdir() pays attention to.
70my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
71
35ae6b54 72sub check_env {
73 my($key) = @_;
8ea155d1 74
89eee1ed 75 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
dc459aad 76 if( $key eq 'SYS$LOGIN' && !$IsVMS && !$IsMacOS ) {
35ae6b54 77 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
78 is( abs_path, $Cwd, ' abs_path() did not change' );
d6cdadd4 79 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
8ea155d1 80 }
81 else {
89eee1ed 82 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
8ea155d1 83 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
35ae6b54 84 chdir($Cwd);
85 is( abs_path, $Cwd, ' and back again' );
86
87 my $warning = '';
88 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
89
8ea155d1 90
35ae6b54 91 # Check the deprecated chdir(undef) feature.
fb7a80d6 92#line 64
35ae6b54 93 ok( chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
94 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
95 is( $warning, <<WARNING, ' got uninit & deprecation warning' );
fb7a80d6 96Use of uninitialized value in chdir at $0 line 64.
97Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
35ae6b54 98WARNING
8ea155d1 99
35ae6b54 100 chdir($Cwd);
101
102 # Ditto chdir('').
103 $warning = '';
fb7a80d6 104#line 76
35ae6b54 105 ok( chdir(''), "chdir('') w/ only \$ENV{$key} set" );
106 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
107 is( $warning, <<WARNING, ' got deprecation warning' );
fb7a80d6 108Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
35ae6b54 109WARNING
110
111 chdir($Cwd);
112 }
8ea155d1 113}
114
fb7a80d6 115my %Saved_Env = ();
d6cdadd4 116sub clean_env {
fb7a80d6 117 foreach my $env (@magic_envs) {
118 $Saved_Env{$env} = $ENV{$env};
119
120 # Can't actually delete SYS$ stuff on VMS.
121 next if $IsVMS && $env eq 'SYS$LOGIN';
122 next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
123
dc459aad 124 unless ($IsMacOS) { # ENV on MacOS is "special" :-)
125 # On VMS, %ENV is many layered.
126 delete $ENV{$env} while exists $ENV{$env};
127 }
58277c14 128 }
fb7a80d6 129
d6cdadd4 130 # The following means we won't really be testing for non-existence,
131 # but in Perl we can only delete from the process table, not the job
132 # table.
133 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
134}
135
fb7a80d6 136END {
137 no warnings 'uninitialized';
138
139 # Restore the environment for VMS (and doesn't hurt for anyone else)
140 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
141}
142
143
35ae6b54 144foreach my $key (@magic_envs) {
8ea155d1 145 # We're going to be using undefs a lot here.
146 no warnings 'uninitialized';
147
d6cdadd4 148 clean_env;
9d4c144b 149 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
58277c14 150
35ae6b54 151 check_env($key);
152}
153
154{
d6cdadd4 155 clean_env;
dc459aad 156 if (($IsVMS || $IsMacOS) && !$Config{'d_setenv'}) {
58277c14 157 pass("Can't reset HOME, so chdir() test meaningless");
158 } else {
159 ok( !chdir(), 'chdir() w/o any ENV set' );
160 }
35ae6b54 161 is( abs_path, $Cwd, ' abs_path() agrees' );
8ea155d1 162}