From b680e4a219618ce88be2e4e3b7816f13fdf2d8da Mon Sep 17 00:00:00 2001 From: Stefan Stefanov Date: Mon, 3 Jun 2024 22:56:56 +0300 Subject: [PATCH] Fix self-expanding undefined variable with default value. --- lib/main.js | 4 +--- tests/main.js | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/main.js b/lib/main.js index 33e38db..e57074e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -32,9 +32,7 @@ function interpolate (value, processEnv, parsed) { if (parsed[key]) { // avoid recursion from EXPAND_SELF=$EXPAND_SELF - if (parsed[key] === value) { - return parsed[key] - } else { + if (parsed[key] !== value) { return interpolate(parsed[key], processEnv, parsed) } } diff --git a/tests/main.js b/tests/main.js index 3db8947..f8f7755 100644 --- a/tests/main.js +++ b/tests/main.js @@ -42,7 +42,20 @@ t.test('expands self without a recursive call stack error', ct => { } const parsed = dotenvExpand.expand(dotenv).parsed - ct.equal(parsed.EXPAND_SELF, '$EXPAND_SELF') // because it ends up accessing parsed[key]. + ct.equal(parsed.EXPAND_SELF, '') // because it ends up accessing parsed[key]. + + ct.end() +}) + +t.test('expands self with undefined variable and default value', ct => { + const dotenv = { + parsed: { + EXPAND_SELF: '${EXPAND_SELF:-default}' + } + } + const parsed = dotenvExpand.expand(dotenv).parsed + + ct.equal(parsed.EXPAND_SELF, 'default') ct.end() }) @@ -466,7 +479,7 @@ 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 - ct.equal(parsed.EXPAND_SELF, '$EXPAND_SELF') // because it ends up accessing parsed[key]. + ct.equal(parsed.EXPAND_SELF, '') // because it ends up accessing parsed[key]. ct.end() })