From acb1efb7dda460a448338548f8e001ebf3b684d3 Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Tue, 9 Jun 2026 00:56:28 +0100 Subject: [PATCH] Centralize AWS test env cleanup --- test/lib/env.js | 19 +++++++++++++++++++ test/unit/src/utils/aws/config.test.js | 18 +++--------------- test/unit/src/utils/aws/credentials.test.js | 18 +++--------------- 3 files changed, 25 insertions(+), 30 deletions(-) create mode 100644 test/lib/env.js diff --git a/test/lib/env.js b/test/lib/env.js new file mode 100644 index 0000000..3cb7107 --- /dev/null +++ b/test/lib/env.js @@ -0,0 +1,19 @@ +'use strict'; + +const withClearedEnv = async (keys, callback) => { + const originalEnv = new Map(keys.map((key) => [key, process.env[key]])); + + for (const key of keys) delete process.env[key]; + + try { + return await callback(); + } finally { + for (const key of keys) { + const value = originalEnv.get(key); + if (value === undefined) delete process.env[key]; + else process.env[key] = value; + } + } +}; + +module.exports = { withClearedEnv }; diff --git a/test/unit/src/utils/aws/config.test.js b/test/unit/src/utils/aws/config.test.js index 9e51396..a46d3a5 100644 --- a/test/unit/src/utils/aws/config.test.js +++ b/test/unit/src/utils/aws/config.test.js @@ -3,6 +3,8 @@ const chai = require('chai'); const proxyquire = require('proxyquire'); +const { withClearedEnv } = require('../../../../lib/env'); + const { expect } = chai; describe('test/unit/src/utils/aws/config.test.js', () => { @@ -25,21 +27,7 @@ describe('test/unit/src/utils/aws/config.test.js', () => { 'https_cafile', ]; - async function withEnv(callback) { - const originalEnv = new Map(envKeys.map((key) => [key, process.env[key]])); - - for (const key of envKeys) delete process.env[key]; - - try { - return await callback(); - } finally { - for (const key of envKeys) { - const value = originalEnv.get(key); - if (value === undefined) delete process.env[key]; - else process.env[key] = value; - } - } - } + const withEnv = (callback) => withClearedEnv(envKeys, callback); function loadConfig() { function FakeNodeHttpHandler(options) { diff --git a/test/unit/src/utils/aws/credentials.test.js b/test/unit/src/utils/aws/credentials.test.js index bd4adb9..d47b797 100644 --- a/test/unit/src/utils/aws/credentials.test.js +++ b/test/unit/src/utils/aws/credentials.test.js @@ -5,6 +5,8 @@ const path = require('path'); const proxyquire = require('proxyquire'); const sinon = require('sinon'); +const { withClearedEnv } = require('../../../../lib/env'); + const { expect } = chai; describe('test/unit/src/utils/aws/credentials.test.js', () => { @@ -25,21 +27,7 @@ describe('test/unit/src/utils/aws/credentials.test.js', () => { 'AWS_CONFIG_FILE', ]; - async function withEnv(callback) { - const originalEnv = new Map(envKeys.map((key) => [key, process.env[key]])); - - for (const key of envKeys) delete process.env[key]; - - try { - return await callback(); - } finally { - for (const key of envKeys) { - const value = originalEnv.get(key); - if (value === undefined) delete process.env[key]; - else process.env[key] = value; - } - } - } + const withEnv = (callback) => withClearedEnv(envKeys, callback); function createMissingFileError() { return Object.assign(new Error('missing'), { code: 'ENOENT' });