Skip to content
Myr

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

3 files+328 added−259 removed

modified:lib/command.js+17 added-3 removed
944 unmodified lines
945 }946947 // 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();950951 // 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 lines
1294 }12951296 /**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 lines
1311 }1312 }13131314 /**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 lines
1341 });1342 }13431344 /**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 }13561357 /**1358 * Parse options from `argv` removing known options,1359 * and return argv split into operands and unknown arguments.1360 *1361 * Examples:
737 unmodified lines
deleted:tests/command.conflicts.test.js-256 removed
1const commander = require('../');23describe('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);2021 return { program, actionMock };22 }2324 beforeEach(() => {25 delete process.env.SILENT;26 delete process.env.JSON;27 delete process.env.DUAL;28 delete process.env.NO_DUAL;29 });3031 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 });3738 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 });4748 test('should exit with error if conflicting options were set', () => {49 const { program } = makeProgram();5051 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 });5556 test('should report the env variable as the conflicting option source, when conflicting option is set', () => {57 const { program } = makeProgram();5859 process.env.SILENT = true;6061 expect(() => {62 program.parse('node test.js foo --json'.split(' '));63 }).toThrow("error: option '-j, --json' cannot be used with environment variable 'SILENT'");64 });6566 test('should report the env variable as the configured option source, when configured option is set', () => {67 const { program } = makeProgram();6869 process.env.JSON = true;7071 expect(() => {72 program.parse('node test.js foo --silent'.split(' '));73 }).toThrow("error: environment variable 'JSON' cannot be used with option '-s, --silent'");74 });7576 test('should report both env variables as sources, when configured option and conflicting option are set', () => {77 const { program } = makeProgram();7879 process.env.SILENT = true;80 process.env.JSON = true;8182 expect(() => {83 program.parse('node test.js foo'.split(' '));84 }).toThrow("error: environment variable 'JSON' cannot be used with environment variable 'SILENT'");85 });8687 test('should allow default value with a conflicting option', () => {88 const { program, actionMock } = makeProgram();8990 program.commands[0].addOption(new commander.Option('-d, --debug', 'print debug logs').default(true).conflicts(['silent']));9192 program.parse('node test.js foo --silent'.split(' '));9394 expect(actionMock).toHaveBeenCalledTimes(1);95 expect(actionMock).toHaveBeenCalledWith({ debug: true, silent: true }, expect.any(Object));96 });9798 test('should report conflict on negated option flag', () => {99 const { program } = makeProgram();100101 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'));106107 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 });111112 test('should report conflict on negated option env variable', () => {113 const { program } = makeProgram();114115 process.env.NO_COLOR = true;116117 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'));122123 expect(() => {124 program.parse('node test.js bar --red'.split(' '));125 }).toThrow("error: option '--red' cannot be used with environment variable 'NO_COLOR'");126 });127128 test('should report correct error for shorthand negated option', () => {129 const { program } = makeProgram();130131 program132 .command('bar')133 .addOption(new commander.Option('--red'))134 .addOption(new commander.Option('-N, --no-color').conflicts(['red']));135136 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 });140141 test('should report correct error for positive option when negated is configured', () => {142 const { program } = makeProgram();143144 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'));149150 expect(() => {151 program.parse('node test.js bar --red --dual'.split(' '));152 }).toThrow("error: option '--dual' cannot be used with option '--red'");153 });154155 test('should report correct error for negated option when positive is configured', () => {156 const { program } = makeProgram();157158 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'));163164 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 });168169 test('should report correct error for positive env variable when negated is configured', () => {170 const { program } = makeProgram();171172 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'));177178 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 });183184 test('should report correct error for negated env variable when positive is configured', () => {185 const { program } = makeProgram();186187 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'));192193 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 });198199 test('should report correct error for positive option with string value when negated is configured', () => {200 const { program } = makeProgram();201202 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'));207208 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 });212213 test('should report correct error for negated option with preset when negated is configured', () => {214 const { program } = makeProgram();215216 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'));221222 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 });226227 test('should not throw error when conflicts is invoked with a single string that includes another option', () => {228 const { program } = makeProgram();229230 const actionMock = jest.fn();231232 program233 .command('bar')234 .addOption(new commander.Option('--a'))235 .addOption(new commander.Option('--b').conflicts('aa'))236 .action(actionMock);237238 program.parse('node test.js bar --a --b'.split(' '));239240 expect(actionMock).toHaveBeenCalledTimes(1);241 expect(actionMock).toHaveBeenCalledWith({ a: true, b: true }, expect.any(Object));242 });243244 test('should throw error when conflicts is invoked with a single string that equals another option', () => {245 const { program } = makeProgram();246247 program248 .command('bar')249 .addOption(new commander.Option('--a'))250 .addOption(new commander.Option('--b').conflicts('a'));251252 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
1const path = require('path');2const commander = require('../');34describe('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);2223 return { program, actionMock };24 }2526 beforeEach(() => {27 delete process.env.SILENT;28 delete process.env.JSON;29 delete process.env.DUAL;30 delete process.env.NO_DUAL;31 });3233 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 });3940 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 });4950 test('should exit with error if conflicting options were set', () => {51 const { program } = makeProgram();5253 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 });5758 test('should report the env variable as the conflicting option source, when conflicting option is set', () => {59 const { program } = makeProgram();6061 process.env.SILENT = true;6263 expect(() => {64 program.parse('node test.js foo --json'.split(' '));65 }).toThrow("error: option '-j, --json' cannot be used with environment variable 'SILENT'");66 });6768 test('should report the env variable as the configured option source, when configured option is set', () => {69 const { program } = makeProgram();7071 process.env.JSON = true;7273 expect(() => {74 program.parse('node test.js foo --silent'.split(' '));75 }).toThrow("error: environment variable 'JSON' cannot be used with option '-s, --silent'");76 });7778 test('should report both env variables as sources, when configured option and conflicting option are set', () => {79 const { program } = makeProgram();8081 process.env.SILENT = true;82 process.env.JSON = true;8384 expect(() => {85 program.parse('node test.js foo'.split(' '));86 }).toThrow("error: environment variable 'JSON' cannot be used with environment variable 'SILENT'");87 });8889 test('should allow default value with a conflicting option', () => {90 const { program, actionMock } = makeProgram();9192 program.commands[0].addOption(new commander.Option('-d, --debug', 'print debug logs').default(true).conflicts(['silent']));9394 program.parse('node test.js foo --silent'.split(' '));9596 expect(actionMock).toHaveBeenCalledTimes(1);97 expect(actionMock).toHaveBeenCalledWith({ debug: true, silent: true }, expect.any(Object));98 });99100 test('should report conflict on negated option flag', () => {101 const { program } = makeProgram();102103 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'));108109 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 });113114 test('should report conflict on negated option env variable', () => {115 const { program } = makeProgram();116117 process.env.NO_COLOR = true;118119 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'));124125 expect(() => {126 program.parse('node test.js bar --red'.split(' '));127 }).toThrow("error: option '--red' cannot be used with environment variable 'NO_COLOR'");128 });129130 test('should report correct error for shorthand negated option', () => {131 const { program } = makeProgram();132133 program134 .command('bar')135 .addOption(new commander.Option('--red'))136 .addOption(new commander.Option('-N, --no-color').conflicts(['red']));137138 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 });142143 test('should report correct error for positive option when negated is configured', () => {144 const { program } = makeProgram();145146 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'));151152 expect(() => {153 program.parse('node test.js bar --red --dual'.split(' '));154 }).toThrow("error: option '--dual' cannot be used with option '--red'");155 });156157 test('should report correct error for negated option when positive is configured', () => {158 const { program } = makeProgram();159160 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'));165166 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 });170171 test('should report correct error for positive env variable when negated is configured', () => {172 const { program } = makeProgram();173174 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'));179180 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 });185186 test('should report correct error for negated env variable when positive is configured', () => {187 const { program } = makeProgram();188189 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'));194195 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 });200201 test('should report correct error for positive option with string value when negated is configured', () => {202 const { program } = makeProgram();203204 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'));209210 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 });214215 test('should report correct error for negated option with preset when negated is configured', () => {216 const { program } = makeProgram();217218 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'));223224 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 });228229 test('should not throw error when conflicts is invoked with a single string that includes another option', () => {230 const { program } = makeProgram();231232 const actionMock = jest.fn();233234 program235 .command('bar')236 .addOption(new commander.Option('--a'))237 .addOption(new commander.Option('--b').conflicts('aa'))238 .action(actionMock);239240 program.parse('node test.js bar --a --b'.split(' '));241242 expect(actionMock).toHaveBeenCalledTimes(1);243 expect(actionMock).toHaveBeenCalledWith({ a: true, b: true }, expect.any(Object));244 });245246 test('should throw error when conflicts is invoked with a single string that equals another option', () => {247 const { program } = makeProgram();248249 program250 .command('bar')251 .addOption(new commander.Option('--a'))252 .addOption(new commander.Option('--b').conflicts('a'));253254 expect(() => {255 program.parse('node test.js bar --a --b'.split(' '));256 }).toThrow("error: option '--b' cannot be used with option '--a'");257 });258259 test('when conflict on program calling action subcommand then throw conflict', () => {260 const { program } = makeProgram();261 let exception;262263 program264 .addOption(new commander.Option('--black'))265 .addOption(new commander.Option('--white').conflicts('black'));266267 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 });275276 test('when conflict on program calling action subcommand with help then show help', () => {277 const { program } = makeProgram();278 let exception;279280 program281 .addOption(new commander.Option('--black'))282 .addOption(new commander.Option('--white').conflicts('black'));283284 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 });292293 test('when conflict on program calling external subcommand then throw conflict', () => {294 const { program } = makeProgram();295 let exception;296297 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 });302303 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});