xref: /openbsd-src/gnu/usr.bin/perl/dist/PathTools/t/crossplatform.t (revision 6fb12b7054efc6b436584db6cef9c2f85c0d7e27)
1*6fb12b70Safresh1#!/usr/bin/perl -w
2*6fb12b70Safresh1
3*6fb12b70Safresh1use strict;
4*6fb12b70Safresh1use File::Spec;
5*6fb12b70Safresh1use lib File::Spec->catfile('t', 'lib');
6*6fb12b70Safresh1use Test::More;
7*6fb12b70Safresh1local $|=1;
8*6fb12b70Safresh1
9*6fb12b70Safresh1my @platforms = qw(Cygwin Epoc Mac OS2 Unix VMS Win32);
10*6fb12b70Safresh1my $tests_per_platform = 10;
11*6fb12b70Safresh1
12*6fb12b70Safresh1my $vms_unix_rpt = 0;
13*6fb12b70Safresh1my $vms_efs = 0;
14*6fb12b70Safresh1my $vms_unix_mode = 0;
15*6fb12b70Safresh1my $vms_real_root = 0;
16*6fb12b70Safresh1
17*6fb12b70Safresh1if ($^O eq 'VMS') {
18*6fb12b70Safresh1    $vms_unix_mode = 0;
19*6fb12b70Safresh1    if (eval 'require VMS::Feature') {
20*6fb12b70Safresh1        $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
21*6fb12b70Safresh1        $vms_efs = VMS::Feature::current("efs_charset");
22*6fb12b70Safresh1    } else {
23*6fb12b70Safresh1        my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
24*6fb12b70Safresh1        my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
25*6fb12b70Safresh1        $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
26*6fb12b70Safresh1        $vms_efs = $efs_charset =~ /^[ET1]/i;
27*6fb12b70Safresh1    }
28*6fb12b70Safresh1
29*6fb12b70Safresh1    # Traditional VMS mode only if VMS is not in UNIX compatible mode.
30*6fb12b70Safresh1    $vms_unix_mode = ($vms_efs && $vms_unix_rpt);
31*6fb12b70Safresh1
32*6fb12b70Safresh1    # If we are in UNIX mode, we may or may not have a real root.
33*6fb12b70Safresh1    if ($vms_unix_mode) {
34*6fb12b70Safresh1        my $rootdir = File::Spec->rootdir;
35*6fb12b70Safresh1        $vms_real_root = 1 if ($rootdir eq '/');
36*6fb12b70Safresh1    }
37*6fb12b70Safresh1
38*6fb12b70Safresh1}
39*6fb12b70Safresh1
40*6fb12b70Safresh1
41*6fb12b70Safresh1plan tests => 1 + @platforms * $tests_per_platform;
42*6fb12b70Safresh1
43*6fb12b70Safresh1my %volumes = (
44*6fb12b70Safresh1	       Mac => 'Macintosh HD',
45*6fb12b70Safresh1	       OS2 => 'A:',
46*6fb12b70Safresh1	       Win32 => 'A:',
47*6fb12b70Safresh1	       VMS => 'v',
48*6fb12b70Safresh1	      );
49*6fb12b70Safresh1my %other_vols = (
50*6fb12b70Safresh1		  Mac => 'Mounted Volume',
51*6fb12b70Safresh1		  OS2 => 'B:',
52*6fb12b70Safresh1		  Win32 => 'B:',
53*6fb12b70Safresh1		  VMS => 'w',
54*6fb12b70Safresh1	      );
55*6fb12b70Safresh1
56*6fb12b70Safresh1ok 1, "Loaded";
57*6fb12b70Safresh1
58*6fb12b70Safresh1foreach my $platform (@platforms) {
59*6fb12b70Safresh1  my $module = "File::Spec::$platform";
60*6fb12b70Safresh1
61*6fb12b70Safresh1 SKIP:
62*6fb12b70Safresh1  {
63*6fb12b70Safresh1    eval "require $module; 1";
64*6fb12b70Safresh1
65*6fb12b70Safresh1    skip "Can't load $module", $tests_per_platform
66*6fb12b70Safresh1      if $@;
67*6fb12b70Safresh1
68*6fb12b70Safresh1    my $v = $volumes{$platform} || '';
69*6fb12b70Safresh1    my $other_v = $other_vols{$platform} || '';
70*6fb12b70Safresh1
71*6fb12b70Safresh1    # Fake out the environment on MacOS and Win32
72*6fb12b70Safresh1    no strict 'refs';
73*6fb12b70Safresh1    my $save_w = $^W;
74*6fb12b70Safresh1    $^W = 0;
75*6fb12b70Safresh1    local *{"File::Spec::Mac::rootdir"} = sub { "Macintosh HD:" };
76*6fb12b70Safresh1    local *{"File::Spec::Win32::_cwd"}  = sub { "C:\\foo" };
77*6fb12b70Safresh1    $^W = $save_w;
78*6fb12b70Safresh1    use strict 'refs';
79*6fb12b70Safresh1
80*6fb12b70Safresh1
81*6fb12b70Safresh1    my ($file, $base, $result);
82*6fb12b70Safresh1
83*6fb12b70Safresh1    $base = $module->catpath($v, $module->catdir('', 'foo'), '');
84*6fb12b70Safresh1    $base = $module->catdir($module->rootdir, 'foo');
85*6fb12b70Safresh1
86*6fb12b70Safresh1    is $module->file_name_is_absolute($base), 1, "$base is absolute on $platform";
87*6fb12b70Safresh1
88*6fb12b70Safresh1    # splitdir('') -> ()
89*6fb12b70Safresh1    my @result = $module->splitdir('');
90*6fb12b70Safresh1    is @result, 0, "$platform->splitdir('') -> ()";
91*6fb12b70Safresh1
92*6fb12b70Safresh1    # canonpath() -> undef
93*6fb12b70Safresh1    $result = $module->canonpath();
94*6fb12b70Safresh1    is $result, undef, "$platform->canonpath() -> undef";
95*6fb12b70Safresh1
96*6fb12b70Safresh1    # canonpath(undef) -> undef
97*6fb12b70Safresh1    $result = $module->canonpath(undef);
98*6fb12b70Safresh1    is $result, undef, "$platform->canonpath(undef) -> undef";
99*6fb12b70Safresh1
100*6fb12b70Safresh1    # abs2rel('A:/foo/bar', 'A:/foo')    ->  'bar'
101*6fb12b70Safresh1    $file = $module->catpath($v, $module->catdir($module->rootdir, 'foo', 'bar'), 'file');
102*6fb12b70Safresh1    $base = $module->catpath($v, $module->catdir($module->rootdir, 'foo'), '');
103*6fb12b70Safresh1    $result = $module->catfile('bar', 'file');
104*6fb12b70Safresh1
105*6fb12b70Safresh1    if ($vms_unix_mode and $platform eq 'VMS') {
106*6fb12b70Safresh1        # test 56 special
107*6fb12b70Safresh1        # If VMS is in UNIX mode, so is the result, but having the volume
108*6fb12b70Safresh1        # parameter present forces the abs2rel into VMS mode.
109*6fb12b70Safresh1        $result = VMS::Filespec::vmsify($result);
110*6fb12b70Safresh1        $result =~ s/\.$//;
111*6fb12b70Safresh1
112*6fb12b70Safresh1        # If we have a real root, then we are dealing with absolute directories
113*6fb12b70Safresh1        $result =~ s/\[\./\[/ if $vms_real_root;
114*6fb12b70Safresh1    }
115*6fb12b70Safresh1
116*6fb12b70Safresh1    is $module->abs2rel($file, $base), $result, "$platform->abs2rel($file, $base)";
117*6fb12b70Safresh1
118*6fb12b70Safresh1
119*6fb12b70Safresh1    # abs2rel('A:/foo/bar', 'B:/foo')    ->  'A:/foo/bar'
120*6fb12b70Safresh1    $base = $module->catpath($other_v, $module->catdir($module->rootdir, 'foo'), '');
121*6fb12b70Safresh1    $result = volumes_differ($module, $file, $base) ? $file : $module->catfile('bar', 'file');
122*6fb12b70Safresh1    is $module->abs2rel($file, $base), $result, "$platform->abs2rel($file, $base)";
123*6fb12b70Safresh1
124*6fb12b70Safresh1
125*6fb12b70Safresh1    # abs2rel('A:/foo/bar', '/foo')      ->  'A:/foo/bar'
126*6fb12b70Safresh1    $base = $module->catpath('', $module->catdir($module->rootdir, 'foo'), '');
127*6fb12b70Safresh1    $result = volumes_differ($module, $file, $base) ? $file : $module->catfile('bar', 'file');
128*6fb12b70Safresh1    is $module->abs2rel($file, $base), $result, "$platform->abs2rel($file, $base)";
129*6fb12b70Safresh1
130*6fb12b70Safresh1
131*6fb12b70Safresh1    # abs2rel('/foo/bar/file', 'A:/foo')    ->  '/foo/bar'
132*6fb12b70Safresh1    $file = $module->catpath('', $module->catdir($module->rootdir, 'foo', 'bar'), 'file');
133*6fb12b70Safresh1    $base = $module->catpath($v, $module->catdir($module->rootdir, 'foo'), '');
134*6fb12b70Safresh1    $result = volumes_differ($module, $file, $base) ? $module->rel2abs($file) : $module->catfile('bar', 'file');
135*6fb12b70Safresh1
136*6fb12b70Safresh1    if ($vms_unix_mode and $platform eq 'VMS') {
137*6fb12b70Safresh1        # test 59 special
138*6fb12b70Safresh1        # If VMS is in UNIX mode, so is the result, but having the volume
139*6fb12b70Safresh1        # parameter present forces the abs2rel into VMS mode.
140*6fb12b70Safresh1        $result = VMS::Filespec::vmsify($result);
141*6fb12b70Safresh1    }
142*6fb12b70Safresh1
143*6fb12b70Safresh1    is $module->abs2rel($file, $base), $result, "$platform->abs2rel($file, $base)";
144*6fb12b70Safresh1
145*6fb12b70Safresh1
146*6fb12b70Safresh1    # abs2rel('/foo/bar', 'B:/foo')    ->  '/foo/bar'
147*6fb12b70Safresh1    $base = $module->catpath($other_v, $module->catdir($module->rootdir, 'foo'), '');
148*6fb12b70Safresh1    $result = volumes_differ($module, $file, $base) ? $module->rel2abs($file) : $module->catfile('bar', 'file');
149*6fb12b70Safresh1
150*6fb12b70Safresh1    if ($vms_unix_mode and $platform eq 'VMS') {
151*6fb12b70Safresh1        # test 60 special
152*6fb12b70Safresh1        # If VMS is in UNIX mode, so is the result, but having the volume
153*6fb12b70Safresh1        # parameter present forces the abs2rel into VMS mode.
154*6fb12b70Safresh1        $result = VMS::Filespec::vmsify($result);
155*6fb12b70Safresh1    }
156*6fb12b70Safresh1
157*6fb12b70Safresh1    is $module->abs2rel($file, $base), $result, "$platform->abs2rel($file, $base)";
158*6fb12b70Safresh1
159*6fb12b70Safresh1
160*6fb12b70Safresh1    # abs2rel('/foo/bar', '/foo')      ->  'bar'
161*6fb12b70Safresh1    $base = $module->catpath('', $module->catdir($module->rootdir, 'foo'), '');
162*6fb12b70Safresh1    $result = $module->catfile('bar', 'file');
163*6fb12b70Safresh1
164*6fb12b70Safresh1    is $module->abs2rel($file, $base), $result, "$platform->abs2rel($file, $base)";
165*6fb12b70Safresh1  }
166*6fb12b70Safresh1}
167*6fb12b70Safresh1
168*6fb12b70Safresh1sub volumes_differ {
169*6fb12b70Safresh1  my ($module, $one, $two) = @_;
170*6fb12b70Safresh1  my ($one_v) = $module->splitpath( $module->rel2abs($one) );
171*6fb12b70Safresh1  my ($two_v) = $module->splitpath( $module->rel2abs($two) );
172*6fb12b70Safresh1  return $one_v ne $two_v;
173*6fb12b70Safresh1}
174