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