UNPKG

14.9 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const assert = require("assert");
19const path_1 = require("./path");
20const chai_1 = require("chai");
21describe('Path', () => {
22 it('new from /foo/bar/file.txt', () => {
23 const path = new path_1.Path('/foo/bar/file.txt');
24 assert.deepStrictEqual(path.isRoot, false);
25 assert.deepStrictEqual(path.isAbsolute, true);
26 assert.deepStrictEqual(path.root.toString(), '/');
27 assert.deepStrictEqual(path.dir.toString(), '/foo/bar');
28 assert.deepStrictEqual(path.hasDir, true);
29 assert.deepStrictEqual(path.base, 'file.txt');
30 assert.deepStrictEqual(path.name, 'file');
31 assert.deepStrictEqual(path.ext, '.txt');
32 });
33 it('new from foo/bar/file.txt', () => {
34 const path = new path_1.Path('foo/bar/file.txt');
35 assert.deepStrictEqual(path.isRoot, false);
36 assert.deepStrictEqual(path.isAbsolute, false);
37 assert.deepStrictEqual(path.root, undefined);
38 assert.deepStrictEqual(path.dir.toString(), 'foo/bar');
39 assert.deepStrictEqual(path.hasDir, true);
40 assert.deepStrictEqual(path.base, 'file.txt');
41 assert.deepStrictEqual(path.name, 'file');
42 assert.deepStrictEqual(path.ext, '.txt');
43 });
44 it('new from /foo', () => {
45 const path = new path_1.Path('/foo');
46 assert.deepStrictEqual(path.isRoot, false);
47 assert.deepStrictEqual(path.isAbsolute, true);
48 assert.deepStrictEqual(path.root.toString(), '/');
49 assert.deepStrictEqual(path.dir.toString(), '/');
50 assert.deepStrictEqual(path.hasDir, true);
51 assert.deepStrictEqual(path.base, 'foo');
52 assert.deepStrictEqual(path.name, 'foo');
53 assert.deepStrictEqual(path.ext, '');
54 });
55 it('new from foo', () => {
56 const path = new path_1.Path('foo');
57 assert.deepStrictEqual(path.isRoot, false);
58 assert.deepStrictEqual(path.isAbsolute, false);
59 assert.deepStrictEqual(path.root, undefined);
60 assert.deepStrictEqual(path.dir.toString(), 'foo');
61 assert.deepStrictEqual(path.hasDir, false);
62 assert.deepStrictEqual(path.base, 'foo');
63 assert.deepStrictEqual(path.name, 'foo');
64 assert.deepStrictEqual(path.ext, '');
65 });
66 it('new from /', () => {
67 const path = new path_1.Path('/');
68 assert.deepStrictEqual(path.isRoot, true);
69 assert.deepStrictEqual(path.isAbsolute, true);
70 assert.deepStrictEqual(path.root.toString(), '/');
71 assert.deepStrictEqual(path.dir.toString(), '/');
72 assert.deepStrictEqual(path.hasDir, false);
73 assert.deepStrictEqual(path.base, '');
74 assert.deepStrictEqual(path.name, '');
75 assert.deepStrictEqual(path.ext, '');
76 });
77 it('new from /c:/foo/bar/file.txt', () => {
78 const path = new path_1.Path('/c:/foo/bar/file.txt');
79 assert.deepStrictEqual(path.isRoot, false);
80 assert.deepStrictEqual(path.isAbsolute, true);
81 assert.deepStrictEqual(path.root.toString(), '/c:');
82 assert.deepStrictEqual(path.dir.toString(), '/c:/foo/bar');
83 assert.deepStrictEqual(path.hasDir, true);
84 assert.deepStrictEqual(path.base, 'file.txt');
85 assert.deepStrictEqual(path.name, 'file');
86 assert.deepStrictEqual(path.ext, '.txt');
87 });
88 it('new from /c:/foo', () => {
89 const path = new path_1.Path('/c:/foo');
90 assert.deepStrictEqual(path.isRoot, false);
91 assert.deepStrictEqual(path.isAbsolute, true);
92 assert.deepStrictEqual(path.root.toString(), '/c:');
93 assert.deepStrictEqual(path.dir.toString(), '/c:');
94 assert.deepStrictEqual(path.hasDir, true);
95 assert.deepStrictEqual(path.base, 'foo');
96 assert.deepStrictEqual(path.name, 'foo');
97 assert.deepStrictEqual(path.ext, '');
98 });
99 it('new from /c:/', () => {
100 const path = new path_1.Path('/c:/');
101 assert.deepStrictEqual(path.isRoot, false);
102 assert.deepStrictEqual(path.isAbsolute, true);
103 assert.deepStrictEqual(path.root.toString(), '/c:');
104 assert.deepStrictEqual(path.dir.toString(), '/c:');
105 assert.deepStrictEqual(path.hasDir, true);
106 assert.deepStrictEqual(path.base, '');
107 assert.deepStrictEqual(path.name, '');
108 assert.deepStrictEqual(path.ext, '');
109 });
110 it('new from /c:', () => {
111 const path = new path_1.Path('/c:');
112 assert.deepStrictEqual(path.isRoot, true);
113 assert.deepStrictEqual(path.isAbsolute, true);
114 assert.deepStrictEqual(path.root.toString(), '/c:');
115 assert.deepStrictEqual(path.dir.toString(), '/c:');
116 assert.deepStrictEqual(path.hasDir, false);
117 assert.deepStrictEqual(path.base, 'c:');
118 assert.deepStrictEqual(path.name, 'c:');
119 assert.deepStrictEqual(path.ext, '');
120 });
121 assertRelative({
122 from: '/foo',
123 to: '/foo',
124 expectation: ''
125 });
126 assertRelative({
127 from: '/foo',
128 to: '/foo/bar',
129 expectation: 'bar'
130 });
131 assertRelative({
132 from: '/foo/',
133 to: '/foo/bar',
134 expectation: 'bar'
135 });
136 assertRelative({
137 from: '/f',
138 to: '/foo/bar',
139 expectation: undefined
140 });
141 function assertRelative({ from, to, expectation }) {
142 it(`the relative path from '${from}' to '${to}' should be '${expectation}'`, () => {
143 const path = new path_1.Path(from).relative(new path_1.Path(to));
144 assert.deepStrictEqual(expectation, path && path.toString());
145 });
146 }
147 assertNormalize({
148 from: '/',
149 expectation: '/'
150 });
151 assertNormalize({
152 from: '/c://',
153 expectation: '/c:/'
154 });
155 assertNormalize({
156 from: '/foo',
157 expectation: '/foo'
158 });
159 assertNormalize({
160 from: '/foo/',
161 expectation: '/foo/'
162 });
163 assertNormalize({
164 from: '/foo/bar',
165 expectation: '/foo/bar'
166 });
167 assertNormalize({
168 from: '/foo/../file.txt',
169 expectation: '/file.txt'
170 });
171 assertNormalize({
172 from: '/foo/bar/../file.txt',
173 expectation: '/foo/file.txt'
174 });
175 assertNormalize({
176 from: '/foo/../../file.txt',
177 expectation: '/file.txt'
178 });
179 assertNormalize({
180 from: '',
181 expectation: '.'
182 });
183 assertNormalize({
184 from: '.',
185 expectation: '.'
186 });
187 assertNormalize({
188 from: '..',
189 expectation: '..'
190 });
191 assertNormalize({
192 from: './foo',
193 expectation: 'foo'
194 });
195 assertNormalize({
196 from: './foo/./.',
197 expectation: 'foo'
198 });
199 assertNormalize({
200 from: './foo/',
201 expectation: 'foo/'
202 });
203 assertNormalize({
204 from: '../foo',
205 expectation: '../foo'
206 });
207 assertNormalize({
208 from: 'foo/..',
209 expectation: '.'
210 });
211 assertNormalize({
212 from: 'foo/bar/../../../',
213 expectation: '../'
214 });
215 function assertNormalize({ from, expectation }) {
216 it(`path ${from} should be normalized as ${expectation}`, () => {
217 assert.deepStrictEqual(new path_1.Path(from).normalize().toString(), expectation);
218 });
219 }
220 describe('Normalize path separator', () => {
221 it('should handle windows styled paths', async () => {
222 const path = 'C:\\a\\b\\c';
223 const expected = '/c:/a/b/c';
224 (0, chai_1.expect)(new path_1.Path(path).toString()).eq(expected);
225 });
226 it('should prefix drive letter with /', async () => {
227 const path = 'c:/a/b/c';
228 const expected = '/c:/a/b/c';
229 (0, chai_1.expect)(new path_1.Path(path).toString()).eq(expected);
230 });
231 });
232 const linuxHome = '/home/test-user';
233 const windowsHome = '/C:/Users/test-user';
234 describe('Linux', () => {
235 it('should shorten path on Linux, path starting with home', async () => {
236 const path = `${linuxHome}/a/b/theia`;
237 const expected = '~/a/b/theia';
238 (0, chai_1.expect)(path_1.Path.tildify(path, linuxHome)).eq(expected);
239 });
240 it('should shorten path on Linux, path starting with home with duplication', async () => {
241 const path = `${linuxHome}/${linuxHome}/a/b/theia`;
242 const expected = `~/${linuxHome}/a/b/theia`;
243 (0, chai_1.expect)(path_1.Path.tildify(path, linuxHome)).eq(expected);
244 });
245 it('should not shorten path on Linux, path not starting with home', async () => {
246 const path = `/test/${linuxHome}/a/b/theia`;
247 const expected = `/test/${linuxHome}/a/b/theia`;
248 (0, chai_1.expect)(path_1.Path.tildify(path, linuxHome)).eq(expected);
249 });
250 it('should not shorten path on Linux, path not starting with correct home', async () => {
251 const path = `/test/${linuxHome}123/a/b/theia`;
252 const expected = `/test/${linuxHome}123/a/b/theia`;
253 (0, chai_1.expect)(path_1.Path.tildify(path, linuxHome)).eq(expected);
254 });
255 it('should not shorten path on Linux when home is empty', async () => {
256 const path = `${linuxHome}/a/b/theia`;
257 const expected = `${linuxHome}/a/b/theia`;
258 (0, chai_1.expect)(path_1.Path.tildify(path, '')).eq(expected);
259 });
260 it('should expand ~ on Linux when path begins with ~', async () => {
261 const path = '~/a/b/theia';
262 const expected = `${linuxHome}/a/b/theia`;
263 (0, chai_1.expect)(path_1.Path.untildify(path, linuxHome)).eq(expected);
264 });
265 it('should expand ~ on Linux when path starts with ~ duplication', async () => {
266 const path = '~/~/a/b/theia';
267 const expected = `${linuxHome}/~/a/b/theia`;
268 (0, chai_1.expect)(path_1.Path.untildify(path, linuxHome)).eq(expected);
269 });
270 it('should not expand ~ on Linux when path does not start with ~', async () => {
271 const path = '/test/~/a/b/theia';
272 const expected = '/test/~/a/b/theia';
273 (0, chai_1.expect)(path_1.Path.untildify(path, linuxHome)).eq(expected);
274 });
275 it('should not expand ~ on Linux when home is empty', async () => {
276 const path = '~/a/b/theia';
277 const expected = '~/a/b/theia';
278 (0, chai_1.expect)(path_1.Path.untildify(path, '')).eq(expected);
279 });
280 });
281 describe('Windows', () => {
282 it('should not shorten path on Windows', async () => {
283 const path = `${windowsHome}/a/b/theia`;
284 const expected = `${windowsHome}/a/b/theia`;
285 (0, chai_1.expect)(path_1.Path.tildify(path, windowsHome)).eq(expected);
286 });
287 it('should not shorten path on Windows when home is empty', async () => {
288 const path = `${windowsHome}/a/b/theia`;
289 const expected = `${windowsHome}/a/b/theia`;
290 (0, chai_1.expect)(path_1.Path.tildify(path, '')).eq(expected);
291 });
292 it('should not expand ~ on Windows', async () => {
293 const path = '~/a/b/theia';
294 const expected = '~/a/b/theia';
295 (0, chai_1.expect)(path_1.Path.untildify(path, windowsHome)).eq(expected);
296 });
297 it('should not expand ~ on Windows when home is empty', async () => {
298 const path = '~/a/b/theia';
299 const expected = '~/a/b/theia';
300 (0, chai_1.expect)(path_1.Path.untildify(path, '')).eq(expected);
301 });
302 });
303 describe('fsPath#windows', () => {
304 it('should retain windows style path', () => {
305 const path = 'C:\\path\\to\\file.txt';
306 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Windows)).eq(path);
307 });
308 it('should create windows style path with slashes', () => {
309 const path = 'C:/path/to/file.txt';
310 const expected = 'C:\\path\\to\\file.txt';
311 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Windows)).eq(expected);
312 });
313 it('should append slashes to drive letter', () => {
314 const path = 'C:';
315 const expected = 'C:\\';
316 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Windows)).eq(expected);
317 });
318 it('should create windows style path from posix', () => {
319 const path = '/path/to/file.txt';
320 const expected = '\\path\\to\\file.txt';
321 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Windows)).eq(expected);
322 });
323 });
324 describe('fsPath#posix', () => {
325 it('should retain posix style path', () => {
326 const path = '/path/to/file.txt';
327 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Posix)).eq(path);
328 });
329 it('should create posix style path from windows with slashes', () => {
330 const path = 'C:/path/to/file.txt';
331 const expected = '/c:/path/to/file.txt';
332 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Posix)).eq(expected);
333 });
334 it('should create posix style path from windows', () => {
335 const path = 'C:\\path\\to\\file.txt';
336 const expected = '/c:/path/to/file.txt';
337 (0, chai_1.expect)(new path_1.Path(path).fsPath(path_1.Path.Format.Posix)).eq(expected);
338 });
339 });
340 function checkResolution(original, segments, expected) {
341 it(`should resolve ${original} and ${segments.join(', ')} to ${expected}`, () => {
342 const start = new path_1.Path(original);
343 const result = start.resolve(...segments);
344 (0, chai_1.expect)(result === null || result === void 0 ? void 0 : result.toString()).eq(expected);
345 });
346 }
347 checkResolution('a/b/c', ['/d/e/f'], '/d/e/f');
348 checkResolution('a/b/c', ['../d/e/f'], undefined);
349 checkResolution('/a/b/c', ['../d', 'e', './f'], '/a/b/d/e/f');
350});
351//# sourceMappingURL=path.spec.js.map
\No newline at end of file