forked from Azure/static-web-apps-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.spec.ts
More file actions
264 lines (221 loc) · 8.58 KB
/
deploy.spec.ts
File metadata and controls
264 lines (221 loc) · 8.58 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
import "../../../../tests/_mocks/fs.js";
import child_process, { spawn } from "node:child_process";
import { EventEmitter } from "node:events";
import path from "node:path";
import { logger } from "../../../core/utils/logger.js";
import { vol } from "memfs";
import * as accountModule from "../../../core/account.js";
import * as deployClientModule from "../../../core/deploy-client.js";
import { deploy } from "./deploy.js";
import * as loginModule from "../login/login.js";
import { loadPackageJson } from "../../../core/utils/json.js";
const pkg = loadPackageJson();
// Prevent transitive jsonwebtoken/buffer-equal-constant-time loading error
// by fully mocking modules that pull in Azure SDK → jsonwebtoken chain
vi.mock("../../../core/account.js", () => ({
chooseOrCreateProjectDetails: vi.fn(() => Promise.resolve({ resourceGroup: "mock-rg", staticSiteName: "mock-site" })),
getStaticSiteDeployment: vi.fn(() => Promise.resolve({})),
authenticateWithAzureIdentity: vi.fn(),
listSubscriptions: vi.fn(),
listTenants: vi.fn(),
}));
vi.mock("../login/login.js", () => ({
login: vi.fn(() =>
Promise.resolve({
credentialChain: {},
subscriptionId: "mock-subscription-id",
}),
),
loginCommand: vi.fn(),
}));
vi.mock("../../../core/utils/logger", () => {
return {
logger: {
error: vi.fn(),
log: vi.fn(),
warn: vi.fn(),
silly: vi.fn(),
},
logGitHubIssueMessageAndExit: vi.fn(),
};
});
vi.mock("node:child_process", async (importOriginal) => {
const actual: typeof child_process = await importOriginal();
return {
...actual,
default: { ...actual, spawn: vi.fn() },
spawn: vi.fn(),
};
});
vi.spyOn(deployClientModule, "getDeployClientPath").mockImplementation(() => {
return Promise.resolve({
binary: "mock-binary",
buildId: "0.0.0",
});
});
vi.spyOn(deployClientModule, "cleanUp").mockImplementation(() => {});
describe("deploy", () => {
const OLD_ENV = process.env;
beforeEach(() => {
vol.reset();
vi.resetModules();
process.env = {};
});
afterAll(() => {
process.env = OLD_ENV;
});
it("should be a function", () => {
expect(typeof deploy).toBe("function");
});
it("should return a Promise", () => {
expect(deploy({ outputLocation: "./dist" })).toBeInstanceOf(Promise);
});
it("should print warning when using dry run mode", async () => {
await deploy({
outputLocation: "./dist",
dryRun: true,
});
expect(logger.warn).toHaveBeenNthCalledWith(1, "***********************************************************************");
expect(logger.warn).toHaveBeenNthCalledWith(2, "* WARNING: Running in dry run mode. This project will not be deployed *");
expect(logger.warn).toHaveBeenNthCalledWith(3, "***********************************************************************");
});
it.skip("should print error and exit when --api-location does not exist", async () => {
await deploy({
outputLocation: "./dist",
apiLocation: "/does/not/exist",
});
expect(logger.error).toHaveBeenNthCalledWith(1, `The provided API folder /does/not/exist does not exist. Abort.`, true);
});
it.skip("should print an error and exit, if --deployment-token is not provided and login failed", async () => {
vi.spyOn(loginModule, "login").mockImplementation(() => Promise.reject("mock-error"));
await deploy({
outputLocation: "./dist",
apiLocation: "./api",
dryRun: false,
});
expect(loginModule.login).toHaveBeenCalled();
// getStaticSiteDeployment should not be called because login failed
expect(accountModule.getStaticSiteDeployment).not.toHaveBeenCalled();
expect(logger.error).toHaveBeenNthCalledWith(1, "A deployment token is required to deploy to Azure Static Web Apps");
expect(logger.error).toHaveBeenNthCalledWith(
2,
"Provide a deployment token using the --deployment-token option or SWA_CLI_DEPLOYMENT_TOKEN environment variable",
true,
);
expect(deployClientModule.getDeployClientPath).not.toBeCalled();
expect(child_process.spawn).not.toBeCalled();
});
it.skip("should accept a deploymentToken provided via --deployment-token", async () => {
await deploy({
outputLocation: "./dist",
apiLocation: "./api",
deploymentToken: "123",
dryRun: false,
});
expect(await deployClientModule.getDeployClientPath()).toEqual({
binary: "mock-binary",
version: "0.0.0",
});
expect(child_process.spawn).toBeCalledWith("mock-binary", [], {
env: {
DEPLOYMENT_ACTION: "upload",
DEPLOYMENT_PROVIDER: "SwaCli",
REPOSITORY_BASE: "./",
SKIP_APP_BUILD: "true",
SKIP_API_BUILD: "true",
DEPLOYMENT_TOKEN: "123",
APP_LOCATION: "./",
OUTPUT_LOCATION: "./dist",
API_LOCATION: "./api",
VERBOSE: "false",
SWA_CLI_DEBUG: undefined,
SWA_CLI_DEPLOY_DRY_RUN: "false",
SWA_RUNTIME_CONFIG_LOCATION: undefined,
SWA_CLI_VERSION: `${pkg.version}`,
SWA_RUNTIME_WORKFLOW_LOCATION: undefined,
SWA_CLI_DEPLOY_BINARY: "mock-binary@0.0.0",
},
});
});
it.skip("should accept a deploymentToken provided via the environment variable SWA_CLI_DEPLOYMENT_TOKEN", async () => {
process.env.SWA_CLI_DEPLOYMENT_TOKEN = "123";
await deploy({
outputLocation: "./dist",
apiLocation: "./api",
dryRun: false,
});
expect(await deployClientModule.getDeployClientPath()).toEqual({
binary: "mock-binary",
version: "0.0.0",
});
expect(child_process.spawn).toBeCalledWith("mock-binary", [], {
env: {
DEPLOYMENT_ACTION: "upload",
DEPLOYMENT_PROVIDER: "SwaCli",
REPOSITORY_BASE: "./",
SKIP_APP_BUILD: "true",
SKIP_API_BUILD: "true",
DEPLOYMENT_TOKEN: "123",
APP_LOCATION: "./",
OUTPUT_LOCATION: "./dist",
API_LOCATION: "./api",
VERBOSE: "false",
SWA_CLI_DEPLOYMENT_TOKEN: "123",
SWA_CLI_DEPLOY_BINARY: "mock-binary@0.0.0",
SWA_CLI_DEBUG: undefined,
SWA_CLI_DEPLOY_DRY_RUN: "false",
SWA_RUNTIME_CONFIG_LOCATION: undefined,
SWA_CLI_VERSION: `${pkg.version}`,
SWA_RUNTIME_WORKFLOW_LOCATION: undefined,
},
});
});
describe("StaticSitesClient process handling", () => {
let mockChild: EventEmitter & { stdout: EventEmitter; stderr: EventEmitter };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let exitSpy: any;
beforeEach(() => {
// Create mock child process with stdout/stderr EventEmitters
const stdout = new EventEmitter();
const stderr = new EventEmitter();
mockChild = Object.assign(new EventEmitter(), { stdout, stderr });
// Set up spawn mock to return the mock child process
vi.mocked(spawn).mockReturnValue(mockChild as any);
vi.spyOn(deployClientModule, "getDeployClientPath").mockResolvedValue({
binary: "mock-binary",
buildId: "0.0.0",
});
vi.spyOn(deployClientModule, "cleanUp").mockImplementation(() => {});
// Mock process.exit to prevent test runner from exiting
exitSpy = vi.spyOn(process, "exit").mockImplementation((() => undefined) as unknown as () => never);
// Provide deployment token via env to skip login flow
process.env.SWA_CLI_DEPLOYMENT_TOKEN = "test-token";
// Create required filesystem structure in memfs
const cwd = process.cwd();
vol.fromJSON({
[path.join("/test-output", "index.html")]: "hello",
[path.join(cwd, "placeholder")]: "",
});
});
it("should capture stderr and pass to logger.error", async () => {
await deploy({ outputLocation: "/test-output", dryRun: false });
mockChild.stderr.emit("data", Buffer.from("some error from binary"));
expect(logger.error).toHaveBeenCalledWith("some error from binary");
});
it("should fail spinner and log error on non-zero exit code", async () => {
await deploy({ outputLocation: "/test-output", dryRun: false });
mockChild.emit("close", 1);
expect(logger.error).toHaveBeenCalledWith("The deployment binary exited with code 1.");
});
it("should call process.exit(1) on non-zero exit code", async () => {
await deploy({ outputLocation: "/test-output", dryRun: false });
mockChild.emit("close", 127);
expect(exitSpy).toHaveBeenCalledWith(1);
});
it("should succeed without calling process.exit on exit code 0", async () => {
await deploy({ outputLocation: "/test-output", dryRun: false });
mockChild.emit("close", 0);
expect(exitSpy).not.toHaveBeenCalled();
});
});
});