log.js
This file
Path
test/verbose/log.js
46 lines · 2.4 KiB · JavaScript
Last changed by
Fix the `verbose` option printing colors when `stderr` is not a terminalSindre Sorhusbefore adoptiona2eda67
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import assert from 'node:assert/strict';import {stripVTControlCharacters} from 'node:util';import test from 'node:test';import {setFixtureDirectory} from '../helpers/fixtures-directory.js';import {foobarString} from '../helpers/input.js';import {nestedSubprocess} from '../helpers/nested.js';import {getNormalizedLines, getCommandLine, getCompletionLine} from '../helpers/verbose.js';import {PARALLEL_COUNT} from '../helpers/parallel.js';/* eslint-disable node-test/no-conditional-assertion -- shared helper functions are called from conditional paths on purpose */
setFixtureDirectory();
const testNoStdout = async (verbose, isSync) => { const {stdout} = await nestedSubprocess('noop.js', [foobarString], {verbose, stdio: 'inherit', isSync}); assert.equal(stdout, foobarString);};
test('Logs on stderr not stdout, verbose "none"', () => testNoStdout('none', false));test('Logs on stderr not stdout, verbose "short"', () => testNoStdout('short', false));test('Logs on stderr not stdout, verbose "full"', () => testNoStdout('full', false));test('Logs on stderr not stdout, verbose "none", sync', () => testNoStdout('none', true));test('Logs on stderr not stdout, verbose "short", sync', () => testNoStdout('short', true));test('Logs on stderr not stdout, verbose "full", sync', () => testNoStdout('full', true));
const testColor = async (expectedResult, forceColor, term) => { const {stderr} = await nestedSubprocess('noop.js', [foobarString], {verbose: 'short'}, {env: {FORCE_COLOR: forceColor, NO_COLOR: undefined, TERM: term}}); assert.equal(stderr !== stripVTControlCharacters(stderr), expectedResult);};
test('Prints with colors if supported', () => testColor(true, '1'));test('Prints without colors if not supported', () => testColor(false, '0'));// `stderr` is not a terminal, e.g. when redirected to a file, even though the terminal type supports colorstest('Prints without colors if stderr is not a terminal', () => testColor(false, undefined, 'xterm-256color'));
test('Prints lines in order when interleaved with subprocess stderr', async () => { const results = await Promise.all(Array.from({length: PARALLEL_COUNT}, () => nestedSubprocess('noop-fd.js', ['2', `${foobarString}\n`], {verbose: 'full', stderr: 'inherit'}, {all: true}))); for (const {all} of results) { assert.deepEqual( getNormalizedLines(all), [getCommandLine(all), foobarString, getCompletionLine(all)], ); }});
/* eslint-enable node-test/no-conditional-assertion */