Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/extpath.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { CharCode } from '../../common/charCode.js';
8
import * as extpath from '../../common/extpath.js';
9
import { isWindows } from '../../common/platform.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
11
12
suite('Paths', () => {
13
14
test('toForwardSlashes', () => {
15
assert.strictEqual(extpath.toSlashes('\\\\server\\share\\some\\path'), '//server/share/some/path');
16
assert.strictEqual(extpath.toSlashes('c:\\test'), 'c:/test');
17
assert.strictEqual(extpath.toSlashes('foo\\bar'), 'foo/bar');
18
assert.strictEqual(extpath.toSlashes('/user/far'), '/user/far');
19
});
20
21
test('getRoot', () => {
22
assert.strictEqual(extpath.getRoot('/user/far'), '/');
23
assert.strictEqual(extpath.getRoot('\\\\server\\share\\some\\path'), '//server/share/');
24
assert.strictEqual(extpath.getRoot('//server/share/some/path'), '//server/share/');
25
assert.strictEqual(extpath.getRoot('//server/share'), '/');
26
assert.strictEqual(extpath.getRoot('//server'), '/');
27
assert.strictEqual(extpath.getRoot('//server//'), '/');
28
assert.strictEqual(extpath.getRoot('c:/user/far'), 'c:/');
29
assert.strictEqual(extpath.getRoot('c:user/far'), 'c:');
30
assert.strictEqual(extpath.getRoot('http://www'), '');
31
assert.strictEqual(extpath.getRoot('http://www/'), 'http://www/');
32
assert.strictEqual(extpath.getRoot('file:///foo'), 'file:///');
33
assert.strictEqual(extpath.getRoot('file://foo'), '');
34
});
35
36
(!isWindows ? test.skip : test)('isUNC', () => {
37
assert.ok(!extpath.isUNC('foo'));
38
assert.ok(!extpath.isUNC('/foo'));
39
assert.ok(!extpath.isUNC('\\foo'));
40
assert.ok(!extpath.isUNC('\\\\foo'));
41
assert.ok(extpath.isUNC('\\\\a\\b'));
42
assert.ok(!extpath.isUNC('//a/b'));
43
assert.ok(extpath.isUNC('\\\\server\\share'));
44
assert.ok(extpath.isUNC('\\\\server\\share\\'));
45
assert.ok(extpath.isUNC('\\\\server\\share\\path'));
46
});
47
48
test('isValidBasename', () => {
49
assert.ok(!extpath.isValidBasename(null));
50
assert.ok(!extpath.isValidBasename(''));
51
assert.ok(extpath.isValidBasename('test.txt'));
52
assert.ok(!extpath.isValidBasename('/test.txt'));
53
54
if (isWindows) {
55
assert.ok(!extpath.isValidBasename('\\test.txt'));
56
assert.ok(!extpath.isValidBasename('aux'));
57
assert.ok(!extpath.isValidBasename('Aux'));
58
assert.ok(!extpath.isValidBasename('LPT0'));
59
assert.ok(!extpath.isValidBasename('aux.txt'));
60
assert.ok(!extpath.isValidBasename('com0.abc'));
61
assert.ok(extpath.isValidBasename('LPT00'));
62
assert.ok(extpath.isValidBasename('aux1'));
63
assert.ok(extpath.isValidBasename('aux1.txt'));
64
assert.ok(extpath.isValidBasename('aux1.aux.txt'));
65
66
assert.ok(!extpath.isValidBasename('test.txt.'));
67
assert.ok(!extpath.isValidBasename('test.txt..'));
68
assert.ok(!extpath.isValidBasename('test.txt '));
69
assert.ok(!extpath.isValidBasename('test.txt\t'));
70
assert.ok(!extpath.isValidBasename('tes:t.txt'));
71
assert.ok(!extpath.isValidBasename('tes"t.txt'));
72
} else {
73
assert.ok(extpath.isValidBasename('\\test.txt'));
74
}
75
});
76
77
test('sanitizeFilePath', () => {
78
if (isWindows) {
79
assert.strictEqual(extpath.sanitizeFilePath('.', 'C:\\the\\cwd'), 'C:\\the\\cwd');
80
assert.strictEqual(extpath.sanitizeFilePath('', 'C:\\the\\cwd'), 'C:\\the\\cwd');
81
82
assert.strictEqual(extpath.sanitizeFilePath('C:', 'C:\\the\\cwd'), 'C:\\');
83
assert.strictEqual(extpath.sanitizeFilePath('C:\\', 'C:\\the\\cwd'), 'C:\\');
84
assert.strictEqual(extpath.sanitizeFilePath('C:\\\\', 'C:\\the\\cwd'), 'C:\\');
85
86
assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my.txt', 'C:\\the\\cwd'), 'C:\\folder\\my.txt');
87
assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my', 'C:\\the\\cwd'), 'C:\\folder\\my');
88
assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\..\\my', 'C:\\the\\cwd'), 'C:\\my');
89
assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my\\', 'C:\\the\\cwd'), 'C:\\folder\\my');
90
assert.strictEqual(extpath.sanitizeFilePath('C:\\folder\\my\\\\\\', 'C:\\the\\cwd'), 'C:\\folder\\my');
91
92
assert.strictEqual(extpath.sanitizeFilePath('my.txt', 'C:\\the\\cwd'), 'C:\\the\\cwd\\my.txt');
93
assert.strictEqual(extpath.sanitizeFilePath('my.txt\\', 'C:\\the\\cwd'), 'C:\\the\\cwd\\my.txt');
94
95
assert.strictEqual(extpath.sanitizeFilePath('\\\\localhost\\folder\\my', 'C:\\the\\cwd'), '\\\\localhost\\folder\\my');
96
assert.strictEqual(extpath.sanitizeFilePath('\\\\localhost\\folder\\my\\', 'C:\\the\\cwd'), '\\\\localhost\\folder\\my');
97
} else {
98
assert.strictEqual(extpath.sanitizeFilePath('.', '/the/cwd'), '/the/cwd');
99
assert.strictEqual(extpath.sanitizeFilePath('', '/the/cwd'), '/the/cwd');
100
assert.strictEqual(extpath.sanitizeFilePath('/', '/the/cwd'), '/');
101
102
assert.strictEqual(extpath.sanitizeFilePath('/folder/my.txt', '/the/cwd'), '/folder/my.txt');
103
assert.strictEqual(extpath.sanitizeFilePath('/folder/my', '/the/cwd'), '/folder/my');
104
assert.strictEqual(extpath.sanitizeFilePath('/folder/../my', '/the/cwd'), '/my');
105
assert.strictEqual(extpath.sanitizeFilePath('/folder/my/', '/the/cwd'), '/folder/my');
106
assert.strictEqual(extpath.sanitizeFilePath('/folder/my///', '/the/cwd'), '/folder/my');
107
108
assert.strictEqual(extpath.sanitizeFilePath('my.txt', '/the/cwd'), '/the/cwd/my.txt');
109
assert.strictEqual(extpath.sanitizeFilePath('my.txt/', '/the/cwd'), '/the/cwd/my.txt');
110
}
111
});
112
113
test('isRootOrDriveLetter', () => {
114
if (isWindows) {
115
assert.ok(extpath.isRootOrDriveLetter('c:'));
116
assert.ok(extpath.isRootOrDriveLetter('D:'));
117
assert.ok(extpath.isRootOrDriveLetter('D:/'));
118
assert.ok(extpath.isRootOrDriveLetter('D:\\'));
119
assert.ok(!extpath.isRootOrDriveLetter('D:\\path'));
120
assert.ok(!extpath.isRootOrDriveLetter('D:/path'));
121
} else {
122
assert.ok(extpath.isRootOrDriveLetter('/'));
123
assert.ok(!extpath.isRootOrDriveLetter('/path'));
124
}
125
});
126
127
test('hasDriveLetter', () => {
128
if (isWindows) {
129
assert.ok(extpath.hasDriveLetter('c:'));
130
assert.ok(extpath.hasDriveLetter('D:'));
131
assert.ok(extpath.hasDriveLetter('D:/'));
132
assert.ok(extpath.hasDriveLetter('D:\\'));
133
assert.ok(extpath.hasDriveLetter('D:\\path'));
134
assert.ok(extpath.hasDriveLetter('D:/path'));
135
} else {
136
assert.ok(!extpath.hasDriveLetter('/'));
137
assert.ok(!extpath.hasDriveLetter('/path'));
138
}
139
});
140
141
test('getDriveLetter', () => {
142
if (isWindows) {
143
assert.strictEqual(extpath.getDriveLetter('c:'), 'c');
144
assert.strictEqual(extpath.getDriveLetter('D:'), 'D');
145
assert.strictEqual(extpath.getDriveLetter('D:/'), 'D');
146
assert.strictEqual(extpath.getDriveLetter('D:\\'), 'D');
147
assert.strictEqual(extpath.getDriveLetter('D:\\path'), 'D');
148
assert.strictEqual(extpath.getDriveLetter('D:/path'), 'D');
149
} else {
150
assert.ok(!extpath.getDriveLetter('/'));
151
assert.ok(!extpath.getDriveLetter('/path'));
152
}
153
});
154
155
test('isWindowsDriveLetter', () => {
156
assert.ok(!extpath.isWindowsDriveLetter(0));
157
assert.ok(!extpath.isWindowsDriveLetter(-1));
158
assert.ok(extpath.isWindowsDriveLetter(CharCode.A));
159
assert.ok(extpath.isWindowsDriveLetter(CharCode.z));
160
});
161
162
test('indexOfPath', () => {
163
assert.strictEqual(extpath.indexOfPath('/foo', '/bar', true), -1);
164
assert.strictEqual(extpath.indexOfPath('/foo', '/FOO', false), -1);
165
assert.strictEqual(extpath.indexOfPath('/foo', '/FOO', true), 0);
166
assert.strictEqual(extpath.indexOfPath('/some/long/path', '/some/long', false), 0);
167
assert.strictEqual(extpath.indexOfPath('/some/long/path', '/PATH', true), 10);
168
});
169
170
test('parseLineAndColumnAware', () => {
171
let res = extpath.parseLineAndColumnAware('/foo/bar');
172
assert.strictEqual(res.path, '/foo/bar');
173
assert.strictEqual(res.line, undefined);
174
assert.strictEqual(res.column, undefined);
175
176
res = extpath.parseLineAndColumnAware('/foo/bar:33');
177
assert.strictEqual(res.path, '/foo/bar');
178
assert.strictEqual(res.line, 33);
179
assert.strictEqual(res.column, 1);
180
181
res = extpath.parseLineAndColumnAware('/foo/bar:33:34');
182
assert.strictEqual(res.path, '/foo/bar');
183
assert.strictEqual(res.line, 33);
184
assert.strictEqual(res.column, 34);
185
186
res = extpath.parseLineAndColumnAware('C:\\foo\\bar');
187
assert.strictEqual(res.path, 'C:\\foo\\bar');
188
assert.strictEqual(res.line, undefined);
189
assert.strictEqual(res.column, undefined);
190
191
res = extpath.parseLineAndColumnAware('C:\\foo\\bar:33');
192
assert.strictEqual(res.path, 'C:\\foo\\bar');
193
assert.strictEqual(res.line, 33);
194
assert.strictEqual(res.column, 1);
195
196
res = extpath.parseLineAndColumnAware('C:\\foo\\bar:33:34');
197
assert.strictEqual(res.path, 'C:\\foo\\bar');
198
assert.strictEqual(res.line, 33);
199
assert.strictEqual(res.column, 34);
200
201
res = extpath.parseLineAndColumnAware('/foo/bar:abb');
202
assert.strictEqual(res.path, '/foo/bar:abb');
203
assert.strictEqual(res.line, undefined);
204
assert.strictEqual(res.column, undefined);
205
});
206
207
test('randomPath', () => {
208
let res = extpath.randomPath('/foo/bar');
209
assert.ok(res);
210
211
res = extpath.randomPath('/foo/bar', 'prefix-');
212
assert.ok(res.indexOf('prefix-'));
213
214
const r1 = extpath.randomPath('/foo/bar');
215
const r2 = extpath.randomPath('/foo/bar');
216
217
assert.notStrictEqual(r1, r2);
218
219
const r3 = extpath.randomPath('', '', 3);
220
assert.strictEqual(r3.length, 3);
221
222
const r4 = extpath.randomPath();
223
assert.ok(r4);
224
});
225
226
ensureNoDisposablesAreLeakedInTestSuite();
227
});
228
229