Skip to content
Merged
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
20 changes: 20 additions & 0 deletions src/emulator/apphosting/serve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as utils from "./developmentServer";
import * as configsImport from "./config";
import * as projectPathImport from "../../projectPath";
import { AppHostingYamlConfig } from "../../apphosting/yaml";
import * as emulatorRegistry from "../registry";
import * as emulatorEnvs from "../env";

describe("serve", () => {
let checkListenableStub: sinon.SinonStub;
Expand All @@ -16,6 +18,8 @@ describe("serve", () => {
let detectStartCommandStub: sinon.SinonStub;
let configsStub: sinon.SinonStubbedInstance<typeof configsImport>;
let resolveProjectPathStub: sinon.SinonStub;
let listRunningWithInfoStub: sinon.SinonStub;
let setEnvVarsForEmulatorsStub: sinon.SinonStub;

beforeEach(() => {
checkListenableStub = sinon.stub(portUtils, "checkListenable");
Expand All @@ -25,6 +29,9 @@ describe("serve", () => {
configsStub = sinon.stub(configsImport);
resolveProjectPathStub = sinon.stub(projectPathImport, "resolveProjectPath");

listRunningWithInfoStub = sinon.stub(emulatorRegistry.EmulatorRegistry, "listRunningWithInfo");
setEnvVarsForEmulatorsStub = sinon.stub(emulatorEnvs, "setEnvVarsForEmulators");

resolveProjectPathStub.returns("");
detectStartCommandStub.returns("npm run dev");
});
Expand All @@ -37,6 +44,10 @@ describe("serve", () => {
});

describe("start", () => {
beforeEach(() => {
listRunningWithInfoStub.returns([]);
});

it("should use user-provided port if one is defined", async () => {
checkListenableStub.onFirstCall().returns(true);
configsStub.getLocalAppHostingConfiguration.returns(
Expand Down Expand Up @@ -71,4 +82,13 @@ describe("serve", () => {
expect(spawnWithCommandStringStub.getCall(0).args[0]).to.eq(startCommand);
});
});

describe("getEmulatorEnvs", () => {
it("should omit apphosting emulator", () => {
listRunningWithInfoStub.returns([{ name: "apphosting" }, { name: "functions" }]);
serve.getEmulatorEnvs();

expect(setEnvVarsForEmulatorsStub).to.be.calledWith({}, [{ name: "functions" }]);
});
});
});
9 changes: 7 additions & 2 deletions src/emulator/apphosting/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
port += 1;
}

serve(port, options?.startCommand, options?.rootDirectory);

Check warning on line 39 in src/emulator/apphosting/serve.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

return { hostname, port };
}
Expand All @@ -53,7 +53,7 @@
const environmentVariablesAsRecord: Record<string, string> = {};

for (const env of apphostingLocalConfig.environmentVariables) {
environmentVariablesAsRecord[env.variable] = env.value!;

Check warning on line 56 in src/emulator/apphosting/serve.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
}

const environmentVariablesToInject = {
Expand Down Expand Up @@ -85,9 +85,14 @@
});
}

function getEmulatorEnvs(): Record<string, string> {
/**
* Exported for unit tests
*/
export function getEmulatorEnvs(): Record<string, string> {
const envs: Record<string, string> = {};
const emulatorInfos = EmulatorRegistry.listRunningWithInfo();
const emulatorInfos = EmulatorRegistry.listRunningWithInfo().filter(
(emulator) => emulator.name !== Emulators.APPHOSTING, // No need to set envs for the apphosting emulator itself.
);
setEnvVarsForEmulators(envs, emulatorInfos);

return envs;
Expand Down
Loading