-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathdeploy.test.ts
More file actions
308 lines (258 loc) · 9.55 KB
/
Copy pathdeploy.test.ts
File metadata and controls
308 lines (258 loc) · 9.55 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import {
ChannelSuccessResult,
ChannelDeployConfig,
deployPreview,
deployProductionSite,
isAlreadyActiveVersionError,
ProductionDeployConfig,
ProductionSuccessResult,
} from "../src/deploy";
import * as exec from "@actions/exec";
import {
alreadyActiveVersionError,
channelError,
channelListSuccess,
channelMultiSiteSuccess,
channelSingleSiteSuccess,
liveDeployMultiSiteSuccess,
liveDeploySingleSiteSuccess,
} from "./samples/cliOutputs";
const baseChannelDeployConfig: ChannelDeployConfig = {
projectId: "my-project",
channelId: "my-channel",
expires: undefined,
};
const baseLiveDeployConfig: ProductionDeployConfig = {
projectId: "my-project",
};
const forceProductionDeployConfig: ProductionDeployConfig = {
projectId: "my-project",
force: true,
};
const forcePreviewDeployConfig: ChannelDeployConfig = {
projectId: "my-project",
channelId: "my-channel",
expires: undefined,
force: true,
};
async function fakeExecFail(
mainCommand: string,
args: string[],
options: exec.ExecOptions
) {
options?.listeners?.stdout(Buffer.from(JSON.stringify(channelError), "utf8"));
throw new Error("I am an error");
}
async function fakeExec(
mainCommand: string,
args: string[],
options: exec.ExecOptions
) {
if (args.includes("--debug")) {
return options?.listeners?.stdout(
Buffer.from("I am a very long debug output", "utf8")
);
}
const isChannelDeploy = args[0] === "hosting:channel:deploy";
let successOutput;
if (args.includes("--target")) {
successOutput = isChannelDeploy
? channelMultiSiteSuccess
: liveDeployMultiSiteSuccess;
} else {
successOutput = isChannelDeploy
? channelSingleSiteSuccess
: liveDeploySingleSiteSuccess;
}
options?.listeners?.stdout(
Buffer.from(JSON.stringify(successOutput), "utf8")
);
}
async function fakeExecAlreadyActive(
mainCommand: string,
args: string[],
options: exec.ExecOptions
) {
if (args[0] === "hosting:channel:list") {
options?.listeners?.stdout(
Buffer.from(JSON.stringify(channelListSuccess), "utf8")
);
return;
}
options?.listeners?.stdout(
Buffer.from(JSON.stringify(alreadyActiveVersionError), "utf8")
);
throw new Error("The process failed with exit code 1");
}
describe("deploy", () => {
it("retries with the --debug flag on error", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec).mockImplementationOnce(fakeExecFail);
const deployOutput: ChannelSuccessResult = (await deployPreview(
"my-file",
baseChannelDeployConfig
)) as ChannelSuccessResult;
expect(exec.exec).toBeCalledTimes(2);
expect(deployOutput).toEqual(channelError);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const firstCallDeployFlags = args[0][1];
const secondCallDeployFlags = args[1][1];
expect(firstCallDeployFlags).toContain("--json");
expect(secondCallDeployFlags).not.toContain("--json");
expect(firstCallDeployFlags).not.toContain("--debug");
expect(secondCallDeployFlags).toContain("--debug");
});
describe("deploy to preview channel", () => {
it("calls exec and interprets the output", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec);
const deployOutput: ChannelSuccessResult = (await deployPreview(
"my-file",
baseChannelDeployConfig
)) as ChannelSuccessResult;
expect(exec.exec).toBeCalled();
expect(deployOutput).toEqual(channelSingleSiteSuccess);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const deployFlags = args[0][1];
expect(deployFlags).toContain("hosting:channel:deploy");
});
it("specifies a target when one is provided", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec);
const config: ChannelDeployConfig = {
...baseChannelDeployConfig,
target: "my-second-site",
};
await deployPreview("my-file", config);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const deployFlags = args[0][1];
expect(deployFlags).toContain("--only");
expect(deployFlags).toContain("my-second-site");
});
});
describe("deploy to preview channel with force flag", () => {
it("calls exec and interprets the output, including the --force flag when force is true", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec);
const deployOutput: ChannelSuccessResult = (await deployPreview(
"my-file",
forcePreviewDeployConfig
)) as ChannelSuccessResult;
expect(exec.exec).toBeCalled();
expect(deployOutput).toEqual(channelSingleSiteSuccess);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const deployFlags = args[0][1];
expect(deployFlags).toContain("hosting:channel:deploy");
expect(deployFlags).toContain("--force");
});
it("specifies a target when one is provided", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec);
const config: ChannelDeployConfig = {
...forcePreviewDeployConfig,
target: "my-second-site",
};
await deployPreview("my-file", config);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const deployFlags = args[0][1];
expect(deployFlags).toContain("--only");
expect(deployFlags).toContain("my-second-site");
expect(deployFlags).toContain("--force");
});
});
describe("deploy to live channel", () => {
it("calls exec and interprets the output", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec);
const deployOutput: ProductionSuccessResult = (await deployProductionSite(
"my-file",
baseLiveDeployConfig
)) as ProductionSuccessResult;
expect(exec.exec).toBeCalled();
expect(deployOutput).toEqual(liveDeploySingleSiteSuccess);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const deployFlags = args[0][1];
expect(deployFlags).toContain("deploy");
expect(deployFlags).toContain("--only");
expect(deployFlags).toContain("hosting");
});
});
describe("deploy to live channel with force flag", () => {
it("includes --force flag when force is true for deploy", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExec);
const forceDeployOutput: ProductionSuccessResult =
(await deployProductionSite(
"my-file",
forceProductionDeployConfig
)) as ProductionSuccessResult;
expect(exec.exec).toBeCalled();
expect(forceDeployOutput).toEqual(liveDeploySingleSiteSuccess);
// Check the arguments that exec was called with
// @ts-ignore Jest adds a magic "mock" property
const args = exec.exec.mock.calls;
const deployFlags = args[0][1];
expect(deployFlags).toContain("deploy");
expect(deployFlags).toContain("--only");
expect(deployFlags).toContain("hosting");
expect(deployFlags).toContain("--force");
});
});
describe("already-active version handling", () => {
it("isAlreadyActiveVersionError matches the FAILED_PRECONDITION message", () => {
expect(isAlreadyActiveVersionError(alreadyActiveVersionError.error)).toBe(
true
);
});
it("isAlreadyActiveVersionError ignores unrelated errors", () => {
expect(isAlreadyActiveVersionError(channelError.error)).toBe(false);
expect(isAlreadyActiveVersionError(undefined)).toBe(false);
});
it("treats an already-active production deploy as a successful no-op", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExecAlreadyActive);
const result = (await deployProductionSite(
"my-file",
baseLiveDeployConfig
)) as ProductionSuccessResult;
expect(result.status).toBe("success");
// It must not retry with the --debug flag for this known error.
expect(exec.exec).toBeCalledTimes(1);
// @ts-ignore Jest adds a magic "mock" property
expect(exec.exec.mock.calls[0][1]).not.toContain("--debug");
});
it("treats an already-active preview deploy as success and reads the channel back", async () => {
// @ts-ignore read-only property
exec.exec = jest.fn(fakeExecAlreadyActive);
const result = (await deployPreview(
"my-file",
baseChannelDeployConfig
)) as ChannelSuccessResult;
expect(result.status).toBe("success");
const siteResult = Object.values(result.result)[0];
expect(siteResult.url).toBe(
"https://my-project--my-channel-abc123.web.app"
);
expect(siteResult.expireTime).toBe("2020-10-27T21:32:57.233344586Z");
// First the channel deploy (which fails), then the channel list lookup —
// and no --debug retry in between.
expect(exec.exec).toBeCalledTimes(2);
// @ts-ignore Jest adds a magic "mock" property
const calls = exec.exec.mock.calls;
expect(calls[0][1]).not.toContain("--debug");
expect(calls[1][1]).toContain("hosting:channel:list");
});
});
});