Handle PERLIO= and document a bit.
[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";
d6cdadd4 12plan(tests => 31);
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 {
9d4c144b 24 $IsVMS ? uc(rel2abs(curdir)) : rel2abs(curdir);
d9c93211 25}
8ea155d1 26
35ae6b54 27my $Cwd = abs_path;
8ea155d1 28
29# Let's get to a known position
30SKIP: {
58277c14 31 my ($vol,$dir) = splitpath(abs_path,1);
fb7a80d6 32 my $test_dir = $IsVMS ? 'T' : 't';
33 skip("Already in t/", 2) if (splitdir($dir))[-1] eq $test_dir;
8ea155d1 34
fb7a80d6 35 ok( chdir($test_dir), 'chdir($test_dir)');
36 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
8ea155d1 37}
38
35ae6b54 39$Cwd = abs_path;
8ea155d1 40
41# The environment variables chdir() pays attention to.
42my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
43
35ae6b54 44sub check_env {
45 my($key) = @_;
8ea155d1 46
89eee1ed 47 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
dc459aad 48 if( $key eq 'SYS$LOGIN' && !$IsVMS && !$IsMacOS ) {
35ae6b54 49 ok( !chdir(), "chdir() on $^O ignores only \$ENV{$key} set" );
50 is( abs_path, $Cwd, ' abs_path() did not change' );
d6cdadd4 51 pass( " no need to test SYS\$LOGIN on $^O" ) for 1..7;
8ea155d1 52 }
53 else {
89eee1ed 54 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
8ea155d1 55 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
35ae6b54 56 chdir($Cwd);
57 is( abs_path, $Cwd, ' and back again' );
58
59 my $warning = '';
60 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
61
8ea155d1 62
35ae6b54 63 # Check the deprecated chdir(undef) feature.
fb7a80d6 64#line 64
35ae6b54 65 ok( chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
66 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
67 is( $warning, <<WARNING, ' got uninit & deprecation warning' );
fb7a80d6 68Use of uninitialized value in chdir at $0 line 64.
69Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
35ae6b54 70WARNING
8ea155d1 71
35ae6b54 72 chdir($Cwd);
73
74 # Ditto chdir('').
75 $warning = '';
fb7a80d6 76#line 76
35ae6b54 77 ok( chdir(''), "chdir('') w/ only \$ENV{$key} set" );
78 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
79 is( $warning, <<WARNING, ' got deprecation warning' );
fb7a80d6 80Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
35ae6b54 81WARNING
82
83 chdir($Cwd);
84 }
8ea155d1 85}
86
fb7a80d6 87my %Saved_Env = ();
d6cdadd4 88sub clean_env {
fb7a80d6 89 foreach my $env (@magic_envs) {
90 $Saved_Env{$env} = $ENV{$env};
91
92 # Can't actually delete SYS$ stuff on VMS.
93 next if $IsVMS && $env eq 'SYS$LOGIN';
94 next if $IsVMS && $env eq 'HOME' && !$Config{'d_setenv'};
95
dc459aad 96 unless ($IsMacOS) { # ENV on MacOS is "special" :-)
97 # On VMS, %ENV is many layered.
98 delete $ENV{$env} while exists $ENV{$env};
99 }
58277c14 100 }
fb7a80d6 101
d6cdadd4 102 # The following means we won't really be testing for non-existence,
103 # but in Perl we can only delete from the process table, not the job
104 # table.
105 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
106}
107
fb7a80d6 108END {
109 no warnings 'uninitialized';
110
111 # Restore the environment for VMS (and doesn't hurt for anyone else)
112 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
113}
114
115
35ae6b54 116foreach my $key (@magic_envs) {
8ea155d1 117 # We're going to be using undefs a lot here.
118 no warnings 'uninitialized';
119
d6cdadd4 120 clean_env;
9d4c144b 121 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
58277c14 122
35ae6b54 123 check_env($key);
124}
125
126{
d6cdadd4 127 clean_env;
dc459aad 128 if (($IsVMS || $IsMacOS) && !$Config{'d_setenv'}) {
58277c14 129 pass("Can't reset HOME, so chdir() test meaningless");
130 } else {
131 ok( !chdir(), 'chdir() w/o any ENV set' );
132 }
35ae6b54 133 is( abs_path, $Cwd, ' abs_path() agrees' );
8ea155d1 134}