Check for conflicts in parent commands (#1711)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Rename test file * Add test for confilcts in parent command of called command * Check for conflicts up hierarchy of commands * Suppress help output from test * Remove bogus parameter
authored by John Gee · committed by GitHub · parent 7d7a674 · Browse files · 3 files·+328 added−259 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+328 added−259 removed
- tests:+311 added−256 removed
- other:+17 added−3 removed
Provenance
- Commit
bf205d1- Tree
04e6dba- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
7d7a674
3 files changed+328 added−259 removed
modified:lib/command.js+17 added-3 removed
944 unmodified lines945 }946 947 // Not checking for help first. Unlikely to have mandatory and executable, and can't robustly test for help flags in external command.948 this._checkForMissingMandatoryOptions();949+ this._checkForConflictingOptions();950 951 // executableFile and executableDir might be full path, or just a name952 let executableFile = subcommand._executableFile || `${this._name}-${subcommand._name}`;953 let executableDir = this._executableDir || '';340 unmodified lines1294 }1295 1296 /**1297 * Display an error message if a mandatory option does not have a value.1297- * Lazy calling after checking for help flags from leaf subcommand.1298+ * Called after checking for help flags in leaf subcommand.1299 *1300 * @api private1301 */1302 8 unmodified lines1311 }1312 }1313 1314 /**1314- * Display an error message if conflicting options are used together.1315+ * Display an error message if conflicting options are used together in this.1316 *1317 * @api private1318 */1318- _checkForConflictingOptions() {1319+ _checkForConflictingLocalOptions() {1320 const definedNonDefaultOptions = this.options.filter(1321 (option) => {1322 const optionKey = option.attributeName();1323 if (this.getOptionValue(optionKey) === undefined) {17 unmodified lines1341 });1342 }1343 1344 /**1345+ * Display an error message if conflicting options are used together.1346+ * Called after checking for help flags in leaf subcommand.1347+ *1348+ * @api private1349+ */1350+ _checkForConflictingOptions() {1351+ // Walk up hierarchy so can call in subcommand after checking for displaying help.1352+ for (let cmd = this; cmd; cmd = cmd.parent) {1353+ cmd._checkForConflictingLocalOptions();1354+ }1355+ }1356+1357+ /**1358 * Parse options from `argv` removing known options,1359 * and return argv split into operands and unknown arguments.1360 *1361 * Examples:737 unmodified linesdeleted:tests/command.conflicts.test.js-256 removed
1-const commander = require('../');2-3-describe('command with conflicting options', () => {4- function makeProgram() {5- const actionMock = jest.fn();6- const program = new commander.Command();7- program8- .exitOverride()9- .configureOutput({10- writeErr: () => {}11- })12- .command('foo')13- .addOption(new commander.Option('-s, --silent', "Don't print anything").env('SILENT'))14- .addOption(15- new commander.Option('-j, --json', 'Format output as json').env('JSON').conflicts([16- 'silent'17- ])18- )19- .action(actionMock);20-21- return { program, actionMock };22- }23-24- beforeEach(() => {25- delete process.env.SILENT;26- delete process.env.JSON;27- delete process.env.DUAL;28- delete process.env.NO_DUAL;29- });30-31- test('should call action if there are no explicit conflicting options set', () => {32- const { program, actionMock } = makeProgram();33- program.parse('node test.js foo --json'.split(' '));34- expect(actionMock).toHaveBeenCalledTimes(1);35- expect(actionMock).toHaveBeenCalledWith({ json: true }, expect.any(Object));36- });37-38- test('should call action when there are no implicit conflicting options set', () => {39- const { program, actionMock } = makeProgram();40- program.parse('node test.js foo --silent'.split(' '));41- expect(actionMock).toHaveBeenCalledTimes(1);42- expect(actionMock).toHaveBeenCalledWith(43- { silent: true },44- expect.any(Object)45- );46- });47-48- test('should exit with error if conflicting options were set', () => {49- const { program } = makeProgram();50-51- expect(() => {52- program.parse('node test.js foo --silent --json'.split(' '));53- }).toThrow("error: option '-j, --json' cannot be used with option '-s, --silent'");54- });55-56- test('should report the env variable as the conflicting option source, when conflicting option is set', () => {57- const { program } = makeProgram();58-59- process.env.SILENT = true;60-61- expect(() => {62- program.parse('node test.js foo --json'.split(' '));63- }).toThrow("error: option '-j, --json' cannot be used with environment variable 'SILENT'");64- });65-66- test('should report the env variable as the configured option source, when configured option is set', () => {67- const { program } = makeProgram();68-69- process.env.JSON = true;70-71- expect(() => {72- program.parse('node test.js foo --silent'.split(' '));73- }).toThrow("error: environment variable 'JSON' cannot be used with option '-s, --silent'");74- });75-76- test('should report both env variables as sources, when configured option and conflicting option are set', () => {77- const { program } = makeProgram();78-79- process.env.SILENT = true;80- process.env.JSON = true;81-82- expect(() => {83- program.parse('node test.js foo'.split(' '));84- }).toThrow("error: environment variable 'JSON' cannot be used with environment variable 'SILENT'");85- });86-87- test('should allow default value with a conflicting option', () => {88- const { program, actionMock } = makeProgram();89-90- program.commands[0].addOption(new commander.Option('-d, --debug', 'print debug logs').default(true).conflicts(['silent']));91-92- program.parse('node test.js foo --silent'.split(' '));93-94- expect(actionMock).toHaveBeenCalledTimes(1);95- expect(actionMock).toHaveBeenCalledWith({ debug: true, silent: true }, expect.any(Object));96- });97-98- test('should report conflict on negated option flag', () => {99- const { program } = makeProgram();100-101- program102- .command('bar')103- .addOption(new commander.Option('--red').conflicts(['color']))104- .addOption(new commander.Option('--color'))105- .addOption(new commander.Option('-N, --no-color'));106-107- expect(() => {108- program.parse('node test.js bar --red -N'.split(' '));109- }).toThrow("error: option '--red' cannot be used with option '-N, --no-color'");110- });111-112- test('should report conflict on negated option env variable', () => {113- const { program } = makeProgram();114-115- process.env.NO_COLOR = true;116-117- program118- .command('bar')119- .addOption(new commander.Option('--red').conflicts(['color']))120- .addOption(new commander.Option('--color'))121- .addOption(new commander.Option('-N, --no-color').env('NO_COLOR'));122-123- expect(() => {124- program.parse('node test.js bar --red'.split(' '));125- }).toThrow("error: option '--red' cannot be used with environment variable 'NO_COLOR'");126- });127-128- test('should report correct error for shorthand negated option', () => {129- const { program } = makeProgram();130-131- program132- .command('bar')133- .addOption(new commander.Option('--red'))134- .addOption(new commander.Option('-N, --no-color').conflicts(['red']));135-136- expect(() => {137- program.parse('node test.js bar --red -N'.split(' '));138- }).toThrow("error: option '-N, --no-color' cannot be used with option '--red'");139- });140-141- test('should report correct error for positive option when negated is configured', () => {142- const { program } = makeProgram();143-144- program145- .command('bar')146- .addOption(new commander.Option('--red'))147- .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))148- .addOption(new commander.Option('--no-dual').env('NO_DUAL'));149-150- expect(() => {151- program.parse('node test.js bar --red --dual'.split(' '));152- }).toThrow("error: option '--dual' cannot be used with option '--red'");153- });154-155- test('should report correct error for negated option when positive is configured', () => {156- const { program } = makeProgram();157-158- program159- .command('bar')160- .addOption(new commander.Option('--red'))161- .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))162- .addOption(new commander.Option('--no-dual').env('NO_DUAL'));163-164- expect(() => {165- program.parse('node test.js bar --red --no-dual'.split(' '));166- }).toThrow("error: option '--no-dual' cannot be used with option '--red'");167- });168-169- test('should report correct error for positive env variable when negated is configured', () => {170- const { program } = makeProgram();171-172- program173- .command('bar')174- .addOption(new commander.Option('--red'))175- .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))176- .addOption(new commander.Option('--no-dual').env('NO_DUAL'));177-178- process.env.DUAL = 'true';179- expect(() => {180- program.parse('node test.js bar --red'.split(' '));181- }).toThrow("error: environment variable 'DUAL' cannot be used with option '--red'");182- });183-184- test('should report correct error for negated env variable when positive is configured', () => {185- const { program } = makeProgram();186-187- program188- .command('bar')189- .addOption(new commander.Option('--red'))190- .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))191- .addOption(new commander.Option('--no-dual').env('NO_DUAL'));192-193- process.env.NO_DUAL = 'true';194- expect(() => {195- program.parse('node test.js bar --red'.split(' '));196- }).toThrow("error: environment variable 'NO_DUAL' cannot be used with option '--red'");197- });198-199- test('should report correct error for positive option with string value when negated is configured', () => {200- const { program } = makeProgram();201-202- program203- .command('bar')204- .addOption(new commander.Option('--red'))205- .addOption(new commander.Option('--dual2 <str>').conflicts(['red']))206- .addOption(new commander.Option('--no-dual2').preset('BAD'));207-208- expect(() => {209- program.parse('node test.js bar --red --dual2 foo'.split(' '));210- }).toThrow("error: option '--dual2 <str>' cannot be used with option '--red'");211- });212-213- test('should report correct error for negated option with preset when negated is configured', () => {214- const { program } = makeProgram();215-216- program217- .command('bar')218- .addOption(new commander.Option('--red'))219- .addOption(new commander.Option('--dual2 <str>').conflicts(['red']))220- .addOption(new commander.Option('--no-dual2').preset('BAD'));221-222- expect(() => {223- program.parse('node test.js bar --red --no-dual2'.split(' '));224- }).toThrow("error: option '--no-dual2' cannot be used with option '--red'");225- });226-227- test('should not throw error when conflicts is invoked with a single string that includes another option', () => {228- const { program } = makeProgram();229-230- const actionMock = jest.fn();231-232- program233- .command('bar')234- .addOption(new commander.Option('--a'))235- .addOption(new commander.Option('--b').conflicts('aa'))236- .action(actionMock);237-238- program.parse('node test.js bar --a --b'.split(' '));239-240- expect(actionMock).toHaveBeenCalledTimes(1);241- expect(actionMock).toHaveBeenCalledWith({ a: true, b: true }, expect.any(Object));242- });243-244- test('should throw error when conflicts is invoked with a single string that equals another option', () => {245- const { program } = makeProgram();246-247- program248- .command('bar')249- .addOption(new commander.Option('--a'))250- .addOption(new commander.Option('--b').conflicts('a'));251-252- expect(() => {253- program.parse('node test.js bar --a --b'.split(' '));254- }).toThrow("error: option '--b' cannot be used with option '--a'");255- });256-});added:tests/options.conflicts.test.js+311 added
1+const path = require('path');2+const commander = require('../');3+4+describe('command with conflicting options', () => {5+ function makeProgram() {6+ const actionMock = jest.fn();7+ const program = new commander.Command();8+ program9+ .exitOverride()10+ .configureOutput({11+ writeErr: () => {},12+ writeOut: () => {}13+ })14+ .command('foo')15+ .addOption(new commander.Option('-s, --silent', "Don't print anything").env('SILENT'))16+ .addOption(17+ new commander.Option('-j, --json', 'Format output as json').env('JSON').conflicts([18+ 'silent'19+ ])20+ )21+ .action(actionMock);22+23+ return { program, actionMock };24+ }25+26+ beforeEach(() => {27+ delete process.env.SILENT;28+ delete process.env.JSON;29+ delete process.env.DUAL;30+ delete process.env.NO_DUAL;31+ });32+33+ test('should call action if there are no explicit conflicting options set', () => {34+ const { program, actionMock } = makeProgram();35+ program.parse('node test.js foo --json'.split(' '));36+ expect(actionMock).toHaveBeenCalledTimes(1);37+ expect(actionMock).toHaveBeenCalledWith({ json: true }, expect.any(Object));38+ });39+40+ test('should call action when there are no implicit conflicting options set', () => {41+ const { program, actionMock } = makeProgram();42+ program.parse('node test.js foo --silent'.split(' '));43+ expect(actionMock).toHaveBeenCalledTimes(1);44+ expect(actionMock).toHaveBeenCalledWith(45+ { silent: true },46+ expect.any(Object)47+ );48+ });49+50+ test('should exit with error if conflicting options were set', () => {51+ const { program } = makeProgram();52+53+ expect(() => {54+ program.parse('node test.js foo --silent --json'.split(' '));55+ }).toThrow("error: option '-j, --json' cannot be used with option '-s, --silent'");56+ });57+58+ test('should report the env variable as the conflicting option source, when conflicting option is set', () => {59+ const { program } = makeProgram();60+61+ process.env.SILENT = true;62+63+ expect(() => {64+ program.parse('node test.js foo --json'.split(' '));65+ }).toThrow("error: option '-j, --json' cannot be used with environment variable 'SILENT'");66+ });67+68+ test('should report the env variable as the configured option source, when configured option is set', () => {69+ const { program } = makeProgram();70+71+ process.env.JSON = true;72+73+ expect(() => {74+ program.parse('node test.js foo --silent'.split(' '));75+ }).toThrow("error: environment variable 'JSON' cannot be used with option '-s, --silent'");76+ });77+78+ test('should report both env variables as sources, when configured option and conflicting option are set', () => {79+ const { program } = makeProgram();80+81+ process.env.SILENT = true;82+ process.env.JSON = true;83+84+ expect(() => {85+ program.parse('node test.js foo'.split(' '));86+ }).toThrow("error: environment variable 'JSON' cannot be used with environment variable 'SILENT'");87+ });88+89+ test('should allow default value with a conflicting option', () => {90+ const { program, actionMock } = makeProgram();91+92+ program.commands[0].addOption(new commander.Option('-d, --debug', 'print debug logs').default(true).conflicts(['silent']));93+94+ program.parse('node test.js foo --silent'.split(' '));95+96+ expect(actionMock).toHaveBeenCalledTimes(1);97+ expect(actionMock).toHaveBeenCalledWith({ debug: true, silent: true }, expect.any(Object));98+ });99+100+ test('should report conflict on negated option flag', () => {101+ const { program } = makeProgram();102+103+ program104+ .command('bar')105+ .addOption(new commander.Option('--red').conflicts(['color']))106+ .addOption(new commander.Option('--color'))107+ .addOption(new commander.Option('-N, --no-color'));108+109+ expect(() => {110+ program.parse('node test.js bar --red -N'.split(' '));111+ }).toThrow("error: option '--red' cannot be used with option '-N, --no-color'");112+ });113+114+ test('should report conflict on negated option env variable', () => {115+ const { program } = makeProgram();116+117+ process.env.NO_COLOR = true;118+119+ program120+ .command('bar')121+ .addOption(new commander.Option('--red').conflicts(['color']))122+ .addOption(new commander.Option('--color'))123+ .addOption(new commander.Option('-N, --no-color').env('NO_COLOR'));124+125+ expect(() => {126+ program.parse('node test.js bar --red'.split(' '));127+ }).toThrow("error: option '--red' cannot be used with environment variable 'NO_COLOR'");128+ });129+130+ test('should report correct error for shorthand negated option', () => {131+ const { program } = makeProgram();132+133+ program134+ .command('bar')135+ .addOption(new commander.Option('--red'))136+ .addOption(new commander.Option('-N, --no-color').conflicts(['red']));137+138+ expect(() => {139+ program.parse('node test.js bar --red -N'.split(' '));140+ }).toThrow("error: option '-N, --no-color' cannot be used with option '--red'");141+ });142+143+ test('should report correct error for positive option when negated is configured', () => {144+ const { program } = makeProgram();145+146+ program147+ .command('bar')148+ .addOption(new commander.Option('--red'))149+ .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))150+ .addOption(new commander.Option('--no-dual').env('NO_DUAL'));151+152+ expect(() => {153+ program.parse('node test.js bar --red --dual'.split(' '));154+ }).toThrow("error: option '--dual' cannot be used with option '--red'");155+ });156+157+ test('should report correct error for negated option when positive is configured', () => {158+ const { program } = makeProgram();159+160+ program161+ .command('bar')162+ .addOption(new commander.Option('--red'))163+ .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))164+ .addOption(new commander.Option('--no-dual').env('NO_DUAL'));165+166+ expect(() => {167+ program.parse('node test.js bar --red --no-dual'.split(' '));168+ }).toThrow("error: option '--no-dual' cannot be used with option '--red'");169+ });170+171+ test('should report correct error for positive env variable when negated is configured', () => {172+ const { program } = makeProgram();173+174+ program175+ .command('bar')176+ .addOption(new commander.Option('--red'))177+ .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))178+ .addOption(new commander.Option('--no-dual').env('NO_DUAL'));179+180+ process.env.DUAL = 'true';181+ expect(() => {182+ program.parse('node test.js bar --red'.split(' '));183+ }).toThrow("error: environment variable 'DUAL' cannot be used with option '--red'");184+ });185+186+ test('should report correct error for negated env variable when positive is configured', () => {187+ const { program } = makeProgram();188+189+ program190+ .command('bar')191+ .addOption(new commander.Option('--red'))192+ .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))193+ .addOption(new commander.Option('--no-dual').env('NO_DUAL'));194+195+ process.env.NO_DUAL = 'true';196+ expect(() => {197+ program.parse('node test.js bar --red'.split(' '));198+ }).toThrow("error: environment variable 'NO_DUAL' cannot be used with option '--red'");199+ });200+201+ test('should report correct error for positive option with string value when negated is configured', () => {202+ const { program } = makeProgram();203+204+ program205+ .command('bar')206+ .addOption(new commander.Option('--red'))207+ .addOption(new commander.Option('--dual2 <str>').conflicts(['red']))208+ .addOption(new commander.Option('--no-dual2').preset('BAD'));209+210+ expect(() => {211+ program.parse('node test.js bar --red --dual2 foo'.split(' '));212+ }).toThrow("error: option '--dual2 <str>' cannot be used with option '--red'");213+ });214+215+ test('should report correct error for negated option with preset when negated is configured', () => {216+ const { program } = makeProgram();217+218+ program219+ .command('bar')220+ .addOption(new commander.Option('--red'))221+ .addOption(new commander.Option('--dual2 <str>').conflicts(['red']))222+ .addOption(new commander.Option('--no-dual2').preset('BAD'));223+224+ expect(() => {225+ program.parse('node test.js bar --red --no-dual2'.split(' '));226+ }).toThrow("error: option '--no-dual2' cannot be used with option '--red'");227+ });228+229+ test('should not throw error when conflicts is invoked with a single string that includes another option', () => {230+ const { program } = makeProgram();231+232+ const actionMock = jest.fn();233+234+ program235+ .command('bar')236+ .addOption(new commander.Option('--a'))237+ .addOption(new commander.Option('--b').conflicts('aa'))238+ .action(actionMock);239+240+ program.parse('node test.js bar --a --b'.split(' '));241+242+ expect(actionMock).toHaveBeenCalledTimes(1);243+ expect(actionMock).toHaveBeenCalledWith({ a: true, b: true }, expect.any(Object));244+ });245+246+ test('should throw error when conflicts is invoked with a single string that equals another option', () => {247+ const { program } = makeProgram();248+249+ program250+ .command('bar')251+ .addOption(new commander.Option('--a'))252+ .addOption(new commander.Option('--b').conflicts('a'));253+254+ expect(() => {255+ program.parse('node test.js bar --a --b'.split(' '));256+ }).toThrow("error: option '--b' cannot be used with option '--a'");257+ });258+259+ test('when conflict on program calling action subcommand then throw conflict', () => {260+ const { program } = makeProgram();261+ let exception;262+263+ program264+ .addOption(new commander.Option('--black'))265+ .addOption(new commander.Option('--white').conflicts('black'));266+267+ try {268+ program.parse('--white --black foo'.split(' '), { from: 'user' });269+ } catch (err) {270+ exception = err;271+ }272+ expect(exception).not.toBeUndefined();273+ expect(exception.code).toBe('commander.conflictingOption');274+ });275+276+ test('when conflict on program calling action subcommand with help then show help', () => {277+ const { program } = makeProgram();278+ let exception;279+280+ program281+ .addOption(new commander.Option('--black'))282+ .addOption(new commander.Option('--white').conflicts('black'));283+284+ try {285+ program.parse('--white --black foo --help'.split(' '), { from: 'user' });286+ } catch (err) {287+ exception = err;288+ }289+ expect(exception).not.toBeUndefined();290+ expect(exception.code).toBe('commander.helpDisplayed');291+ });292+293+ test('when conflict on program calling external subcommand then throw conflict', () => {294+ const { program } = makeProgram();295+ let exception;296+297+ program298+ .addOption(new commander.Option('--black'))299+ .addOption(new commander.Option('--white').conflicts('black'));300+ const pm = path.join(__dirname, './fixtures/pm');301+ program.command('ext', 'external command', { executableFile: pm });302+303+ try {304+ program.parse('--white --black ext'.split(' '), { from: 'user' });305+ } catch (err) {306+ exception = err;307+ }308+ expect(exception).not.toBeUndefined();309+ expect(exception.code).toBe('commander.conflictingOption');310+ });311+});