diff --git a/CHANGELOG.md b/CHANGELOG.md index 2135a11a..d8c244ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. See [standa ### Changed * Simplify automatic loading: use `import 'dotenv/config'` or `dotenv run -- yourcommand` ([#1035](https://github.com/motdotla/dotenv/pull/1035)) +* Injecting message sent to stderr rather than stdout ([#1037](https://github.com/motdotla/dotenv/pull/1037)) ### Removed diff --git a/cli.js b/cli.js index ab738577..d5b9271d 100755 --- a/cli.js +++ b/cli.js @@ -137,7 +137,7 @@ function run (argv) { if (result.loadedPaths.length > 0) { message += ` from ${result.loadedPaths.join(', ')}` } - console.log(message) + console.error(message) } } catch (e) { console.error(`dotenv: ${e.message}`) diff --git a/lib/main.js b/lib/main.js index 3eb71113..0bc2d618 100644 --- a/lib/main.js +++ b/lib/main.js @@ -55,7 +55,7 @@ function _debug (message) { } function _log (message) { - console.log(`◇ ${message}`) + console.error(`◇ ${message}`) } function _resolveHome (envPath) { diff --git a/tests/test-cli.js b/tests/test-cli.js index 9e8ef1d5..053125ab 100644 --- a/tests/test-cli.js +++ b/tests/test-cli.js @@ -47,8 +47,8 @@ t.test('dotenv run loads .env by default', ct => { removeDir(cwd) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (1) from .env\nbasic\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, 'basic\n') + ct.equal(result.stderr, '◇ injected env (1) from .env\n') ct.end() }) @@ -68,8 +68,8 @@ t.test('dotenv run continues when default .env is missing', ct => { removeDir(cwd) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (0)\nok\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, 'ok\n') + ct.equal(result.stderr, '◇ injected env (0)\n') ct.end() }) @@ -85,8 +85,8 @@ t.test('dotenv run supports -f path', ct => { ]) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (2) from ./tests/.env.local\nlocal_basic\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, 'local_basic\n') + ct.equal(result.stderr, '◇ injected env (2) from ./tests/.env.local\n') ct.end() }) @@ -101,8 +101,8 @@ t.test('dotenv run supports -f=path', ct => { ]) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (2) from ./tests/.env.local\nlocal_basic\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, 'local_basic\n') + ct.equal(result.stderr, '◇ injected env (2) from ./tests/.env.local\n') ct.end() }) @@ -120,8 +120,8 @@ t.test('dotenv run supports multiple -f paths without override', ct => { ]) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (41) from ./tests/.env.local, ./tests/.env\nlocal_basic\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, 'local_basic\n') + ct.equal(result.stderr, '◇ injected env (41) from ./tests/.env.local, ./tests/.env\n') ct.end() }) @@ -142,8 +142,8 @@ t.test('dotenv run does not override existing environment variables', ct => { ) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (39) from ./tests/.env\nexisting\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, 'existing\n') + ct.equal(result.stderr, '◇ injected env (39) from ./tests/.env\n') ct.end() }) @@ -164,8 +164,8 @@ t.test('dotenv run does not expand variables', ct => { removeDir(cwd) ct.equal(result.status, 0) - ct.equal(result.stdout, '◇ injected env (2) from .env\n$BASIC\n') - ct.equal(result.stderr, '') + ct.equal(result.stdout, '$BASIC\n') + ct.equal(result.stderr, '◇ injected env (2) from .env\n') ct.end() }) diff --git a/tests/test-config.js b/tests/test-config.js index 818a60eb..8227137f 100644 --- a/tests/test-config.js +++ b/tests/test-config.js @@ -7,14 +7,17 @@ const t = require('tap') const dotenv = require('../lib/main') let logStub +let errorStub t.beforeEach(() => { logStub = null + errorStub = null delete process.env.BASIC // reset }) t.afterEach(() => { if (logStub) logStub.restore() + if (errorStub) errorStub.restore() delete process.env.DOTENV_CONFIG_ENCODING delete process.env.DOTENV_CONFIG_PATH delete process.env.DOTENV_CONFIG_QUIET @@ -27,12 +30,12 @@ t.test('uses DOTENV_CONFIG_* values as config defaults', ct => { process.env.DOTENV_CONFIG_QUIET = 'true' process.env.DOTENV_CONFIG_OVERRIDE = 'true' const processEnv = { BASIC: 'existing' } - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config({ processEnv }) ct.equal(processEnv.BASIC, 'local_basic') - ct.ok(logStub.notCalled) + ct.ok(errorStub.notCalled) ct.end() }) @@ -41,12 +44,12 @@ t.test('config options override DOTENV_CONFIG_* defaults', ct => { process.env.DOTENV_CONFIG_QUIET = 'true' process.env.DOTENV_CONFIG_OVERRIDE = 'true' const processEnv = { BASIC: 'existing' } - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config({ path: 'tests/.env', quiet: false, override: false, processEnv }) ct.equal(processEnv.BASIC, 'existing') - ct.ok(logStub.called) + ct.ok(errorStub.called) ct.end() }) @@ -287,7 +290,7 @@ t.test('logs any errors parsing when in debug and override mode', ct => { }) t.test('deals with file:// path', ct => { - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') const testPath = 'file:///tests/.env' const env = dotenv.config({ path: testPath }) @@ -296,7 +299,7 @@ t.test('deals with file:// path', ct => { ct.equal(process.env.BASIC, undefined) ct.equal(env.error.message, "ENOENT: no such file or directory, open 'file:///tests/.env'") - ct.ok(logStub.called) + ct.ok(errorStub.called) ct.end() }) @@ -336,62 +339,62 @@ t.test('path.relative fails somehow', ct => { t.test('displays the injected env message without tips', ct => { ct.plan(1) - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') const testPath = 'tests/.env' dotenv.config({ path: testPath }) - ct.match(logStub.firstCall.args[0], /^◇ injected env \(\d+\) from tests\/\.env$/) + ct.match(errorStub.firstCall.args[0], /^◇ injected env \(\d+\) from tests\/\.env$/) ct.end() }) t.test('logs when no path is set', ct => { ct.plan(1) - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config() - ct.ok(logStub.called) + ct.ok(errorStub.called) }) t.test('does log by default', ct => { ct.plan(1) const testPath = 'tests/.env' - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config({ path: testPath }) - ct.ok(logStub.called) + ct.ok(errorStub.called) }) t.test('does not log if quiet flag passed true', ct => { ct.plan(1) const testPath = 'tests/.env' - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config({ path: testPath, quiet: true }) - ct.ok(logStub.notCalled) + ct.ok(errorStub.notCalled) }) t.test('does log if quiet flag false', ct => { ct.plan(1) const testPath = 'tests/.env' - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config({ path: testPath, quiet: false }) - ct.ok(logStub.called) + ct.ok(errorStub.called) }) t.test('does log if quiet flag present and undefined/null', ct => { ct.plan(1) const testPath = 'tests/.env' - logStub = sinon.stub(console, 'log') + errorStub = sinon.stub(console, 'error') dotenv.config({ path: testPath, quiet: undefined }) - ct.ok(logStub.called) + ct.ok(errorStub.called) }) t.test('logs if debug set', ct => {