From 3b0e800f18133674ae30b4c6cd5729c12fa054ff Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Wed, 21 Jun 2017 16:37:42 +0300 Subject: [PATCH] util: make util.debuglog() consistent with doc Previous realization produces some false positive and false negative results due to: * conflicts between unescaped user input and RegExp special characters; * conflicts between parsing with `\b` RegExp symbol and non alphanumeric characters in section names. The doc does not mention any such restrictions. PR-URL: https://github.com/nodejs/node/pull/13841 Fixes: https://github.com/nodejs/node/issues/13728 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/util.js | 8 ++++-- test/sequential/test-util-debug.js | 44 ++++++++++++++++++------------ 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/lib/util.js b/lib/util.js index c9140069e237e0..c75117b711a58f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -147,11 +147,13 @@ exports.deprecate = internalUtil.deprecate; var debugs = {}; var debugEnviron; exports.debuglog = function(set) { - if (debugEnviron === undefined) - debugEnviron = process.env.NODE_DEBUG || ''; + if (debugEnviron === undefined) { + debugEnviron = new Set( + (process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())); + } set = set.toUpperCase(); if (!debugs[set]) { - if (new RegExp(`\\b${set}\\b`, 'i').test(debugEnviron)) { + if (debugEnviron.has(set)) { var pid = process.pid; debugs[set] = function() { var msg = exports.format.apply(exports, arguments); diff --git a/test/sequential/test-util-debug.js b/test/sequential/test-util-debug.js index 242c536125aff2..68c272090a6de6 100644 --- a/test/sequential/test-util-debug.js +++ b/test/sequential/test-util-debug.js @@ -23,34 +23,42 @@ const common = require('../common'); const assert = require('assert'); -if (process.argv[2] === 'child') - child(); +const [, , modeArgv, sectionArgv] = process.argv; + +if (modeArgv === 'child') + child(sectionArgv); else parent(); function parent() { - test('foo,tud,bar', true); - test('foo,tud', true); - test('tud,bar', true); - test('tud', true); - test('foo,bar', false); - test('', false); + test('foo,tud,bar', true, 'tud'); + test('foo,tud', true, 'tud'); + test('tud,bar', true, 'tud'); + test('tud', true, 'tud'); + test('foo,bar', false, 'tud'); + test('', false, 'tud'); + + test('###', true, '###'); + test('hi:)', true, 'hi:)'); + test('f$oo', true, 'f$oo'); + test('f$oo', false, 'f.oo'); + test('no-bar-at-all', false, 'bar'); } -function test(environ, shouldWrite) { +function test(environ, shouldWrite, section) { let expectErr = ''; - if (shouldWrite) { - expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' + - 'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n'; - } const expectOut = 'ok\n'; const spawn = require('child_process').spawn; - const child = spawn(process.execPath, [__filename, 'child'], { + const child = spawn(process.execPath, [__filename, 'child', section], { env: Object.assign(process.env, { NODE_DEBUG: environ }) }); - expectErr = expectErr.split('%PID%').join(child.pid); + if (shouldWrite) { + expectErr = + `${section.toUpperCase()} ${child.pid}: this { is: 'a' } /debugging/\n${ + section.toUpperCase()} ${child.pid}: num=1 str=a obj={"foo":"bar"}\n`; + } let err = ''; child.stderr.setEncoding('utf8'); @@ -72,10 +80,10 @@ function test(environ, shouldWrite) { } -function child() { +function child(section) { const util = require('util'); - const debug = util.debuglog('tud'); + const debug = util.debuglog(section); debug('this', { is: 'a' }, /debugging/); - debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' }); + debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' }); console.log('ok'); }