Skip to content

Commit f63c011

Browse files
authored
Merge pull request #121 from motdotla/expansion
demonstrate expansion from prior process.env
2 parents 3f5116c + e980331 commit f63c011

File tree

6 files changed

+57
-30
lines changed

6 files changed

+57
-30
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
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.
44

5-
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v11.0.4...master)
5+
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v11.0.5...master)
6+
7+
## [11.0.5](https://github.com/motdotla/dotenv-expand/compare/v11.0.4...v11.0.5) (2024-02-17)
8+
9+
### Changed
10+
11+
- 🐞 fix recursive expansion when expansion key is sourced from `process.env` ([#121](https://github.com/motdotla/dotenv-expand/pull/121))
612

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

lib/main.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ function interpolate (value, processEnv, parsed) {
2222
return match.slice(1)
2323
} else {
2424
if (processEnv[key]) {
25-
return processEnv[key]
25+
if (processEnv[key] === parsed[key]) {
26+
return processEnv[key]
27+
} else {
28+
// scenario: PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
29+
return interpolate(processEnv[key], processEnv, parsed)
30+
}
2631
}
2732

2833
if (parsed[key]) {
@@ -57,7 +62,6 @@ function expand (options) {
5762
let value = options.parsed[key]
5863

5964
const inProcessEnv = Object.prototype.hasOwnProperty.call(processEnv, key)
60-
6165
if (inProcessEnv) {
6266
if (processEnv[key] === options.parsed[key]) {
6367
// assume was set to processEnv from the .env file if the values match and therefore interpolate

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@
5151
"node": ">=12"
5252
},
5353
"dependencies": {
54-
"dotenv": "^16.4.1"
54+
"dotenv": "^16.4.4"
5555
}
5656
}

tests/.env.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ EXPAND_SELF=$EXPAND_SELF
7272
# https://github.com/motdotla/dotenv-expand/issues/112#issuecomment-1937330651
7373
HOST="something"
7474
DOMAIN="https://${HOST}"
75+
76+
# https://github.com/motdotla/dotenv-expand/issues/120
77+
PASSWORD=password
78+
PASSWORD_EXPAND=${PASSWORD}
79+
PASSWORD_EXPAND_SIMPLE=$PASSWORD
80+
PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
81+
PASSWORD_EXPAND_NESTED_NESTED=${PASSWORD_EXPAND_NESTED}

tests/main.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,6 @@ t.test('uses environment variables existing already on the machine for expansion
6363
ct.end()
6464
})
6565

66-
t.test('does not expand environment variables existing already on the machine that look like they could expand', ct => {
67-
process.env.PASSWORD = 'pas$word'
68-
const dotenv = {
69-
parsed: {
70-
PASSWORD: 'dude',
71-
PASSWORD_EXPAND: '${PASSWORD}',
72-
PASSWORD_EXPAND_SIMPLE: '$PASSWORD'
73-
}
74-
}
75-
const parsed = dotenvExpand.expand(dotenv).parsed
76-
77-
ct.equal(parsed.PASSWORD_EXPAND, 'pas$word')
78-
ct.equal(parsed.PASSWORD_EXPAND_SIMPLE, 'pas$word')
79-
ct.equal(parsed.PASSWORD, 'pas$word')
80-
81-
ct.end()
82-
})
83-
8466
t.test('expands missing environment variables to an empty string', ct => {
8567
const dotenv = {
8668
parsed: {
@@ -556,3 +538,31 @@ t.test('expands recursively reverse order', ct => {
556538

557539
ct.end()
558540
})
541+
542+
t.test('expands recursively', ct => {
543+
const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
544+
dotenvExpand.expand(dotenv)
545+
546+
ct.equal(process.env.PASSWORD_EXPAND, 'password')
547+
ct.equal(process.env.PASSWORD_EXPAND_SIMPLE, 'password')
548+
ct.equal(process.env.PASSWORD, 'password')
549+
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'password')
550+
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'password')
551+
552+
ct.end()
553+
})
554+
555+
t.test('expands recursively but is smart enough to not attempt expansion of a pre-set env in process.env', ct => {
556+
process.env.PASSWORD = 'pas$word'
557+
558+
const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
559+
dotenvExpand.expand(dotenv)
560+
561+
ct.equal(process.env.PASSWORD_EXPAND, 'pas$word')
562+
ct.equal(process.env.PASSWORD_EXPAND_SIMPLE, 'pas$word')
563+
ct.equal(process.env.PASSWORD, 'pas$word')
564+
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'pas$word')
565+
ct.equal(process.env.PASSWORD_EXPAND_NESTED, 'pas$word')
566+
567+
ct.end()
568+
})

0 commit comments

Comments
 (0)