Skip to content
Myr

options.conflicts.test.js

This file

Path

tests/options.conflicts.test.js

311 lines · 10.7 KiB · JavaScript

Last changed by

Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1

History of this file

Take it

Check for conflicts in parent commands (#1711)John Geebefore adoptionbf205d1

lines 1–311why, one step back
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});