Skip to content

Commit 20d6183

Browse files
committed
⚗️ Add tests to verify shrinkWorkspaceFolderCwdPairs function
Signed-off-by: Babak K. Shandiz <[email protected]>
1 parent f00fa56 commit 20d6183

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 { deepStrictEqual } from 'assert';
7+
import { URI } from 'vs/base/common/uri';
8+
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
9+
import { WorkspaceFolderCwdPair, shrinkWorkspaceFolderCwdPairs } from 'vs/workbench/contrib/terminal/browser/terminalActions';
10+
11+
function makeFakeFolder(name: string, uri: URI): IWorkspaceFolder {
12+
return {
13+
name,
14+
uri,
15+
index: 0,
16+
toResource: () => uri,
17+
};
18+
}
19+
20+
function makePair(folder: IWorkspaceFolder, cwd?: URI | IWorkspaceFolder, isAbsolute?: boolean): WorkspaceFolderCwdPair {
21+
return {
22+
folder,
23+
cwd: !cwd ? folder.uri : (cwd instanceof URI ? cwd : cwd.uri),
24+
isAbsolute: !!isAbsolute,
25+
isOverridden: !!cwd && cwd.toString() !== folder.uri.toString(),
26+
};
27+
}
28+
29+
suite('terminalActions', () => {
30+
const root: URI = URI.file('/some-root');
31+
const a = makeFakeFolder('a', URI.joinPath(root, 'a'));
32+
const b = makeFakeFolder('b', URI.joinPath(root, 'b'));
33+
const c = makeFakeFolder('c', URI.joinPath(root, 'c'));
34+
const d = makeFakeFolder('d', URI.joinPath(root, 'd'));
35+
36+
suite('shrinkWorkspaceFolderCwdPairs', () => {
37+
test('should return empty when given array is empty', () => {
38+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs([]), []);
39+
});
40+
41+
test('should return the only single pair when given argument is a single element array', () => {
42+
const pairs = [makePair(a)];
43+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs(pairs), pairs);
44+
});
45+
46+
test('should return all pairs when no repeated cwds', () => {
47+
const pairs = [makePair(a), makePair(b), makePair(c)];
48+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs(pairs), pairs);
49+
});
50+
51+
suite('should select the pair that has the same URI when repeated cwds exist', () => {
52+
test('all repeated', () => {
53+
const pairA = makePair(a);
54+
const pairB = makePair(b, a); // CWD points to A
55+
const pairC = makePair(c, a); // CWD points to A
56+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs([pairA, pairB, pairC]), [pairA]);
57+
});
58+
59+
test('two repeated + one different', () => {
60+
const pairA = makePair(a);
61+
const pairB = makePair(b, a); // CWD points to A
62+
const pairC = makePair(c);
63+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs([pairA, pairB, pairC]), [pairA, pairC]);
64+
});
65+
66+
test('two repeated + two repeated', () => {
67+
const pairA = makePair(a);
68+
const pairB = makePair(b, a); // CWD points to A
69+
const pairC = makePair(c);
70+
const pairD = makePair(d, c);
71+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs([pairA, pairB, pairC, pairD]), [pairA, pairC]);
72+
});
73+
74+
test('two repeated + two repeated (reverse order)', () => {
75+
const pairB = makePair(b, a); // CWD points to A
76+
const pairA = makePair(a);
77+
const pairD = makePair(d, c);
78+
const pairC = makePair(c);
79+
deepStrictEqual(shrinkWorkspaceFolderCwdPairs([pairA, pairB, pairC, pairD]), [pairA, pairC]);
80+
});
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)