Skip to content

Commit 4bbf014

Browse files
Merge pull request #27 from browserify/debuglog
Update util.debuglog to Node 10.4.0.
2 parents 53026a2 + 44c5de2 commit 4bbf014

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"devDependencies": {
2020
"airtap": "~1.0.0",
2121
"is-async-supported": "~1.2.0",
22+
"object.assign": "~4.1.0",
2223
"run-series": "~1.1.4",
2324
"tape": "~4.9.0"
2425
},

test/node/debug.js

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,68 +19,87 @@
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('../../');
24+
var ObjectAssign = require('object.assign');
2425

25-
if (process.argv[2] === 'child')
26-
child();
26+
var modeArgv = process.argv[2]
27+
var sectionArgv = process.argv[3]
28+
29+
if (modeArgv === 'child')
30+
child(sectionArgv);
2731
else
2832
parent();
2933

3034
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);
35+
test('foo,tud,bar', true, 'tud');
36+
test('foo,tud', true, 'tud');
37+
test('tud,bar', true, 'tud');
38+
test('tud', true, 'tud');
39+
test('foo,bar', false, 'tud');
40+
test('', false, 'tud');
41+
42+
test('###', true, '###');
43+
test('hi:)', true, 'hi:)');
44+
test('f$oo', true, 'f$oo');
45+
test('f$oo', false, 'f.oo');
46+
test('no-bar-at-all', false, 'bar');
47+
48+
test('test-abc', true, 'test-abc');
49+
test('test-a', false, 'test-abc');
50+
test('test-*', true, 'test-abc');
51+
test('test-*c', true, 'test-abc');
52+
test('test-*abc', true, 'test-abc');
53+
test('abc-test', true, 'abc-test');
54+
test('a*-test', true, 'abc-test');
55+
test('*-test', true, 'abc-test');
3756
}
3857

39-
function test(environ, shouldWrite) {
58+
function test(environ, shouldWrite, section) {
4059
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-
}
4560
var expectOut = 'ok\n';
46-
var didTest = false;
4761

4862
var spawn = require('child_process').spawn;
49-
var child = spawn(process.execPath, [__filename, 'child'], {
50-
env: { NODE_DEBUG: environ }
63+
var child = spawn(process.execPath, [__filename, 'child', section], {
64+
env: ObjectAssign(process.env, { NODE_DEBUG: environ })
5165
});
5266

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

5573
var err = '';
5674
child.stderr.setEncoding('utf8');
57-
child.stderr.on('data', function(c) {
75+
child.stderr.on('data', function (c) {
5876
err += c;
5977
});
6078

6179
var out = '';
6280
child.stdout.setEncoding('utf8');
63-
child.stdout.on('data', function(c) {
81+
child.stdout.on('data', function (c) {
6482
out += c;
6583
});
6684

67-
child.on('close', function(c) {
85+
var didTest = false;
86+
child.on('close', function (c) {
6887
assert(!c);
69-
assert.equal(err, expectErr);
70-
assert.equal(out, expectOut);
88+
assert.strictEqual(err, expectErr);
89+
assert.strictEqual(out, expectOut);
7190
didTest = true;
72-
console.log('ok %j %j', environ, shouldWrite);
7391
});
7492

75-
process.on('exit', function() {
93+
process.on('exit', function () {
7694
assert(didTest);
7795
});
7896
}
7997

8098

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

util.js

Lines changed: 11 additions & 4 deletions
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)