Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/commands/internaltesting-functions-discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ export const command = new Command("internaltesting:functions:discover")
}
}

const wantBuilds = await loadCodebases(
fnConfig,
options,
const result = await loadCodebases({
projectId,
projectDir: options.config.projectDir,
projectAlias: options.projectAlias,
config: fnConfig,
firebaseConfig,
runtimeConfig,
undefined, // no filters
);
});
const wantBuilds = result.builds;

logger.info(JSON.stringify(wantBuilds, null, 2));
return wantBuilds;
Expand Down
197 changes: 168 additions & 29 deletions src/deploy/functions/prepare.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import * as prompt from "../../prompt";
import { RuntimeDelegate } from "./runtimes";
import { FirebaseError } from "../../error";
import { Options } from "../../options";
import { ValidatedConfig } from "../../functions/projectConfig";
import * as functionsEnv from "../../functions/env";
import { BEFORE_CREATE_EVENT, BEFORE_SIGN_IN_EVENT } from "../../functions/events/v1";
import { latest } from "./runtimes/supported";
import * as path from "path";
import * as prepareFunctionsUpload from "./prepareFunctionsUpload";

Check failure on line 17 in src/deploy/functions/prepare.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'prepareFunctionsUpload' is defined but never used
import * as remoteSource from "./remoteSource";

describe("prepare", () => {
const ENDPOINT_BASE: Omit<backend.Endpoint, "httpsTrigger"> = {
Expand All @@ -33,6 +36,11 @@
let sandbox: sinon.SinonSandbox;
let runtimeDelegateStub: RuntimeDelegate;
let discoverBuildStub: sinon.SinonStub;
let getRemoteSourceStub: sinon.SinonStub;

Check failure on line 39 in src/deploy/functions/prepare.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'getRemoteSourceStub' is assigned a value but never used
let loadUserEnvsStub: sinon.SinonStub;

Check failure on line 40 in src/deploy/functions/prepare.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

'loadUserEnvsStub' is assigned a value but never used

const EXTRACTED_SOURCE_DIR = "/tmp/extracted-source";
const DEFAULT_ENVS = { FOO: "bar" };

beforeEach(() => {
sandbox = sinon.createSandbox();
Expand All @@ -58,6 +66,9 @@
}),
);
sandbox.stub(runtimes, "getRuntimeDelegate").resolves(runtimeDelegateStub);
getRemoteSourceStub = sandbox.stub(remoteSource, "getRemoteSource").resolves(EXTRACTED_SOURCE_DIR);

Check failure on line 69 in src/deploy/functions/prepare.spec.ts

View workflow job for this annotation

GitHub Actions / unit (24)

Replace `.stub(remoteSource,·"getRemoteSource")` with `⏎········.stub(remoteSource,·"getRemoteSource")⏎········`
sandbox.stub(remoteSource, "requireFunctionsYaml");
loadUserEnvsStub = sandbox.stub(functionsEnv, "loadUserEnvs").returns(DEFAULT_ENVS);
});

afterEach(() => {
Expand All @@ -68,16 +79,16 @@
const config: ValidatedConfig = [
{ source: "source", codebase: "codebase", prefix: "my-prefix", runtime: "nodejs22" },
];
const options = {
config: {
path: (p: string) => p,
},
projectId: "project",
} as unknown as Options;
const firebaseConfig = { projectId: "project" };
const runtimeConfig = {};

const builds = await prepare.loadCodebases(config, options, firebaseConfig, runtimeConfig);
const { builds } = await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(Object.keys(builds.codebase.endpoints)).to.deep.equal(["my-prefix-test"]);
});
Expand All @@ -86,16 +97,16 @@
const config: ValidatedConfig = [
{ source: "source", codebase: "codebase", runtime: "nodejs20" },
];
const options = {
config: {
path: (p: string) => p,
},
projectId: "project",
} as unknown as Options;
const firebaseConfig = { projectId: "project" };
const runtimeConfig = {};

const builds = await prepare.loadCodebases(config, options, firebaseConfig, runtimeConfig);
const { builds } = await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(builds.codebase.runtime).to.equal("nodejs20");
});
Expand All @@ -109,16 +120,16 @@
runtime: "nodejs22",
},
];
const options = {
config: {
path: (p: string) => p,
},
projectId: "project",
} as unknown as Options;
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig, customKey: "customValue" };

await prepare.loadCodebases(config, options, firebaseConfig, runtimeConfig);
await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(discoverBuildStub.calledOnce).to.be.true;
const callArgs = discoverBuildStub.firstCall.args;
Expand All @@ -135,22 +146,150 @@
runtime: "nodejs22",
},
];
const options = {
config: {
path: (p: string) => p,
},
projectId: "project",
} as unknown as Options;
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig, customKey: "customValue" };

await prepare.loadCodebases(config, options, firebaseConfig, runtimeConfig);
await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(discoverBuildStub.calledOnce).to.be.true;
const callArgs = discoverBuildStub.firstCall.args;
expect(callArgs[0]).to.deep.equal(runtimeConfig);
expect(callArgs[0]).to.have.property("customKey", "customValue");
});

it("should handle remote sources by extracting and discovering", async () => {
const config: ValidatedConfig = [
{
codebase: "remote",
remoteSource: { repository: "user/repo", ref: "main" },
runtime: "nodejs20",
},
];
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig };

const { builds, sourceDirs, envs } = await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(remoteSource.getRemoteSource).to.have.been.calledWith(
/* repository= */ "user/repo",
/* ref= */ "main",
/* destDir= */ sinon.match.string,
/* subDir= */ undefined,
);
expect(builds.remote).to.exist;
expect(sourceDirs.remote).to.equal(EXTRACTED_SOURCE_DIR);
expect(envs.remote).to.deep.equal({});
});

it("should pass configDir to loadUserEnvs for remote sources", async () => {
const config: ValidatedConfig = [
{
codebase: "remote",
remoteSource: { repository: "user/repo", ref: "main" },
runtime: "nodejs20",
configDir: "config-dir",
},
];
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig };

const { envs, sourceDirs } = await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(sourceDirs.remote).to.equal(EXTRACTED_SOURCE_DIR);
expect(functionsEnv.loadUserEnvs).to.have.been.calledWith(
sinon.match({
functionsSource: EXTRACTED_SOURCE_DIR,
configDir: path.resolve("/project", "config-dir"),
}),
);
expect(envs.remote).to.deep.equal(DEFAULT_ENVS);
});

it("should pass dir to getRemoteSource for monorepo support", async () => {
const config: ValidatedConfig = [
{
codebase: "remote",
remoteSource: { repository: "user/repo", ref: "v1.0.0", dir: "packages/functions" },
runtime: "nodejs20",
},
];
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig };

await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(remoteSource.getRemoteSource).to.have.been.calledWith(
"user/repo",
"v1.0.0",
sinon.match.string,
"packages/functions",
);
});

it("should require functions.yaml for remote sources", async () => {
const config: ValidatedConfig = [
{
codebase: "remote",
remoteSource: { repository: "user/repo", ref: "main" },
runtime: "nodejs20",
},
];
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig };

await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(remoteSource.requireFunctionsYaml).to.have.been.calledWith(EXTRACTED_SOURCE_DIR);
});

it("should not call getRemoteSource for local sources", async () => {
const config: ValidatedConfig = [
{ source: "functions", codebase: "local", runtime: "nodejs20" },
];
const firebaseConfig = { projectId: "project" };
const runtimeConfig = { firebase: firebaseConfig };

await prepare.loadCodebases({
projectId: "project",
projectDir: "/project",
config,
firebaseConfig,
runtimeConfig,
});

expect(remoteSource.getRemoteSource).to.not.have.been.called;
expect(remoteSource.requireFunctionsYaml).to.not.have.been.called;
});
});

describe("inferDetailsFromExisting", () => {
Expand Down
Loading
Loading