Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"devDependencies": {
"airtap": "~1.0.0",
"is-async-supported": "~1.2.0",
"object.assign": "~4.1.0",
"run-series": "~1.1.4",
"tape": "~4.9.0"
},
Expand Down
75 changes: 47 additions & 28 deletions test/node/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,68 +19,87 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
var assert = require('assert');
var util = require('../../');
var ObjectAssign = require('object.assign');

if (process.argv[2] === 'child')
child();
var modeArgv = process.argv[2]
var sectionArgv = process.argv[3]

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');

test('test-abc', true, 'test-abc');
test('test-a', false, 'test-abc');
test('test-*', true, 'test-abc');
test('test-*c', true, 'test-abc');
test('test-*abc', true, 'test-abc');
test('abc-test', true, 'abc-test');
test('a*-test', true, 'abc-test');
test('*-test', true, 'abc-test');
}

function test(environ, shouldWrite) {
function test(environ, shouldWrite, section) {
var expectErr = '';
if (shouldWrite) {
expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +
'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';
}
var expectOut = 'ok\n';
var didTest = false;

var spawn = require('child_process').spawn;
var child = spawn(process.execPath, [__filename, 'child'], {
env: { NODE_DEBUG: environ }
var child = spawn(process.execPath, [__filename, 'child', section], {
env: ObjectAssign(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';
}

var err = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) {
child.stderr.on('data', function (c) {
err += c;
});

var out = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', function(c) {
child.stdout.on('data', function (c) {
out += c;
});

child.on('close', function(c) {
var didTest = false;
child.on('close', function (c) {
assert(!c);
assert.equal(err, expectErr);
assert.equal(out, expectOut);
assert.strictEqual(err, expectErr);
assert.strictEqual(out, expectOut);
didTest = true;
console.log('ok %j %j', environ, shouldWrite);
});

process.on('exit', function() {
process.on('exit', function () {
assert(didTest);
});
}


function child() {
var debug = util.debuglog('tud');
function child(section) {
var util = require('../../util');
var 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');
}
15 changes: 11 additions & 4 deletions util.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,20 @@ exports.deprecate = function(fn, msg) {


var debugs = {};
var debugEnviron;
var debugEnvRegex = /^$/;

if (process.env.NODE_DEBUG) {
var debugEnv = process.env.NODE_DEBUG;
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&')
.replace(/\*/g, '.*')
.replace(/,/g, '$|^')
.toUpperCase();
debugEnvRegex = new RegExp('^' + debugEnv + '$', 'i');
}
exports.debuglog = function(set) {
if (isUndefined(debugEnviron))
debugEnviron = process.env.NODE_DEBUG || '';
set = set.toUpperCase();
if (!debugs[set]) {
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
if (debugEnvRegex.test(set)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
Expand Down