-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcrashlytics-sourcemap-upload.spec.ts
More file actions
334 lines (297 loc) · 11.7 KB
/
Copy pathcrashlytics-sourcemap-upload.spec.ts
File metadata and controls
334 lines (297 loc) · 11.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import * as chai from "chai";
import * as sinon from "sinon";
import * as fs from "fs";
import { command } from "./crashlytics-sourcemap-upload";
import * as gcs from "../gcp/storage";
import { SourceMap } from "../crashlytics/sourcemap";
import * as projectUtils from "../projectUtils";
import * as getProjectNumber from "../getProjectNumber";
import { FirebaseError } from "../error";
import * as childProcess from "child_process";
import * as utils from "../utils";
import { Client } from "../apiv2";
const expect = chai.expect;
const PROJECT_ID = "test-project";
const PROJECT_NUMBER = "12345";
const BUCKET_NAME = "test-bucket";
const DIR_PATH = "src/test/fixtures/mapping-files";
const DIR_WITH_JS_PATH = "src/test/fixtures/mapping-files-with-js";
const FILE_PATH = "src/test/fixtures/mapping-files/mock_mapping.js.map";
describe("crashlytics:sourcemap:upload", () => {
let sandbox: sinon.SinonSandbox;
let gcsMock: sinon.SinonStubbedInstance<typeof gcs>;
let projectUtilsMock: sinon.SinonStubbedInstance<typeof projectUtils>;
let getProjectNumberMock: sinon.SinonStubbedInstance<typeof getProjectNumber>;
let execSyncStub: sinon.SinonStub;
let commandExistsSyncStub: sinon.SinonStub;
let clientPatchStub: sinon.SinonStub;
let logLabeledWarningStub: sinon.SinonStub;
let logLabeledBulletStub: sinon.SinonStub;
beforeEach(() => {
(command as unknown as { befores: unknown[] }).befores = []; // Bypass pre-action hooks for unit testing action
sandbox = sinon.createSandbox();
gcsMock = sandbox.stub(gcs);
projectUtilsMock = sandbox.stub(projectUtils);
getProjectNumberMock = sandbox.stub(getProjectNumber);
projectUtilsMock.needProjectId.returns(PROJECT_ID);
getProjectNumberMock.getProjectNumber.resolves(PROJECT_NUMBER);
gcsMock.upsertBucket.resolves(BUCKET_NAME);
gcsMock.uploadObject.resolves({
bucket: BUCKET_NAME,
object: "test-object",
generation: "1",
});
execSyncStub = sandbox.stub(childProcess, "execSync");
commandExistsSyncStub = sandbox.stub(utils, "commandExistsSync");
logLabeledWarningStub = sandbox.stub(utils, "logLabeledWarning");
logLabeledBulletStub = sandbox.stub(utils, "logLabeledBullet");
// Default to git working
commandExistsSyncStub.withArgs("git").returns(true);
execSyncStub.withArgs("git rev-parse HEAD").returns(Buffer.from("a".repeat(40)));
clientPatchStub = sandbox.stub(Client.prototype, "patch").resolves({
status: 200,
response: {} as unknown as Response,
body: {},
});
});
afterEach(() => {
sandbox.restore();
});
it("should throw an error if no app ID is provided", async () => {
await expect(command.runner()("filename", {})).to.be.rejectedWith(
FirebaseError,
"set --app <appId> to a valid Firebase application id",
);
});
it("should create the default cloud storage bucket", async () => {
await command.runner()(DIR_PATH, {
app: "test-app",
});
expect(gcsMock.upsertBucket).to.be.calledOnce;
const args = gcsMock.upsertBucket.firstCall.args;
expect(args[0].req.baseName).to.equal("firebasecrashlytics-sourcemaps-12345-us-central1");
expect(args[0].req.location).to.equal("US-CENTRAL1");
});
it("should create a custom cloud storage bucket", async () => {
const options = {
app: "test-app",
bucketLocation: "a-different-LoCaTiOn",
};
await command.runner()(DIR_PATH, options);
expect(gcsMock.upsertBucket).to.be.calledOnce;
const args = gcsMock.upsertBucket.firstCall.args;
expect(args[0].req.baseName).to.equal(
"firebasecrashlytics-sourcemaps-12345-a-different-location",
);
expect(args[0].req.location).to.equal("A-DIFFERENT-LOCATION");
});
it("should throw an error if the mapping file path is invalid", async () => {
await expect(
command.runner()("invalid/path", {
app: "test-app",
}),
).to.be.rejectedWith(FirebaseError, "provide a valid directory to mapping file(s)");
});
it("should throw an error if the mapping file path is not a directory", async () => {
await expect(
command.runner()(FILE_PATH, {
app: "test-app",
}),
).to.be.rejectedWith(FirebaseError, "provide a valid directory to mapping file(s)");
});
it("should find and upload mapping files in a directory", async () => {
await command.runner()(DIR_PATH, { app: "test-app" });
expect(gcsMock.uploadObject).to.be.calledTwice;
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
expect(uploadedFiles[0]).to.match(
/test-app-.*-src-test-fixtures-mapping-files-mock_mapping\.js\.map\.zip/,
);
expect(uploadedFiles[1]).to.match(
/test-app-.*-src-test-fixtures-mapping-files-subdir-subdir_mock_mapping\.js\.map\.zip/,
);
});
it("should find and upload mapping files in the current directory if no path is provided", async () => {
const originalCwd = process.cwd();
try {
process.chdir("src/test");
await command.runner()(undefined, { app: "test-app" });
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
expect(uploadedFiles[0]).to.match(
/test-app-.*-fixtures-mapping-files-mock_mapping\.js\.map\.zip/,
);
expect(uploadedFiles[1]).to.match(
/test-app-.*-fixtures-mapping-files-subdir-subdir_mock_mapping\.js\.map\.zip/,
);
expect(uploadedFiles[2]).to.match(/test-app-.*-fixtures-mapping-files-with-js-main\.js\.zip/);
expect(uploadedFiles[3]).to.match(
/test-app-.*-fixtures-mapping-files-with-js-other\.js\.map\.zip/,
);
} finally {
process.chdir(originalCwd);
}
});
it("should find obfuscated mapping files linked by sourceMappingURL in a directory", async () => {
await command.runner()(DIR_WITH_JS_PATH, {
app: "test-app",
});
expect(gcsMock.uploadObject).to.be.calledTwice;
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
// The zip name is based on the obfuscated path, so the first one is the "main.js.map" pretending to be the name
expect(uploadedFiles[0]).to.match(
/test-app-.*-src-test-fixtures-mapping-files-with-js-main\.js\.zip/,
);
expect(uploadedFiles[1]).to.match(
/test-app-.*-src-test-fixtures-mapping-files-with-js-other\.js\.map\.zip/,
);
expect(clientPatchStub).to.be.calledTwice;
const apiPayloads = clientPatchStub
.getCalls()
.map((call) => (call.args[1] as SourceMap).obfuscatedFilePath)
.sort();
expect(apiPayloads[0]).to.equal("/src/test/fixtures/mapping-files-with-js/main.js");
expect(apiPayloads[1]).to.equal("/src/test/fixtures/mapping-files-with-js/other.js.map");
});
it("should use the provided app version", async () => {
await command.runner()(DIR_PATH, {
app: "test-app",
appVersion: "1.0.0",
});
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
expect(uploadedFiles[0]).to.eq(
"test-app-1.0.0-src-test-fixtures-mapping-files-mock_mapping.js.map.zip",
);
});
it("should fall back to the git commit for app version", async () => {
await command.runner()(DIR_PATH, {
app: "test-app",
});
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
expect(uploadedFiles[0]).to.match(
/test-app-a{40}-src-test-fixtures-mapping-files-mock_mapping.js.map.zip/,
);
});
it("should fall back to the package version for app version", async () => {
commandExistsSyncStub.withArgs("git").returns(true);
execSyncStub.withArgs("git rev-parse HEAD").throws(new Error("git failed"));
commandExistsSyncStub.withArgs("npm").returns(true);
execSyncStub.withArgs("npm pkg get version").returns(Buffer.from("1.2.3"));
await command.runner()(DIR_PATH, {
app: "test-app",
});
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
expect(uploadedFiles[0]).to.eq(
"test-app-1.2.3-src-test-fixtures-mapping-files-mock_mapping.js.map.zip",
);
});
it("should fall back to the 'unset' for app version", async () => {
commandExistsSyncStub.withArgs("git").returns(false);
commandExistsSyncStub.withArgs("npm").returns(false);
await command.runner()(DIR_PATH, {
app: "test-app",
});
const uploadedFiles = gcsMock.uploadObject
.getCalls()
.map((call) => call.args[0].file)
.sort();
expect(uploadedFiles[0]).to.eq(
"test-app-unset-src-test-fixtures-mapping-files-mock_mapping.js.map.zip",
);
});
it("should register the source map after upload", async () => {
await command.runner()(DIR_PATH, {
app: "test-app",
});
expect(clientPatchStub).to.be.calledTwice;
const payloads = clientPatchStub
.getCalls()
.map((call) => call.args[1] as SourceMap)
.sort((a, b) => a.obfuscatedFilePath.localeCompare(b.obfuscatedFilePath));
expect(payloads[0].name).to.match(
/projects\/test-project\/locations\/global\/mappingFiles\/2906062618/,
);
expect(payloads[0]).to.deep.equal({
name: "projects/test-project/locations/global/mappingFiles/2906062618",
appId: "test-app",
version: "a".repeat(40),
obfuscatedFilePath: "/src/test/fixtures/mapping-files/mock_mapping.js.map",
fileUri: `gs://${BUCKET_NAME}/test-object`,
});
expect(
(clientPatchStub.firstCall.args[2] as { queryParams: Record<string, string> }).queryParams,
).to.deep.equal({ allowMissing: "true" });
});
it("should warn if registration fails", async () => {
clientPatchStub.rejects(new Error("Registration failed"));
await command.runner()(DIR_PATH, {
app: "test-app",
retryDelay: 10,
});
expect(clientPatchStub.callCount).to.equal(4);
expect(logLabeledWarningStub).to.be.calledTwice;
expect(logLabeledWarningStub).to.be.calledWith(
"crashlytics",
sinon.match(/Failed to upload mapping file/),
);
});
it("should log failed files", async () => {
clientPatchStub.rejects(new Error("Registration failed"));
await command.runner()(DIR_PATH, { app: "test-app", retryDelay: 10 });
// Should verify that logLabeledBullet is called with the specific failed files
expect(logLabeledBulletStub).to.be.calledWith(
"crashlytics",
sinon.match(/Could not upload the following files:/),
);
expect(logLabeledBulletStub).to.be.calledWith(
"crashlytics",
sinon.match(/subdir_mock_mapping\.js\.map/),
);
expect(logLabeledBulletStub).to.be.calledWith(
"crashlytics",
sinon.match(/mock_mapping\.js\.map/),
);
});
it("should clean up temporary zip files from disk after success path upload", async () => {
const rmSyncSpy = sandbox.spy(fs, "rmSync");
await command.runner()(DIR_PATH, {
app: "test-app",
});
expect(rmSyncSpy.callCount).to.equal(2);
for (const call of rmSyncSpy.getCalls()) {
const filePath = call.args[0] as string;
expect(filePath).to.include(".zip");
expect(fs.existsSync(filePath)).to.be.false;
}
});
it("should clean up temporary zip files from disk even if upload fails and retries", async () => {
const rmSyncSpy = sandbox.spy(fs, "rmSync");
clientPatchStub.rejects(new Error("Registration failed"));
await command.runner()(DIR_PATH, {
app: "test-app",
retryDelay: 10,
});
expect(rmSyncSpy.callCount).to.equal(4);
for (const call of rmSyncSpy.getCalls()) {
const filePath = call.args[0] as string;
expect(filePath).to.include(".zip");
expect(fs.existsSync(filePath)).to.be.false;
}
});
});