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);
14 my $IsVMS = $^O eq 'VMS';
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);
20 # Can't use Cwd::abs_path() because it has different ideas about
21 # path separators than File::Spec.
23 $IsVMS ? uc(rel2abs(curdir)) : rel2abs(curdir);
28 # Let's get to a known position
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;
34 ok( chdir($test_dir), 'chdir($test_dir)');
35 is( abs_path, catdir($Cwd, $test_dir), ' abs_path() agrees' );
40 # The environment variables chdir() pays attention to.
41 my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
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;
53 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
54 is( abs_path, $ENV{$key}, ' abs_path() agrees' );
56 is( abs_path, $Cwd, ' and back again' );
59 local $SIG{__WARN__} = sub { $warning .= join '', @_ };
62 # Check the deprecated chdir(undef) feature.
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.
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.
88 foreach my $env (@magic_envs) {
89 $Saved_Env{$env} = $ENV{$env};
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'};
95 # On VMS, %ENV is many layered.
96 delete $ENV{$env} while exists $ENV{$env};
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
102 $ENV{'SYS$LOGIN'} = '' if $IsVMS;
106 no warnings 'uninitialized';
108 # Restore the environment for VMS (and doesn't hurt for anyone else)
109 @ENV{@magic_envs} = @Saved_Env{@magic_envs};
113 foreach my $key (@magic_envs) {
114 # We're going to be using undefs a lot here.
115 no warnings 'uninitialized';
118 $ENV{$key} = catdir $Cwd, ($IsVMS ? 'OP' : 'op');
125 if ($IsVMS && !$Config{'d_setenv'}) {
126 pass("Can't reset HOME, so chdir() test meaningless");
128 ok( !chdir(), 'chdir() w/o any ENV set' );
130 is( abs_path, $Cwd, ' abs_path() agrees' );