From 0e69a09a4e05858e279b3bcd54f7722ad85361e3 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Thu, 9 Aug 2018 15:35:46 +0530 Subject: [PATCH 1/4] test: add a common.debuglog function --- test/common/README.md | 7 +++++++ test/common/index.js | 3 +++ test/common/index.mjs | 6 ++++-- test/parallel/test-common-debuglog.js | 21 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-common-debuglog.js diff --git a/test/common/README.md b/test/common/README.md index 36f3afb5c9a465..445c4ba26e6bba 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -57,6 +57,13 @@ On non-Windows platforms, this always returns `true`. Platform normalizes the `dd` command +### debuglog([fmt, ]msg[, msgs]) +* `fmt` [<string>] Optional format string +* `msg` [<string>] The string to be printed to stderr +* `msgs` [<string>] Additional arguments to the function + +Prints messages (optionally, formatted using `fmt`) to `process.stderr`, using `util.debuglog('test')`. + ### disableCrashOnUnhandledRejection() Removes the `process.on('unhandledRejection')` handler that crashes the process diff --git a/test/common/index.js b/test/common/index.js index 18f102c5c3a637..afe0070532570a 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -804,3 +804,6 @@ exports.runWithInvalidFD = function(func) { exports.printSkipMessage('Could not generate an invalid fd'); }; + +const utilDebugLog = util.debuglog('test'); +exports.debuglog = utilDebugLog; diff --git a/test/common/index.mjs b/test/common/index.mjs index c8e6295b5ca1c6..d3fffe1a012caa 100644 --- a/test/common/index.mjs +++ b/test/common/index.mjs @@ -51,7 +51,8 @@ const { getBufferSources, disableCrashOnUnhandledRejection, getTTYfd, - runWithInvalidFD + runWithInvalidFD, + debuglog, } = common; export { @@ -103,5 +104,6 @@ export { getBufferSources, disableCrashOnUnhandledRejection, getTTYfd, - runWithInvalidFD + runWithInvalidFD, + debuglog, }; diff --git a/test/parallel/test-common-debuglog.js b/test/parallel/test-common-debuglog.js new file mode 100644 index 00000000000000..657818f6703bc3 --- /dev/null +++ b/test/parallel/test-common-debuglog.js @@ -0,0 +1,21 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); + +const message = 'message'; +const debugEnv = (process.env.NODE_DEBUG || '').split(','); + +if (!debugEnv.includes('test')) { + process.env.NODE_DEBUG = 'test,' + (process.env.NODE_DEBUG || ''); +} + +const listener = common.mustCallAtLeast((data) => { + assert.ok(data.startsWith('TEST')); + assert.ok(data.endsWith(message + '\n')); +}, 1); + +common.hijackStderr(listener); +common.debuglog(message); +assert.ok(process.stderr.writeTimes, 'not called!'); +common.restoreStderr(); From 1c5cbb5c4700e78612f62ba0b3154ec388a488aa Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Thu, 9 Aug 2018 17:57:41 +0530 Subject: [PATCH 2/4] test: add tests for common.debuglog --- test/parallel/test-common-debuglog-dummy.js | 6 +++++ test/parallel/test-common-debuglog.js | 27 +++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 test/parallel/test-common-debuglog-dummy.js diff --git a/test/parallel/test-common-debuglog-dummy.js b/test/parallel/test-common-debuglog-dummy.js new file mode 100644 index 00000000000000..27ffc3ec7dc66e --- /dev/null +++ b/test/parallel/test-common-debuglog-dummy.js @@ -0,0 +1,6 @@ +'use strict'; + +const common = require('../common'); + +// argv[0] is the execPath, argv[1] is the test script filename +common.debuglog.apply(null, process.argv.slice(2)); diff --git a/test/parallel/test-common-debuglog.js b/test/parallel/test-common-debuglog.js index 657818f6703bc3..feca4afca96101 100644 --- a/test/parallel/test-common-debuglog.js +++ b/test/parallel/test-common-debuglog.js @@ -1,21 +1,18 @@ 'use strict'; -const common = require('../common'); +const common = require('../common'); // eslint-disable-line no-unused-vars const assert = require('assert'); +const path = require('path'); +const { spawnSync } = require('child_process'); -const message = 'message'; -const debugEnv = (process.env.NODE_DEBUG || '').split(','); +{ + const message = 'message'; + process.env.NODE_DEBUG = 'test'; + const { stderr } = spawnSync(process.execPath, [ + path.resolve(__dirname, 'test-common-debuglog-dummy.js'), + message + ], { encoding: 'utf8' }); -if (!debugEnv.includes('test')) { - process.env.NODE_DEBUG = 'test,' + (process.env.NODE_DEBUG || ''); + assert.ok(stderr.startsWith('TEST')); + assert.ok(stderr.endsWith(`${message}\n`)); } - -const listener = common.mustCallAtLeast((data) => { - assert.ok(data.startsWith('TEST')); - assert.ok(data.endsWith(message + '\n')); -}, 1); - -common.hijackStderr(listener); -common.debuglog(message); -assert.ok(process.stderr.writeTimes, 'not called!'); -common.restoreStderr(); From a9fb74aee37333b872a1fed28ca6d2806e8ebd33 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Fri, 10 Aug 2018 10:39:43 +0530 Subject: [PATCH 3/4] test: incorporating suggested changes for common.debuglog --- test/common/README.md | 3 +- .../common-debuglog.js} | 0 test/parallel/test-common-debuglog.js | 31 ++++++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) rename test/{parallel/test-common-debuglog-dummy.js => fixtures/common-debuglog.js} (100%) diff --git a/test/common/README.md b/test/common/README.md index 445c4ba26e6bba..35e0ae8da68fc9 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -62,7 +62,8 @@ Platform normalizes the `dd` command * `msg` [<string>] The string to be printed to stderr * `msgs` [<string>] Additional arguments to the function -Prints messages (optionally, formatted using `fmt`) to `process.stderr`, using `util.debuglog('test')`. +Prints messages (optionally, formatted using `fmt`) to `process.stderr`, +using `util.debuglog('test')`. ### disableCrashOnUnhandledRejection() diff --git a/test/parallel/test-common-debuglog-dummy.js b/test/fixtures/common-debuglog.js similarity index 100% rename from test/parallel/test-common-debuglog-dummy.js rename to test/fixtures/common-debuglog.js diff --git a/test/parallel/test-common-debuglog.js b/test/parallel/test-common-debuglog.js index feca4afca96101..31a5b2f7a82067 100644 --- a/test/parallel/test-common-debuglog.js +++ b/test/parallel/test-common-debuglog.js @@ -1,18 +1,39 @@ 'use strict'; -const common = require('../common'); // eslint-disable-line no-unused-vars +require('../common'); const assert = require('assert'); const path = require('path'); const { spawnSync } = require('child_process'); +const message = 'message'; + { - const message = 'message'; process.env.NODE_DEBUG = 'test'; const { stderr } = spawnSync(process.execPath, [ - path.resolve(__dirname, 'test-common-debuglog-dummy.js'), + path.resolve(__dirname, '../fixtures/common-debuglog.js'), + message + ], { encoding: 'utf8' }); + + assert.ok(stderr.toString().startsWith('TEST')); + assert.ok(stderr.toString().trim().endsWith(message)); +} + +{ + delete process.env.NODE_DEBUG; + const { stderr } = spawnSync(process.execPath, [ + path.resolve(__dirname, '../fixtures/common-debuglog.js'), + message + ], { encoding: 'utf8' }); + + assert.strictEqual(stderr.toString().trim(), ''); +} + +{ + process.env.NODE_DEBUG = 'fs'; + const { stderr } = spawnSync(process.execPath, [ + path.resolve(__dirname, '../fixtures/common-debuglog.js'), message ], { encoding: 'utf8' }); - assert.ok(stderr.startsWith('TEST')); - assert.ok(stderr.endsWith(`${message}\n`)); + assert.strictEqual(stderr.toString().trim(), ''); } From 93aa055ea4259f302ed6a6bf9a3e12650dc0fca4 Mon Sep 17 00:00:00 2001 From: Sarat Addepalli Date: Fri, 10 Aug 2018 12:20:55 +0530 Subject: [PATCH 4/4] test: add docs for common.debuglog behaviour --- test/common/README.md | 16 +++++++++++++--- test/fixtures/common-debuglog.js | 2 +- test/parallel/test-common-debuglog.js | 8 ++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/test/common/README.md b/test/common/README.md index 35e0ae8da68fc9..1256f315cc9084 100644 --- a/test/common/README.md +++ b/test/common/README.md @@ -59,11 +59,20 @@ Platform normalizes the `dd` command ### debuglog([fmt, ]msg[, msgs]) * `fmt` [<string>] Optional format string -* `msg` [<string>] The string to be printed to stderr +* `msg` [<string>] The string to be printed * `msgs` [<string>] Additional arguments to the function -Prints messages (optionally, formatted using `fmt`) to `process.stderr`, -using `util.debuglog('test')`. +Prints messages (optionally, formatted using `fmt`). Since [`util.debuglog()`] +is used internally (invoked as `util.debuglog('test')`), these messages will be +visible only when `NODE_DEBUG` environment variable is set with `test`. + +```sh +// when NODE_DEBUG is not set, or doesn\'t contain `test`, prints nothing. +$ NODE_DEBUG=fs,net ./node test/fixtures/common-debuglog.js hello world +// when NODE_DEBUG contains `test`, messages are printed to `process.stderr` +$ NODE_DEBUG=test,fs,net ./node test/fixtures/common-debuglog.js hello world +TEST 89440: hello world +``` ### disableCrashOnUnhandledRejection() @@ -810,3 +819,4 @@ implementation with tests from [`hijackstdio.hijackStdErr()`]: #hijackstderrlistener [`hijackstdio.hijackStdOut()`]: #hijackstdoutlistener [internationalization]: https://github.com/nodejs/node/wiki/Intl +[`util.debuglog()`]: https://nodejs.org/api/util.html#util_util_debuglog_section diff --git a/test/fixtures/common-debuglog.js b/test/fixtures/common-debuglog.js index 27ffc3ec7dc66e..37467e58c20ca2 100644 --- a/test/fixtures/common-debuglog.js +++ b/test/fixtures/common-debuglog.js @@ -3,4 +3,4 @@ const common = require('../common'); // argv[0] is the execPath, argv[1] is the test script filename -common.debuglog.apply(null, process.argv.slice(2)); +common.debuglog(...process.argv.slice(2)); diff --git a/test/parallel/test-common-debuglog.js b/test/parallel/test-common-debuglog.js index 31a5b2f7a82067..37b210f0f7f884 100644 --- a/test/parallel/test-common-debuglog.js +++ b/test/parallel/test-common-debuglog.js @@ -2,7 +2,7 @@ require('../common'); const assert = require('assert'); -const path = require('path'); +const fixtures = require('../common/fixtures'); const { spawnSync } = require('child_process'); const message = 'message'; @@ -10,7 +10,7 @@ const message = 'message'; { process.env.NODE_DEBUG = 'test'; const { stderr } = spawnSync(process.execPath, [ - path.resolve(__dirname, '../fixtures/common-debuglog.js'), + fixtures.path('common-debuglog.js'), message ], { encoding: 'utf8' }); @@ -21,7 +21,7 @@ const message = 'message'; { delete process.env.NODE_DEBUG; const { stderr } = spawnSync(process.execPath, [ - path.resolve(__dirname, '../fixtures/common-debuglog.js'), + fixtures.path('common-debuglog.js'), message ], { encoding: 'utf8' }); @@ -31,7 +31,7 @@ const message = 'message'; { process.env.NODE_DEBUG = 'fs'; const { stderr } = spawnSync(process.execPath, [ - path.resolve(__dirname, '../fixtures/common-debuglog.js'), + fixtures.path('common-debuglog.js'), message ], { encoding: 'utf8' });