diff --git a/test/unit/configuration/configuration_finding_test.js b/test/unit/configuration/configuration_finding_test.js index 8f1d13e2d..b563c43f3 100644 --- a/test/unit/configuration/configuration_finding_test.js +++ b/test/unit/configuration/configuration_finding_test.js @@ -37,19 +37,8 @@ describe('Configuration finding tests', function () { .mockFile(configInDriverDirectory, 'random content') .mockFile(configInHomeDirectory, 'random content') .mockFile(configInTempDirectory, 'random content'); - mock('fs/promises', { - access: async function (path) { - return fsMock.access(path); - }, - readFile: async function (path){ - return fsMock.readFile(path); - } - }); - mock('process', { - env: { - SF_CLIENT_CONFIG_FILE: configFromEnvVariable - } - }); + mockFiles(fsMock); + mockClientConfigFileEnvVariable(configFromEnvVariable); const fsPromises = require('fs/promises'); const process = require('process'); const configUtil = new ConfigurationUtil(fsPromises, process); @@ -68,19 +57,8 @@ describe('Configuration finding tests', function () { .mockFile(configInDriverDirectory, 'random content') .mockFile(configInHomeDirectory, 'random content') .mockFile(configInTempDirectory, 'random content'); - mock('fs/promises', { - access: async function (path) { - return fsMock.access(path); - }, - readFile: async function (path){ - return fsMock.readFile(path); - } - }); - mock('process', { - env: { - SF_CLIENT_CONFIG_FILE: configFromEnvVariable - } - }); + mockFiles(fsMock); + mockClientConfigFileEnvVariable(configFromEnvVariable); const fsPromises = require('fs/promises'); const process = require('process'); const configUtil = new ConfigurationUtil(fsPromises, process); @@ -98,19 +76,8 @@ describe('Configuration finding tests', function () { .mockFile(configInDriverDirectory, fileContent) .mockFile(configInHomeDirectory, 'random content') .mockFile(configInTempDirectory, 'random content'); - mock('fs/promises', { - access: async function (path) { - return fsMock.access(path); - }, - readFile: async function (path){ - return fsMock.readFile(path); - } - }); - mock('process', { - env: { - SF_CLIENT_CONFIG_FILE: undefined - } - }); + mockFiles(fsMock); + mockClientConfigFileEnvVariable(undefined); const fsPromises = require('fs/promises'); const process = require('process'); const configUtil = new ConfigurationUtil(fsPromises, process); @@ -127,19 +94,8 @@ describe('Configuration finding tests', function () { const fsMock = new FsMock() .mockFile(configInHomeDirectory, fileContent) .mockFile(configInTempDirectory, 'random content'); - mock('fs/promises', { - access: async function (path) { - return fsMock.access(path); - }, - readFile: async function (path){ - return fsMock.readFile(path); - } - }); - mock('process', { - env: { - SF_CLIENT_CONFIG_FILE: undefined - } - }); + mockFiles(fsMock); + mockClientConfigFileEnvVariable(undefined); const fsPromises = require('fs/promises'); const process = require('process'); const configUtil = new ConfigurationUtil(fsPromises, process); @@ -155,19 +111,8 @@ describe('Configuration finding tests', function () { // given const fsMock = new FsMock() .mockFile(configInTempDirectory, fileContent); - mock('fs/promises', { - access: async function (path) { - return fsMock.access(path); - }, - readFile: async function (path){ - return fsMock.readFile(path); - } - }); - mock('process', { - env: { - SF_CLIENT_CONFIG_FILE: undefined - } - }); + mockFiles(fsMock); + mockClientConfigFileEnvVariable(undefined); const fsPromises = require('fs/promises'); const process = require('process'); const configUtil = new ConfigurationUtil(fsPromises, process); @@ -182,19 +127,8 @@ describe('Configuration finding tests', function () { it('should return null if config could not be found', async function () { // given const fsMock = new FsMock(); - mock('fs/promises', { - access: async function (path) { - return fsMock.access(path); - }, - readFile: async function (path){ - return fsMock.readFile(path); - } - }); - mock('process', { - env: { - SF_CLIENT_CONFIG_FILE: undefined - } - }); + mockFiles(fsMock); + mockClientConfigFileEnvVariable(undefined); const fsPromises = require('fs/promises'); const process = require('process'); const configUtil = new ConfigurationUtil(fsPromises, process); @@ -207,6 +141,25 @@ describe('Configuration finding tests', function () { }); }); +function mockFiles(fsMock) { + mock('fs/promises', { + access: async function (path) { + return fsMock.access(path); + }, + readFile: async function (path){ + return fsMock.readFile(path); + } + }); +} + +function mockClientConfigFileEnvVariable(envClientConfigFileValue) { + mock('process', { + env: { + SF_CLIENT_CONFIG_FILE: envClientConfigFileValue + } + }); +} + class FsMock { existingFiles = new Map();