Skip to content

Commit

Permalink
Merge pull request #118 from motdotla/expand-test
Browse files Browse the repository at this point in the history
test showing failure of recursive expansion
  • Loading branch information
motdotla committed Feb 15, 2024
2 parents 1e19f1e + c2654e9 commit 60acad3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
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.3...master)
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v11.0.4...master)

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

### Changed

- 🐞 fix recursive expansion when expansion keys in reverse order ([#118](https://github.com/motdotla/dotenv-expand/pull/118))

## [11.0.3](https://github.com/motdotla/dotenv-expand/compare/v11.0.2...v11.0.3) (2024-02-11)

Expand Down
7 changes: 6 additions & 1 deletion lib/main.js
Expand Up @@ -26,7 +26,12 @@ function interpolate (value, processEnv, parsed) {
}

if (parsed[key]) {
return parsed[key]
// avoid recursion from EXPAND_SELF=$EXPAND_SELF
if (parsed[key] === value) {
return parsed[key]
} else {
return interpolate(parsed[key], processEnv, parsed)
}
}

if (defaultValue) {
Expand Down
51 changes: 49 additions & 2 deletions tests/main.js
Expand Up @@ -34,6 +34,19 @@ t.test('expands environment variables', ct => {
ct.end()
})

t.test('expands self without a recursive call stack error', ct => {
const dotenv = {
parsed: {
EXPAND_SELF: '$EXPAND_SELF'
}
}
const parsed = dotenvExpand.expand(dotenv).parsed

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

ct.end()
})

t.test('uses environment variables existing already on the machine for expansion', ct => {
process.env.MACHINE = 'machine'
const dotenv = {
Expand Down Expand Up @@ -163,7 +176,7 @@ t.test('handle mixed values', ct => {
ct.end()
})

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

Expand Down Expand Up @@ -467,7 +480,7 @@ t.test('does not choke', ct => {
ct.end()
})

t.test('expands self without a recursive call stack error', ct => {
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

Expand Down Expand Up @@ -509,3 +522,37 @@ t.test('does not expand dollar sign that are not variables', ct => {

ct.end()
})

t.test('expands recursively', ct => {
const dotenv = {
parsed: {
MOCK_SERVER_PORT: '8090',
MOCK_SERVER_HOST: 'http://localhost:${MOCK_SERVER_PORT}',
BACKEND_API_HEALTH_CHECK_URL: '${MOCK_SERVER_HOST}/ci-health-check'
}
}
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.MOCK_SERVER_PORT, '8090')
ct.equal(parsed.MOCK_SERVER_HOST, 'http://localhost:8090')
ct.equal(parsed.BACKEND_API_HEALTH_CHECK_URL, 'http://localhost:8090/ci-health-check')

ct.end()
})

t.test('expands recursively reverse order', ct => {
const dotenv = {
parsed: {
BACKEND_API_HEALTH_CHECK_URL: '${MOCK_SERVER_HOST}/ci-health-check',
MOCK_SERVER_HOST: 'http://localhost:${MOCK_SERVER_PORT}',
MOCK_SERVER_PORT: '8090'
}
}
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.MOCK_SERVER_PORT, '8090')
ct.equal(parsed.MOCK_SERVER_HOST, 'http://localhost:8090')
ct.equal(parsed.BACKEND_API_HEALTH_CHECK_URL, 'http://localhost:8090/ci-health-check')

ct.end()
})

0 comments on commit 60acad3

Please sign in to comment.