Skip to content

Commit

Permalink
fix: no longer include default command when listing subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Xunnamius committed Mar 21, 2024
1 parent 68092dc commit be2960a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ export async function discoverCommands(
// ! This next line excludes aliases and positionals in an attempt to
// ! address yargs bugs around help text output. See the docs for details.
['$0'],
config.description,
false,
makeVanillaYargsBuilder(programs.helper, config, 'first-pass'),
async (parsedArgv) => {
const debug_ = debug.extend('helper');
Expand Down
5 changes: 0 additions & 5 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ export function getFixturePath(fixture: string | string[]) {
export function expectedCommandsRegex(
childCommands: (string | [command: string, descriptionRegex: string])[],
parentFullName = require(`./fixtures/package.json`).name,
parentDescriptionRegex?: string,
childDescriptionRegex = '[A-Z]',
includeFinalNewline = true
) {
return new RegExp(
'Commands:\\n\\s+' +
parentFullName +
`\\s+${parentDescriptionRegex ?? childDescriptionRegex}[^\\n]*${
parentDescriptionRegex ? '' : '\\s+\\[default][^\\n]*'
}\\n` +
childCommands
.map(
(cmd) =>
Expand Down
42 changes: 9 additions & 33 deletions test/unit-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2244,12 +2244,7 @@ describe('<command module auto-discovery>', () => {

expect(logSpy.mock.calls[0]).toStrictEqual([
expect.stringMatching(
expectedCommandsRegex(
['n'],
'nsf',
'Root program.+\\s+\\[default]\\s+\\[deprecated]',
'Parent program.+\\s+\\[deprecated]'
)
expectedCommandsRegex(['n'], 'nsf', 'Parent program.+\\s+\\[deprecated]')
)
]);

Expand All @@ -2258,7 +2253,6 @@ describe('<command module auto-discovery>', () => {
expectedCommandsRegex(
[['f', 'Child program.+\\s+\\[deprecated]'], 's', 't'],
'nsf n',
'Parent program.+\\s+\\[default]\\s+\\[deprecated]',
'Child program.+\\s+\\[(?!deprecated)[^\\n]*]'
)
)
Expand All @@ -2278,10 +2272,6 @@ describe('<command module auto-discovery>', () => {

expect(errorSpy).not.toHaveBeenCalled();

expect(logSpy.mock.calls).toStrictEqual([
[expect.stringContaining('[deprecated: this is a deprecation message!]')]
]);

expect(logSpy.mock.calls).toStrictEqual([
[expect.stringContaining('[deprecated: deprecation message 2]')]
]);
Expand All @@ -2303,13 +2293,11 @@ describe('<command module auto-discovery>', () => {
expect(bf_util.isNullArguments(parentResult)).toBeTrue();

expect(logSpy.mock.calls[0]).toStrictEqual([
expect.stringMatching(expectedCommandsRegex(['n'], 'nsf', 'Root', 'Parent'))
expect.stringMatching(expectedCommandsRegex(['n'], 'nsf', 'Parent'))
]);

expect(logSpy.mock.calls[1]).toStrictEqual([
expect.stringMatching(
expectedCommandsRegex(['f', 's', 't'], 'nsf n', 'Parent', 'Child')
)
expect.stringMatching(expectedCommandsRegex(['f', 's', 't'], 'nsf n', 'Child'))
]);
});
});
Expand All @@ -2331,15 +2319,11 @@ describe('<command module auto-discovery>', () => {
expect(bf_util.isNullArguments(childResult)).toBeTrue();

expect(logSpy.mock.calls[0]).toStrictEqual([
expect.stringMatching(
/Commands:\n\s+test\s+\[default]\n\s+test nested\n\nOptions:/
)
expect.stringMatching(/Commands:\n\s+test nested\n\nOptions:/)
]);

expect(logSpy.mock.calls[1]).toStrictEqual([
expect.stringMatching(
/Commands:\n\s+test nested\s+\[default]\n\s+test nested child\n\nOptions:/
)
expect.stringMatching(/Commands:\n\s+test nested child\n\nOptions:/)
]);

expect(logSpy.mock.calls[2]).toStrictEqual([
Expand Down Expand Up @@ -2466,7 +2450,6 @@ describe('<command module auto-discovery>', () => {
expectedCommandsRegex(
['s-p-a-c-e-d-name', 'spaced-name'],
'badly-named-package',
'',
''
)
)
Expand All @@ -2476,7 +2459,6 @@ describe('<command module auto-discovery>', () => {
expectedCommandsRegex(
['bad-ly-name-d'],
'badly-named-package spaced-name',
'',
''
)
)
Expand Down Expand Up @@ -2842,12 +2824,12 @@ describe('<command module auto-discovery>', () => {
expect(logSpy.mock.calls).toStrictEqual([
[
expect.stringMatching(
/Commands:\n\s+nsf\s+Root program description text\s+\[default] \[deprecated]\n\s+nsf n\s+Parent program description text\s+\[aliases: parent, p] \[deprecated]\n\nOptions:/
/Commands:\n\s+nsf n\s+Parent program description text\s+\[aliases: parent, p] \[deprecated]\n\nOptions:/
)
],
[
expect.stringMatching(
/Commands:\n\s+nsf n\s+Parent program description text\s+\[default] \[deprecated]\n\s+nsf n f\s+Child program description text\s+\[aliases: child-1] \[deprecated]\n\s+nsf n s\s+Child program description text\s+\[aliases: child-2]\n\s+nsf n t\s+Child program description text\s+\[aliases: child-3]\n\nOptions:/
/Commands:\n\s+nsf n f\s+Child program description text\s+\[aliases: child-1] \[deprecated]\n\s+nsf n s\s+Child program description text\s+\[aliases: child-2]\n\s+nsf n t\s+Child program description text\s+\[aliases: child-3]\n\nOptions:/
)
],
[expect.not.stringContaining('Commands:')]
Expand Down Expand Up @@ -3154,7 +3136,7 @@ describe('<command module auto-discovery>', () => {
await bf.runProgram(getFixturePath('nested-same-names'), '--help');

expect(logSpy.mock.calls).toStrictEqual([
[expect.stringMatching(expectedCommandsRegex(['conflict'], 'conflict', '', ''))]
[expect.stringMatching(expectedCommandsRegex(['conflict'], 'conflict', ''))]
]);
});
});
Expand Down Expand Up @@ -3274,7 +3256,6 @@ describe('<command module auto-discovery>', () => {
['no-conflict', '\\[aliases: alias3]']
],
'name',
'',
''
)
)
Expand All @@ -3292,12 +3273,7 @@ describe('<command module auto-discovery>', () => {
expect(logSpy.mock.calls).toStrictEqual([
[
expect.stringMatching(
expectedCommandsRegex(
['five', 'four', 'one', 'three', 'two'],
'alpha',
'',
''
)
expectedCommandsRegex(['five', 'four', 'one', 'three', 'two'], 'alpha', '')
)
]
]);
Expand Down

0 comments on commit be2960a

Please sign in to comment.