Skip to content

Commit

Permalink
Merge pull request #63 from mrsteele/feature/systemVarsCheck
Browse files Browse the repository at this point in the history
Feature/system vars check
  • Loading branch information
mrsteele committed Jun 29, 2017
2 parents 3f604f0 + 1c16c29 commit a1154d7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Dotenv {
}

Object.keys(blueprint).map(key => {
const value = env[key]
const value = (env[key] || env[key] === '') ? env[key] : vars[key]
if (!value && options.safe) {
throw new Error(`Missing environment variable: ${key}`)
} else {
Expand Down
1 change: 1 addition & 0 deletions test/envs/.systemvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PATH=
1 change: 1 addition & 0 deletions test/envs/.systemvars.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PATH=
30 changes: 22 additions & 8 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const envSimple = path.resolve(__dirname, './envs/.simple')
const envSimpleExample = path.resolve(__dirname, './envs/.simple.example')
const envMissingOne = path.resolve(__dirname, './envs/.missingone')
const envMissingOneExample = path.resolve(__dirname, './envs/.missingone.example')
const envSystemvars = path.resolve(__dirname, './envs/.systemvars')
const envSystemvarsExample = path.resolve(__dirname, './envs/.systemvars.example')

const envDefJson = {'process.env.TEST': '"hi"'}
const envEmptyJson = {}
Expand Down Expand Up @@ -50,14 +52,6 @@ function runTests (Obj, name) {
envTest({path: envSimple}).should.deep.equal(envSimpleJson)
})

it('Should allow system env variables', () => {
const test = envTest({path: envSimple, systemvars: true})
const key = Object.keys(envSimpleJson)[0]
const value = envSimpleJson[key]
test[key].should.equal(value)
Object.keys(test).length.should.be.above(Object.keys(envSimpleJson).length)
})

it('Should be an empty object when no environment variables exist in .env file.', () => {
envTest({path: false}).should.deep.equal(envEmptyJson)
})
Expand Down Expand Up @@ -90,6 +84,26 @@ function runTests (Obj, name) {
})
})

describe('System variables', () => {
it('Should allow system env variables', () => {
const test = envTest({path: envSimple, systemvars: true})
const key = Object.keys(envSimpleJson)[0]
const value = envSimpleJson[key]
test[key].should.equal(value)
Object.keys(test).length.should.be.above(Object.keys(envSimpleJson).length)
})

it('should pass if the systemvar satisfies the requirement', () => {
const PATH = envTest({ safe: envSystemvarsExample, systemvars: true })['process.env.PATH']
PATH.should.be.a('string')
PATH.should.contain('/')
})

it('should allow local variables to override systemvars', () => {
envTest({path: envSystemvars, systemvars: true})['process.env.PATH'].should.equal('""')
})
})

describe('Missing a variable', () => {
it('Should load fine (not-safe)', () => {
envTest({path: envMissingOne}).should.deep.equal(envMissingOneJson)
Expand Down

0 comments on commit a1154d7

Please sign in to comment.