Skip to content

Commit

Permalink
fix: defaulting keys to 'undefined' interfered with conflicting key l…
Browse files Browse the repository at this point in the history
…ogic
  • Loading branch information
Benjamin Coe authored and bcoe committed Jul 4, 2017
1 parent 9168fdf commit a8e0cff
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lib/validation.js
Expand Up @@ -308,10 +308,10 @@ module.exports = function (yargs, usage, y18n) {

self.conflicting = function (argv) {
var args = Object.getOwnPropertyNames(argv)

args.forEach(function (arg) {
// if there is a conflict defined for this key and both conflicting keys have been specified
if (conflicting[arg] && args.indexOf(conflicting[arg]) !== -1 && argv[arg] && argv[conflicting[arg]]) {
// we default keys to 'undefined' that have been configured, we should not
// apply conflicting check unless they are a value other than 'undefined'.
if (conflicting[arg] && args.indexOf(conflicting[arg]) !== -1 && argv[arg] !== undefined && argv[conflicting[arg]] !== undefined) {
usage.fail(__('Arguments %s and %s are mutually exclusive', arg, conflicting[arg]))
}
})
Expand Down
76 changes: 48 additions & 28 deletions test/validation.js
Expand Up @@ -158,38 +158,36 @@ describe('validation tests', function () {
.argv
})

it('should not fail if no conflicting arguments are provided and the .command()' +
'syntax is used (first conflicting option specified)', function () {
it('should not fail if argument with conflict is provided, but not the argument it conflicts with', function () {
yargs(['command', '-f', '-c'])
.command('command')
.option('f', {
describe: 'a foo'
})
.option('b', {
describe: 'a bar'
})
.conflicts('f', 'b')
.fail(function (msg) {
expect.fail()
})
.argv
.command('command')
.option('f', {
describe: 'a foo'
})
.option('b', {
describe: 'a bar'
})
.conflicts('f', 'b')
.fail(function (msg) {
expect.fail()
})
.argv
})

it('should not fail if no conflicting arguments are provided and the .command()' +
'syntax is used (second conflicting option specified)', function () {
it('should not fail if conflicting argument is provided, without argument with conflict', function () {
yargs(['command', '-b', '-c'])
.command('command')
.option('f', {
describe: 'a foo'
})
.option('b', {
describe: 'a bar'
})
.conflicts('f', 'b')
.fail(function (msg) {
expect.fail()
})
.argv
.command('command')
.option('f', {
describe: 'a foo'
})
.option('b', {
describe: 'a bar'
})
.conflicts('f', 'b')
.fail(function (msg) {
expect.fail()
})
.argv
})

it('allows an object to be provided defining conflicting option pairs', function (done) {
Expand Down Expand Up @@ -230,6 +228,28 @@ describe('validation tests', function () {
})
.argv
})

it('should fail if alias of conflicting argument is provided', function (done) {
yargs(['-f', '--batman=99'])
.conflicts('f', 'b')
.alias('b', 'batman')
.fail(function (msg) {
msg.should.equal('Arguments f and b are mutually exclusive')
return done()
})
.argv
})

it('should fail if alias of argument with conflict is provided', function (done) {
yargs(['--foo', '-b'])
.conflicts('f', 'b')
.alias('foo', 'f')
.fail(function (msg) {
msg.should.equal('Arguments f and b are mutually exclusive')
return done()
})
.argv
})
})

describe('demand', function () {
Expand Down

0 comments on commit a8e0cff

Please sign in to comment.