env-cmd/test/get-env-vars.spec.ts
Gregory Waxman ae7981691e Added Env default locations (#81)
* Added env default locations to match rc file locations

* Updated README to clarify all supported files and their formats

* Added new test to verify all env files are searched for
2019-08-28 02:25:15 -05:00

158 lines
5.8 KiB
TypeScript

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<any, any>
let getEnvFileVarsStub: sinon.SinonStub<any, any>
const pathError = new Error()
pathError.name = 'PathError'
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<void> => {
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<void> => {
getRCFileVarsStub.rejects(pathError)
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<void> => {
getRCFileVarsStub.rejects(pathError)
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<void> => {
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<void> => {
getRCFileVarsStub.rejects(pathError)
try {
await getEnvVars({
rc: { environments: ['production'], filePath: '../.custom-rc' }
})
assert.fail('should not get here.')
} catch (e) {
console.log(e)
assert.match(e.message, /locate \.rc/gi)
}
})
it('should parse the env file from a custom location', async (): Promise<void> => {
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<void> => {
getEnvFileVarsStub.rejects('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<void> => {
getEnvFileVarsStub.onFirstCall().rejects('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<void> => {
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 search all default env file locations', async (): Promise<void> => {
getEnvFileVarsStub.throws('Not found.')
getEnvFileVarsStub.onThirdCall().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, 3)
assert.isTrue(getEnvFileVarsStub.calledWithExactly('./.env.json'))
assert.isTrue(getEnvFileVarsStub.calledWithExactly('./.env.json'))
})
it('should fail to find env file at default location', async (): Promise<void> => {
getEnvFileVarsStub.rejects('Not found.')
try {
await getEnvVars()
assert.fail('should not get here.')
} catch (e) {
assert.match(e.message, /locate env/gi)
}
})
})