This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
VMS fixage and cleanup
[perl5.git] / ext / Cwd / t / cwd.t
CommitLineData
ed4a5f99
BS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use Config;
9use Cwd;
10use strict;
11use warnings;
1279e177 12use File::Path;
ed4a5f99 13
ca7ced35
MS
14use Test::More tests => 16;
15
16my $IsVMS = $^O eq 'VMS';
ed4a5f99
BS
17
18# check imports
ca7ced35
MS
19can_ok('main', qw(cwd getcwd fastcwd fastgetcwd));
20ok( !defined(&chdir), 'chdir() not exported by default' );
21ok( !defined(&abs_path), ' nor abs_path()' );
22ok( !defined(&fast_abs_path), ' nor fast_abs_path()');
23
ed4a5f99 24
0d2079fa
BS
25# XXX force Cwd to bootsrap its XSUBs since we have set @INC = "../lib"
26# XXX and subsequent chdir()s can make them impossible to find
27eval { fastcwd };
28
da3f15f4
JH
29# Must find an external pwd (or equivalent) command.
30
31my $pwd_cmd =
2986a63f 32 ($^O eq "MSWin32" || $^O eq "NetWare") ? "cd" : (grep { -x && -f } map { "$_/pwd" }
da3f15f4
JH
33 split m/$Config{path_sep}/, $ENV{PATH})[0];
34
ca7ced35
MS
35$pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
36
37SKIP: {
38 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
2390ecbc 39
da3f15f4 40 chomp(my $start = `$pwd_cmd`);
14107c42 41 # Win32's cd returns native C:\ style
2986a63f 42 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
2390ecbc 43 # DCL SHOW DEFAULT has leading spaces
ca7ced35
MS
44 $start =~ s/^\s+// if $IsVMS;
45 SKIP: {
46 skip "'$pwd_cmd' failed, nothing to test against", 4 if $?;
47
da3f15f4
JH
48 my $cwd = cwd;
49 my $getcwd = getcwd;
50 my $fastcwd = fastcwd;
51 my $fastgetcwd = fastgetcwd;
ca7ced35
MS
52 is(cwd(), $start, 'cwd()');
53 is(getcwd(), $start, 'getcwd()');
54 is(fastcwd(), $start, 'fastcwd()');
55 is(fastgetcwd(),$start, 'fastgetcwd()');
da3f15f4
JH
56 }
57}
58
ca7ced35
MS
59my $Top_Test_Dir = '_ptrslt_';
60my $Test_Dir = "$Top_Test_Dir/_path_/_to_/_a_/_dir_";
61my $want = "t/$Test_Dir";
62if( $IsVMS ) {
63 # translate the unixy path to VMSish
64 $want = uc $want;
65 $want =~ s|/|\.|g;
66 $want .= '\]';
67}
68
69mkpath(["$Test_Dir"], 0, 0777);
70Cwd::chdir "$Test_Dir";
71
72like(cwd(), qr|$want$|, 'chdir() + cwd()');
73like(getcwd(), qr|$want$|, ' + getcwd()');
74like(fastcwd(), qr|$want$|, ' + fastcwd()');
75like(fastgetcwd(), qr|$want$|, ' + fastgetcwd()');
ed4a5f99
BS
76
77# Cwd::chdir should also update $ENV{PWD}
ca7ced35 78like($ENV{PWD}, qr|$want$|, 'Cwd::chdir() updates $ENV{PWD}');
1279e177 79Cwd::chdir "..";
14107c42 80print "#$ENV{PWD}\n";
1279e177 81Cwd::chdir "..";
14107c42 82print "#$ENV{PWD}\n";
1279e177 83Cwd::chdir "..";
14107c42 84print "#$ENV{PWD}\n";
1279e177 85Cwd::chdir "..";
14107c42 86print "#$ENV{PWD}\n";
1279e177 87Cwd::chdir "..";
14107c42 88print "#$ENV{PWD}\n";
1279e177 89
ca7ced35 90rmtree([$Top_Test_Dir], 0, 0);
1279e177 91
ca7ced35
MS
92if ($IsVMS) {
93 like($ENV{PWD}, qr|\bT\]$|);
2390ecbc
PP
94}
95else {
ca7ced35 96 like($ENV{PWD}, qr|\bt$|);
2390ecbc 97}
ed4a5f99 98
ca7ced35
MS
99SKIP: {
100 skip "no symlinks on this platform", 2 unless $Config{d_symlink};
101
102 mkpath([$Test_Dir], 0, 0777);
103 symlink $Test_Dir => "linktest";
7040f5d5
BS
104
105 my $abs_path = Cwd::abs_path("linktest");
106 my $fast_abs_path = Cwd::fast_abs_path("linktest");
ca7ced35 107 my $want = "t/$Test_Dir";
7040f5d5 108
ca7ced35
MS
109 like($abs_path, qr|$want$|);
110 like($fast_abs_path, qr|$want$|);
7040f5d5 111
ca7ced35 112 rmtree([$Top_Test_Dir], 0, 0);
7040f5d5 113 unlink "linktest";
ed4a5f99 114}