Skip to content
Myr

options.conflicts.test.js

This file

Path

tests/options.conflicts.test.js

395 lines · 11.7 KiB · JavaScript

Last changed by

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

History of this file

Take it

The walk stopped after 200 commits: lines older than 471ec40 point at it.

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

lines 1–5why, one step back
1import * as path from 'path';
2import * as commander from '../index.js';3import { createTestCommand } from './testHelpers.js';4import { test, describe } from 'node:test';5import assert from 'node:assert/strict';

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 6why, one step back
6

Add describe wrappers so test output makes sense without filenames (#2472)John Geebefore adoptionb3e6795

line 7why, one step back
7describe('Option.conflicts()', () => {

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 8–10why, one step back
8 function makeProgram(t) {
9 const actionMock = t.mock.fn();10 const program = createTestCommand();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 11–13why, one step back
11 program
12 .command('foo')13 .addOption(

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 14–16why, one step back
14 new commander.Option('-s, --silent', "Don't print anything").env(
15 'SILENT',16 ),

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 17why, one step back
17 )

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 18–22why, one step back
18 .addOption(
19 new commander.Option('-j, --json', 'Format output as json')20 .env('JSON')21 .conflicts(['silent']),22 )

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 23–27why, one step back
23 .action(actionMock);
2425 return { program, actionMock };26 }27

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 28why, one step back
28 test.beforeEach(() => {

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 29–34why, one step back
29 delete process.env.SILENT;
30 delete process.env.JSON;31 delete process.env.DUAL;32 delete process.env.NO_DUAL;33 });34

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 35–36why, one step back
35 test('should call action if there are no explicit conflicting options set', (t) => {
36 const { program, actionMock } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 37why, one step back
37 program.parse('node test.js foo --json'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 38–40why, one step back
38 assert.equal(actionMock.mock.callCount(), 1);
39 const callArgs = actionMock.mock.calls[0].arguments;40 assert.deepEqual(callArgs[0], { json: true });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 41–42why, one step back
41 });
42

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 43–44why, one step back
43 test('should call action when there are no implicit conflicting options set', (t) => {
44 const { program, actionMock } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 45why, one step back
45 program.parse('node test.js foo --silent'.split(' '));

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 46–48why, one step back
46 assert.equal(actionMock.mock.callCount(), 1);
47 const callArgs = actionMock.mock.calls[0].arguments;48 assert.deepEqual(callArgs[0], { silent: true });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 49–50why, one step back
49 });
50

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 51–52why, one step back
51 test('should exit with error if conflicting options were set', (t) => {
52 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 53why, one step back
53

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 54–61why, one step back
54 assert.throws(
55 () => {56 program.parse('node test.js foo --silent --json'.split(' '));57 },58 {59 message:60 "error: option '-j, --json' cannot be used with option '-s, --silent'",61 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 62why, one step back
62 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 63–64why, one step back
63 });
64

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 65–66why, one step back
65 test('should report the env variable as the conflicting option source, when conflicting option is set', (t) => {
66 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 67–69why, one step back
67
68 process.env.SILENT = true;69

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 70–77why, one step back
70 assert.throws(
71 () => {72 program.parse('node test.js foo --json'.split(' '));73 },74 {75 message:76 "error: option '-j, --json' cannot be used with environment variable 'SILENT'",77 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 78why, one step back
78 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 79–80why, one step back
79 });
80

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 81–82why, one step back
81 test('should report the env variable as the configured option source, when configured option is set', (t) => {
82 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 83–85why, one step back
83
84 process.env.JSON = true;85

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 86–93why, one step back
86 assert.throws(
87 () => {88 program.parse('node test.js foo --silent'.split(' '));89 },90 {91 message:92 "error: environment variable 'JSON' cannot be used with option '-s, --silent'",93 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 94why, one step back
94 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 95–96why, one step back
95 });
96

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 97–98why, one step back
97 test('should report both env variables as sources, when configured option and conflicting option are set', (t) => {
98 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 99–102why, one step back
99
100 process.env.SILENT = true;101 process.env.JSON = true;102

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 103–110why, one step back
103 assert.throws(
104 () => {105 program.parse('node test.js foo'.split(' '));106 },107 {108 message:109 "error: environment variable 'JSON' cannot be used with environment variable 'SILENT'",110 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 111why, one step back
111 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 112–113why, one step back
112 });
113

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 114–115why, one step back
114 test('should allow default value with a conflicting option', (t) => {
115 const { program, actionMock } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 116why, one step back
116

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

lines 117–121why, one step back
117 program.commands[0].addOption(
118 new commander.Option('-d, --debug', 'print debug logs')119 .default(true)120 .conflicts(['silent']),121 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 122–124why, one step back
122
123 program.parse('node test.js foo --silent'.split(' '));124

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 125–127why, one step back
125 assert.equal(actionMock.mock.callCount(), 1);
126 const callArgs = actionMock.mock.calls[0].arguments;127 assert.deepEqual(callArgs[0], { debug: true, silent: true });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 128–129why, one step back
128 });
129

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 130–131why, one step back
130 test('should report conflict on negated option flag', (t) => {
131 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 132–138why, one step back
132
133 program134 .command('bar')135 .addOption(new commander.Option('--red').conflicts(['color']))136 .addOption(new commander.Option('--color'))137 .addOption(new commander.Option('-N, --no-color'));138

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 139–146why, one step back
139 assert.throws(
140 () => {141 program.parse('node test.js bar --red -N'.split(' '));142 },143 {144 message:145 "error: option '--red' cannot be used with option '-N, --no-color'",146 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 147why, one step back
147 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 148–149why, one step back
148 });
149

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 150–151why, one step back
150 test('should report conflict on negated option env variable', (t) => {
151 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 152–160why, one step back
152
153 process.env.NO_COLOR = true;154155 program156 .command('bar')157 .addOption(new commander.Option('--red').conflicts(['color']))158 .addOption(new commander.Option('--color'))159 .addOption(new commander.Option('-N, --no-color').env('NO_COLOR'));160

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 161–168why, one step back
161 assert.throws(
162 () => {163 program.parse('node test.js bar --red'.split(' '));164 },165 {166 message:167 "error: option '--red' cannot be used with environment variable 'NO_COLOR'",168 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 169why, one step back
169 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 170–171why, one step back
170 });
171

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 172–173why, one step back
172 test('should report correct error for shorthand negated option', (t) => {
173 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 174–179why, one step back
174
175 program176 .command('bar')177 .addOption(new commander.Option('--red'))178 .addOption(new commander.Option('-N, --no-color').conflicts(['red']));179

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 180–187why, one step back
180 assert.throws(
181 () => {182 program.parse('node test.js bar --red -N'.split(' '));183 },184 {185 message:186 "error: option '-N, --no-color' cannot be used with option '--red'",187 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 188why, one step back
188 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 189–190why, one step back
189 });
190

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 191–192why, one step back
191 test('should report correct error for positive option when negated is configured', (t) => {
192 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 193–199why, one step back
193
194 program195 .command('bar')196 .addOption(new commander.Option('--red'))197 .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))198 .addOption(new commander.Option('--no-dual').env('NO_DUAL'));199

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 200–205why, one step back
200 assert.throws(
201 () => {202 program.parse('node test.js bar --red --dual'.split(' '));203 },204 { message: "error: option '--dual' cannot be used with option '--red'" },205 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 206–207why, one step back
206 });
207

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 208–209why, one step back
208 test('should report correct error for negated option when positive is configured', (t) => {
209 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 210–216why, one step back
210
211 program212 .command('bar')213 .addOption(new commander.Option('--red'))214 .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))215 .addOption(new commander.Option('--no-dual').env('NO_DUAL'));216

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 217–224why, one step back
217 assert.throws(
218 () => {219 program.parse('node test.js bar --red --no-dual'.split(' '));220 },221 {222 message: "error: option '--no-dual' cannot be used with option '--red'",223 },224 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 225–226why, one step back
225 });
226

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 227–228why, one step back
227 test('should report correct error for positive env variable when negated is configured', (t) => {
228 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 229–236why, one step back
229
230 program231 .command('bar')232 .addOption(new commander.Option('--red'))233 .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))234 .addOption(new commander.Option('--no-dual').env('NO_DUAL'));235236 process.env.DUAL = 'true';

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 237–244why, one step back
237 assert.throws(
238 () => {239 program.parse('node test.js bar --red'.split(' '));240 },241 {242 message:243 "error: environment variable 'DUAL' cannot be used with option '--red'",244 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 245why, one step back
245 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 246–247why, one step back
246 });
247

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 248–249why, one step back
248 test('should report correct error for negated env variable when positive is configured', (t) => {
249 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 250–257why, one step back
250
251 program252 .command('bar')253 .addOption(new commander.Option('--red'))254 .addOption(new commander.Option('--dual').env('DUAL').conflicts(['red']))255 .addOption(new commander.Option('--no-dual').env('NO_DUAL'));256257 process.env.NO_DUAL = 'true';

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 258–265why, one step back
258 assert.throws(
259 () => {260 program.parse('node test.js bar --red'.split(' '));261 },262 {263 message:264 "error: environment variable 'NO_DUAL' cannot be used with option '--red'",265 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 266why, one step back
266 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 267–268why, one step back
267 });
268

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 269–270why, one step back
269 test('should report correct error for positive option with string value when negated is configured', (t) => {
270 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 271–277why, one step back
271
272 program273 .command('bar')274 .addOption(new commander.Option('--red'))275 .addOption(new commander.Option('--dual2 <str>').conflicts(['red']))276 .addOption(new commander.Option('--no-dual2').preset('BAD'));277

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 278–285why, one step back
278 assert.throws(
279 () => {280 program.parse('node test.js bar --red --dual2 foo'.split(' '));281 },282 {283 message:284 "error: option '--dual2 <str>' cannot be used with option '--red'",285 },

Reformat code with Prettier (#2180)John Geebefore adoption1bdc749

line 286why, one step back
286 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 287–288why, one step back
287 });
288

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 289–290why, one step back
289 test('should report correct error for negated option with preset when negated is configured', (t) => {
290 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 291–297why, one step back
291
292 program293 .command('bar')294 .addOption(new commander.Option('--red'))295 .addOption(new commander.Option('--dual2 <str>').conflicts(['red']))296 .addOption(new commander.Option('--no-dual2').preset('BAD'));297

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 298–306why, one step back
298 assert.throws(
299 () => {300 program.parse('node test.js bar --red --no-dual2'.split(' '));301 },302 {303 message:304 "error: option '--no-dual2' cannot be used with option '--red'",305 },306 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 307–308why, one step back
307 });
308

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 309–310why, one step back
309 test('should not throw error when conflicts is invoked with a single string that includes another option', (t) => {
310 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

line 311why, one step back
311

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

line 312why, one step back
312 const actionMock = t.mock.fn();

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 313–321why, one step back
313
314 program315 .command('bar')316 .addOption(new commander.Option('--a'))317 .addOption(new commander.Option('--b').conflicts('aa'))318 .action(actionMock);319320 program.parse('node test.js bar --a --b'.split(' '));321

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 322–324why, one step back
322 assert.equal(actionMock.mock.callCount(), 1);
323 const callArgs = actionMock.mock.calls[0].arguments;324 assert.deepEqual(callArgs[0], { a: true, b: true });

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 325–326why, one step back
325 });
326

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 327–328why, one step back
327 test('should throw error when conflicts is invoked with a single string that equals another option', (t) => {
328 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 329–334why, one step back
329
330 program331 .command('bar')332 .addOption(new commander.Option('--a'))333 .addOption(new commander.Option('--b').conflicts('a'));334

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 335–340why, one step back
335 assert.throws(
336 () => {337 program.parse('node test.js bar --a --b'.split(' '));338 },339 { message: "error: option '--b' cannot be used with option '--a'" },340 );

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 341–342why, one step back
341 });
342

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 343–344why, one step back
343 test('when conflict on program calling action subcommand then throw conflict', (t) => {
344 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 345–355why, one step back
345 let exception;
346347 program348 .addOption(new commander.Option('--black'))349 .addOption(new commander.Option('--white').conflicts('black'));350351 try {352 program.parse('--white --black foo'.split(' '), { from: 'user' });353 } catch (err) {354 exception = err;355 }

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 356–357why, one step back
356 assert.notEqual(exception, undefined);
357 assert.equal(exception.code, 'commander.conflictingOption');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 358–359why, one step back
358 });
359

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 360–361why, one step back
360 test('when conflict on program calling action subcommand with help then show help', (t) => {
361 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 362–372why, one step back
362 let exception;
363364 program365 .addOption(new commander.Option('--black'))366 .addOption(new commander.Option('--white').conflicts('black'));367368 try {369 program.parse('--white --black foo --help'.split(' '), { from: 'user' });370 } catch (err) {371 exception = err;372 }

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 373–374why, one step back
373 assert.notEqual(exception, undefined);
374 assert.equal(exception.code, 'commander.helpDisplayed');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 375–376why, one step back
375 });
376

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 377–378why, one step back
377 test('when conflict on program calling external subcommand then throw conflict', (t) => {
378 const { program } = makeProgram(t);

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 379–383why, one step back
379 let exception;
380381 program382 .addOption(new commander.Option('--black'))383 .addOption(new commander.Option('--white').conflicts('black'));

Switching to esm-only (#2464)John Geebefore adoptionf4bd470

line 384why, one step back
384 const pm = path.join(import.meta.dirname, './fixtures/pm');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 385–391why, one step back
385 program.command('ext', 'external command', { executableFile: pm });
386387 try {388 program.parse('--white --black ext'.split(' '), { from: 'user' });389 } catch (err) {390 exception = err;391 }

Switch from Jest to Node.js built-in test runner (#2463)John Geebefore adoption3dd6086

lines 392–393why, one step back
392 assert.notEqual(exception, undefined);
393 assert.equal(exception.code, 'commander.conflictingOption');

Add copyInheritedSettings to README (#1798)John Geebefore adoption471ec40

lines 394–395why, one step back
394 });
395});