Skip to content

Commit

Permalink
Rework end-to-end style test to use outputHelp()
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Nov 10, 2024
1 parent ed54240 commit f5aabe7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
13 changes: 6 additions & 7 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2281,7 +2281,6 @@ Expecting one of '${allowedValues.join("', '")}'`);
* @property {number} helpWidth
* @property {boolean} hasColors
* @property {function} write - includes stripAnsi if needed
* @property {function} plainWrite
*
* @returns {HelpContext}
* @private
Expand All @@ -2290,23 +2289,23 @@ Expecting one of '${allowedValues.join("', '")}'`);
_getOutputContext(contextOptions) {
contextOptions = contextOptions || {};
const error = !!contextOptions.error;
let plainWrite;
let baseWrite;
let hasColors;
let helpWidth;
if (error) {
plainWrite = (str) => this._outputConfiguration.writeErr(str);
baseWrite = (str) => this._outputConfiguration.writeErr(str);
hasColors = this._outputConfiguration.getErrHasColors();
helpWidth = this._outputConfiguration.getErrHelpWidth();
} else {
plainWrite = (str) => this._outputConfiguration.writeOut(str);
baseWrite = (str) => this._outputConfiguration.writeOut(str);
hasColors = this._outputConfiguration.getOutHasColors();
helpWidth = this._outputConfiguration.getOutHelpWidth();
}
const write = (str) => {
if (!hasColors) str = this._outputConfiguration.stripAnsi(str);
return plainWrite(str);
return baseWrite(str);
};
return { error, write, plainWrite, hasColors, helpWidth };
return { error, write, hasColors, helpWidth };
}

/**
Expand Down Expand Up @@ -2347,7 +2346,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
throw new Error('outputHelp callback must return a string or a Buffer');
}
}
outputContext.plainWrite(helpInformation);
outputContext.write(helpInformation);

if (this._getHelpOption()?.long) {
this.emit(this._getHelpOption().long); // deprecated
Expand Down
62 changes: 31 additions & 31 deletions tests/help.style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,52 +197,52 @@ describe('override style methods and check help information', () => {
});

describe('check styles with configureOutput overrides for color', () => {
test('when getOutHasColors returns true then helpInformation has color', () => {
const program = new Command('program')
function makeProgram(hasColors) {
const program = new Command('program');
program.myHelpText = [];
program
.description('program description')
.argument('<file>', 'arg description')
.configureOutput({
getOutHasColors: () => true,
stripAnsi: stripRed,
getOutHasColors: () => hasColors,
stripAnsi: (str) => stripRed(str),
writeOut: (str) => {
program.myHelpText.push(str);
},
});
program.configureHelp({
styleCommandText: (str) => red(str),
displayWidth,
});
const helpText = program.helpInformation();

return program;
}

test('when getOutHasColors returns true then help has color', () => {
const program = makeProgram(true);
program.outputHelp();
const helpText = program.myHelpText.join('');
expect(helpText).toMatch(red('program'));
});

test('when getOutHasColors returns false then helpInformation does not have color', () => {
const program = new Command('program')
.description('program description')
.configureOutput({
getOutHasColors: () => false,
stripAnsi: stripRed,
});
program.configureHelp({
styleCommandText: (str) => red(str),
displayWidth,
});
const helpText = program.helpInformation();
test('when getOutHasColors returns false then help does not have color', () => {
const program = makeProgram(false);
program.outputHelp();
const helpText = program.myHelpText.join('');
expect(helpText).not.toMatch(red('program'));
});

test('when getOutHasColors returns false then style still called', () => {
const program = new Command('program')
.description('program description')
.configureOutput({
getOutHasColors: () => false,
stripAnsi: stripRed,
});
const program = makeProgram(true);
// Overwrite styleCommandText so we can track whether called.
let styleCalled = false;
program.configureHelp({
styleCommandText: (str) => {
styleCalled = true;
red(str);
},
displayWidth,
});
const helpText = program.helpInformation();
const config = program.configureHelp();
config.styleCommandText = (str) => {
styleCalled = true;
return red(str);
};
program.configureHelp(config);
program.outputHelp();
expect(styleCalled).toBe(true);
});
});

0 comments on commit f5aabe7

Please sign in to comment.