Skip to content

Commit

Permalink
Merge pull request #121 from motdotla/expansion
Browse files Browse the repository at this point in the history
demonstrate expansion from prior process.env
  • Loading branch information
motdotla authored Feb 18, 2024
2 parents 3f5116c + e980331 commit f63c011
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 30 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v11.0.4...master)
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v11.0.5...master)

## [11.0.5](https://github.com/motdotla/dotenv-expand/compare/v11.0.4...v11.0.5) (2024-02-17)

### Changed

- 🐞 fix recursive expansion when expansion key is sourced from `process.env` ([#121](https://github.com/motdotla/dotenv-expand/pull/121))

## [11.0.4](https://github.com/motdotla/dotenv-expand/compare/v11.0.3...v11.0.4) (2024-02-15)

Expand Down
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"
}
}
7 changes: 7 additions & 0 deletions tests/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ EXPAND_SELF=$EXPAND_SELF
# https://github.com/motdotla/dotenv-expand/issues/112#issuecomment-1937330651
HOST="something"
DOMAIN="https://${HOST}"

# 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}
46 changes: 28 additions & 18 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 @@ -556,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 f63c011

Please sign in to comment.