This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Display the native pwd command.
[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 =
023b4a43
JH
32 ($^O eq "MSWin32" || $^O eq "NetWare") ?
33 "cd" :
34 (grep { -x && -f } map { "$_/pwd" }
35 split m/$Config{path_sep}/, $ENV{PATH})[0];
da3f15f4 36
ca7ced35
MS
37$pwd_cmd = 'SHOW DEFAULT' if $IsVMS;
38
023b4a43
JH
39print "# native pwd = '$pwd_cmd'\n";
40
ca7ced35
MS
41SKIP: {
42 skip "No native pwd command found to test against", 4 unless $pwd_cmd;
2390ecbc 43
da3f15f4 44 chomp(my $start = `$pwd_cmd`);
14107c42 45 # Win32's cd returns native C:\ style
2986a63f 46 $start =~ s,\\,/,g if ($^O eq 'MSWin32' || $^O eq "NetWare");
2390ecbc 47 # DCL SHOW DEFAULT has leading spaces
ca7ced35
MS
48 $start =~ s/^\s+// if $IsVMS;
49 SKIP: {
50 skip "'$pwd_cmd' failed, nothing to test against", 4 if $?;
51
da3f15f4
JH
52 my $cwd = cwd;
53 my $getcwd = getcwd;
54 my $fastcwd = fastcwd;
55 my $fastgetcwd = fastgetcwd;
ca7ced35
MS
56 is(cwd(), $start, 'cwd()');
57 is(getcwd(), $start, 'getcwd()');
58 is(fastcwd(), $start, 'fastcwd()');
59 is(fastgetcwd(),$start, 'fastgetcwd()');
da3f15f4
JH
60 }
61}
62
ca7ced35
MS
63my $Top_Test_Dir = '_ptrslt_';
64my $Test_Dir = "$Top_Test_Dir/_path_/_to_/_a_/_dir_";
65my $want = "t/$Test_Dir";
66if( $IsVMS ) {
67 # translate the unixy path to VMSish
68 $want = uc $want;
69 $want =~ s|/|\.|g;
70 $want .= '\]';
71}
72
73mkpath(["$Test_Dir"], 0, 0777);
74Cwd::chdir "$Test_Dir";
75
76like(cwd(), qr|$want$|, 'chdir() + cwd()');
77like(getcwd(), qr|$want$|, ' + getcwd()');
78like(fastcwd(), qr|$want$|, ' + fastcwd()');
79like(fastgetcwd(), qr|$want$|, ' + fastgetcwd()');
ed4a5f99
BS
80
81# Cwd::chdir should also update $ENV{PWD}
ca7ced35 82like($ENV{PWD}, qr|$want$|, 'Cwd::chdir() updates $ENV{PWD}');
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 89Cwd::chdir "..";
14107c42 90print "#$ENV{PWD}\n";
1279e177 91Cwd::chdir "..";
14107c42 92print "#$ENV{PWD}\n";
1279e177 93
ca7ced35 94rmtree([$Top_Test_Dir], 0, 0);
1279e177 95
ca7ced35
MS
96if ($IsVMS) {
97 like($ENV{PWD}, qr|\bT\]$|);
2390ecbc
PP
98}
99else {
ca7ced35 100 like($ENV{PWD}, qr|\bt$|);
2390ecbc 101}
ed4a5f99 102
ca7ced35
MS
103SKIP: {
104 skip "no symlinks on this platform", 2 unless $Config{d_symlink};
105
106 mkpath([$Test_Dir], 0, 0777);
107 symlink $Test_Dir => "linktest";
7040f5d5
BS
108
109 my $abs_path = Cwd::abs_path("linktest");
110 my $fast_abs_path = Cwd::fast_abs_path("linktest");
ca7ced35 111 my $want = "t/$Test_Dir";
7040f5d5 112
ca7ced35
MS
113 like($abs_path, qr|$want$|);
114 like($fast_abs_path, qr|$want$|);
7040f5d5 115
ca7ced35 116 rmtree([$Top_Test_Dir], 0, 0);
7040f5d5 117 unlink "linktest";
ed4a5f99 118}