Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test showing failure of recursive expansion #118

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
})
Loading