/
io-spec-unix.t
221 lines (197 loc) · 10.4 KB
/
io-spec-unix.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
use v6;
use Test;
# L<S32::IO/IO::Spec>
plan 133;
my $Unix := IO::Spec::Unix;
my %canonpath = (
'a/b/c' => 'a/b/c',
'//a//b//' => '/a/b',
'a/../../b/c' => 'a/../../b/c',
'/.' => '/',
'/./' => '/',
'/a/./' => '/a',
'/a/.' => '/a',
'/../../' => '/',
'/../..' => '/',
'/..' => '/',
'a\\\\b' => 'a\\\\b',
'' => '',
'0' => '0',
'///../../..//./././a//b/.././c/././' => '/a/b/../c',
);
for %canonpath.kv -> $get, $want {
is $Unix.canonpath( $get ), $want, "canonpath: '$get' -> '$want'";
}
my %canonpath-parent = (
"foo/bar/.." => "foo",
"foo/bar/baz/../.." => "foo",
"/foo/.." => "/",
"foo/.." => '.',
"foo/../bar/../baz" => "baz",
"foo/../../bar" => "../bar",
"../../.." => "../../..",
"/../../.." => "/",
"/foo/../.." => "/",
"0" => "0",
'' => '',
"//../..usr/bin/../foo/.///ef" => "/..usr/foo/ef",
'///../../..//./././a//b/.././c/././' => '/a/c',
);
for %canonpath-parent.kv -> $get, $want {
is $Unix.canonpath( $get , :parent ), $want, "canonpath(:parent): '$get' -> '$want'";
}
say "# warning expected here:";
is $Unix.canonpath( Any , :parent ), '', "canonpath(:parent): Any -> ''";
is $Unix.catdir( ), '', "catdir: no arg -> ''";
is $Unix.catdir( '' ), '/', "catdir: '' -> '/'";
is $Unix.catdir( '/' ), '/', "catdir: '/' -> '/'";
is $Unix.catdir( '','d1','d2','d3','' ), '/d1/d2/d3', "catdir: ('','d1','d2','d3','') -> '/d1/d2/d3'";
is $Unix.catdir( 'd1','d2','d3','' ), 'd1/d2/d3', "catdir: ('d1','d2','d3','') -> 'd1/d2/d3'";
is $Unix.catdir( '','d1','d2','d3' ), '/d1/d2/d3', "catdir: ('','d1','d2','d3') -> '/d1/d2/d3'";
is $Unix.catdir( 'd1','d2','d3' ), 'd1/d2/d3', "catdir: ('d1','d2','d3') -> 'd1/d2/d3'";
is $Unix.catdir( '/','d2/d3' ), '/d2/d3', "catdir: ('/','d2/d3') -> '/d2/d3'";
is $Unix.catfile('a','b','c'), 'a/b/c', "catfile: ('a','b','c') -> 'a/b/c'";
is $Unix.catfile('a','b','./c'), 'a/b/c', "catfile: ('a','b','./c') -> 'a/b/c'";
is $Unix.catfile('./a','b','c'), 'a/b/c', "catfile: ('./a','b','c') -> 'a/b/c'";
is $Unix.catfile('c'), 'c', "catfile: 'c' -> 'c'";
is $Unix.catfile('./c'), 'c', "catfile: './c' -> 'c'";
is $Unix.curdir, '.', 'curdir is "."';
is $Unix.devnull, '/dev/null', 'devnull is /dev/null';
is $Unix.rootdir, '/', 'rootdir is "/"';
is $Unix.updir, '..', 'updir is ".."';
isnt '.', $Unix.curupdir, "curupdir: '.'";
isnt '..', $Unix.curupdir, "curupdir: '..'";
is '.git', $Unix.curupdir, "curupdir: '.git'";
is 'file', $Unix.curupdir, "curupdir: 'file'";
ok $Unix.is-absolute( '/abcd/ef' ), 'is-absolute: ok "/abcd/ef"';
ok $Unix.is-absolute( '/' ), 'is-absolute: ok "/"';
nok $Unix.is-absolute( 'abcd/ef' ), 'is-absolute: nok "abcd"';
nok $Unix.is-absolute( '..' ), 'is-absolute: nok ".."';
my $path = %*ENV<PATH>;
%*ENV<PATH> = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:';
my @want = </usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games .>;
is-deeply $Unix.path, @want.Seq, 'path';
%*ENV<PATH> = '';
is-deeply $Unix.path, ().Seq, 'no path';
%*ENV<PATH> = $path;
my %splitpath = (
'file' => ('', '', 'file'),
'/d1/d2/d3/' => ('', '/d1/d2/d3/', ''),
'd1/d2/d3/' => ('', 'd1/d2/d3/', ''),
'/d1/d2/d3/.' => ('', '/d1/d2/d3/.', ''),
'/d1/d2/d3/..' => ('', '/d1/d2/d3/..', ''),
'/d1/d2/d3/.file' => ('', '/d1/d2/d3/', '.file'),
'd1/d2/d3/file' => ('', 'd1/d2/d3/', 'file'),
'/../../d1/' => ('', '/../../d1/', ''),
'/././d1/' => ('', '/././d1/', ''),
);
for %splitpath.kv -> $get, $want {
is $Unix.splitpath( $get ), $want, "splitpath: '$get' -> '$want'";
}
my %split = (
'/' => ('', '/', '/'),
'.' => ('', '.', '.'),
'file' => ('', '.', 'file'),
'dir/' => ('', '.', 'dir'),
'/dir/' => ('', '/', 'dir'),
'/d1/d2/d3/' => ('', '/d1/d2', 'd3'),
'd1/d2/d3/' => ('', 'd1/d2', 'd3'),
'/d1/d2/d3/.' => ('', '/d1/d2/d3', '.'),
'/d1/d2/d3/..' => ('', '/d1/d2/d3', '..'),
'/d1/d2/d3/.file' => ('', '/d1/d2/d3', '.file'),
'd1/d2/d3/file' => ('', 'd1/d2/d3', 'file'),
'/../../d1/' => ('', '/../..', 'd1'),
'/././d1/' => ('', '/./.', 'd1'),
);
for %split.kv -> $get, $want {
is $Unix.split( $get ).hash.<volume dirname basename>, $want, "split: '$get' -> '$want'";
}
my @join = (
$('','.','.'), '.',
$('','/','/'), '/',
$('','.','file'), 'file',
$('','','file'), 'file',
$('','dir','.'), 'dir/.',
$('','/d1/d2/d3/',''), '/d1/d2/d3/',
$('','d1/d2/d3/',''), 'd1/d2/d3/',
$('','/d1/d2/d3/.',''), '/d1/d2/d3/.',
$('','/d1/d2/d3/..',''), '/d1/d2/d3/..',
$('','/d1/d2/d3/','.file'), '/d1/d2/d3/.file',
$('','d1/d2/d3/','file'), 'd1/d2/d3/file',
$('','/../../d1/',''), '/../../d1/',
$('','/././d1/',''), '/././d1/',
$('d1','d2/d3/',''), 'd2/d3/',
$('d1','d2','d3/'), 'd2/d3/'
);
for @join -> $get, $want {
is $Unix.join( |$get ), $want, "join: '$get' -> '$want'";
}
my %splitdir = (
'' => '',
'/d1/d2/d3/' => ('', 'd1', 'd2', 'd3', ''),
'd1/d2/d3/' => ('d1', 'd2', 'd3', ''),
'/d1/d2/d3' => ('', 'd1', 'd2', 'd3'),
'd1/d2/d3' => ('d1', 'd2', 'd3'),
);
for %splitdir.kv -> $get, $want {
is $Unix.splitdir( $get ), $want, "splitdir: '$get' -> '$want'";
}
is $Unix.catpath('','','file'), 'file', "catpath: ('','','file') -> 'file'";
is $Unix.catpath('','/d1/d2/d3/',''), '/d1/d2/d3/', "catpath: ('','/d1/d2/d3/','') -> '/d1/d2/d3/'";
is $Unix.catpath('','d1/d2/d3/',''), 'd1/d2/d3/', "catpath: ('','d1/d2/d3/','') -> 'd1/d2/d3/'";
is $Unix.catpath('','/d1/d2/d3/.',''), '/d1/d2/d3/.', "catpath: ('','/d1/d2/d3/.','') -> '/d1/d2/d3/.'";
is $Unix.catpath('','/d1/d2/d3/..',''), '/d1/d2/d3/..', "catpath: ('','/d1/d2/d3/..','') -> '/d1/d2/d3/..'";
is $Unix.catpath('','/d1/d2/d3/','.file'), '/d1/d2/d3/.file', "catpath: ('','/d1/d2/d3/','.file') -> '/d1/d2/d3/.file'";
is $Unix.catpath('','d1/d2/d3/','file'), 'd1/d2/d3/file', "catpath: ('','d1/d2/d3/','file') -> 'd1/d2/d3/file'";
is $Unix.catpath('','/../../d1/',''), '/../../d1/', "catpath: ('','/../../d1/','') -> '/../../d1/'";
is $Unix.catpath('','/././d1/',''), '/././d1/', "catpath: ('','/././d1/','') -> '/././d1/'";
is $Unix.catpath('d1','d2/d3/',''), 'd2/d3/', "catpath: ('d1','d2/d3/','') -> 'd2/d3/'";
is $Unix.catpath('d1','d2','d3/'), 'd2/d3/', "catpath: ('d1','d2','d3/') -> 'd2/d3/'";
is $Unix.abs2rel('/t1/t2/t3','/t1/t2/t3'), '.', "abs2rel: ('/t1/t2/t3','/t1/t2/t3') -> '.'";
is $Unix.abs2rel('/t1/t2/t4','/t1/t2/t3'), '../t4', "abs2rel: ('/t1/t2/t4','/t1/t2/t3') -> '../t4'";
is $Unix.abs2rel('/t1/t2','/t1/t2/t3'), '..', "abs2rel: ('/t1/t2','/t1/t2/t3') -> '..'";
is $Unix.abs2rel('/t1/t2/t3/t4','/t1/t2/t3'), 't4', "abs2rel: ('/t1/t2/t3/t4','/t1/t2/t3') -> 't4'";
is $Unix.abs2rel('/t4/t5/t6','/t1/t2/t3'), '../../../t4/t5/t6', "abs2rel: ('/t4/t5/t6','/t1/t2/t3') -> '../../../t4/t5/t6'";
is $Unix.abs2rel('/','/t1/t2/t3'), '../../..', "abs2rel: ('/','/t1/t2/t3') -> '../../..'";
is $Unix.abs2rel('///','/t1/t2/t3'), '../../..', "abs2rel: ('///','/t1/t2/t3') -> '../../..'";
is $Unix.abs2rel('/.','/t1/t2/t3'), '../../..', "abs2rel: ('/.','/t1/t2/t3') -> '../../..'";
is $Unix.abs2rel('/./','/t1/t2/t3'), '../../..', "abs2rel: ('/./','/t1/t2/t3') -> '../../..'";
# "Unix->abs2rel('../t4','/t1/t2/t3'), '../t4', "abs2rel: ('../t4','/t1/t2/t3') -> '../t4'";
is $Unix.abs2rel('/t1/t2/t3', '/'), 't1/t2/t3', "abs2rel: ('/t1/t2/t3', '/') -> 't1/t2/t3'";
is $Unix.abs2rel('/t1/t2/t3', '/t1'), 't2/t3', "abs2rel: ('/t1/t2/t3', '/t1') -> 't2/t3'";
is $Unix.abs2rel('t1/t2/t3', 't1'), 't2/t3', "abs2rel: ('t1/t2/t3', 't1') -> 't2/t3'";
is $Unix.abs2rel('t1/t2/t3', 't4'), '../t1/t2/t3', "abs2rel: ('t1/t2/t3', 't4') -> '../t1/t2/t3'";
is $Unix.rel2abs('t4','/t1/t2/t3'), '/t1/t2/t3/t4', "rel2abs: ('t4','/t1/t2/t3') -> '/t1/t2/t3/t4'";
is $Unix.rel2abs('t4/t5','/t1/t2/t3'), '/t1/t2/t3/t4/t5', "rel2abs: ('t4/t5','/t1/t2/t3') -> '/t1/t2/t3/t4/t5'";
is $Unix.rel2abs('.','/t1/t2/t3'), '/t1/t2/t3', "rel2abs: ('.','/t1/t2/t3') -> '/t1/t2/t3'";
is $Unix.rel2abs('..','/t1/t2/t3'), '/t1/t2/t3/..', "rel2abs: ('..','/t1/t2/t3') -> '/t1/t2/t3/..'";
is $Unix.rel2abs('../t4','/t1/t2/t3'), '/t1/t2/t3/../t4', "rel2abs: ('../t4','/t1/t2/t3') -> '/t1/t2/t3/../t4'";
is $Unix.rel2abs('/t1','/t1/t2/t3'), '/t1', "rel2abs: ('/t1','/t1/t2/t3') -> '/t1'";
if $*DISTRO.name ~~ any(<mswin32 os2 netware symbian dos cygwin>) {
skip-rest 'Unix on-platform tests'
}
else {
isa-ok $*SPEC, IO::Spec::Unix, "unix: loads correct module";
is $*SPEC.rel2abs( $*SPEC.curdir ), $*CWD, "rel2abs: \$*CWD test";
ok {.IO.d && .IO.w}.( $*SPEC.tmpdir ), "tmpdir: {$*SPEC.tmpdir} is a writable directory";
}
subtest '.basename' => {
my @tests = '' => '', '.' => '.', '/' => '', 'foo/' => '', '/.' => '.',
'foo/.' => '.', 'foo/..' => '..', 'foo/...' => '...', 「y/\z」 => 「\z」,
'bar/♥foo' => '♥foo', '♥foo' => '♥foo', '♥foo/..' => '..';
plan +@tests;
for @tests -> (:key($in), :value($out)) {
is-deeply IO::Spec::Unix.basename($in), $out, $in;
}
}
subtest '.extension' => {
my @tests = '' => '', '.' => '', '/.' => '', 'foo.bar/ber' => 'bar/ber',
'.tar' => 'tar', 'foo.tar' => 'tar', 'foo.tar.gz' => 'gz',
'.tar.gz' => 'gz', 'I ♥ Perl 6' => '', 'I ♥ Perl 6.♥' => '♥';
plan +@tests;
for @tests -> (:key($in), :value($out)) {
is-deeply IO::Spec::Unix.extension($in), $out, $in;
}
}
is-deeply IO::Spec::Unix.is-absolute("/\x[308]"), True,
'combiners on "/" do not interfere with absolute path detection';