From 9af456796e6758da58fc9aa48ad1e84edb83aafc Mon Sep 17 00:00:00 2001 From: Joy Chang Date: Wed, 1 May 2019 15:23:55 -0500 Subject: [PATCH 1/2] fix: do not read credentials file in webpack override scenario --- lib/read-credentials-file.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/read-credentials-file.ts b/lib/read-credentials-file.ts index 466bd4c64..ee91c7042 100644 --- a/lib/read-credentials-file.ts +++ b/lib/read-credentials-file.ts @@ -6,6 +6,10 @@ import path = require('path'); const filename: string = 'ibm-credentials.env'; export function readCredentialsFile() { + if (!fs.existsSync) { + return {}; + } + // first look for an env variable called IBM_CREDENTIALS_FILE // it should be the path to the file @@ -48,4 +52,4 @@ export function constructFilepath(filepath): string { } return filepath; -} \ No newline at end of file +} From 0c5b5341df4170d488cd29be52b968105d003686 Mon Sep 17 00:00:00 2001 From: Joy Chang Date: Tue, 7 May 2019 11:44:57 -0500 Subject: [PATCH 2/2] test: exclude fs for browser scenario --- test/unit/readCredentialsFile.test.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/unit/readCredentialsFile.test.js b/test/unit/readCredentialsFile.test.js index 9d8347602..bb53c98c7 100644 --- a/test/unit/readCredentialsFile.test.js +++ b/test/unit/readCredentialsFile.test.js @@ -4,6 +4,23 @@ const readCredentialsFunctions = require('../../lib/read-credentials-file'); const constructFilepath = readCredentialsFunctions.constructFilepath; const fileExistsAtPath = readCredentialsFunctions.fileExistsAtPath; const readCredentialsFile = readCredentialsFunctions.readCredentialsFile; +const fs = require('fs'); + +describe('browser scenario', () => { + const existSync = fs.existsSync; + beforeAll(() => { + fs.existsSync = undefined; + }); + + it('should return empty object when webpack override fs with empty object', () => { + const cred = readCredentialsFile(); + expect(cred).toEqual({}); + }); + + afterAll(() => { + fs.existsSync = existSync; + }); +}); describe('read ibm credentials file', () => { const locationOfActualFile = __dirname + '/../resources';