From b7a6e901dd302feb4e88ae147dd74b4c94a280a1 Mon Sep 17 00:00:00 2001 From: Konrad Baumgart Date: Fri, 10 Dec 2021 08:36:03 +0100 Subject: [PATCH] completion tests for executing yargs with or without script name --- test/completion.cjs | 614 +++++++++++++++++++++----------------------- 1 file changed, 288 insertions(+), 326 deletions(-) diff --git a/test/completion.cjs b/test/completion.cjs index df3e07072..d4b934178 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -16,363 +16,325 @@ describe('Completion', () => { }); describe('default completion behavior', () => { - it('avoids repeating already included commands', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage( - () => - yargs(['./completion', '--get-yargs-completions', 'apple']) - .command('foo', 'bar') - .command('apple', 'banana').argv - ); - - // should not suggest foo for completion unless foo is subcommand of apple - r.logs.should.not.include('apple'); - }); - - it('avoids repeating already included options', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--foo', - '--', - ]) - .options({ - foo: {describe: 'foo option'}, - bar: {describe: 'bar option'}, - }) - .completion().argv - ); - - r.logs.should.include('--bar'); - r.logs.should.not.include('--foo'); - }); - - it('avoids repeating options whose aliases are already included', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '-f', - '--', - ]) - .options({ - foo: {describe: 'foo option', alias: 'f'}, - bar: {describe: 'bar option'}, - }) - .completion().argv - ); - - r.logs.should.include('--bar'); - r.logs.should.not.include('--foo'); - }); + const firstArgumentOptions = [ + ['--get-yargs-completions'], // proper args according to docs + ['./completion', '--get-yargs-completions'], // yargs is called like this in tests, don't know why + ]; + for (const firstArguments of firstArgumentOptions) { + describe(`calling yargs(${firstArguments.join(', ')}, …)`, () => { + it('avoids repeating already included commands', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, 'apple']) + .command('foo', 'bar') + .command('apple', 'banana').argv + ); + + // should not suggest foo for completion unless foo is subcommand of apple + r.logs.should.not.include('apple'); + }); - it('completes short options with a single dash when the user did not already enter two dashes', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '', - ]).options({ - f: {describe: 'f option', alias: 'foo'}, - }).argv - ); + it('avoids repeating already included options', () => { + const r = checkUsage( + () => + yargs([...firstArguments, './completion', '--foo', '--']) + .options({ + foo: {describe: 'foo option'}, + bar: {describe: 'bar option'}, + }) + .completion().argv + ); - r.logs.should.include('-f'); - r.logs.should.not.include('--f'); - }); + r.logs.should.include('--bar'); + r.logs.should.not.include('--foo'); + }); - it('completes short options with two dashes when the user already entered two dashes', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--', - ]).options({ - f: {describe: 'f option', alias: 'foo'}, - }).argv - ); + it('avoids repeating options whose aliases are already included', () => { + const r = checkUsage( + () => + yargs([...firstArguments, './completion', '-f', '--']) + .options({ + foo: {describe: 'foo option', alias: 'f'}, + bar: {describe: 'bar option'}, + }) + .completion().argv + ); - r.logs.should.include('--f'); - r.logs.should.not.include('-f'); - }); + r.logs.should.include('--bar'); + r.logs.should.not.include('--foo'); + }); - it('completes single digit options with two dashes', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '', - ]).options({ - 1: {describe: '1 option', alias: 'one'}, - }).argv - ); + it('completes short options with a single dash when the user did not already enter two dashes', () => { + const r = checkUsage( + () => + yargs([...firstArguments, './completion', '']).options({ + f: {describe: 'f option', alias: 'foo'}, + }).argv + ); - r.logs.should.include('--1'); - r.logs.should.not.include('-1'); - }); + r.logs.should.include('-f'); + r.logs.should.not.include('--f'); + }); - it('completes with no- prefix flags defaulting to true when boolean-negation is set', () => { - const r = checkUsage( - () => - yargs(['./completion', '--get-yargs-completions', './completion', '']) - .options({ - foo: {describe: 'foo flag', type: 'boolean', default: true}, - bar: {describe: 'bar flag', type: 'boolean'}, - }) - .parserConfiguration({'boolean-negation': true}).argv - ); + it('completes short options with two dashes when the user already entered two dashes', () => { + const r = checkUsage( + () => + yargs([...firstArguments, './completion', '--']).options({ + f: {describe: 'f option', alias: 'foo'}, + }).argv + ); - r.logs.should.include('--no-foo'); - r.logs.should.include('--foo'); - r.logs.should.not.include('--no-bar'); - r.logs.should.include('--bar'); - }); + r.logs.should.include('--f'); + r.logs.should.not.include('-f'); + }); - it('avoids repeating flags whose negated counterparts are already included', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--no-foo', - '--no-bar', - '', - ]) - .options({ - foo: {describe: 'foo flag', type: 'boolean', default: true}, - bar: {describe: 'bar flag', type: 'boolean'}, - baz: {describe: 'bar flag', type: 'boolean'}, - }) - .parserConfiguration({'boolean-negation': true}).argv - ); + it('completes single digit options with two dashes', () => { + const r = checkUsage( + () => + yargs([...firstArguments, './completion', '']).options({ + 1: {describe: '1 option', alias: 'one'}, + }).argv + ); - r.logs.should.not.include('--no-foo'); - r.logs.should.not.include('--foo'); - r.logs.should.not.include('--no-bar'); - r.logs.should.not.include('--bar'); - r.logs.should.include('--baz'); - }); + r.logs.should.include('--1'); + r.logs.should.not.include('-1'); + }); - it('ignores no- prefix flags when boolean-negation is not set', () => { - const r = checkUsage( - () => - yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--no-bar', - '', - ]).options({ - foo: {describe: 'foo flag', type: 'boolean', default: true}, - bar: {describe: 'bar flag', type: 'boolean'}, - }).argv - ); + it('completes with no- prefix flags defaulting to true when boolean-negation is set', () => { + const r = checkUsage( + () => + yargs([...firstArguments, './completion', '']) + .options({ + foo: {describe: 'foo flag', type: 'boolean', default: true}, + bar: {describe: 'bar flag', type: 'boolean'}, + }) + .parserConfiguration({'boolean-negation': true}).argv + ); - r.logs.should.not.include('--no-foo'); - r.logs.should.include('--foo'); - r.logs.should.not.include('--no-bar'); - r.logs.should.include('--bar'); - }); + r.logs.should.include('--no-foo'); + r.logs.should.include('--foo'); + r.logs.should.not.include('--no-bar'); + r.logs.should.include('--bar'); + }); - it('completes options for the correct command', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage( - () => - yargs(['./completion', '--get-yargs-completions', 'cmd2', '--o']) - .help(false) - .version(false) - .command('cmd1', 'first command', subYargs => { - subYargs.options({ - opt1: { - describe: 'first option', - }, - }); - }) - .command('cmd2', 'second command', subYargs => { - subYargs.options({ - opt2: { - describe: 'second option', - }, - }); - }) - .completion().argv - ); + it('avoids repeating flags whose negated counterparts are already included', () => { + const r = checkUsage( + () => + yargs([ + ...firstArguments, + './completion', + '--no-foo', + '--no-bar', + '', + ]) + .options({ + foo: {describe: 'foo flag', type: 'boolean', default: true}, + bar: {describe: 'bar flag', type: 'boolean'}, + baz: {describe: 'bar flag', type: 'boolean'}, + }) + .parserConfiguration({'boolean-negation': true}).argv + ); + + r.logs.should.not.include('--no-foo'); + r.logs.should.not.include('--foo'); + r.logs.should.not.include('--no-bar'); + r.logs.should.not.include('--bar'); + r.logs.should.include('--baz'); + }); - r.logs.should.have.length(1); - r.logs.should.include('--opt2'); - }); + it('ignores no- prefix flags when boolean-negation is not set', () => { + const r = checkUsage( + () => + yargs([ + ...firstArguments, + './completion', + '--no-bar', + '', + ]).options({ + foo: {describe: 'foo flag', type: 'boolean', default: true}, + bar: {describe: 'bar flag', type: 'boolean'}, + }).argv + ); + + r.logs.should.not.include('--no-foo'); + r.logs.should.include('--foo'); + r.logs.should.not.include('--no-bar'); + r.logs.should.include('--bar'); + }); - it('ignores positionals for the correct command', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage( - () => - yargs(['./completion', '--get-yargs-completions', 'cmd', '--o']) - .help(false) - .version(false) - .command('cmd', 'command', subYargs => { - subYargs - .options({ - opt: { - describe: 'option', - }, + it('completes options for the correct command', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, 'cmd2', '--o']) + .help(false) + .version(false) + .command('cmd1', 'first command', subYargs => { + subYargs.options({ + opt1: { + describe: 'first option', + }, + }); }) - .positional('pos-opt', {type: 'string'}); - }).argv - ); + .command('cmd2', 'second command', subYargs => { + subYargs.options({ + opt2: { + describe: 'second option', + }, + }); + }) + .completion().argv + ); - r.logs.should.have.length(1); - r.logs.should.include('--opt'); - r.logs.should.not.include('--pos-opt'); - }); + r.logs.should.have.length(1); + r.logs.should.include('--opt2'); + }); - it('does not complete hidden commands', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage( - () => - yargs(['./completion', '--get-yargs-completions', 'cmd']) - .command('cmd1', 'first command') - .command('cmd2', false) - .completion('completion', false).argv - ); + it('ignores positionals for the correct command', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, 'cmd', '--o']) + .help(false) + .version(false) + .command('cmd', 'command', subYargs => { + subYargs + .options({ + opt: { + describe: 'option', + }, + }) + .positional('pos-opt', {type: 'string'}); + }).argv + ); + + r.logs.should.have.length(1); + r.logs.should.include('--opt'); + r.logs.should.not.include('--pos-opt'); + }); - r.logs.should.have.length(1); - r.logs.should.include('cmd1'); - }); + it('does not complete hidden commands', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, 'cmd']) + .command('cmd1', 'first command') + .command('cmd2', false) + .completion('completion', false).argv + ); + + r.logs.should.have.length(1); + r.logs.should.include('cmd1'); + }); - it('does not include positional arguments', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage(() => { - return yargs(['./completion', '--get-yargs-completions', 'cmd']) - .command('cmd1 [arg]', 'first command') - .command('cmd2 ', 'second command') - .completion('completion', false).argv; - }); + it('does not include positional arguments', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage(() => { + return yargs([...firstArguments, 'cmd']) + .command('cmd1 [arg]', 'first command') + .command('cmd2 ', 'second command') + .completion('completion', false).argv; + }); + + r.logs.should.have.length(2); + r.logs.should.include('cmd1'); + r.logs.should.include('cmd2'); + }); - r.logs.should.have.length(2); - r.logs.should.include('cmd1'); - r.logs.should.include('cmd2'); - }); + it('works if command has no options', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, 'foo', '--b']) + .help(false) + .version(false) + .command('foo', 'foo command', subYargs => { + return subYargs.completion().parse(); + }) + .completion().argv + ); - it('works if command has no options', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage( - () => - yargs(['./completion', '--get-yargs-completions', 'foo', '--b']) - .help(false) - .version(false) - .command('foo', 'foo command', subYargs => { - return subYargs.completion().parse(); - }) - .completion().argv - ); + r.logs.should.have.length(0); + }); - r.logs.should.have.length(0); - }); + it("returns arguments as completion suggestion, if next contains '-'", () => { + process.env.SHELL = '/bin/basg'; + const r = checkUsage( + () => + yargs([...firstArguments, '-f']) + .option('foo', { + describe: 'foo option', + }) + .command('bar', 'bar command') + .completion().argv + ); - it("returns arguments as completion suggestion, if next contains '-'", () => { - process.env.SHELL = '/bin/basg'; - const r = checkUsage( - () => - yargs(['./usage', '--get-yargs-completions', '-f']) - .option('foo', { - describe: 'foo option', - }) - .command('bar', 'bar command') - .completion().argv - ); + r.logs.should.include('--foo'); + r.logs.should.not.include('bar'); + }); - r.logs.should.include('--foo'); - r.logs.should.not.include('bar'); - }); + it('completes choices if previous option requires a choice', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage(() => { + return yargs([...firstArguments, './completion', '--fruit']) + .options({ + fruit: { + describe: 'fruit option', + choices: ['apple', 'banana', 'pear'], + }, + amount: {describe: 'amount', type: 'number'}, + }) + .completion('completion', false).argv; + }); - it('completes choices if previous option requires a choice', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage(() => { - return yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--fruit', - ]) - .options({ - fruit: { - describe: 'fruit option', - choices: ['apple', 'banana', 'pear'], - }, - amount: {describe: 'amount', type: 'number'}, - }) - .completion('completion', false).argv; - }); + r.logs.should.have.length(3); + r.logs.should.include('apple'); + r.logs.should.include('banana'); + r.logs.should.include('pear'); + }); - r.logs.should.have.length(3); - r.logs.should.include('apple'); - r.logs.should.include('banana'); - r.logs.should.include('pear'); - }); + it('completes choices if previous option requires a choice and space has been entered', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage(() => { + return yargs([...firstArguments, './completion', '--fruit', '']) + .options({ + fruit: { + describe: 'fruit option', + choices: ['apple', 'banana', 'pear'], + }, + amount: {describe: 'amount', type: 'number'}, + }) + .completion('completion', false).argv; + }); - it('completes choices if previous option requires a choice and space has been entered', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage(() => { - return yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--fruit', - '', - ]) - .options({ - fruit: { - describe: 'fruit option', - choices: ['apple', 'banana', 'pear'], - }, - amount: {describe: 'amount', type: 'number'}, - }) - .completion('completion', false).argv; - }); + r.logs.should.have.length(3); + r.logs.should.include('apple'); + r.logs.should.include('banana'); + r.logs.should.include('pear'); + }); - r.logs.should.have.length(3); - r.logs.should.include('apple'); - r.logs.should.include('banana'); - r.logs.should.include('pear'); - }); + it('completes choices if previous option requires a choice and a partial choice has been entered', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage(() => { + return yargs([...firstArguments, './completion', '--fruit', 'ap']) + .options({ + fruit: { + describe: 'fruit option', + choices: ['apple', 'banana', 'pear'], + }, + amount: {describe: 'amount', type: 'number'}, + }) + .completion('completion', false).argv; + }); - it('completes choices if previous option requires a choice and a partial choice has been entered', () => { - process.env.SHELL = '/bin/bash'; - const r = checkUsage(() => { - return yargs([ - './completion', - '--get-yargs-completions', - './completion', - '--fruit', - 'ap', - ]) - .options({ - fruit: { - describe: 'fruit option', - choices: ['apple', 'banana', 'pear'], - }, - amount: {describe: 'amount', type: 'number'}, - }) - .completion('completion', false).argv; + r.logs.should.have.length(1); + r.logs.should.include('apple'); + r.logs.should.not.include('banana'); + r.logs.should.not.include('pear'); + }); }); - - r.logs.should.have.length(1); - r.logs.should.include('apple'); - r.logs.should.not.include('banana'); - r.logs.should.not.include('pear'); - }); + } }); describe('generateCompletionScript()', () => {