-
Notifications
You must be signed in to change notification settings - Fork 13
/
mtaBuildCommand.spec.ts
220 lines (208 loc) · 6.6 KB
/
mtaBuildCommand.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import * as sinon from "sinon";
import * as os from "os";
import { Uri } from "vscode";
import { mockVscode, testVscode } from "../mockUtil";
mockVscode("src/commands/mtaBuildCommand");
import { MtaBuildCommand } from "../../src/commands/mtaBuildCommand";
mockVscode("src/utils/utils");
import { Utils } from "../../src/utils/utils";
import { messages } from "../../src/i18n/messages";
import { SelectionItem } from "../../src/utils/selectionItem";
import * as loggerWraper from "../../src/logger/logger-wrapper";
import { IChildLogger } from "@vscode-logging/logger";
import { SWATracker } from "@sap/swa-for-sapbas-vsx";
describe("MTA build command unit tests", () => {
let sandbox: sinon.SinonSandbox;
let mtaBuildCommand: MtaBuildCommand;
let utilsMock: sinon.SinonMock;
let windowMock: sinon.SinonMock;
let workspaceMock: sinon.SinonMock;
let selectionItemMock: sinon.SinonMock;
let tasksMock: sinon.SinonMock;
let loggerWraperMock: sinon.SinonMock;
let swa: SWATracker;
let swaMock: sinon.SinonMock;
const loggerImpl: IChildLogger = {
fatal: () => {
"fatal";
},
error: () => {
"error";
},
warn: () => {
"warn";
},
info: () => {
"info";
},
debug: () => {
"debug";
},
trace: () => {
"trace";
},
getChildLogger: () => {
return loggerImpl;
},
};
const selected: Partial<Uri> = {
path: "mtaProject/mta.yaml",
};
const MBT_CMD = "mbt";
const BUILD = "build";
const SOURCE_FLAG = "-s";
const expectedPath = "mtaProject";
const homeDir = os.homedir();
const execution = new testVscode.ShellExecution(MBT_CMD, [
BUILD,
SOURCE_FLAG,
expectedPath,
]);
const buildTask = new testVscode.Task(
{ type: "shell" },
testVscode.TaskScope.Workspace,
messages.BUILD_MTA,
"MTA",
execution
);
beforeEach(() => {
sandbox = sinon.createSandbox();
loggerWraperMock = sandbox.mock(loggerWraper);
loggerWraperMock.expects("getClassLogger").atLeast(1).returns(loggerImpl);
mtaBuildCommand = new MtaBuildCommand();
utilsMock = sandbox.mock(Utils);
windowMock = sandbox.mock(testVscode.window);
workspaceMock = sandbox.mock(testVscode.workspace);
selectionItemMock = sandbox.mock(SelectionItem);
tasksMock = sandbox.mock(testVscode.tasks);
swa = new SWATracker("", "");
swaMock = sandbox.mock(swa);
});
afterEach(() => {
utilsMock.verify();
windowMock.verify();
workspaceMock.verify();
selectionItemMock.verify();
tasksMock.verify();
loggerWraperMock.verify();
swaMock.verify();
sandbox.restore();
});
it("mtaBuildCommand - build MTA from context menu", async () => {
utilsMock
.expects("execCommand")
.once()
.withExactArgs(MBT_CMD, ["-v"], { cwd: homeDir })
.returns("v1.2.3");
tasksMock.expects("executeTask").once().withExactArgs(buildTask);
swaMock
.expects("track")
.once()
.withExactArgs(messages.EVENT_TYPE_BUILD_MTA, [
messages.CUSTOM_EVENT_CONTEXT_MENU,
])
.returns({});
await mtaBuildCommand.mtaBuildCommand(selected as Uri, swa);
});
it("mtaBuildCommand - build MTA from command when no mta.yaml file in the project", async () => {
workspaceMock.expects("findFiles").returns(Promise.resolve([]));
utilsMock
.expects("execCommand")
.once()
.withExactArgs(MBT_CMD, ["-v"], { cwd: homeDir })
.returns("v1.2.3");
windowMock
.expects("showErrorMessage")
.withExactArgs(messages.NO_PROJECT_DESCRIPTOR);
swaMock
.expects("track")
.once()
.withExactArgs(messages.EVENT_TYPE_BUILD_MTA, [
messages.CUSTOM_EVENT_COMMAND_PALETTE,
])
.returns({});
await mtaBuildCommand.mtaBuildCommand(undefined, swa);
});
it("mtaBuildCommand - build MTA from command with only one mta.yaml file in the project", async () => {
workspaceMock.expects("findFiles").returns(Promise.resolve([selected]));
utilsMock
.expects("execCommand")
.once()
.withExactArgs(MBT_CMD, ["-v"], { cwd: homeDir })
.returns("v1.2.3");
tasksMock.expects("executeTask").once().withExactArgs(buildTask);
swaMock
.expects("track")
.once()
.withExactArgs(messages.EVENT_TYPE_BUILD_MTA, [
messages.CUSTOM_EVENT_COMMAND_PALETTE,
])
.returns({});
await mtaBuildCommand.mtaBuildCommand(undefined, swa);
});
it("mtaBuildCommand - build MTA from command with several mta.yaml files in the project", async () => {
utilsMock
.expects("execCommand")
.once()
.withExactArgs(MBT_CMD, ["-v"], { cwd: homeDir })
.returns("v1.2.3");
workspaceMock
.expects("findFiles")
.returns(Promise.resolve([selected, { path: "mtaProject2/mta.yaml" }]));
selectionItemMock
.expects("getSelectionItems")
.once()
.returns(Promise.resolve());
utilsMock
.expects("displayOptions")
.once()
.returns(Promise.resolve({ label: "mtaProject/mta.yaml" }));
tasksMock.expects("executeTask").once().withExactArgs(buildTask);
swaMock
.expects("track")
.once()
.withExactArgs(messages.EVENT_TYPE_BUILD_MTA, [
messages.CUSTOM_EVENT_COMMAND_PALETTE,
])
.returns({});
await mtaBuildCommand.mtaBuildCommand(undefined, swa);
});
it("mtaBuildCommand - build MTA from command with several mta.yaml files in the project - cancel selection", async () => {
utilsMock
.expects("execCommand")
.once()
.withExactArgs(MBT_CMD, ["-v"], { cwd: homeDir })
.returns("v1.2.3");
workspaceMock
.expects("findFiles")
.returns(Promise.resolve([selected, { path: "mtaProject2/mta.yaml" }]));
selectionItemMock
.expects("getSelectionItems")
.once()
.returns(Promise.resolve());
utilsMock
.expects("displayOptions")
.once()
.returns(Promise.resolve(undefined));
tasksMock.expects("executeTask").never();
swaMock
.expects("track")
.once()
.withExactArgs(messages.EVENT_TYPE_BUILD_MTA, [
messages.CUSTOM_EVENT_COMMAND_PALETTE,
])
.returns({});
await mtaBuildCommand.mtaBuildCommand(undefined, swa);
});
it("mtaBuildCommand - build MTA with no mbt installed", async () => {
utilsMock
.expects("execCommand")
.once()
.withExactArgs(MBT_CMD, ["-v"], { cwd: homeDir })
.returns({ exitCode: "ENOENT" });
swaMock.expects("track").never();
tasksMock.expects("executeTask").never();
windowMock.expects("showErrorMessage").withExactArgs(messages.INSTALL_MBT);
await mtaBuildCommand.mtaBuildCommand(selected as Uri, swa);
});
});