Skip to content

Commit

Permalink
Merge 37dade6 into 825c1b2
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Mar 31, 2017
2 parents 825c1b2 + 37dade6 commit a308b15
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ function config (options) {
try {
// specifying an encoding returns a string instead of a buffer
var parsedObj = parse(fs.readFileSync(path, { encoding: encoding }))
var insertedObj = {}

Object.keys(parsedObj).forEach(function (key) {
process.env[key] = process.env[key] || parsedObj[key]
var value = process.env[key] || parsedObj[key]

insertedObj[key] = value
process.env[key] = value
})

return { parsed: parsedObj }
return { parsed: parsedObj, inserted: insertedObj }
} catch (e) {
return { error: e }
}
Expand Down
20 changes: 20 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe('dotenv', function () {
beforeEach(function (done) {
readFileSyncStub = s.stub(fs, 'readFileSync').returns('test=val')
parseStub = s.stub(dotenv, 'parse').returns({test: 'val'})
delete process.env.test // clean up

done()
})

Expand Down Expand Up @@ -82,6 +84,24 @@ describe('dotenv', function () {
done()
})

it('returns inserted object', function (done) {
var env = dotenv.config()

env.should.not.have.property('error')
env.inserted.should.eql({ test: 'val' })
done()
})

it('returns inserted values and parsed values (which can be different if already preset)', function (done) {
process.env.test = 'test'

var env = dotenv.config()

env.parsed.should.eql({ test: 'val' })
env.inserted.should.eql({ test: 'test' })
done()
})

it('returns any errors thrown from reading file or parsing', function (done) {
readFileSyncStub.throws()

Expand Down

0 comments on commit a308b15

Please sign in to comment.