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
124 changes: 124 additions & 0 deletions src/deploy/functions/runtimes/python/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import * as sinon from "sinon";

import * as python from ".";
import * as pythonFunctions from "../../../../functions/python";

const PROJECT_ID = "test-project";
const SOURCE_DIR = "/some/path/fns";
Expand Down Expand Up @@ -34,4 +35,127 @@
expect(delegate.getPythonBinary()).to.equal("python.exe");
});
});

describe("serveAdmin", () => {
let delegate: python.Delegate;
let runWithVirtualEnvStub: sinon.SinonStub;
let originalEnv: NodeJS.ProcessEnv;

beforeEach(() => {
delegate = new python.Delegate(PROJECT_ID, SOURCE_DIR, "python310");

// Mock the modulesDir method
sinon.stub(delegate, "modulesDir" as any).resolves("/mock/modules/dir");

Check warning on line 48 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

// Mock runWithVirtualEnv from the imported module
runWithVirtualEnvStub = sinon.stub(pythonFunctions, "runWithVirtualEnv");

// Store original environment
originalEnv = { ...process.env };
});

afterEach(() => {
// Restore original environment
process.env = originalEnv;
sinon.restore();
});

it("should propagate process.env including GOOGLE_APPLICATION_CREDENTIALS", async () => {
// Set up test environment variables
process.env.GOOGLE_APPLICATION_CREDENTIALS = "/path/to/service-account.json";
process.env.CUSTOM_VAR = "custom_value";

const testEnvs = {
FIREBASE_PROJECT_ID: PROJECT_ID,
FUNCTION_TARGET: "test_function",
};

// Mock the runWithVirtualEnv function
const mockChildProcess = {
stdout: { on: sinon.stub() },
stderr: { on: sinon.stub() },
killed: false,
kill: sinon.stub(),
once: sinon.stub(),
};
runWithVirtualEnvStub.returns(mockChildProcess);

await delegate.serveAdmin(8080, testEnvs);

// Verify runWithVirtualEnv was called with correct environment
expect(runWithVirtualEnvStub.calledOnce).to.be.true;
const callArgs = runWithVirtualEnvStub.getCall(0).args;
const passedEnvs = callArgs[2]; // Third argument is the environment

Check warning on line 88 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

// Verify process.env variables are included
expect(passedEnvs.GOOGLE_APPLICATION_CREDENTIALS).to.equal("/path/to/service-account.json");

Check warning on line 91 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .GOOGLE_APPLICATION_CREDENTIALS on an `any` value
expect(passedEnvs.CUSTOM_VAR).to.equal("custom_value");

Check warning on line 92 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .CUSTOM_VAR on an `any` value

// Verify provided envs are included
expect(passedEnvs.FIREBASE_PROJECT_ID).to.equal(PROJECT_ID);

Check warning on line 95 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .FIREBASE_PROJECT_ID on an `any` value
expect(passedEnvs.FUNCTION_TARGET).to.equal("test_function");

Check warning on line 96 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .FUNCTION_TARGET on an `any` value

// Verify ADMIN_PORT is set
expect(passedEnvs.ADMIN_PORT).to.equal("8080");

Check warning on line 99 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .ADMIN_PORT on an `any` value
});

it("should preserve process.env when no additional envs are provided", async () => {
// Set up test environment variables
process.env.GOOGLE_APPLICATION_CREDENTIALS = "/path/to/service-account.json";
(process.env as any).NODE_ENV = "test";

Check warning on line 105 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 105 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .NODE_ENV on an `any` value

const mockChildProcess = {
stdout: { on: sinon.stub() },
stderr: { on: sinon.stub() },
killed: false,
kill: sinon.stub(),
once: sinon.stub(),
};
runWithVirtualEnvStub.returns(mockChildProcess);

await delegate.serveAdmin(8080, {});

// Verify runWithVirtualEnv was called with correct environment
expect(runWithVirtualEnvStub.calledOnce).to.be.true;
const callArgs = runWithVirtualEnvStub.getCall(0).args;
const passedEnvs = callArgs[2];

Check warning on line 121 in src/deploy/functions/runtimes/python/index.spec.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

// Verify process.env variables are preserved
expect(passedEnvs.GOOGLE_APPLICATION_CREDENTIALS).to.equal("/path/to/service-account.json");
expect(passedEnvs.NODE_ENV).to.equal("test");
expect(passedEnvs.ADMIN_PORT).to.equal("8080");
});

it("should override process.env with provided envs when there are conflicts", async () => {
// Set up conflicting environment variables
process.env.GOOGLE_APPLICATION_CREDENTIALS = "/original/path.json";
process.env.FIREBASE_PROJECT_ID = "original-project";

const testEnvs = {
GOOGLE_APPLICATION_CREDENTIALS: "/new/path.json",
FIREBASE_PROJECT_ID: PROJECT_ID,
};

const mockChildProcess = {
stdout: { on: sinon.stub() },
stderr: { on: sinon.stub() },
killed: false,
kill: sinon.stub(),
once: sinon.stub(),
};
runWithVirtualEnvStub.returns(mockChildProcess);

await delegate.serveAdmin(8080, testEnvs);

// Verify runWithVirtualEnv was called with correct environment
expect(runWithVirtualEnvStub.calledOnce).to.be.true;
const callArgs = runWithVirtualEnvStub.getCall(0).args;
const passedEnvs = callArgs[2];

// Verify provided envs override process.env
expect(passedEnvs.GOOGLE_APPLICATION_CREDENTIALS).to.equal("/new/path.json");
expect(passedEnvs.FIREBASE_PROJECT_ID).to.equal(PROJECT_ID);
expect(passedEnvs.ADMIN_PORT).to.equal("8080");
});
});
});
1 change: 1 addition & 0 deletions src/deploy/functions/runtimes/python/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export class Delegate implements runtimes.RuntimeDelegate {
async serveAdmin(port: number, envs: backend.EnvironmentVariables) {
const modulesDir = await this.modulesDir();
const envWithAdminPort = {
...process.env,
...envs,
ADMIN_PORT: port.toString(),
};
Expand Down
Loading