diff --git a/spec/index.spec.ts b/spec/index.spec.ts index 2904409..2fefa1d 100644 --- a/spec/index.spec.ts +++ b/spec/index.spec.ts @@ -67,6 +67,7 @@ describe('index', () => { import './lifecycle.spec'; import './main.spec'; +import './secretmanager.spec'; import './v2.spec'; import './cloudevent/generate'; import './app.spec'; diff --git a/spec/secretmanager.spec.ts b/spec/secretmanager.spec.ts new file mode 100644 index 0000000..6df0cbe --- /dev/null +++ b/spec/secretmanager.spec.ts @@ -0,0 +1,46 @@ +import { expect } from 'chai'; +import { mockSecretManager } from '../src/secretManager'; + +describe('mockSecretManager', () => { + let originalEnv; + + before(() => { + // Capture the original environment variables + originalEnv = { ...process.env }; + }); + + afterEach(() => { + // Reset any mutations made by the test run + process.env = { ...originalEnv }; + }); + + it('applies each key/value pair to process.env', () => { + const conf = { FOO: 'bar', BAZ: 'qux' }; + + mockSecretManager(conf); + + expect(process.env.FOO).to.equal('bar'); + expect(process.env.BAZ).to.equal('qux'); + }); + + it('overwrites an existing variable with the new value', () => { + process.env.EXISTING = 'old'; + const conf = { EXISTING: 'new' }; + + mockSecretManager(conf); + + expect(process.env.EXISTING).to.equal('new'); + }); + + it('supports non-string values (coerced to string)', () => { + const conf: Record = { + NUM_VALUE: '123', + BOOL_VALUE: 'true', + }; + + mockSecretManager(conf); + + expect(process.env.NUM_VALUE).to.equal('123'); + expect(process.env.BOOL_VALUE).to.equal('true'); + }); +}); diff --git a/src/main.ts b/src/main.ts index a2e621d..13a23bb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -45,6 +45,8 @@ type HttpsFunctionOrCloudFunctionV1 = U extends HttpsFunction & ? HttpsFunction & Runnable : CloudFunctionV1; +export { mockSecretManager } from './secretManager'; + // Re-exporting V1 (to reduce breakage) export { ContextOptions, diff --git a/src/secretManager.ts b/src/secretManager.ts new file mode 100644 index 0000000..3156845 --- /dev/null +++ b/src/secretManager.ts @@ -0,0 +1,6 @@ +/** Mock values returned by `functions.config()`. */ +export function mockSecretManager(conf: Record) { + for (const [key, value] of Object.entries(conf)) { + process.env[key] = value; + } +}