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