11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ciconst assert = require('assert'); 251cb0ef41Sopenharmony_ciconst util = require('util'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst [, , modeArgv, sectionArgv] = process.argv; 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciif (modeArgv === 'child') 301cb0ef41Sopenharmony_ci child(sectionArgv); 311cb0ef41Sopenharmony_cielse 321cb0ef41Sopenharmony_ci parent(); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_cifunction parent() { 351cb0ef41Sopenharmony_ci test('foo,tud,bar', true, 'tud'); 361cb0ef41Sopenharmony_ci test('foo,tud', true, 'tud'); 371cb0ef41Sopenharmony_ci test('tud,bar', true, 'tud'); 381cb0ef41Sopenharmony_ci test('tud', true, 'tud'); 391cb0ef41Sopenharmony_ci test('foo,bar', false, 'tud'); 401cb0ef41Sopenharmony_ci test('', false, 'tud'); 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci test('###', true, '###'); 431cb0ef41Sopenharmony_ci test('hi:)', true, 'hi:)'); 441cb0ef41Sopenharmony_ci test('f$oo', true, 'f$oo'); 451cb0ef41Sopenharmony_ci test('f$oo', false, 'f.oo'); 461cb0ef41Sopenharmony_ci test('no-bar-at-all', false, 'bar'); 471cb0ef41Sopenharmony_ci 481cb0ef41Sopenharmony_ci test('test-abc', true, 'test-abc'); 491cb0ef41Sopenharmony_ci test('test-a', false, 'test-abc'); 501cb0ef41Sopenharmony_ci test('test-*', true, 'test-abc'); 511cb0ef41Sopenharmony_ci test('test-*c', true, 'test-abc'); 521cb0ef41Sopenharmony_ci test('test-*abc', true, 'test-abc'); 531cb0ef41Sopenharmony_ci test('abc-test', true, 'abc-test'); 541cb0ef41Sopenharmony_ci test('a*-test', true, 'abc-test'); 551cb0ef41Sopenharmony_ci test('*-test', true, 'abc-test'); 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cifunction test(environ, shouldWrite, section, forceColors = false) { 591cb0ef41Sopenharmony_ci let expectErr = ''; 601cb0ef41Sopenharmony_ci const expectOut = shouldWrite ? 'enabled\n' : 'disabled\n'; 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci const spawn = require('child_process').spawn; 631cb0ef41Sopenharmony_ci const child = spawn(process.execPath, [__filename, 'child', section], { 641cb0ef41Sopenharmony_ci env: Object.assign(process.env, { 651cb0ef41Sopenharmony_ci NODE_DEBUG: environ, 661cb0ef41Sopenharmony_ci FORCE_COLOR: forceColors ? 'true' : 'false' 671cb0ef41Sopenharmony_ci }) 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci if (shouldWrite) { 711cb0ef41Sopenharmony_ci if (forceColors) { 721cb0ef41Sopenharmony_ci const { colors, styles } = util.inspect; 731cb0ef41Sopenharmony_ci const addCodes = (arr) => [`\x1B[${arr[0]}m`, `\x1B[${arr[1]}m`]; 741cb0ef41Sopenharmony_ci const num = addCodes(colors[styles.number]); 751cb0ef41Sopenharmony_ci const str = addCodes(colors[styles.string]); 761cb0ef41Sopenharmony_ci const regexp = addCodes(colors[styles.regexp]); 771cb0ef41Sopenharmony_ci const start = `${section.toUpperCase()} ${num[0]}${child.pid}${num[1]}`; 781cb0ef41Sopenharmony_ci const debugging = `${regexp[0]}/debugging/${regexp[1]}`; 791cb0ef41Sopenharmony_ci expectErr = 801cb0ef41Sopenharmony_ci `${start}: this { is: ${str[0]}'a'${str[1]} } ${debugging}\n` + 811cb0ef41Sopenharmony_ci `${start}: num=1 str=a obj={"foo":"bar"}\n`; 821cb0ef41Sopenharmony_ci } else { 831cb0ef41Sopenharmony_ci const start = `${section.toUpperCase()} ${child.pid}`; 841cb0ef41Sopenharmony_ci expectErr = 851cb0ef41Sopenharmony_ci `${start}: this { is: 'a' } /debugging/\n` + 861cb0ef41Sopenharmony_ci `${start}: num=1 str=a obj={"foo":"bar"}\n`; 871cb0ef41Sopenharmony_ci } 881cb0ef41Sopenharmony_ci } 891cb0ef41Sopenharmony_ci 901cb0ef41Sopenharmony_ci let err = ''; 911cb0ef41Sopenharmony_ci child.stderr.setEncoding('utf8'); 921cb0ef41Sopenharmony_ci child.stderr.on('data', (c) => { 931cb0ef41Sopenharmony_ci err += c; 941cb0ef41Sopenharmony_ci }); 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci let out = ''; 971cb0ef41Sopenharmony_ci child.stdout.setEncoding('utf8'); 981cb0ef41Sopenharmony_ci child.stdout.on('data', (c) => { 991cb0ef41Sopenharmony_ci out += c; 1001cb0ef41Sopenharmony_ci }); 1011cb0ef41Sopenharmony_ci 1021cb0ef41Sopenharmony_ci child.on('close', common.mustCall((c) => { 1031cb0ef41Sopenharmony_ci assert(!c); 1041cb0ef41Sopenharmony_ci assert.strictEqual(err, expectErr); 1051cb0ef41Sopenharmony_ci assert.strictEqual(out, expectOut); 1061cb0ef41Sopenharmony_ci // Run the test again, this time with colors enabled. 1071cb0ef41Sopenharmony_ci if (!forceColors) { 1081cb0ef41Sopenharmony_ci test(environ, shouldWrite, section, true); 1091cb0ef41Sopenharmony_ci } 1101cb0ef41Sopenharmony_ci })); 1111cb0ef41Sopenharmony_ci} 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_cifunction child(section) { 1151cb0ef41Sopenharmony_ci const tty = require('tty'); 1161cb0ef41Sopenharmony_ci // Make sure we check for colors, no matter of the stream's default. 1171cb0ef41Sopenharmony_ci Object.defineProperty(process.stderr, 'hasColors', { 1181cb0ef41Sopenharmony_ci value: tty.WriteStream.prototype.hasColors 1191cb0ef41Sopenharmony_ci }); 1201cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 1211cb0ef41Sopenharmony_ci const debug = util.debuglog(section, common.mustCall((cb) => { 1221cb0ef41Sopenharmony_ci assert.strictEqual(typeof cb, 'function'); 1231cb0ef41Sopenharmony_ci })); 1241cb0ef41Sopenharmony_ci debug('this', { is: 'a' }, /debugging/); 1251cb0ef41Sopenharmony_ci debug('num=%d str=%s obj=%j', 1, 'a', { foo: 'bar' }); 1261cb0ef41Sopenharmony_ci console.log(debug.enabled ? 'enabled' : 'disabled'); 1271cb0ef41Sopenharmony_ci} 128