-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.spec.ts
More file actions
139 lines (122 loc) · 4.71 KB
/
index.spec.ts
File metadata and controls
139 lines (122 loc) · 4.71 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
import { expect } from "chai";
import * as sinon from "sinon";
import * as path from "path";
import * as node from ".";
import * as versioning from "./versioning";
import * as utils from "../../../../utils";
import { FirebaseError } from "../../../../error";
import { Runtime } from "../supported";
import * as discovery from "../discovery";
const PROJECT_ID = "test-project";
const PROJECT_DIR = "/some/path";
const SOURCE_DIR = "/some/path/fns";
describe("NodeDelegate", () => {
describe("getNodeBinary", () => {
let warnSpy: sinon.SinonSpy;
let successSpy: sinon.SinonSpy;
let hostVersionMock: sinon.SinonStub;
let localVersionMock: sinon.SinonStub;
beforeEach(() => {
warnSpy = sinon.spy(utils, "logLabeledWarning");
successSpy = sinon.spy(utils, "logLabeledSuccess");
hostVersionMock = sinon.stub(process, "versions");
localVersionMock = sinon.stub(versioning, "findModuleVersion");
});
afterEach(() => {
warnSpy.restore();
successSpy.restore();
hostVersionMock.restore();
localVersionMock.restore();
});
it("prefers locally cached node version if matched with requested version", () => {
localVersionMock.returns("12.0.0");
hostVersionMock.value({ node: "14.5.0" });
const requestedRuntime = "nodejs12";
const delegate = new node.Delegate(PROJECT_ID, PROJECT_DIR, SOURCE_DIR, requestedRuntime);
expect(delegate.getNodeBinary()).to.equal(path.join(SOURCE_DIR, "node_modules", "node"));
expect(successSpy).to.have.been.calledWith(
"functions",
sinon.match("node@12 from local cache."),
);
expect(warnSpy).to.not.have.been.called;
});
it("checks if requested and hosted runtime version matches", () => {
hostVersionMock.value({ node: "12.5.0" });
const requestedRuntime = "nodejs12";
const delegate = new node.Delegate(PROJECT_ID, PROJECT_DIR, SOURCE_DIR, requestedRuntime);
expect(delegate.getNodeBinary()).to.equal(process.execPath);
expect(successSpy).to.have.been.calledWith("functions", sinon.match("node@12 from host."));
expect(warnSpy).to.not.have.been.called;
});
it("warns users if hosted and requested runtime version differs", () => {
hostVersionMock.value({ node: "12.0.0" });
const requestedRuntime = "nodejs10";
const delegate = new node.Delegate(PROJECT_ID, PROJECT_DIR, SOURCE_DIR, requestedRuntime);
expect(delegate.getNodeBinary()).to.equal(process.execPath);
expect(successSpy).to.not.have.been.called;
expect(warnSpy).to.have.been.calledWith("functions", sinon.match("doesn't match"));
});
it("throws errors if requested runtime version is invalid", () => {
const invalidRuntime = "foobar";
const delegate = new node.Delegate(
PROJECT_ID,
PROJECT_DIR,
SOURCE_DIR,
invalidRuntime as Runtime,
);
expect(() => delegate.getNodeBinary()).to.throw(FirebaseError);
});
});
describe("discoverBuild", () => {
let getFunctionsSDKVersionMock: sinon.SinonStub;
let detectFromYamlMock: sinon.SinonStub;
beforeEach(() => {
getFunctionsSDKVersionMock = sinon.stub(versioning, "getFunctionsSDKVersion");
detectFromYamlMock = sinon.stub(discovery, "detectFromYaml");
});
afterEach(() => {
getFunctionsSDKVersionMock.restore();
detectFromYamlMock.restore();
});
it("throws error if SDK version is too old", async () => {
getFunctionsSDKVersionMock.returns("3.19.0");
const delegate = new node.Delegate(
PROJECT_ID,
PROJECT_DIR,
SOURCE_DIR,
"nodejs16" as Runtime,
);
try {
await delegate.discoverBuild({}, {});
throw new Error("Should have thrown");
} catch (err: any) {
expect(err).to.be.instanceOf(FirebaseError);
expect(err.message).to.include("Please update firebase-functions SDK");
}
});
it("proceeds if SDK version is valid", async () => {
getFunctionsSDKVersionMock.returns("4.0.0");
detectFromYamlMock.resolves({ endpoints: {} });
const delegate = new node.Delegate(
PROJECT_ID,
PROJECT_DIR,
SOURCE_DIR,
"nodejs16" as Runtime,
);
await delegate.discoverBuild({}, {});
expect(detectFromYamlMock).to.have.been.called;
});
it("proceeds if SDK version is invalid", async () => {
getFunctionsSDKVersionMock.returns("not-a-version");
detectFromYamlMock.resolves({ endpoints: {} });
const delegate = new node.Delegate(
PROJECT_ID,
PROJECT_DIR,
SOURCE_DIR,
"nodejs16" as Runtime,
);
await delegate.discoverBuild({}, {});
expect(detectFromYamlMock).to.have.been.called;
});
});
});