Skip to content
Myr

help.style.test.js

This file

Path

tests/help.style.test.js

296 lines · 8.4 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 1–3why, one step back
1import { test, describe } from 'node:test';
2import assert from 'node:assert/strict';3import { Command } from '../index.js';

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

lines 4–5why, one step back
4
5function red(str) {

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

line 6why, one step back
6 // Use plain characters so not stripped in failure messages. (Means displayWidth is bogus though.)

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

lines 7–16why, one step back
7 return `RED ${str} DER`;
8}9function stripRed(str) {10 return str.replace(/RED /g, '').replace(/ DER/g, '');11}12function displayWidth(str) {13 // Not really zero width for the "color", but pretend so spacing matches no-color output.14 return stripRed(str).length;15}16

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

line 17why, one step back
17describe('Help.configureHelp() overrides of style methods', () => {

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

lines 18–39why, one step back
18 function makeProgram() {
19 const program = new Command('program')20 .description('program description')21 .argument('<file>', 'arg description')22 .configureOutput({23 getOutHasColors: () => true, // avoid interactions with testing environment24 });25 program26 .command('subcommand')27 .description('sub description')28 .option('--suboption')29 .argument('[subarg]');3031 return program;32 }3334 const plainHelpInformation = makeProgram().helpInformation();3536 test('styleTitle', () => {37 const program = makeProgram();38 program.configureHelp({ styleTitle: (str) => red(str) }, displayWidth);39 const helpText = program.helpInformation();

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

lines 40–41why, one step back
40 assert.equal(
41 helpText,

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

lines 42–54why, one step back
42 plainHelpInformation
43 .replace('Usage:', red('Usage:'))44 .replace('Arguments:', red('Arguments:'))45 .replace('Options:', red('Options:'))46 .replace('Commands:', red('Commands:')),47 );48 });4950 test('styleUsage', () => {51 const program = makeProgram();52 program.configureHelp({ styleUsage: (str) => red(str), displayWidth });53 const helpText = program.helpInformation();54 const usageString = program.createHelp().commandUsage(program);

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

lines 55–56why, one step back
55 assert.equal(
56 helpText,

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

lines 57–60why, one step back
57 plainHelpInformation.replace(usageString, red(usageString)),
58 );59 });60

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

line 61why, one step back
61 test('styleCommandDescription', () => {

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

lines 62–63why, one step back
62 const program = makeProgram();
63 program.configureHelp({

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

line 64why, one step back
64 styleCommandDescription: (str) => red(str),

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

lines 65–67why, one step back
65 displayWidth,
66 });67 const helpText = program.helpInformation();

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

lines 68–69why, one step back
68 assert.equal(
69 helpText,

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

lines 70–73why, one step back
70 plainHelpInformation.replace(
71 'program description',72 red('program description'),73 ),

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

lines 74–76why, one step back
74 );
75 });76

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

line 77why, one step back
77 test('styleOptionDescription', () => {

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

lines 78–79why, one step back
78 const program = makeProgram();
79 program.configureHelp({

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

line 80why, one step back
80 styleOptionDescription: (str) => red(str),

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

lines 81–83why, one step back
81 displayWidth,
82 });83 const helpText = program.helpInformation();

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

lines 84–85why, one step back
84 assert.equal(
85 helpText,

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

line 86why, one step back
86 plainHelpInformation.replace(

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

lines 87–88why, one step back
87 /(-h, --help *)(display help for command)/,
88 (match, p1, p2) => p1 + red(p2),

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

line 89why, one step back
89 ),

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

lines 90–99why, one step back
90 );
91 });9293 test('styleSubcommandDescription', () => {94 const program = makeProgram();95 program.configureHelp({96 styleSubcommandDescription: (str) => red(str),97 displayWidth,98 });99 const helpText = program.helpInformation();

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

lines 100–101why, one step back
100 assert.equal(
101 helpText,

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

lines 102–120why, one step back
102 plainHelpInformation
103 .replace(104 /(\[subarg\] *)(sub description)/,105 (match, p1, p2) => p1 + red(p2),106 )107 .replace(108 /(help \[command\] *)(display help for command)/,109 (match, p1, p2) => p1 + red(p2),110 ),111 );112 });113114 test('styleArgumentDescription', () => {115 const program = makeProgram();116 program.configureHelp({117 styleArgumentDescription: (str) => red(str),118 displayWidth,119 });120 const helpText = program.helpInformation();

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

lines 121–122why, one step back
121 assert.equal(
122 helpText,

Split styleItemDescription into option/subcommand/argument (#2283)John Geebefore adoption437e4e3

line 123why, one step back
123 plainHelpInformation.replace('arg description', red('arg description')),

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

lines 124–133why, one step back
124 );
125 });126127 test('styleDescriptionText', () => {128 const program = makeProgram();129 program.configureHelp({130 styleDescriptionText: (str) => red(str),131 displayWidth,132 });133 const helpText = program.helpInformation();

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

lines 134–135why, one step back
134 assert.equal(
135 helpText,

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

lines 136–147why, one step back
136 plainHelpInformation
137 .replace('program description', red('program description'))138 .replace('arg description', red('arg description'))139 .replace('sub description', red('sub description'))140 .replace(/display help for command/g, red('display help for command')),141 );142 });143144 test('styleOptionTerm', () => {145 const program = makeProgram();146 program.configureHelp({ styleOptionTerm: (str) => red(str), displayWidth });147 const helpText = program.helpInformation();

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

lines 148–149why, one step back
148 assert.equal(
149 helpText,

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

lines 150–160why, one step back
150 plainHelpInformation.replace('-h, --help', red('-h, --help')),
151 );152 });153154 test('styleSubcommandTerm', () => {155 const program = makeProgram();156 program.configureHelp({157 styleSubcommandTerm: (str) => red(str),158 displayWidth,159 });160 const helpText = program.helpInformation();

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

lines 161–162why, one step back
161 assert.equal(
162 helpText,

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

lines 163–178why, one step back
163 plainHelpInformation
164 .replace('help [command]', red('help [command]'))165 .replace(166 'subcommand [options] [subarg]',167 red('subcommand [options] [subarg]'),168 ),169 );170 });171172 test('styleArgumentTerm', () => {173 const program = makeProgram();174 program.configureHelp({175 styleArgumentTerm: (str) => red(str),176 displayWidth,177 });178 const helpText = program.helpInformation();

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

lines 179–180why, one step back
179 assert.equal(
180 helpText,

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

lines 181–191why, one step back
181 plainHelpInformation.replace(' file ', ` ${red('file')} `),
182 );183 });184185 test('styleOptionText', () => {186 const program = makeProgram();187 program.configureHelp({188 styleOptionText: (str) => red(str),189 displayWidth,190 });191 const helpText = program.helpInformation();

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

lines 192–193why, one step back
192 assert.equal(
193 helpText,

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

lines 194–206why, one step back
194 plainHelpInformation
195 .replace(/\[options\]/g, red('[options]'))196 .replace('-h, --help', red('-h, --help')),197 );198 });199200 test('styleArgumentText', () => {201 const program = makeProgram();202 program.configureHelp({203 styleArgumentText: (str) => red(str),204 displayWidth,205 });206 const helpText = program.helpInformation();

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

lines 207–208why, one step back
207 assert.equal(
208 helpText,

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

lines 209–223why, one step back
209 plainHelpInformation
210 .replace('<file>', red('<file>'))211 .replace(' file ', ` ${red('file')} `)212 .replace('[subarg]', red('[subarg]'))213 .replace('help [command]', `help ${red('[command]')}`),214 );215 });216217 test('styleSubcommandText', () => {218 const program = makeProgram();219 program.configureHelp({220 styleSubcommandText: (str) => red(str),221 displayWidth,222 });223 const helpText = program.helpInformation();

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

lines 224–225why, one step back
224 assert.equal(
225 helpText,

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

lines 226–239why, one step back
226 plainHelpInformation
227 .replace('[command] <file>', `${red('[command]')} <file>`)228 .replace('help [command]', `${red('help')} [command]`)229 .replace('subcommand', red('subcommand')),230 );231 });232233 test('styleCommandText', () => {234 const program = makeProgram();235 program.configureHelp({236 styleCommandText: (str) => red(str),237 displayWidth,238 });239 const helpText = program.helpInformation();

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

lines 240–241why, one step back
240 assert.equal(
241 helpText,

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

lines 242–246why, one step back
242 plainHelpInformation.replace('program', red('program')),
243 );244 });245});246

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

line 247why, one step back
247describe('Help.configureHelp() overrides of color methods', () => {

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

lines 248–272why, one step back
248 function makeProgram(hasColors) {
249 const program = new Command('program');250 program.myHelpText = [];251 program252 .description('program description')253 .argument('<file>', 'arg description')254 .configureOutput({255 getOutHasColors: () => hasColors,256 stripColor: (str) => stripRed(str),257 writeOut: (str) => {258 program.myHelpText.push(str);259 },260 });261 program.configureHelp({262 styleCommandText: (str) => red(str),263 displayWidth,264 });265266 return program;267 }268269 test('when getOutHasColors returns true then help has color', () => {270 const program = makeProgram(true);271 program.outputHelp();272 const helpText = program.myHelpText.join('');

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

line 273why, one step back
273 assert.match(helpText, new RegExp(red('program')));

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

lines 274–279why, one step back
274 });
275276 test('when getOutHasColors returns false then help does not have color', () => {277 const program = makeProgram(false);278 program.outputHelp();279 const helpText = program.myHelpText.join('');

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

line 280why, one step back
280 assert.doesNotMatch(helpText, new RegExp(red('program')));

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

lines 281–293why, one step back
281 });
282283 test('when getOutHasColors returns false then style still called', () => {284 const program = makeProgram(true);285 // Overwrite styleCommandText so we can track whether called.286 let styleCalled = false;287 const config = program.configureHelp();288 config.styleCommandText = (str) => {289 styleCalled = true;290 return red(str);291 };292 program.configureHelp(config);293 program.outputHelp();

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

line 294why, one step back
294 assert.equal(styleCalled, true);

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

lines 295–296why, one step back
295 });
296});