1. Typo detected by Schwern (#25151)
[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");
51 ok(chdir($dh), "fchdir back");
52 ok(-d "op", "verify that we are back");
53}
54
55SKIP: {
56 skip("has fchdir", 1) if ($Config{d_fchdir} || "") eq "define";
57 opendir(my $dh, "op");
58 eval { chdir($dh); };
59 like($@, qr/^The fchdir function is unimplemented at/, "fchdir is unimplemented");
60}
61
8ea155d1 62# The environment variables chdir() pays attention to.
63my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
64
35ae6b54 65sub check_env {
66 my($key) = @_;
8ea155d1 67
89eee1ed 68 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
dc459aad 69 if( $key eq 'SYS$LOGIN' && !$IsVMS && !$IsMacOS ) {
35ae6b54 70 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
71 is( abs_path, $Cwd, ' abs_path() did not change' );
d6cdadd4 72 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
8ea155d1 73 }
74 else {
89eee1ed 75 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
8ea155d1 76 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
35ae6b54 77 chdir($Cwd);
78 is( abs_path, $Cwd, ' and back again' );
79
80 my $warning = '';
81 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
82
8ea155d1 83
35ae6b54 84 # Check the deprecated chdir(undef) feature.
fb7a80d6 85#line 64
35ae6b54 86 ok( chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
87 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
88 is( $warning, <<WARNING, ' got uninit & deprecation warning' );
fb7a80d6 89Use of uninitialized value in chdir at $0 line 64.
90Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
35ae6b54 91WARNING
8ea155d1 92
35ae6b54 93 chdir($Cwd);
94
95 # Ditto chdir('').
96 $warning = '';
fb7a80d6 97#line 76
35ae6b54 98 ok( chdir(''), "chdir('') w/ only \$ENV{$key} set" );
99 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
100 is( $warning, <<WARNING, ' got deprecation warning' );
fb7a80d6 101Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
35ae6b54 102WARNING
103
104 chdir($Cwd);
105 }
8ea155d1 106}
107
fb7a80d6 108my %Saved_Env = ();
d6cdadd4 109sub clean_env {
fb7a80d6 110 foreach my $env (@magic_envs) {
111 $Saved_Env{$env} = $ENV{$env};
112
113 # Can't actually delete SYS$ stuff on VMS.
114 next if $IsVMS && $env eq 'SYS$LOGIN';
115 next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
116
dc459aad 117 unless ($IsMacOS) { # ENV on MacOS is "special" :-)
118 # On VMS, %ENV is many layered.
119 delete $ENV{$env} while exists $ENV{$env};
120 }
58277c14 121 }
fb7a80d6 122
d6cdadd4 123 # The following means we won't really be testing for non-existence,
124 # but in Perl we can only delete from the process table, not the job
125 # table.
126 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
127}
128
fb7a80d6 129END {
130 no warnings 'uninitialized';
131
132 # Restore the environment for VMS (and doesn't hurt for anyone else)
133 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
134}
135
136
35ae6b54 137foreach my $key (@magic_envs) {
8ea155d1 138 # We're going to be using undefs a lot here.
139 no warnings 'uninitialized';
140
d6cdadd4 141 clean_env;
9d4c144b 142 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
58277c14 143
35ae6b54 144 check_env($key);
145}
146
147{
d6cdadd4 148 clean_env;
dc459aad 149 if (($IsVMS || $IsMacOS) && !$Config{'d_setenv'}) {
58277c14 150 pass("Can't reset HOME, so chdir() test meaningless");
151 } else {
152 ok( !chdir(), 'chdir() w/o any ENV set' );
153 }
35ae6b54 154 is( abs_path, $Cwd, ' abs_path() agrees' );
8ea155d1 155}