Skip to content
Myr

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 file

Take 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 back
1import { 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 back
5

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 6–10why, one step back
6describe('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 back
11

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 12–16why, one step back
12 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 back
17

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 18–19why, one step back
18 assert.equal(writeSpy.mock.callCount(), 1);
19 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 20why, one step back
20

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 21–25why, one step back
21 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 back
26

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 27–31why, one step back
27 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 back
32

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 33–34why, one step back
33 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 back
35 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 36why, one step back
36

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 37–40why, one step back
37 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 back
41

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 42–44why, one step back
42 assert.throws(() => {
43 program.parse(['--version'], { from: 'user' });44 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 45why, one step back
45

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 46why, one step back
46 assert.equal(writeSpy.mock.callCount(), 1);

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 47why, one step back
47 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 48why, one step back
48

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 49–56why, one step back
49 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 back
57

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 58–60why, one step back
58 assert.throws(() => {
59 program.parse(['--version'], { from: 'user' });60 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 61why, one step back
61

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 62–64why, one step back
62 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 back
65

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 66–69why, one step back
66 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 back
70

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 71–72why, one step back
71 assert.equal(writeSpy.mock.callCount(), 1);
72 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 73why, one step back
73

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 74–79why, one step back
74 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 back
80

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 81–83why, one step back
81 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 back
84

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 85–88why, one step back
85 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 back
89

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 90–91why, one step back
90 assert.equal(writeSpy.mock.callCount(), 1);
91 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 92why, one step back
92

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 93–98why, one step back
93 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 back
99

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 100–101why, one step back
100 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 back
102 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 103why, one step back
103

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 104–108why, one step back
104 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 back
109

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 110–114why, one step back
110 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 back
115 formatHelp: (cmd, helper) => {
116 helpWidth = helper.helpWidth;117 return '';

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 118why, one step back
118 },

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 119why, one step back
119 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 120why, one step back
120 program.outputHelp();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 121why, one step back
121

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 122–125why, one step back
122 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 back
126

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 127–129why, one step back
127 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 back
130

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 131–144why, one step back
131 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 back
145 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 146why, one step back
146

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 147–151why, one step back
147 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 back
152

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 153–156why, one step back
153 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 back
157 formatHelp: (cmd, helper) => {
158 helpWidth = helper.helpWidth;159 return '';

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 160why, one step back
160 },

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 161why, one step back
161 });

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 162why, one step back
162 program.outputHelp({ error: true });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 163why, one step back
163

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 164–167why, one step back
164 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 back
168

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 169–171why, one step back
169 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 back
172

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 173–184why, one step back
173 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 back
185

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 186–187why, one step back
186 assert.equal(helpWidth, expectedColumns);
187 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 188why, one step back
188

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 189–191why, one step back
189 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 back
192

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 193–205why, one step back
193 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 back
206

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 207–208why, one step back
207 assert.equal(helpWidth, expectedColumns);
208 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 209why, one step back
209

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 210–212why, one step back
210 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 back
213

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 214–226why, one step back
214 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 back
227

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 228–229why, one step back
228 assert.equal(helpWidth, expectedColumns);
229 });

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

line 230why, one step back
230

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 231–244why, one step back
231 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 back
245

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 246–253why, one step back
246 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 back
254

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 255–261why, one step back
255 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 back
262 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 263why, one step back
263

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 264–277why, one step back
264 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 back
278 });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 279why, one step back
279

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 280–285why, one step back
280 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 back
286

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 287–297why, one step back
287 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 back
298 });

Support color ansi code sequences in custom help (#2251)John Geebefore adoption5629947

line 299why, one step back
299

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 300–304why, one step back
300 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 back
305

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

lines 306–342why, one step back
306 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 back
343 });

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 344–345why, one step back
344 });
345});