diff --git a/package.json b/package.json index ad7cf51..9d86a43 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/node/debug.js b/test/node/debug.js index ef5f69f..70df304 100644 --- a/test/node/debug.js +++ b/test/node/debug.js @@ -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'); } diff --git a/util.js b/util.js index 6aad97c..6eea657 100644 --- a/util.js +++ b/util.js @@ -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);