Skip to content

Commit

Permalink
Merge pull request #274 from tobias-trozowski/highest-priority-for-sy…
Browse files Browse the repository at this point in the history
…stem-variables

BREAKING CHANGE: Highest priority for system variables
  • Loading branch information
mrsteele committed Nov 25, 2020
2 parents 61925bc + 45a48d8 commit ce5958f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ class Dotenv {

// add the leftovers
if (safe) {
Object.assign(vars, env)
Object.keys(env).forEach((key) => {
if (!Object.prototype.hasOwnProperty.call(vars, key)) {
vars[key] = env[key]
}
})
}

return vars
Expand Down
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ function runTests (Obj, name) {
test('should not allow local variables to override systemvars', () => {
expect(envTest({ path: envSystemvars, systemvars: true })['process.env.PATH2'] !== '""').toEqual(true)
})

test('Should give the highest priority for the system variables', () => {
process.env.TEST = 'production'
const test = envTest({ safe: true, systemvars: true, defaults: true })
expect(test['process.env.TEST']).toEqual('"production"')
expect(test['process.env.TEST2']).toEqual('"hidefault"')
delete process.env.TEST
})
})

describe('Empty variables', () => {
Expand Down

0 comments on commit ce5958f

Please sign in to comment.