Skip to content

Commit

Permalink
Add more style tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Nov 10, 2024
1 parent 7310ffb commit ed54240
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
21 changes: 11 additions & 10 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,9 @@ Expecting one of '${allowedValues.join("', '")}'`);
helpWidth: context.helpWidth,
outputHasColors: context.hasColors,
});
return helper.formatHelp(this, helper);
const text = helper.formatHelp(this, helper);
if (context.hasColors) return text;
return this._outputConfiguration.stripAnsi(text);
}

/**
Expand All @@ -2278,7 +2280,8 @@ Expecting one of '${allowedValues.join("', '")}'`);
* @property {boolean} error
* @property {number} helpWidth
* @property {boolean} hasColors
* @property {function} write
* @property {function} write - includes stripAnsi if needed
* @property {function} plainWrite
*
* @returns {HelpContext}
* @private
Expand All @@ -2287,25 +2290,23 @@ Expecting one of '${allowedValues.join("', '")}'`);
_getOutputContext(contextOptions) {
contextOptions = contextOptions || {};
const error = !!contextOptions.error;
let baseWrite;
let plainWrite;
let hasColors;
let helpWidth;
if (error) {
baseWrite = (str) => this._outputConfiguration.writeErr(str);
plainWrite = (str) => this._outputConfiguration.writeErr(str);
hasColors = this._outputConfiguration.getErrHasColors();
helpWidth = this._outputConfiguration.getErrHelpWidth();
hasColors = this._outputConfiguration.getErrHasColors();
} else {
baseWrite = (str) => this._outputConfiguration.writeOut(str);
plainWrite = (str) => this._outputConfiguration.writeOut(str);
hasColors = this._outputConfiguration.getOutHasColors();
helpWidth = this._outputConfiguration.getOutHelpWidth();
hasColors = this._outputConfiguration.getOutHasColors();
}
const write = (str) => {
if (!hasColors) str = this._outputConfiguration.stripAnsi(str);
return baseWrite(str);
return plainWrite(str);
};
return { error, write, hasColors, helpWidth };
return { error, write, plainWrite, hasColors, helpWidth };
}

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

if (this._getHelpOption()?.long) {
this.emit(this._getHelpOption().long); // deprecated
Expand Down
63 changes: 58 additions & 5 deletions tests/help.style.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const { Command } = require('../');

function red(str) {
// Dummy colouring to keep test outputs plain text for easier visual compares.
// Use plain characters so not stripped in Jest failure messages. (Means displayWidth is bogus though.)
return `RED ${str} DER`;
}
function stripRed(str) {
return str.replace(/RED /g, '').replace(/ DER/g, '');
}
function displayWidth(str) {
return str.replace(/RED /g, '').replace(/ DER/g, '').length;
// Not really zero width for the "color", but pretend so spacing matches no-color output.
return stripRed(str).length;
}

describe('override each style method and check help information', () => {
describe('override style methods and check help information', () => {
function makeProgram() {
const program = new Command('program')
.description('program description')
Expand All @@ -29,7 +33,7 @@ describe('override each style method and check help information', () => {

test('styleTitle', () => {
const program = makeProgram();
program.configureHelp({ styleTitle: (str) => red(str), displayWidth });
program.configureHelp({ styleTitle: (str) => red(str) }, displayWidth);
const helpText = program.helpInformation();
expect(helpText).toEqual(
plainHelpInformation
Expand Down Expand Up @@ -192,4 +196,53 @@ describe('override each style method and check help information', () => {
});
});

describe('ENV overrides for Help styles', () => {});
describe('check styles with configureOutput overrides for color', () => {
test('when getOutHasColors returns true then helpInformation has color', () => {
const program = new Command('program')
.description('program description')
.configureOutput({
getOutHasColors: () => true,
stripAnsi: stripRed,
});
program.configureHelp({
styleCommandText: (str) => red(str),
displayWidth,
});
const helpText = program.helpInformation();
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();
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,
});
let styleCalled = false;
program.configureHelp({
styleCommandText: (str) => {
styleCalled = true;
red(str);
},
displayWidth,
});
const helpText = program.helpInformation();
expect(styleCalled).toBe(true);
});
});

0 comments on commit ed54240

Please sign in to comment.