Skip to content

Commit

Permalink
Merge pull request #126 from stvvt/expand-self-with-default
Browse files Browse the repository at this point in the history
Fix self-expanding undefined variable with default value.
  • Loading branch information
motdotla authored Nov 13, 2024
2 parents ff7e2ef + b680e4a commit 54bcda0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 1 addition & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ function interpolate (value, processEnv, parsed) {

if (parsed[key]) {
// avoid recursion from EXPAND_SELF=$EXPAND_SELF
if (parsed[key] === value) {
return parsed[key]
} else {
if (parsed[key] !== value) {
return interpolate(parsed[key], processEnv, parsed)
}
}
Expand Down
17 changes: 15 additions & 2 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ t.test('expands self without a recursive call stack error', ct => {
}
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_SELF, '$EXPAND_SELF') // because it ends up accessing parsed[key].
ct.equal(parsed.EXPAND_SELF, '') // because it ends up accessing parsed[key].

ct.end()
})

t.test('expands self with undefined variable and default value', ct => {
const dotenv = {
parsed: {
EXPAND_SELF: '${EXPAND_SELF:-default}'
}
}
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_SELF, 'default')

ct.end()
})
Expand Down Expand Up @@ -466,7 +479,7 @@ t.test('expands self without a recursive call stack error (process.env)', ct =>
const dotenv = require('dotenv').config({ path: 'tests/.env.test', processEnv: {} })
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_SELF, '$EXPAND_SELF') // because it ends up accessing parsed[key].
ct.equal(parsed.EXPAND_SELF, '') // because it ends up accessing parsed[key].

ct.end()
})
Expand Down

0 comments on commit 54bcda0

Please sign in to comment.