Integrate mainline
[p5sagit/p5-mst-13.2.git] / t / op / chdir.t
1 BEGIN {
2     # We're not going to chdir() into 't' because we don't know if
3     # chdir() works!  Instead, we'll hedge our bets and put both
4     # possibilities into @INC.
5     @INC = ('lib', '../lib');
6 }
7
8
9 # Might be a little early in the testing process to start using these,
10 # but I can't think of a way to write this test without them.
11 use File::Spec::Functions qw(:DEFAULT splitdir rel2abs);
12
13 # Can't use Cwd::abs_path() because it has different ideas about
14 # path seperators than File::Spec.
15 sub abs_path {
16     rel2abs(curdir);
17 }
18
19 use Test::More tests => 25;
20
21 my $cwd = abs_path;
22
23 # Let's get to a known position
24 SKIP: {
25     skip("Already in t/", 2) if (splitdir(abs_path))[-1] eq 't';
26
27     ok( chdir('t'),     'chdir("t")');
28     is( abs_path, catdir($cwd, 't'),       '  abs_path() agrees' );
29 }
30
31 $cwd = abs_path;
32
33 # The environment variables chdir() pays attention to.
34 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
35
36 foreach my $key (@magic_envs) {
37     # We're going to be using undefs a lot here.
38     no warnings 'uninitialized';
39
40     delete @ENV{@magic_envs};
41     local $ENV{$key} = catdir $cwd, 'op';
42     
43     # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
44     if( $key eq 'SYS$LOGIN' && $^O ne 'VMS' ) {
45         ok( !chdir(),             "chdir() on $^O ignores only \$ENV{$key} set" );
46         is( abs_path, $cwd,       '  abs_path() did not change' );
47         ok( 1,                    "  no need to chdir back on $^O" );
48     }
49     else {
50         ok( chdir(),              "chdir() w/ only \$ENV{$key} set" );
51         is( abs_path, $ENV{$key}, '  abs_path() agrees' );
52         chdir($cwd);
53         is( abs_path, $cwd,       '  and back again' );
54     }
55
56     # Bug had chdir(undef) being the same as chdir()
57     ok( !chdir(undef),              "chdir(undef) w/ only \$ENV{$key} set" );
58     is( abs_path, $cwd,             '  abs_path() agrees' );
59
60     # Ditto chdir('').
61     ok( !chdir(''),                 "chdir('') w/ only \$ENV{$key} set" );
62     is( abs_path, $cwd,             '  abs_path() agrees' );
63 }
64
65 {
66     # We're going to be using undefs a lot here.
67     no warnings 'uninitialized';
68
69     # Unset all the environment variables chdir() pay attention to.
70     local @ENV{@magic_envs} = (undef) x @magic_envs;
71
72     ok( !chdir(),                   'chdir() w/o any ENV set' );
73     is( abs_path, $cwd,             '  abs_path() agrees' );
74 }