command.configureOutput.test.js
This file
Path
tests/command.configureOutput.test.js
345 lines · 10.7 KiB · JavaScript
Last changed by
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
History of this fileTake it
The walk stopped after 200 commits: lines older than 471ec40 point at it.
Switching to esm-only (#2464)John Geebefore adoptionf4bd470
lines 1–4why, one step back1import { test, describe } from 'node:test';2import assert from 'node:assert/strict';3import * as commander from '../index.js';4import process from 'node:process';Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 5why, one step back5Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 6–10why, one step back6describe('Command.configureOutput()', () => {7 test('when default writeErr() then error on stderr', (t) => {8 const writeSpy = t.mock.method(process.stderr, 'write', () => {});9 const program = new commander.Command();10 program.exitOverride();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 11why, one step back11Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 12–16why, one step back12 try {13 program.parse(['--unknown'], { from: 'user' });14 } catch (err) {15 /* empty */16 }Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 17why, one step back17Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 18–19why, one step back18 assert.equal(writeSpy.mock.callCount(), 1);19 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 20why, one step back20Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 21–25why, one step back21 test('when custom writeErr() then error on custom output', (t) => {22 const writeSpy = t.mock.method(process.stderr, 'write', () => {});23 const customWrite = t.mock.fn();24 const program = new commander.Command();25 program.exitOverride().configureOutput({ writeErr: customWrite });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 26why, one step back26Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 27–31why, one step back27 try {28 program.parse(['--unknown'], { from: 'user' });29 } catch (err) {30 /* empty */31 }Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 32why, one step back32Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 33–34why, one step back33 assert.equal(writeSpy.mock.callCount(), 0);34 assert.equal(customWrite.mock.callCount(), 1);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 35why, one step back35 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 36why, one step back36Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 37–40why, one step back37 test('when default write() then version on stdout', (t) => {38 const writeSpy = t.mock.method(process.stdout, 'write', () => {});39 const program = new commander.Command();40 program.exitOverride().version('1.2.3');Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 41why, one step back41Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 42–44why, one step back42 assert.throws(() => {43 program.parse(['--version'], { from: 'user' });44 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 45why, one step back45Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 46why, one step back46 assert.equal(writeSpy.mock.callCount(), 1);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 47why, one step back47 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 48why, one step back48Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 49–56why, one step back49 test('when custom write() then version on custom output', (t) => {50 const writeSpy = t.mock.method(process.stdout, 'write', () => {});51 const customWrite = t.mock.fn();52 const program = new commander.Command();53 program54 .exitOverride()55 .version('1.2.3')56 .configureOutput({ writeOut: customWrite });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 57why, one step back57Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 58–60why, one step back58 assert.throws(() => {59 program.parse(['--version'], { from: 'user' });60 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 61why, one step back61Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 62–64why, one step back62 assert.equal(writeSpy.mock.callCount(), 0);63 assert.equal(customWrite.mock.callCount(), 1);64 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 65why, one step back65Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 66–69why, one step back66 test('when default write() then help on stdout', (t) => {67 const writeSpy = t.mock.method(process.stdout, 'write', () => {});68 const program = new commander.Command();69 program.outputHelp();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 70why, one step back70Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 71–72why, one step back71 assert.equal(writeSpy.mock.callCount(), 1);72 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 73why, one step back73Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 74–79why, one step back74 test('when custom write() then help error on custom output', (t) => {75 const writeSpy = t.mock.method(process.stdout, 'write', () => {});76 const customWrite = t.mock.fn();77 const program = new commander.Command();78 program.configureOutput({ writeOut: customWrite });79 program.outputHelp();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 80why, one step back80Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 81–83why, one step back81 assert.equal(writeSpy.mock.callCount(), 0);82 assert.equal(customWrite.mock.callCount(), 1);83 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 84why, one step back84Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 85–88why, one step back85 test('when default writeErr then help error on stderr', (t) => {86 const writeSpy = t.mock.method(process.stderr, 'write', () => {});87 const program = new commander.Command();88 program.outputHelp({ error: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 89why, one step back89Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 90–91why, one step back90 assert.equal(writeSpy.mock.callCount(), 1);91 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 92why, one step back92Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 93–98why, one step back93 test('when custom writeErr then help error on custom output', (t) => {94 const writeSpy = t.mock.method(process.stderr, 'write', () => {});95 const customWrite = t.mock.fn();96 const program = new commander.Command();97 program.configureOutput({ writeErr: customWrite });98 program.outputHelp({ error: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 99why, one step back99Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 100–101why, one step back100 assert.equal(writeSpy.mock.callCount(), 0);101 assert.equal(customWrite.mock.callCount(), 1);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 102why, one step back102 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 103why, one step back103Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 104–108why, one step back104 test('when default getOutHelpWidth then help helpWidth from stdout', () => {105 const expectedColumns = 123;106 const holdIsTTY = process.stdout.isTTY;107 const holdColumns = process.stdout.columns;108 let helpWidth;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 109why, one step back109Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 110–114why, one step back110 process.stderr.isTTY = true;111 process.stdout.columns = expectedColumns;112 process.stdout.isTTY = true;113 const program = new commander.Command();114 program.configureHelp({Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 115–117why, one step back115 formatHelp: (cmd, helper) => {116 helpWidth = helper.helpWidth;117 return '';Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 118why, one step back118 },Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 119why, one step back119 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 120why, one step back120 program.outputHelp();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 121why, one step back121Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 122–125why, one step back122 assert.equal(helpWidth, expectedColumns);123 process.stdout.columns = holdColumns;124 process.stdout.isTTY = holdIsTTY;125 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 126why, one step back126Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 127–129why, one step back127 test('when custom getOutHelpWidth then help helpWidth custom', () => {128 const expectedColumns = 123;129 let helpWidth;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 130why, one step back130Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 131–144why, one step back131 const program = new commander.Command();132 program133 .configureHelp({134 formatHelp: (cmd, helper) => {135 helpWidth = helper.helpWidth;136 return '';137 },138 })139 .configureOutput({140 getOutHelpWidth: () => expectedColumns,141 });142 program.outputHelp();143144 assert.equal(helpWidth, expectedColumns);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 145why, one step back145 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 146why, one step back146Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 147–151why, one step back147 test('when default getErrHelpWidth then help error helpWidth from stderr', () => {148 const expectedColumns = 123;149 const holdIsTTY = process.stderr.isTTY;150 const holdColumns = process.stderr.columns;151 let helpWidth;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 152why, one step back152Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 153–156why, one step back153 process.stderr.isTTY = true;154 process.stderr.columns = expectedColumns;155 const program = new commander.Command();156 program.configureHelp({Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
lines 157–159why, one step back157 formatHelp: (cmd, helper) => {158 helpWidth = helper.helpWidth;159 return '';Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 160why, one step back160 },Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 161why, one step back161 });Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
line 162why, one step back162 program.outputHelp({ error: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 163why, one step back163Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 164–167why, one step back164 assert.equal(helpWidth, expectedColumns);165 process.stderr.isTTY = holdIsTTY;166 process.stderr.columns = holdColumns;167 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 168why, one step back168Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 169–171why, one step back169 test('when custom getErrHelpWidth then help error helpWidth custom', () => {170 const expectedColumns = 123;171 let helpWidth;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 172why, one step back172Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 173–184why, one step back173 const program = new commander.Command();174 program175 .configureHelp({176 formatHelp: (cmd, helper) => {177 helpWidth = helper.helpWidth;178 return '';179 },180 })181 .configureOutput({182 getErrHelpWidth: () => expectedColumns,183 });184 program.outputHelp({ error: true });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 185why, one step back185Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 186–187why, one step back186 assert.equal(helpWidth, expectedColumns);187 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 188why, one step back188Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 189–191why, one step back189 test('when custom getOutHelpWidth and configureHelp:helpWidth then help helpWidth from configureHelp', () => {190 const expectedColumns = 123;191 let helpWidth;Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 192why, one step back192Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 193–205why, one step back193 const program = new commander.Command();194 program195 .configureHelp({196 formatHelp: (cmd, helper) => {197 helpWidth = helper.helpWidth;198 return '';199 },200 helpWidth: expectedColumns,201 })202 .configureOutput({203 getOutHelpWidth: () => 999,204 });205 program.outputHelp();Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 206why, one step back206Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 207–208why, one step back207 assert.equal(helpWidth, expectedColumns);208 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 209why, one step back209Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 210–212why, one step back210 test('when custom getErrHelpWidth and configureHelp:helpWidth then help error helpWidth from configureHelp', () => {211 const expectedColumns = 123;212 let helpWidth;Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
line 213why, one step back213Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 214–226why, one step back214 const program = new commander.Command();215 program216 .configureHelp({217 formatHelp: (cmd, helper) => {218 helpWidth = helper.helpWidth;219 return '';220 },221 helpWidth: expectedColumns,222 })223 .configureOutput({224 getErrHelpWidth: () => 999,225 });226 program.outputHelp({ error: true });Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
line 227why, one step back227Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 228–229why, one step back228 assert.equal(helpWidth, expectedColumns);229 });Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
line 230why, one step back230Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 231–244why, one step back231 test('when no custom setup and call formatHelp direct then effective helpWidth is fallback 80', () => {232 // Not an important case, but filling out testing coverage.233 const helper = new commander.Help();234 let wrapWidth;235 helper.boxWrap = (str, width) => {236 wrapWidth = wrapWidth ?? width;237 return '';238 };239 const program = new commander.Command()240 .description('description')241 .helpOption(false);242 helper.formatHelp(program, helper);243 assert.equal(wrapWidth, 80);244 });Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
line 245why, one step back245Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 246–253why, one step back246 test('when no custom setup and call formatItem direct then effective helpWidth is fallback 80', () => {247 // Not an important case, but filling out testing coverage.248 const helper = new commander.Help();249 let wrapWidth;250 helper.boxWrap = (str, width) => {251 wrapWidth = wrapWidth ?? width;252 return '';253 };Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 254why, one step back254Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 255–261why, one step back255 const termWidth = 8;256 helper.formatItem('term', termWidth, 'description', helper);257 const itemIndent = 2;258 const spacerWidth = 2; // between term and description259 const remainingWidth = 80 - termWidth - spacerWidth - itemIndent;260261 assert.equal(wrapWidth, remainingWidth);Reformat code with Prettier (#2180)John Geebefore adoption1bdc749
line 262why, one step back262 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 263why, one step back263Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 264–277why, one step back264 test('when set configureOutput then get configureOutput', (t) => {265 const outputOptions = {266 writeOut: t.mock.fn(),267 writeErr: t.mock.fn(),268 getOutHelpWidth: t.mock.fn(),269 getErrHelpWidth: t.mock.fn(),270 getOutHasColors: t.mock.fn(),271 getErrHasColors: t.mock.fn(),272 outputError: t.mock.fn(),273 stripColor: t.mock.fn(),274 };275 const program = new commander.Command();276 program.configureOutput(outputOptions);277 assert.deepEqual(program.configureOutput(), outputOptions);Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 278why, one step back278 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 279why, one step back279Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 280–285why, one step back280 test('when custom outputErr and error then outputErr called', (t) => {281 const outputError = t.mock.fn();282 const program = new commander.Command();283 program.exitOverride().configureOutput({284 outputError,285 });Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40
line 286why, one step back286Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 287–297why, one step back287 assert.throws(() => {288 program.parse(['--unknownOption'], { from: 'user' });289 });290 assert.equal(291 outputError.mock.calls[0].arguments[0],292 "error: unknown option '--unknownOption'\n",293 );294 assert.equal(295 outputError.mock.calls[0].arguments[1],296 program._outputConfiguration.writeErr,297 );Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
line 298why, one step back298 });Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
line 299why, one step back299Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 300–304why, one step back300 test('when custom outputErr and writeErr and error then outputErr passed writeErr', (t) => {301 const writeErr = () => t.mock.fn();302 const outputError = t.mock.fn();303 const program = new commander.Command();304 program.exitOverride().configureOutput({ writeErr, outputError });Make .configureOutput() create copy of settings instead of in-place change (#2350)John Geebefore adoption68199e6
line 305why, one step back305Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795
lines 306–342why, one step back306 assert.throws(() => {307 program.parse(['--unknownOption'], { from: 'user' });308 });309 assert.equal(310 outputError.mock.calls[0].arguments[0],311 "error: unknown option '--unknownOption'\n",312 );313 assert.equal(outputError.mock.calls[0].arguments[1], writeErr);314 });315316 test('when configureOutput after copyInheritedSettings then original unchanged', () => {317 const program = new commander.Command();318 program.configureOutput({ getOutHelpWidth: () => 80 });319 const copy = program.createCommand('copy');320 copy.copyInheritedSettings(program);321 assert.equal(copy.configureOutput().getOutHelpWidth(), 80);322 copy.configureOutput({ getOutHelpWidth: () => 40 });323 assert.equal(copy.configureOutput().getOutHelpWidth(), 40);324 assert.equal(program.configureOutput().getOutHelpWidth(), 80);325 });326327 describe('environment variable tests', () => {328 [329 ['NO_COLOR', false],330 ['FORCE_COLOR', true],331 ['CLICOLOR_FORCE', true],332 ].forEach(([envvar, expected]) => {333 test(`when ${envvar} then getFooHasColors returns ${expected}`, () => {334 // Would like to vary process.istty too, but too hard, so tests here provide only partial cover.335 const holdEnv = process.env[envvar];336 process.env[envvar] = '1';337 const config = new commander.Command().configureOutput();338 assert.equal(config.getOutHasColors(), expected);339 assert.equal(config.getErrHasColors(), expected);340 if (holdEnv === undefined) delete process.env[envvar];341 else process.env[envvar] = holdEnv;342 });Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947
line 343why, one step back343 });Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086
lines 344–345why, one step back344 });345});