import * as sinon from 'sinon' import { assert } from 'chai' import { getEnvVars } from '../src/get-env-vars' import * as rcFile from '../src/parse-rc-file' import * as envFile from '../src/parse-env-file' describe('getEnvVars', (): void => { let getRCFileVarsStub: sinon.SinonStub let getEnvFileVarsStub: sinon.SinonStub before((): void => { getRCFileVarsStub = sinon.stub(rcFile, 'getRCFileVars') getEnvFileVarsStub = sinon.stub(envFile, 'getEnvFileVars') }) after((): void => { sinon.restore() }) afterEach((): void => { sinon.resetHistory() sinon.resetBehavior() }) it('should parse the json .rc file from the default location with the given environment', async (): Promise => { getRCFileVarsStub.returns({ THANKS: 'FORALLTHEFISH' }) const envs = await getEnvVars({ rc: { environments: ['production'] } }) assert.isOk(envs) assert.lengthOf(Object.keys(envs), 1) assert.equal(envs.THANKS, 'FORALLTHEFISH') assert.equal(getRCFileVarsStub.callCount, 1) assert.lengthOf(getRCFileVarsStub.args[0][0].environments, 1) assert.equal(getRCFileVarsStub.args[0][0].environments[0], 'production') assert.equal(getRCFileVarsStub.args[0][0].filePath, './.env-cmdrc') } ) it('should search all default .rc file locations', async (): Promise => { getRCFileVarsStub.throws('Not found.') getRCFileVarsStub.onThirdCall().returns({ THANKS: 'FORALLTHEFISH' }) const envs = await getEnvVars({ rc: { environments: ['production'] } }) assert.isOk(envs) assert.lengthOf(Object.keys(envs), 1) assert.equal(envs.THANKS, 'FORALLTHEFISH') assert.equal(getRCFileVarsStub.callCount, 3) assert.lengthOf(getRCFileVarsStub.args[2][0].environments, 1) assert.equal(getRCFileVarsStub.args[2][0].environments[0], 'production') assert.equal(getRCFileVarsStub.args[2][0].filePath, './.env-cmdrc.json') }) it('should fail to find .rc file at default location', async (): Promise => { getRCFileVarsStub.throws('Not found.') try { await getEnvVars({ rc: { environments: ['production'] } }) assert.fail('should not get here.') } catch (e) { assert.match(e.message, /locate \.rc/gi) } }) it('should find .rc file at custom path location', async (): Promise => { getRCFileVarsStub.returns({ THANKS: 'FORALLTHEFISH' }) const envs = await getEnvVars({ rc: { environments: ['production'], filePath: '../.custom-rc' } }) assert.isOk(envs) assert.lengthOf(Object.keys(envs), 1) assert.equal(envs.THANKS, 'FORALLTHEFISH') assert.equal(getRCFileVarsStub.callCount, 1) assert.lengthOf(getRCFileVarsStub.args[0][0].environments, 1) assert.equal(getRCFileVarsStub.args[0][0].environments[0], 'production') assert.equal(getRCFileVarsStub.args[0][0].filePath, '../.custom-rc') }) it('should fail to find .rc file at custom path location', async (): Promise => { getRCFileVarsStub.throws('Not found.') try { await getEnvVars({ rc: { environments: ['production'], filePath: '../.custom-rc' } }) assert.fail('should not get here.') } catch (e) { assert.match(e.message, /locate \.rc/gi) } }) it('should parse the env file from a custom location', async (): Promise => { getEnvFileVarsStub.returns({ THANKS: 'FORALLTHEFISH' }) const envs = await getEnvVars({ envFile: { filePath: '../.env-file' } }) assert.isOk(envs) assert.lengthOf(Object.keys(envs), 1) assert.equal(envs.THANKS, 'FORALLTHEFISH') assert.equal(getEnvFileVarsStub.callCount, 1) assert.equal(getEnvFileVarsStub.args[0][0], '../.env-file') }) it('should fail to find env file at custom location', async (): Promise => { getEnvFileVarsStub.throws('Not found.') try { await getEnvVars({ envFile: { filePath: '../.env-file' } }) assert.fail('should not get here.') } catch (e) { assert.match(e.message, /locate env/gi) } }) it('should parse the env file from the default location if custom ' + 'location not found and fallback option provided', async (): Promise => { getEnvFileVarsStub.onFirstCall().throws('File not found.') getEnvFileVarsStub.returns({ THANKS: 'FORALLTHEFISH' }) const envs = await getEnvVars({ envFile: { filePath: '../.env-file', fallback: true } }) assert.isOk(envs) assert.lengthOf(Object.keys(envs), 1) assert.equal(envs.THANKS, 'FORALLTHEFISH') assert.equal(getEnvFileVarsStub.callCount, 2) assert.equal(getEnvFileVarsStub.args[1][0], './.env') } ) it('should parse the env file from the default location', async (): Promise => { getEnvFileVarsStub.returns({ THANKS: 'FORALLTHEFISH' }) const envs = await getEnvVars() assert.isOk(envs) assert.lengthOf(Object.keys(envs), 1) assert.equal(envs.THANKS, 'FORALLTHEFISH') assert.equal(getEnvFileVarsStub.callCount, 1) assert.equal(getEnvFileVarsStub.args[0][0], './.env') }) it('should fail to find env file at default location', async (): Promise => { getEnvFileVarsStub.throws('Not found.') try { await getEnvVars() assert.fail('should not get here.') } catch (e) { assert.match(e.message, /locate env/gi) } }) })