node/test/parallel/test-repl-colors.js
Ruben Bridgewater b3e93a91eb
util: do not escape single quotes if not necessary
Right now util.inspect will always escape single quotes. That is not
necessary though in case the string that will be escaped does not
contain double quotes. In that case the string can simply be wrapped
in double quotes instead.
If the string contains single and double quotes and it does not
contain `${` as part of the string, backticks will be used instead.
That makes sure only very few strings have to escape quotes at all.
Thus it increases the readability of these strings.

PR-URL: https://github.com/nodejs/node/pull/21624
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2018-07-16 14:18:25 +02:00

32 lines
988 B
JavaScript

/* eslint-disable quotes */
'use strict';
require('../common');
const { Duplex } = require('stream');
const { inspect } = require('util');
const { strictEqual } = require('assert');
const { REPLServer } = require('repl');
let output = '';
const inout = new Duplex({ decodeStrings: false });
inout._read = function() {
this.push('util.inspect("string")\n');
this.push(null);
};
inout._write = function(s, _, cb) {
output += s;
cb();
};
const repl = new REPLServer({ input: inout, output: inout, useColors: true });
process.on('exit', function() {
// https://github.com/nodejs/node/pull/16485#issuecomment-350428638
// The color setting of the REPL should not have leaked over into
// the color setting of `util.inspect.defaultOptions`.
strictEqual(output.includes(`"'string'"`), true);
strictEqual(output.includes(`'\u001b[32m\\'string\\'\u001b[39m'`), false);
strictEqual(inspect.defaultOptions.colors, false);
strictEqual(repl.writer.options.colors, true);
});