-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.spec.ts
121 lines (108 loc) · 3.38 KB
/
utils.spec.ts
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
import { mockVscode, testVscode } from "../mockUtil";
mockVscode("src/utils/utils");
import { expect } from "chai";
import { Uri } from "vscode";
import * as sinon from "sinon";
import { Utils } from "../../src/utils/utils";
import { SelectionItem } from "../../src/utils/selectionItem";
import { IChildLogger } from "@vscode-logging/logger";
import * as fsExtra from "fs-extra";
describe("Utils unit tests", () => {
const path1 = "some/path/to/file1";
const path2 = "some/path/to/file2";
let sandbox: sinon.SinonSandbox;
let windowMock: sinon.SinonMock;
let utilsMock: sinon.SinonMock;
const loggerImpl: IChildLogger = {
fatal: () => {
"fatal";
},
error: () => {
"error";
},
warn: () => {
"warn";
},
info: () => {
"info";
},
debug: () => {
"debug";
},
trace: () => {
"trace";
},
getChildLogger: () => {
return loggerImpl;
},
};
before(() => {
sandbox = sinon.createSandbox();
});
after(() => {
sinon.restore();
});
beforeEach(() => {
windowMock = sandbox.mock(testVscode.window);
utilsMock = sandbox.mock(Utils);
});
afterEach(() => {
windowMock.verify();
utilsMock.verify();
});
it("displayOptions - display options in QuickPick list", async () => {
const inputRequest = "request";
const selectionItems: SelectionItem[] = [
{ description: "", detail: "", label: path1 },
{ description: "", detail: "", label: path2 },
];
const options = {
placeHolder: inputRequest,
canPickMany: false,
matchOnDetail: true,
ignoreFocusOut: true,
};
windowMock
.expects("showQuickPick")
.once()
.withExactArgs(selectionItems, options);
await Utils.displayOptions(inputRequest, selectionItems);
});
it("execCommand - execute command in child process", async () => {
const response = await Utils.execCommand("sh", ["-c", "echo test"], {});
expect(response.stdout).to.include("test");
});
it("execCommand - execute unsupported command in child process", async () => {
const response = await Utils.execCommand("bla", ["bla"], {});
expect(response.exitCode).to.equal("ENOENT");
});
it("getConfigFileField - unable to fetch field from non existing config file", async () => {
utilsMock
.expects("getConfigFilePath")
.once()
.returns("path/to/non/existing/file");
const response = await Utils.getConfigFileField("field1", loggerImpl);
expect(response).to.equal(undefined);
});
it("getConfigFileField - fetch field from existing config file", async () => {
utilsMock
.expects("getConfigFilePath")
.once()
.returns("path/to/existing/file");
// Converting SinonStub because it takes the wrong overload
((sinon.stub(fsExtra, "readFile") as unknown) as sinon.SinonStub<
[string, string],
Promise<string>
>)
.withArgs("path/to/existing/file", "utf8")
.resolves(`{"field1":"field1_value"}`);
const response = await Utils.getConfigFileField("field1", loggerImpl);
expect(response).to.equal("field1_value");
});
it("getFilePaths - get paths of a non windows platform", () => {
const filePaths = [{ path: path1 } as Uri, { path: path2 } as Uri];
sandbox.stub(Utils, "isWindows").returns(false);
const paths = Utils.getFilePaths(filePaths);
expect(paths).to.deep.equal([path1, path2]);
});
});