shared hash keys and ++/--
[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 use Config;
11 require "test.pl";
12 plan(tests => 31);
13
14 my $IsVMS = $^O eq 'VMS';
15
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.
18 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs splitpath);
19
20 # Can't use Cwd::abs_path() because it has different ideas about
21 # path separators than File::Spec.
22 sub abs_path {
23     $IsVMS ? uc(rel2abs(curdir)) : rel2abs(curdir);
24 }
25
26 my $Cwd = abs_path;
27
28 # Let's get to a known position
29 SKIP: {
30     my ($vol,$dir) = splitpath(abs_path,1);
31     my $test_dir = $IsVMS ? 'T' : 't';
32     skip("Already in t/", 2) if (splitdir($dir))[-1] eq $test_dir;
33
34     ok( chdir($test_dir),     'chdir($test_dir)');
35     is( abs_path, catdir($Cwd, $test_dir),    '  abs_path() agrees' );
36 }
37
38 $Cwd = abs_path;
39
40 # The environment variables chdir() pays attention to.
41 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
42
43 sub check_env {
44     my($key) = @_;
45
46     # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
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' );
50         pass( "  no need to test SYS\$LOGIN on $^O" ) for 1..7;
51     }
52     else {
53         ok( chdir(),              "chdir() w/ only \$ENV{$key} set" );
54         is( abs_path, $ENV{$key}, '  abs_path() agrees' );
55         chdir($Cwd);
56         is( abs_path, $Cwd,       '  and back again' );
57
58         my $warning = '';
59         local $SIG{__WARN__} = sub { $warning .= join '', @_ };
60
61
62         # Check the deprecated chdir(undef) feature.
63 #line 64
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' );
67 Use of uninitialized value in chdir at $0 line 64.
68 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 64.
69 WARNING
70
71         chdir($Cwd);
72
73         # Ditto chdir('').
74         $warning = '';
75 #line 76
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' );
79 Use of chdir('') or chdir(undef) as chdir() is deprecated at $0 line 76.
80 WARNING
81
82         chdir($Cwd);
83     }
84 }
85
86 my %Saved_Env = ();
87 sub clean_env {
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};
97     }
98
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
105 END {
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
113 foreach my $key (@magic_envs) {
114     # We're going to be using undefs a lot here.
115     no warnings 'uninitialized';
116
117     clean_env;
118     $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
119
120     check_env($key);
121 }
122
123 {
124     clean_env;
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     }
130     is( abs_path, $Cwd,             '  abs_path() agrees' );
131 }