// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.2021var assert = require('assert');22var util = require('../../');2324if (process.argv[2] === 'child')25child();26else27parent();2829function parent() {30test('foo,tud,bar', true);31test('foo,tud', true);32test('tud,bar', true);33test('tud', true);34test('foo,bar', false);35test('', false);36}3738function test(environ, shouldWrite) {39var expectErr = '';40if (shouldWrite) {41expectErr = 'TUD %PID%: this { is: \'a\' } /debugging/\n' +42'TUD %PID%: number=1234 string=asdf obj={"foo":"bar"}\n';43}44var expectOut = 'ok\n';45var didTest = false;4647var spawn = require('child_process').spawn;48var child = spawn(process.execPath, [__filename, 'child'], {49env: { NODE_DEBUG: environ }50});5152expectErr = expectErr.split('%PID%').join(child.pid);5354var err = '';55child.stderr.setEncoding('utf8');56child.stderr.on('data', function(c) {57err += c;58});5960var out = '';61child.stdout.setEncoding('utf8');62child.stdout.on('data', function(c) {63out += c;64});6566child.on('close', function(c) {67assert(!c);68assert.equal(err, expectErr);69assert.equal(out, expectOut);70didTest = true;71console.log('ok %j %j', environ, shouldWrite);72});7374process.on('exit', function() {75assert(didTest);76});77}787980function child() {81var debug = util.debuglog('tud');82debug('this', { is: 'a' }, /debugging/);83debug('number=%d string=%s obj=%j', 1234, 'asdf', { foo: 'bar' });84console.log('ok');85}868788