chdir.t finally (I hope)
[p5sagit/p5-mst-13.2.git] / t / op / chdir.t
1 #!./perl -w
2
3 BEGIN {
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.
7     @INC = qw(t . lib ../lib);
8 }
9
10 require "test.pl";
11 plan(tests => 31);
12
13 my $IsVMS = $^O eq 'VMS';
14 my ($saved_sys_login);
15 BEGIN {
16     $saved_sys_login = $ENV{'SYS$LOGIN'};
17 }
18 END {
19     $ENV{'SYS$LOGIN'} = $saved_sys_login;
20 }
21
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.
24 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs);
25
26 # Can't use Cwd::abs_path() because it has different ideas about
27 # path separators than File::Spec.
28 sub abs_path {
29     $IsVMS ? uc(rel2abs(curdir)) : rel2abs(curdir);
30 }
31
32 my $Cwd = abs_path;
33
34 # Let's get to a known position
35 SKIP: {
36     skip("Already in t/", 2) if (splitdir(abs_path))[-1] eq ($IsVMS ? 'T' : 't');
37
38     ok( chdir('t'),     'chdir("t")');
39     is( abs_path, catdir($Cwd, 't'),       '  abs_path() agrees' );
40 }
41
42 $Cwd = abs_path;
43
44 # The environment variables chdir() pays attention to.
45 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
46
47 sub check_env {
48     my($key) = @_;
49
50     # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
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' );
54         pass( "  no need to test SYS\$LOGIN on $^O" ) for 1..7;
55     }
56     else {
57         ok( chdir(),              "chdir() w/ only \$ENV{$key} set" );
58         is( abs_path, $ENV{$key}, '  abs_path() agrees' );
59         chdir($Cwd);
60         is( abs_path, $Cwd,       '  and back again' );
61
62         my $warning = '';
63         local $SIG{__WARN__} = sub { $warning .= join '', @_ };
64
65
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' );
71 Use of uninitialized value in chdir at $0 line 60.
72 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 60.
73 WARNING
74
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' );
83 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 72.
84 WARNING
85
86         chdir($Cwd);
87     }
88 }
89
90 sub 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
98 foreach my $key (@magic_envs) {
99     # We're going to be using undefs a lot here.
100     no warnings 'uninitialized';
101
102     clean_env;
103     $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
104     
105     check_env($key);
106 }
107
108 {
109     clean_env;
110
111     ok( !chdir(),                   'chdir() w/o any ENV set' );
112     is( abs_path, $Cwd,             '  abs_path() agrees' );
113 }