-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathfunctionsEmulatorShared.spec.ts
More file actions
342 lines (320 loc) · 10.3 KB
/
Copy pathfunctionsEmulatorShared.spec.ts
File metadata and controls
342 lines (320 loc) · 10.3 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
335
336
337
338
339
340
341
342
import { expect } from "chai";
import * as path from "path";
import { BackendInfo, EmulatableBackend } from "./functionsEmulator";
import * as functionsEmulatorShared from "./functionsEmulatorShared";
import {
Extension,
ExtensionSpec,
ExtensionVersion,
RegistryLaunchStage,
Visibility,
} from "../extensions/types";
const baseDef = {
platform: "gcfv1" as const,
id: "trigger-id",
region: "us-central1",
entryPoint: "fn",
name: "name",
};
describe("FunctionsEmulatorShared", () => {
describe(`${functionsEmulatorShared.getFunctionService.name}`, () => {
it("should get service from event trigger definition", () => {
const def = {
...baseDef,
eventTrigger: {
resource: "projects/my-project/topics/my-topic",
eventType: "google.cloud.pubsub.topic.v1.messagePublished",
service: "pubsub.googleapis.com",
},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql("pubsub.googleapis.com");
});
it("should infer https service from http trigger", () => {
const def = {
...baseDef,
httpsTrigger: {},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql("https");
});
it("should infer pubsub service based on eventType", () => {
const def = {
...baseDef,
eventTrigger: {
resource: "projects/my-project/topics/my-topic",
eventType: "google.cloud.pubsub.topic.v1.messagePublished",
},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql("pubsub.googleapis.com");
});
it("should infer firestore service based on eventType", () => {
const def = {
...baseDef,
eventTrigger: {
resource: "projects/my-project/databases/(default)/documents/my-collection/{docId}",
eventType: "providers/cloud.firestore/eventTypes/document.write",
},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql("firestore.googleapis.com");
});
it("should infer database service based on eventType", () => {
const def = {
...baseDef,
eventTrigger: {
resource: "projects/_/instances/my-project/refs/messages/{pushId}",
eventType: "providers/google.firebase.database/eventTypes/ref.write",
},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql("firebaseio.com");
});
it("should infer storage service based on eventType", () => {
const def = {
...baseDef,
eventTrigger: {
resource: "projects/_/buckets/mybucket",
eventType: "google.storage.object.finalize",
},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql("storage.googleapis.com");
});
it("should infer auth service based on eventType", () => {
const def = {
...baseDef,
eventTrigger: {
resource: "projects/my-project",
eventType: "providers/firebase.auth/eventTypes/user.create",
},
};
expect(functionsEmulatorShared.getFunctionService(def)).to.be.eql(
"firebaseauth.googleapis.com",
);
});
});
describe(`${functionsEmulatorShared.getSecretLocalPath.name}`, () => {
const testProjectDir = "project/dir";
const tests: {
desc: string;
in: EmulatableBackend;
expected: string;
}[] = [
{
desc: "should return the correct location for an Extension backend",
in: {
functionsDir: "extensions/functions",
env: {},
secretEnv: [],
extensionInstanceId: "my-extension-instance",
codebase: "",
},
expected: "project/dir/extensions/my-extension-instance.secret.local",
},
{
desc: "should return the correct location for a CF3 backend",
in: {
functionsDir: "test/cf3",
env: {},
secretEnv: [],
codebase: "",
},
expected: "test/cf3/.secret.local",
},
];
for (const t of tests) {
it(t.desc, () => {
expect(functionsEmulatorShared.getSecretLocalPath(t.in, testProjectDir)).to.equal(
t.expected,
);
});
}
});
describe(`${functionsEmulatorShared.toBackendInfo.name}`, () => {
const testCF3Triggers: functionsEmulatorShared.ParsedTriggerDefinition[] = [
{
entryPoint: "cf3",
platform: "gcfv1",
name: "cf3-trigger",
codebase: "",
},
];
const testExtTriggers: functionsEmulatorShared.ParsedTriggerDefinition[] = [
{
entryPoint: "ext",
platform: "gcfv1",
name: "ext-trigger",
},
];
const testSpec: ExtensionSpec = {
name: "my-extension",
version: "0.1.0",
resources: [],
sourceUrl: "test.com",
params: [],
systemParams: [],
postinstallContent: "Should subsitute ${param:KEY}",
};
const testSubbedSpec: ExtensionSpec = {
name: "my-extension",
version: "0.1.0",
resources: [],
sourceUrl: "test.com",
params: [],
systemParams: [],
postinstallContent: "Should subsitute value",
};
const testExtension: Extension = {
name: "my-extension",
ref: "pubby/my-extensions",
state: "PUBLISHED",
createTime: "",
visibility: Visibility.PUBLIC,
registryLaunchStage: RegistryLaunchStage.BETA,
};
const testExtensionVersion = (spec: ExtensionSpec): ExtensionVersion => {
return {
name: "my-extension",
ref: "pubby/my-extensions@0.1.0",
state: "PUBLISHED",
spec,
hash: "abc123",
sourceDownloadUri: "test.com",
};
};
const tests: {
desc: string;
in: EmulatableBackend;
expected: BackendInfo;
}[] = [
{
desc: "should transform a published Extension backend",
in: {
functionsDir: "test",
env: {
KEY: "value",
},
secretEnv: [],
predefinedTriggers: testExtTriggers,
extension: testExtension,
extensionVersion: testExtensionVersion(testSpec),
extensionInstanceId: "my-instance",
codebase: "",
},
expected: {
directory: "test",
env: {
KEY: "value",
},
functionTriggers: testExtTriggers,
extension: testExtension,
extensionVersion: testExtensionVersion(testSubbedSpec),
extensionInstanceId: "my-instance",
},
},
{
desc: "should transform a local Extension backend",
in: {
functionsDir: "test",
env: {
KEY: "value",
},
secretEnv: [],
predefinedTriggers: testExtTriggers,
extensionSpec: testSpec,
extensionInstanceId: "my-local-instance",
codebase: "",
},
expected: {
directory: "test",
env: {
KEY: "value",
},
functionTriggers: testExtTriggers,
extensionSpec: testSubbedSpec,
extensionInstanceId: "my-local-instance",
},
},
{
desc: "should transform a CF3 backend",
in: {
functionsDir: "test",
env: {
KEY: "value",
},
secretEnv: [],
codebase: "",
},
expected: {
directory: "test",
env: {
KEY: "value",
},
functionTriggers: testCF3Triggers,
},
},
{
desc: "should add secretEnvVar into env",
in: {
functionsDir: "test",
env: {
KEY: "value",
},
secretEnv: [
{
key: "secret",
secret: "asecret",
projectId: "test",
},
],
codebase: "",
},
expected: {
directory: "test",
env: {
KEY: "value",
secret: "projects/test/secrets/asecret/versions/latest",
},
functionTriggers: testCF3Triggers,
},
},
];
for (const tc of tests) {
it(tc.desc, () => {
expect(functionsEmulatorShared.toBackendInfo(tc.in, testCF3Triggers)).to.deep.equal(
tc.expected,
);
});
}
});
describe(`${functionsEmulatorShared.shouldIgnoreWatchPath.name}`, () => {
const functionsDir = path.join("/", "home", "user", ".worktrees", "project", "functions");
it("ignores hidden files inside the functions directory", () => {
const target = path.join(functionsDir, ".git", "HEAD");
expect(functionsEmulatorShared.shouldIgnoreWatchPath(functionsDir, target)).to.be.true;
});
it("ignores a hidden segment nested within the functions directory", () => {
const target = path.join(functionsDir, "src", ".cache", "module.js");
expect(functionsEmulatorShared.shouldIgnoreWatchPath(functionsDir, target)).to.be.true;
});
it("does not ignore a regular source file even when the functions directory is nested under a dot-prefixed ancestor", () => {
const target = path.join(functionsDir, "src", "index.ts");
expect(functionsEmulatorShared.shouldIgnoreWatchPath(functionsDir, target)).to.be.false;
});
it("does not ignore a nested source file without any hidden segment", () => {
const target = path.join(functionsDir, "lib", "handlers", "index.js");
expect(functionsEmulatorShared.shouldIgnoreWatchPath(functionsDir, target)).to.be.false;
});
it("does not ignore the functions directory itself", () => {
expect(functionsEmulatorShared.shouldIgnoreWatchPath(functionsDir, functionsDir)).to.be.false;
});
describe("when the functions directory has no dot-prefixed ancestor", () => {
const plainFunctionsDir = path.join("/", "home", "user", "project", "functions");
it("still ignores hidden files inside the functions directory", () => {
const target = path.join(plainFunctionsDir, ".git", "HEAD");
expect(functionsEmulatorShared.shouldIgnoreWatchPath(plainFunctionsDir, target)).to.be.true;
});
it("does not ignore a regular source file", () => {
const target = path.join(plainFunctionsDir, "src", "index.ts");
expect(functionsEmulatorShared.shouldIgnoreWatchPath(plainFunctionsDir, target)).to.be
.false;
});
});
});
});