Skip to content

Commit

Permalink
fix recursive expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
motdotla committed Feb 18, 2024
1 parent 8fdcb93 commit 678f008
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 56 deletions.
8 changes: 6 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ function interpolate (value, processEnv, parsed) {
return match.slice(1)
} else {
if (processEnv[key]) {
return processEnv[key]
if (processEnv[key] === parsed[key]) {
return processEnv[key]
} else {
// scenario: PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
return interpolate(processEnv[key], processEnv, parsed)
}
}

if (parsed[key]) {
Expand Down Expand Up @@ -57,7 +62,6 @@ function expand (options) {
let value = options.parsed[key]

const inProcessEnv = Object.prototype.hasOwnProperty.call(processEnv, key)

if (inProcessEnv) {
if (processEnv[key] === options.parsed[key]) {
// assume was set to processEnv from the .env file if the values match and therefore interpolate
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@
"node": ">=12"
},
"dependencies": {
"dotenv": "^16.4.1"
"dotenv": "^16.4.4"
}
}
10 changes: 6 additions & 4 deletions tests/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ EXPAND_SELF=$EXPAND_SELF
HOST="something"
DOMAIN="https://${HOST}"

https://github.com/motdotla/dotenv-expand/issues/120
BASE_URL=https://${APP_DOMAIN}/api/
PUBLIC_DOMAIN="${DEVCONTAINER_CADDY_PUBLIC_DOMAIN}"
PUBLIC_BASE_URL="https://${PUBLIC_DOMAIN}"
# https://github.com/motdotla/dotenv-expand/issues/120
PASSWORD=password
PASSWORD_EXPAND=${PASSWORD}
PASSWORD_EXPAND_SIMPLE=$PASSWORD
PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
PASSWORD_EXPAND_NESTED_NESTED=${PASSWORD_EXPAND_NESTED}
69 changes: 28 additions & 41 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,6 @@ t.test('uses environment variables existing already on the machine for expansion
ct.end()
})

t.test('does not expand environment variables existing already on the machine that look like they could expand', ct => {
process.env.PASSWORD = 'pas$word'
const dotenv = {
parsed: {
PASSWORD: 'dude',
PASSWORD_EXPAND: '${PASSWORD}',
PASSWORD_EXPAND_SIMPLE: '$PASSWORD'
}
}
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.PASSWORD_EXPAND, 'pas$word')
ct.equal(parsed.PASSWORD_EXPAND_SIMPLE, 'pas$word')
ct.equal(parsed.PASSWORD, 'pas$word')

ct.end()
})

t.test('expands missing environment variables to an empty string', ct => {
const dotenv = {
parsed: {
Expand Down Expand Up @@ -510,29 +492,6 @@ t.test('does not attempt to expand password if already existed in processEnv', c
ct.end()
})

t.test('expands using processEnv already set on docker machine', ct => {
process.env.APP_DOMAIN = 'example.com' // simulate env set on docker already

const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
dotenvExpand.expand(dotenv)

ct.equal(process.env.BASE_URL, 'https://example.com/api/')

ct.end()
})

t.test('recursively expands using processEnv already set on docker machine', ct => {
process.env.DEVCONTAINER_CADDY_PUBLIC_DOMAIN = 'mydom.test' // simulate env set on docker already

const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
dotenvExpand.expand(dotenv)

ct.equal(process.env.PUBLIC_DOMAIN, 'mydom.test')
ct.equal(process.env.PUBLIC_BASE_URL, 'https://mydom.test')

ct.end()
})

t.test('does not expand dollar sign that are not variables', ct => {
const dotenv = {
parsed: {
Expand Down Expand Up @@ -579,3 +538,31 @@ t.test('expands recursively reverse order', ct => {

ct.end()
})

t.test('expands recursively', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
dotenvExpand.expand(dotenv)

ct.equal(process.env.PASSWORD_EXPAND, 'password')
ct.equal(process.env.PASSWORD_EXPAND_SIMPLE, 'password')
ct.equal(process.env.PASSWORD, 'password')
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'password')
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'password')

ct.end()
})

t.test('expands recursively but is smart enough to not attempt expansion of a pre-set env in process.env', ct => {
process.env.PASSWORD = 'pas$word'

const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
dotenvExpand.expand(dotenv)

ct.equal(process.env.PASSWORD_EXPAND, 'pas$word')
ct.equal(process.env.PASSWORD_EXPAND_SIMPLE, 'pas$word')
ct.equal(process.env.PASSWORD, 'pas$word')
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'pas$word')
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'pas$word')

ct.end()
})

0 comments on commit 678f008

Please sign in to comment.