Skip to content

Commit f6bfce9

Browse files
committed
PR feedback
1 parent 61552be commit f6bfce9

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/functions/iac/export.spec.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ describe("export", () => {
1414

1515
const mockDelegate = {
1616
language: "nodejs",
17-
runtime: "nodejs18",
17+
runtime: supported.latest("nodejs"),
1818
validate: sinon.stub(),
1919
build: sinon.stub(),
2020
discoverBuild: sinon.stub(),
21-
};
21+
bin: "node",
22+
watch: sinon.stub(),
23+
} as const;
2224

2325
beforeEach(() => {
2426
sinon.stub(functionsConfig, "getFirebaseConfig").resolves({ projectId: "my-project" });
2527
sinon.stub(functionsEnv, "loadFirebaseEnvs").returns({});
26-
sinon.stub(runtimes, "getRuntimeDelegate").resolves(mockDelegate as any);
28+
sinon.stub(runtimes, "getRuntimeDelegate").resolves(mockDelegate);
2729
sinon.stub(supported, "guardVersionSupport");
2830
needProjectIdStub = sinon.stub(projectUtils, "needProjectId").returns("my-project");
2931
});
@@ -44,7 +46,7 @@ describe("export", () => {
4446
const codebase: projectConfig.ValidatedSingle = {
4547
source: "src",
4648
codebase: "default",
47-
runtime: "nodejs18",
49+
runtime: supported.latest("nodejs") as supported.ActiveRuntime,
4850
};
4951

5052
const result = await exportIac.getInternalIac(options, codebase);
@@ -57,5 +59,32 @@ describe("export", () => {
5759
"functions.yaml": yaml.dump(mockBuild),
5860
});
5961
});
62+
63+
it("should throw if codebase has no source", async () => {
64+
const options = { config: { path: (s: string) => s, projectDir: "dir" } };
65+
const codebase: projectConfig.ValidatedSingle = {
66+
codebase: "default",
67+
runtime: supported.latest("nodejs") as supported.ActiveRuntime,
68+
} as unknown as projectConfig.ValidatedSingle;
69+
70+
await expect(exportIac.getInternalIac(options, codebase)).to.be.rejectedWith(
71+
"Cannot export a codebase with no source",
72+
);
73+
});
74+
75+
it("should throw an error if discoverBuild fails", async () => {
76+
mockDelegate.discoverBuild.rejects(new Error("Failed to discover build"));
77+
78+
const options = { config: { path: (s: string) => s, projectDir: "dir" } };
79+
const codebase: projectConfig.ValidatedSingle = {
80+
source: "src",
81+
codebase: "default",
82+
runtime: supported.latest("nodejs") as supported.ActiveRuntime,
83+
};
84+
85+
await expect(exportIac.getInternalIac(options, codebase)).to.be.rejectedWith(
86+
"Failed to discover build",
87+
);
88+
});
6089
});
6190
});

src/functions/iac/export.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@ import * as functionsEnv from "../../functions/env";
66
import { logger } from "../../logger";
77
import * as yaml from "js-yaml";
88
import { needProjectId } from "../../projectUtils";
9+
import { FirebaseError } from "../../error";
910

1011
export type Exporter = (
1112
options: any,
1213
codebase: projectConfig.ValidatedSingle,
1314
) => Promise<Record<string, string>>;
1415

1516
/**
16-
*
17+
* Exports the functions.yaml format of the codebase.
1718
*/
1819
export async function getInternalIac(
1920
options: any,
2021
codebase: projectConfig.ValidatedSingle,
2122
): Promise<Record<string, string>> {
23+
if (!codebase.source) {
24+
throw new FirebaseError("Cannot export a codebase with no source");
25+
}
2226
const projectId = needProjectId(options);
2327

2428
const firebaseConfig = await functionsConfig.getFirebaseConfig(options);
2529
const firebaseEnvs = functionsEnv.loadFirebaseEnvs(firebaseConfig, projectId);
2630

2731
const delegateContext: runtimes.DelegateContext = {
2832
projectId,
29-
sourceDir: options.config.path(codebase.source!),
33+
sourceDir: options.config.path(codebase.source),
3034
projectDir: options.config.projectDir,
3135
runtime: codebase.runtime,
3236
};

0 commit comments

Comments
 (0)