Skip to content

Commit

Permalink
Merge branch 'next' into feat/multi-entry
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jun 7, 2020
2 parents c261aa4 + 885e0a2 commit 6628137
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
49 changes: 33 additions & 16 deletions packages/webpack-cli/__tests__/arg-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ const basicOptions = [
type: Boolean,
description: 'boolean flag',
},
{
name: 'specific-bool',
alias: 's',
usage: '--specific-bool',
type: Boolean,
description: 'boolean flag',
},
{
name: 'no-specific-bool',
usage: '--no-specific-bool',
type: Boolean,
description: 'boolean flag',
},
{
name: 'string-flag',
usage: '--string-flag <value>',
Expand Down Expand Up @@ -82,57 +95,62 @@ describe('arg-parser', () => {
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses negated boolean flags', () => {
it('should not parse negated boolean flags which are not specified', () => {
const res = argParser(basicOptions, ['--no-bool-flag'], true);
expect(res.unknownArgs.includes('--no-bool-flag')).toBeTruthy();
});

it('parses boolean flag alias', () => {
const res = argParser(basicOptions, ['-b'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: false,
boolFlag: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('parses boolean flag alias', () => {
const res = argParser(basicOptions, ['-b'], true);
it('parses specified negated boolean flag', () => {
const res = argParser(basicOptions, ['--no-specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
specificBool: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(0);
});

it('warns on usage of both flag and same negated flag, setting it to false', () => {
const res = argParser(basicOptions, ['--bool-flag', '--no-bool-flag'], true);
const res = argParser(basicOptions, ['--specific-bool', '--no-specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: false,
specificBool: false,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --bool-flag and --no-bool-flag');
expect(warnMock.mock.calls[0][0]).toContain('You provided both --specific-bool and --no-specific-bool');
});

it('warns on usage of both flag and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-bool-flag', '--bool-flag'], true);
const res = argParser(basicOptions, ['--no-specific-bool', '--specific-bool'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
specificBool: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both --bool-flag and --no-bool-flag');
expect(warnMock.mock.calls[0][0]).toContain('You provided both --specific-bool and --no-specific-bool');
});

it('warns on usage of both flag alias and same negated flag, setting it to true', () => {
const res = argParser(basicOptions, ['--no-bool-flag', '-b'], true);
const res = argParser(basicOptions, ['--no-specific-bool', '-s'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts).toEqual({
boolFlag: true,
specificBool: true,
stringFlagWithDefault: 'default-value',
});
expect(warnMock.mock.calls.length).toEqual(1);
expect(warnMock.mock.calls[0][0]).toContain('You provided both -b and --no-bool-flag');
expect(warnMock.mock.calls[0][0]).toContain('You provided both -s and --no-specific-bool');
});

it('parses string flag using equals sign', () => {
Expand Down Expand Up @@ -190,12 +208,11 @@ describe('arg-parser', () => {
});

it('parses webpack args', () => {
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/', '--no-watch'], true);
const res = argParser(core, ['--entry', 'test.js', '--hot', '-o', './dist/'], true);
expect(res.unknownArgs.length).toEqual(0);
expect(res.opts.entry).toEqual('test.js');
expect(res.opts.hot).toBeTruthy();
expect(res.opts.output).toEqual('./dist/');
expect(res.opts.watch).toBeFalsy();
expect(warnMock.mock.calls.length).toEqual(0);
});
});
5 changes: 0 additions & 5 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ function argParser(options, args, argsOnly = false, name = '', helpFunction = un
const flagsWithType = option.type !== Boolean ? flags + ' <value>' : flags;
if (option.type === Boolean || option.type === String) {
parserInstance.option(flagsWithType, option.description, option.defaultValue);
if (option.type === Boolean) {
// commander requires explicitly adding the negated version of boolean flags
const negatedFlag = `--no-${option.name}`;
parserInstance.option(negatedFlag, `negates ${option.name}`);
}
} else {
// in this case the type is a parsing function
parserInstance.option(flagsWithType, option.description, option.type, option.defaultValue);
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11964,9 +11964,9 @@ websocket-driver@>=0.5.1:
websocket-extensions ">=0.1.1"

websocket-extensions@>=0.1.1:
version "0.1.3"
resolved "https://registry.yarnpkg.com/websocket-extensions/-/websocket-extensions-0.1.3.tgz#5d2ff22977003ec687a4b87073dfbbac146ccf29"
integrity sha512-nqHUnMXmBzT0w570r2JpJxfiSD1IzoI+HGVdd3aZ0yNi3ngvQ4jv1dtHt5VGxfI2yj5yqImPhOK4vmIh2xMbGg==
version "0.1.4"
resolved "https://registry.yarnpkg.com/websocket-extensions/-/websocket-extensions-0.1.4.tgz#7f8473bc839dfd87608adb95d7eb075211578a42"
integrity sha512-OqedPIGOfsDlo31UNwYbCFMSaO9m9G/0faIHj5/dZFDMFqPTcx6UwqyOy3COEaEOg/9VsGIpdqn62W5KhoKSpg==

whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.5:
version "1.0.5"
Expand Down

0 comments on commit 6628137

Please sign in to comment.