Skip to content

Commit

Permalink
Merge pull request #113 from motdotla/process-env
Browse files Browse the repository at this point in the history
fix tests to show failures
  • Loading branch information
motdotla committed Feb 11, 2024
2 parents c73d8a1 + 7dc56d1 commit 2ea51ad
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 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.2...master)
## [Unreleased](https://github.com/motdotla/dotenv-expand/compare/v11.0.3...master)

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

### Changed

- 🐞 bug fix when `processEnv` set to process.env rather than empty object (also test fixes which hid the bug) ([#113](https://github.com/motdotla/dotenv-expand/pull/113))

## [11.0.2](https://github.com/motdotla/dotenv-expand/compare/v11.0.1...v11.0.2) (2024-02-10)

Expand Down
20 changes: 16 additions & 4 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function interpolate (value, processEnv, parsed) {
return processEnv[key]
}

if (parsed[key]) {
return parsed[key]
}

if (defaultValue) {
if (defaultValue.startsWith('$')) {
return interpolate(defaultValue, processEnv, parsed)
Expand All @@ -33,7 +37,7 @@ function interpolate (value, processEnv, parsed) {
}
}

return parsed[key] || ''
return ''
}
})
}
Expand All @@ -47,10 +51,18 @@ function expand (options) {
for (const key in options.parsed) {
let value = options.parsed[key]

// don't interpolate if it exists already in processEnv
if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
value = processEnv[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
value = interpolate(value, processEnv, options.parsed)
} else {
// do not interpolate - assume processEnv had the intended value even if containing a $.
value = processEnv[key]
}
} else {
// not inProcessEnv so assume interpolation for this .env key
value = interpolate(value, processEnv, options.parsed)
}

Expand Down
4 changes: 4 additions & 0 deletions tests/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ DONT_CHOKE8='@}:[4#g%[R-CFR});bY(Z[KcDQDsVn2_y4cSdU<Mjy!c^F`G<!Ks7]kbS]N1:bP:'

# https://github.com/motdotla/dotenv-expand/issues/98
EXPAND_SELF=$EXPAND_SELF

# https://github.com/motdotla/dotenv-expand/issues/112#issuecomment-1937330651
HOST="something"
DOMAIN="https://${HOST}"
83 changes: 79 additions & 4 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
const t = require('tap')
const dotenvExpand = require('../lib/main')

t.beforeEach((ct) => {
// important, clear process.env before each test
process.env = {}
})

t.test('returns object', ct => {
const dotenv = { parsed: {} }
const parsed = dotenvExpand.expand(dotenv).parsed
Expand Down Expand Up @@ -167,6 +172,15 @@ t.test('expands environment variables', ct => {
ct.end()
})

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

ct.equal(process.env.BASIC_EXPAND, 'basic')

ct.end()
})

t.test('expands environment variables existing already on the machine', ct => {
process.env.MACHINE = 'machine'

Expand All @@ -178,6 +192,17 @@ t.test('expands environment variables existing already on the machine', ct => {
ct.end()
})

t.test('expands environment variables existing already on the machine (process.env)', ct => {
process.env.MACHINE = 'machine'

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

ct.equal(process.env.MACHINE_EXPAND, 'machine')

ct.end()
})

t.test('expands missing environment variables to an empty string', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test', processEnv: {} })
const parsed = dotenvExpand.expand(dotenv).parsed
Expand All @@ -187,6 +212,15 @@ t.test('expands missing environment variables to an empty string', ct => {
ct.end()
})

t.test('expands missing environment variables to an empty string (process.env)', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.UNDEFINED_EXPAND, '')

ct.end()
})

t.test('expands environment variables existing already on the machine even with a default', ct => {
process.env.MACHINE = 'machine'

Expand Down Expand Up @@ -235,8 +269,18 @@ t.test('expands environent variables and concats with default nested', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test', processEnv: {} })
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_DEFAULT_NESTED_TWICE, 'machinedefault')
ct.equal(parsed.EXPAND_DEFAULT_NESTED_TWICE2, 'machinedefault')
ct.equal(parsed.EXPAND_DEFAULT_NESTED_TWICE, 'machine_envdefault')
ct.equal(parsed.EXPAND_DEFAULT_NESTED_TWICE2, 'machine_envdefault')

ct.end()
})

t.test('expands environent variables and concats with default nested', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_DEFAULT_NESTED_TWICE, 'machine_envdefault')
ct.equal(parsed.EXPAND_DEFAULT_NESTED_TWICE2, 'machine_envdefault')

ct.end()
})
Expand Down Expand Up @@ -330,8 +374,18 @@ t.test('expands environment variables existing already on the machine even with
const dotenv = require('dotenv').config({ path: 'tests/.env.test', processEnv: {} })
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_DEFAULT_SPECIAL_CHARACTERS, 'machine')
ct.equal(parsed.EXPAND_DEFAULT_SPECIAL_CHARACTERS2, 'machine')
ct.equal(parsed.EXPAND_DEFAULT_SPECIAL_CHARACTERS, 'machine_env')
ct.equal(parsed.EXPAND_DEFAULT_SPECIAL_CHARACTERS2, 'machine_env')

ct.end()
})

t.test('expands environment variables existing already on the machine even with a default with special characters (process.env)', ct => {
const dotenv = require('dotenv').config({ path: 'tests/.env.test' })
const parsed = dotenvExpand.expand(dotenv).parsed

ct.equal(parsed.EXPAND_DEFAULT_SPECIAL_CHARACTERS, 'machine_env')
ct.equal(parsed.EXPAND_DEFAULT_SPECIAL_CHARACTERS2, 'machine_env')

ct.end()
})
Expand Down Expand Up @@ -421,3 +475,24 @@ t.test('expands self without a recursive call stack error', ct => {

ct.end()
})

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

ct.equal(parsed.HOST, 'something')
ct.equal(parsed.DOMAIN, 'https://something')

ct.end()
})

t.test('does not attempt to expand password if already existed in processEnv', ct => {
process.env.PASSWORD = 'pas$word'

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

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

ct.end()
})

0 comments on commit 2ea51ad

Please sign in to comment.