This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Reconciling the Cwd/File::Spec differences
[perl5.git] / t / op / chdir.t
CommitLineData
8ea155d1
MS
1BEGIN {
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.
d9c93211
MS
11use 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.
15sub abs_path {
16 rel2abs(curdir);
17}
8ea155d1 18
89eee1ed 19use Test::More tests => 25;
8ea155d1
MS
20
21my $cwd = abs_path;
22
23# Let's get to a known position
24SKIP: {
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.
34my @magic_envs = qw(HOME LOGDIR SYS$LOGIN);
35
36foreach 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
89eee1ed 43 # Make sure $ENV{'SYS$LOGIN'} is only honored on VMS.
8ea155d1 44 if( $key eq 'SYS$LOGIN' && $^O ne 'VMS' ) {
89eee1ed
BZ
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" );
8ea155d1
MS
48 }
49 else {
89eee1ed 50 ok( chdir(), "chdir() w/ only \$ENV{$key} set" );
8ea155d1
MS
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()
89eee1ed 57 ok( !chdir(undef), "chdir(undef) w/ only \$ENV{$key} set" );
8ea155d1
MS
58 is( abs_path, $cwd, ' abs_path() agrees' );
59
60 # Ditto chdir('').
89eee1ed 61 ok( !chdir(''), "chdir('') w/ only \$ENV{$key} set" );
8ea155d1
MS
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}