Skip to content

Commit 6b82825

Browse files
committed
Update util.debuglog to Node 10.4.0.
1 parent f090e96 commit 6b82825

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

test/node/debug.js

+46-28
Original file line numberDiff line numberDiff line change
@@ -19,68 +19,86 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
'use strict';
2223
var assert = require('assert');
23-
var util = require('../../');
2424

25-
if (process.argv[2] === 'child')
26-
child();
25+
var modeArgv = process.argv[2]
26+
var sectionArgv = process.argv[3]
27+
28+
if (modeArgv === 'child')
29+
child(sectionArgv);
2730
else
2831
parent();
2932

3033
function parent() {
31-
test('foo,tud,bar', true);
32-
test('foo,tud', true);
33-
test('tud,bar', true);
34-
test('tud', true);
35-
test('foo,bar', false);
36-
test('', false);
34+
test('foo,tud,bar', true, 'tud');
35+
test('foo,tud', true, 'tud');
36+
test('tud,bar', true, 'tud');
37+
test('tud', true, 'tud');
38+
test('foo,bar', false, 'tud');
39+
test('', false, 'tud');
40+
41+
test('###', true, '###');
42+
test('hi:)', true, 'hi:)');
43+
test('f$oo', true, 'f$oo');
44+
test('f$oo', false, 'f.oo');
45+
test('no-bar-at-all', false, 'bar');
46+
47+
test('test-abc', true, 'test-abc');
48+
test('test-a', false, 'test-abc');
49+
test('test-*', true, 'test-abc');
50+
test('test-*c', true, 'test-abc');
51+
test('test-*abc', true, 'test-abc');
52+
test('abc-test', true, 'abc-test');
53+
test('a*-test', true, 'abc-test');
54+
test('*-test', true, 'abc-test');
3755
}
3856

39-
function test(environ, shouldWrite) {
57+
function test(environ, shouldWrite, section) {
4058
var expectErr = '';
41-
if (shouldWrite) {
42-
expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
43-
'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
44-
}
4559
var expectOut = 'ok\n';
46-
var didTest = false;
4760

4861
var spawn = require('child_process').spawn;
49-
var child = spawn(process.execPath, [__filename, 'child'], {
50-
env: { NODE_DEBUG: environ }
62+
var child = spawn(process.execPath, [__filename, 'child', section], {
63+
env: Object.assign(process.env, { NODE_DEBUG: environ })
5164
});
5265

53-
expectErr = expectErr.split('%PID%').join(child.pid);
66+
if (shouldWrite) {
67+
expectErr =
68+
section.toUpperCase() + ' ' + child.pid + ': this { is: \'a\' } /debugging/\n' +
69+
section.toUpperCase() + ' ' + child.pid + ': num=1 str=a obj={"foo":"bar"}\n';
70+
}
5471

5572
var err = '';
5673
child.stderr.setEncoding('utf8');
57-
child.stderr.on('data', function(c) {
74+
child.stderr.on('data', function (c) {
5875
err += c;
5976
});
6077

6178
var out = '';
6279
child.stdout.setEncoding('utf8');
63-
child.stdout.on('data', function(c) {
80+
child.stdout.on('data', function (c) {
6481
out += c;
6582
});
6683

67-
child.on('close', function(c) {
84+
var didTest = false;
85+
child.on('close', function (c) {
6886
assert(!c);
69-
assert.equal(err, expectErr);
70-
assert.equal(out, expectOut);
87+
assert.strictEqual(err, expectErr);
88+
assert.strictEqual(out, expectOut);
7189
didTest = true;
72-
console.log('ok %j %j', environ, shouldWrite);
7390
});
7491

75-
process.on('exit', function() {
92+
process.on('exit', function () {
7693
assert(didTest);
7794
});
7895
}
7996

8097

81-
function child() {
82-
var debug = util.debuglog('tud');
98+
function child(section) {
99+
var util = require('../../util');
100+
var debug = util.debuglog(section);
83101
debug('this', { is: 'a' }, /debugging/);
84-
debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' });
102+
debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' });
85103
console.log('ok');
86104
}

util.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,20 @@ exports.deprecate = function(fn, msg) {
104104

105105

106106
var debugs = {};
107-
var debugEnviron;
107+
var debugEnvRegex = /^$/;
108+
109+
if (process.env.NODE_DEBUG) {
110+
var debugEnv = process.env.NODE_DEBUG;
111+
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
112+
.replace(/\*/g, '.*')
113+
.replace(/,/g, '$|^')
114+
.toUpperCase();
115+
debugEnvRegex = new RegExp('^' + debugEnv + '$', 'i');
116+
}
108117
exports.debuglog = function(set) {
109-
if (isUndefined(debugEnviron))
110-
debugEnviron = process.env.NODE_DEBUG || '';
111118
set = set.toUpperCase();
112119
if (!debugs[set]) {
113-
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
120+
if (debugEnvRegex.test(set)) {
114121
var pid = process.pid;
115122
debugs[set] = function() {
116123
var msg = exports.format.apply(exports, arguments);

0 commit comments

Comments
 (0)