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
19 changes: 19 additions & 0 deletions test/lib/env.js
Original file line number Diff line number Diff line change
@@ -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 };
18 changes: 3 additions & 15 deletions test/unit/src/utils/aws/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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) {
Expand Down
18 changes: 3 additions & 15 deletions test/unit/src/utils/aws/credentials.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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' });
Expand Down
Loading