This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Replace the use Test::More in t/{op,io,run} with t/test.pl.
[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.
69026470 5 @INC = qw(t . lib ../lib);
8ea155d1
MS
6}
7
69026470
JH
8require "test.pl";
9plan(tests => 25);
8ea155d1
MS
10
11# Might be a little early in the testing process to start using these,
12# but I can't think of a way to write this test without them.
d9c93211
MS
13use File::Spec::Functions qw(:DEFAULT splitdir rel2abs);
14
15# Can't use Cwd::abs_path() because it has different ideas about
16# path seperators than File::Spec.
17sub abs_path {
18 rel2abs(curdir);
19}
8ea155d1 20
8ea155d1
MS
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}