Skip to content

Commit 54bcda0

Browse files
authored
Merge pull request #126 from stvvt/expand-self-with-default
Fix self-expanding undefined variable with default value.
2 parents ff7e2ef + b680e4a commit 54bcda0

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/main.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ function interpolate (value, processEnv, parsed) {
3232

3333
if (parsed[key]) {
3434
// avoid recursion from EXPAND_SELF=$EXPAND_SELF
35-
if (parsed[key] === value) {
36-
return parsed[key]
37-
} else {
35+
if (parsed[key] !== value) {
3836
return interpolate(parsed[key], processEnv, parsed)
3937
}
4038
}

tests/main.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,20 @@ t.test('expands self without a recursive call stack error', ct => {
4242
}
4343
const parsed = dotenvExpand.expand(dotenv).parsed
4444

45-
ct.equal(parsed.EXPAND_SELF, '$EXPAND_SELF') // because it ends up accessing parsed[key].
45+
ct.equal(parsed.EXPAND_SELF, '') // because it ends up accessing parsed[key].
46+
47+
ct.end()
48+
})
49+
50+
t.test('expands self with undefined variable and default value', ct => {
51+
const dotenv = {
52+
parsed: {
53+
EXPAND_SELF: '${EXPAND_SELF:-default}'
54+
}
55+
}
56+
const parsed = dotenvExpand.expand(dotenv).parsed
57+
58+
ct.equal(parsed.EXPAND_SELF, 'default')
4659

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

469-
ct.equal(parsed.EXPAND_SELF, '$EXPAND_SELF') // because it ends up accessing parsed[key].
482+
ct.equal(parsed.EXPAND_SELF, '') // because it ends up accessing parsed[key].
470483

471484
ct.end()
472485
})

0 commit comments

Comments
 (0)