From 22a5ea5db9490ae722f3ab38cbb7db80ed2f6421 Mon Sep 17 00:00:00 2001 From: Corie Watson Date: Tue, 13 May 2025 00:55:58 +0100 Subject: [PATCH 1/5] feat: add SecretManager mocking --- src/main.ts | 2 ++ src/secretManager.ts | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 src/secretManager.ts 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..8c9d357 --- /dev/null +++ b/src/secretManager.ts @@ -0,0 +1,6 @@ +/** Mock values returned by `functions.config()`. */ +export function mockSecretManager(conf: { [key: string]: any }) { + for (const [key, value] of Object.entries(conf)) { + process.env[key] = value; + } +} \ No newline at end of file From abe8faf0d848042f0cf9e0f608d4f58a5c6bf933 Mon Sep 17 00:00:00 2001 From: Corie Watson Date: Mon, 19 May 2025 09:57:49 +0100 Subject: [PATCH 2/5] chore: format --- src/secretManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/secretManager.ts b/src/secretManager.ts index 8c9d357..1e1f62d 100644 --- a/src/secretManager.ts +++ b/src/secretManager.ts @@ -3,4 +3,4 @@ export function mockSecretManager(conf: { [key: string]: any }) { for (const [key, value] of Object.entries(conf)) { process.env[key] = value; } -} \ No newline at end of file +} From f2996f5ae6779378eeeed35e3e8588e211a52073 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 19 May 2025 13:09:43 -0700 Subject: [PATCH 3/5] Respond to comments. --- src/secretManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/secretManager.ts b/src/secretManager.ts index 1e1f62d..3156845 100644 --- a/src/secretManager.ts +++ b/src/secretManager.ts @@ -1,5 +1,5 @@ /** Mock values returned by `functions.config()`. */ -export function mockSecretManager(conf: { [key: string]: any }) { +export function mockSecretManager(conf: Record) { for (const [key, value] of Object.entries(conf)) { process.env[key] = value; } From 2b21fb6de0d57029674df2e47cb0f02e039af32f Mon Sep 17 00:00:00 2001 From: Corie Watson Date: Tue, 20 May 2025 02:04:31 +0100 Subject: [PATCH 4/5] feat(tests): add secretmanager tests --- spec/index.spec.ts | 1 + spec/secretmanager.spec.ts | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 spec/secretmanager.spec.ts 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..93d85ee --- /dev/null +++ b/spec/secretmanager.spec.ts @@ -0,0 +1,43 @@ +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'); + }); +}); From 8828d7e6b64da762a3137842cd6f07d5e440858b Mon Sep 17 00:00:00 2001 From: Corie Watson Date: Tue, 20 May 2025 02:16:32 +0100 Subject: [PATCH 5/5] chore: format --- spec/secretmanager.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/secretmanager.spec.ts b/spec/secretmanager.spec.ts index 93d85ee..6df0cbe 100644 --- a/spec/secretmanager.spec.ts +++ b/spec/secretmanager.spec.ts @@ -33,7 +33,10 @@ describe('mockSecretManager', () => { }); it('supports non-string values (coerced to string)', () => { - const conf: Record = { NUM_VALUE: '123', BOOL_VALUE: 'true' }; + const conf: Record = { + NUM_VALUE: '123', + BOOL_VALUE: 'true', + }; mockSecretManager(conf);