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