Skip to content

Commit 9228436

Browse files
committed
Port test-path-join.
1 parent ebe38ed commit 9228436

File tree

3 files changed

+135
-3
lines changed

3 files changed

+135
-3
lines changed

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,21 @@ var posix = {
161161
},
162162

163163
join: function join() {
164-
if (arguments.length === 0) return '.';
164+
if (arguments.length === 0)
165+
return '.';
165166
var joined;
166167
for (var i = 0; i < arguments.length; ++i) {
167168
var arg = arguments[i];
168169
assertPath(arg);
169170
if (arg.length > 0) {
170-
if (joined === undefined) joined = arg;else joined += '/' + arg;
171+
if (joined === undefined)
172+
joined = arg;
173+
else
174+
joined += '/' + arg;
171175
}
172176
}
173-
if (joined === undefined) return '.';
177+
if (joined === undefined)
178+
return '.';
174179
return posix.normalize(joined);
175180
},
176181

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require('./test-path-basename');
22
require('./test-path-dirname');
33
require('./test-path-extname');
4+
require('./test-path-join');
45
require('./test-path-relative');

test/test-path-join.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
'use strict';
2+
var tape = require('tape');
3+
var path = require('../');
4+
5+
var backslashRE = /\\/g;
6+
7+
var joinTests =
8+
// arguments result
9+
[[['.', 'x/b', '..', '/b/c.js'], 'x/b/c.js'],
10+
[[], '.'],
11+
[['/.', 'x/b', '..', '/b/c.js'], '/x/b/c.js'],
12+
[['/foo', '../../../bar'], '/bar'],
13+
[['foo', '../../../bar'], '../../bar'],
14+
[['foo/', '../../../bar'], '../../bar'],
15+
[['foo/x', '../../../bar'], '../bar'],
16+
[['foo/x', './bar'], 'foo/x/bar'],
17+
[['foo/x/', './bar'], 'foo/x/bar'],
18+
[['foo/x/', '.', 'bar'], 'foo/x/bar'],
19+
[['./'], './'],
20+
[['.', './'], './'],
21+
[['.', '.', '.'], '.'],
22+
[['.', './', '.'], '.'],
23+
[['.', '/./', '.'], '.'],
24+
[['.', '/////./', '.'], '.'],
25+
[['.'], '.'],
26+
[['', '.'], '.'],
27+
[['', 'foo'], 'foo'],
28+
[['foo', '/bar'], 'foo/bar'],
29+
[['', '/foo'], '/foo'],
30+
[['', '', '/foo'], '/foo'],
31+
[['', '', 'foo'], 'foo'],
32+
[['foo', ''], 'foo'],
33+
[['foo/', ''], 'foo/'],
34+
[['foo', '', '/bar'], 'foo/bar'],
35+
[['./', '..', '/foo'], '../foo'],
36+
[['./', '..', '..', '/foo'], '../../foo'],
37+
[['.', '..', '..', '/foo'], '../../foo'],
38+
[['', '..', '..', '/foo'], '../../foo'],
39+
[['/'], '/'],
40+
[['/', '.'], '/'],
41+
[['/', '..'], '/'],
42+
[['/', '..', '..'], '/'],
43+
[[''], '.'],
44+
[['', ''], '.'],
45+
[[' /foo'], ' /foo'],
46+
[[' ', 'foo'], ' /foo'],
47+
[[' ', '.'], ' '],
48+
[[' ', '/'], ' /'],
49+
[[' ', ''], ' '],
50+
[['/', 'foo'], '/foo'],
51+
[['/', '/foo'], '/foo'],
52+
[['/', '//foo'], '/foo'],
53+
[['/', '', '/foo'], '/foo'],
54+
[['', '/', 'foo'], '/foo'],
55+
[['', '/', '/foo'], '/foo']
56+
];
57+
58+
// Windows-specific join tests
59+
var windowsJoinTests =
60+
[// arguments result
61+
// UNC path expected
62+
[['//foo/bar'], '\\\\foo\\bar\\'],
63+
[['\\/foo/bar'], '\\\\foo\\bar\\'],
64+
[['\\\\foo/bar'], '\\\\foo\\bar\\'],
65+
// UNC path expected - server and share separate
66+
[['//foo', 'bar'], '\\\\foo\\bar\\'],
67+
[['//foo/', 'bar'], '\\\\foo\\bar\\'],
68+
[['//foo', '/bar'], '\\\\foo\\bar\\'],
69+
// UNC path expected - questionable
70+
[['//foo', '', 'bar'], '\\\\foo\\bar\\'],
71+
[['//foo/', '', 'bar'], '\\\\foo\\bar\\'],
72+
[['//foo/', '', '/bar'], '\\\\foo\\bar\\'],
73+
// UNC path expected - even more questionable
74+
[['', '//foo', 'bar'], '\\\\foo\\bar\\'],
75+
[['', '//foo/', 'bar'], '\\\\foo\\bar\\'],
76+
[['', '//foo/', '/bar'], '\\\\foo\\bar\\'],
77+
// No UNC path expected (no double slash in first component)
78+
[['\\', 'foo/bar'], '\\foo\\bar'],
79+
[['\\', '/foo/bar'], '\\foo\\bar'],
80+
[['', '/', '/foo/bar'], '\\foo\\bar'],
81+
// No UNC path expected (no non-slashes in first component -
82+
// questionable)
83+
[['//', 'foo/bar'], '\\foo\\bar'],
84+
[['//', '/foo/bar'], '\\foo\\bar'],
85+
[['\\\\', '/', '/foo/bar'], '\\foo\\bar'],
86+
[['//'], '/'],
87+
// No UNC path expected (share name missing - questionable).
88+
[['//foo'], '\\foo'],
89+
[['//foo/'], '\\foo\\'],
90+
[['//foo', '/'], '\\foo\\'],
91+
[['//foo', '', '/'], '\\foo\\'],
92+
// No UNC path expected (too many leading slashes - questionable)
93+
[['///foo/bar'], '\\foo\\bar'],
94+
[['////foo', 'bar'], '\\foo\\bar'],
95+
[['\\\\\\/foo/bar'], '\\foo\\bar'],
96+
// Drive-relative vs drive-absolute paths. This merely describes the
97+
// status quo, rather than being obviously right
98+
[['c:'], 'c:.'],
99+
[['c:.'], 'c:.'],
100+
[['c:', ''], 'c:.'],
101+
[['', 'c:'], 'c:.'],
102+
[['c:.', '/'], 'c:.\\'],
103+
[['c:.', 'file'], 'c:file'],
104+
[['c:', '/'], 'c:\\'],
105+
[['c:', 'file'], 'c:\\file']
106+
];
107+
108+
tape('path.posix.join', function (t) {
109+
joinTests.forEach(function (p) {
110+
var actual = path.posix.join.apply(null, p[0]);
111+
t.strictEqual(actual, p[1]);
112+
});
113+
t.end();
114+
});
115+
116+
tape('path.win32.join', { skip: true }, function (t) {
117+
joinTests.forEach(function (p) {
118+
var actual = path.win32.join.apply(null, p[0]).replace(backslashRE, '/');
119+
t.strictEqual(actual, p[1]);
120+
});
121+
windowsJoinTests.forEach(function (p) {
122+
var actual = path.win32.join.apply(null, p[0]);
123+
t.strictEqual(actual, p[1]);
124+
});
125+
t.end();
126+
});

0 commit comments

Comments
 (0)