forked from microsoft/vscode-node-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathUtilities.test.ts
More file actions
169 lines (127 loc) · 5.78 KB
/
Copy pathpathUtilities.test.ts
File metadata and controls
169 lines (127 loc) · 5.78 KB
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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as assert from 'assert';
import * as PathUtils from '../node/pathUtilities';
suite('pathUtilities', () => {
suite('normalize(path)', () => {
test('should return a path with forward slashes and \'..\' removed', () => {
assert.equal(PathUtils.normalize('/a/b/c'), '/a/b/c');
assert.equal(PathUtils.normalize('/a/b//c'), '/a/b/c');
assert.equal(PathUtils.normalize('/a/./b/c'), '/a/b/c');
assert.equal(PathUtils.normalize('/a/b/../c'), '/a/c');
assert.equal(PathUtils.normalize('c:\\a\\b'), '/c:/a/b');
assert.equal(PathUtils.normalize('C:\\a\\b'), '/C:/a/b');
assert.equal(PathUtils.normalize('C:\\a\\..\\b'), '/C:/b');
assert.equal(PathUtils.normalize('C:\\a\\.\\b'), '/C:/a/b');
assert.equal(PathUtils.normalize('c:/a/b'), '/c:/a/b');
assert.equal(PathUtils.normalize('C:/a/b'), '/C:/a/b');
});
});
suite('join(absPath, relPath)', () => {
test('should return a path with forward slashes', () => {
assert.equal(PathUtils.join('/a/b', 'c'), '/a/b/c');
assert.equal(PathUtils.join('/a/b/', 'c'), '/a/b/c');
assert.equal(PathUtils.join('c:\\a\\b', 'c'), '/c:/a/b/c');
assert.equal(PathUtils.join('c:\\a\\b\\', 'c'), '/c:/a/b/c');
assert.equal(PathUtils.join('C:\\a\\b', 'c'), '/C:/a/b/c');
});
});
suite('isAbsolutePath(path)', () => {
test('should return true when the path is absolute', () => {
assert.equal(PathUtils.isAbsolutePath('/x/y'), true);
assert.equal(PathUtils.isAbsolutePath('c:/x/y'), true);
assert.equal(PathUtils.isAbsolutePath('C:/x/y'), true);
assert.equal(PathUtils.isAbsolutePath('c:\\x\\y'), true);
assert.equal(PathUtils.isAbsolutePath('C:\\x\\y'), true);
});
test('should return false when the path is relative', () => {
assert.equal(PathUtils.isAbsolutePath(null), false);
assert.equal(PathUtils.isAbsolutePath(''), false);
assert.equal(PathUtils.isAbsolutePath('x'), false);
assert.equal(PathUtils.isAbsolutePath('./x'), false);
assert.equal(PathUtils.isAbsolutePath('../y'), false);
assert.equal(PathUtils.isAbsolutePath('.\\x'), false);
assert.equal(PathUtils.isAbsolutePath('..\\y'), false);
});
});
suite('makeRelative(target, path)', () => {
test('identical paths should return empty string', () => {
if (process.platform === 'win32') {
assert.equal(PathUtils.makeRelative('c:\\a\\b', 'c:\\a\\b'), '');
} else {
assert.equal(PathUtils.makeRelative('/a/b', '/a/b'), '');
}
});
test('target and path same length', () => {
if (process.platform === 'win32') {
assert.equal(PathUtils.makeRelative('c:\\a\\b\\c\\d\\e\\f', 'c:\\a\\b\\c\\g\\h\\j'), 'g\\h\\j');
} else {
assert.equal(PathUtils.makeRelative('/a/b/c/d/e/f', '/a/b/c/g/h/j'), 'g/h/j');
}
});
test('target is longer', () => {
if (process.platform === 'win32') {
assert.equal(PathUtils.makeRelative('c:\\a\\b\\c\\d', 'c:\\a\\b\\c'), '');
} else {
assert.equal(PathUtils.makeRelative('/a/b/c/d', '/a/b/c'), '');
}
});
test('path is longer', () => {
if (process.platform === 'win32') {
assert.equal(PathUtils.makeRelative('c:\\a\\b\\c\\d', 'c:\\a\\b\\c\\d\\e'), 'e');
} else {
assert.equal(PathUtils.makeRelative('/a/b/c/d', '/a/b/c/d/e'), 'e');
}
});
});
suite('makeRelative2(from, to)', () => {
test('identical paths should return empty string', () => {
assert.equal(PathUtils.makeRelative2('/common/a', '/common/a'), '');
});
test('from and to same length', () => {
assert.equal(PathUtils.makeRelative2('/a/b/c/d/e/f','/a/b/c/g/h/j'), '../../g/h/j');
});
test('from is longer', () => {
assert.equal(PathUtils.makeRelative2('/a/b/c/d', '/a/b/d'), '../d');
assert.equal(PathUtils.makeRelative2('/a/b/c/d/e', '/a/d/e'), '../../d/e');
});
test('to is longer', () => {
assert.equal(PathUtils.makeRelative2('/a/b/c/d', '/a/b/c/d/e'), 'e');
assert.equal(PathUtils.makeRelative2('/a/b/c/d', '/a/b/c/d/e/f'), 'e/f');
assert.equal(PathUtils.makeRelative2('/', '/a/b'), 'a/b');
});
});
suite('realPath(path)', () => {
const path = __filename;
if (process.platform === 'win32' || process.platform === 'darwin') {
// assume case insensitive file system
test('on a case insensitive file system realPath might return different casing for a given path', () => {
const upper = path.toUpperCase();
const real = PathUtils.realPath(upper);
assert.notEqual(real, upper);
assert.equal(real, PathUtils.normalizeDriveLetter(path)); // make sure that drive letter is normalized on both
assert.equal(real.toUpperCase(), upper);
});
} else {
// linux, unix, etc. -> assume case sensitive file system
test('on a case sensitive file system realPath should always return the same path', () => {
const real = PathUtils.realPath(path);
assert.equal(real, path);
});
}
});
suite('normalizeDriveLetter', () => {
test('should return an unmodified path if there is no drive letter', () => {
assert.equal(PathUtils.normalizeDriveLetter('/a/b/c/d'), '/a/b/c/d');
assert.equal(PathUtils.normalizeDriveLetter('C/hello/world'), 'C/hello/world');
});
test('should return a path with lower case drive letter', () => {
assert.equal(PathUtils.normalizeDriveLetter('C:/hello/world'), 'c:/hello/world');
assert.equal(PathUtils.normalizeDriveLetter('c:/hello/world'), 'c:/hello/world');
assert.equal(PathUtils.normalizeDriveLetter('C:\\hello\\world'), 'c:\\hello\\world');
assert.equal(PathUtils.normalizeDriveLetter('c:\\hello\\world'), 'c:\\hello\\world');
});
});
});