Skip to content

Commit

Permalink
fix: address bug with handling of arrays of implications
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Sep 3, 2017
1 parent abdc7da commit c240661
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
70 changes: 34 additions & 36 deletions lib/validation.js
Expand Up @@ -255,44 +255,42 @@ module.exports = function validation (yargs, usage, y18n) {
const implyFail = []

Object.keys(implied).forEach((key) => {
if (implied[key]) {
implied[key].forEach((value) => {
let num
const origKey = key

// convert string '1' to number 1
num = Number(key)
key = isNaN(num) ? key : num

if (typeof key === 'number') {
// check length of argv._
key = argv._.length >= key
} else if (key.match(/^--no-.+/)) {
// check if key doesn't exist
key = key.match(/^--no-(.+)/)[1]
key = !argv[key]
} else {
// check if key exists
key = argv[key]
}

num = Number(value)
value = isNaN(num) ? value : num
const origKey = key
;(implied[key] || []).forEach((value) => {
let num
let key = origKey

// convert string '1' to number 1
num = Number(key)
key = isNaN(num) ? key : num

if (typeof key === 'number') {
// check length of argv._
key = argv._.length >= key
} else if (key.match(/^--no-.+/)) {
// check if key doesn't exist
key = key.match(/^--no-(.+)/)[1]
key = !argv[key]
} else {
// check if key exists
key = argv[key]
}

if (typeof value === 'number') {
value = argv._.length >= value
} else if (value.match(/^--no-.+/)) {
value = value.match(/^--no-(.+)/)[1]
value = !argv[value]
} else {
value = argv[value]
}
num = Number(value)
value = isNaN(num) ? value : num

if (key && !value) {
implyFail.push(origKey)
}
})
}
if (typeof value === 'number') {
value = argv._.length >= value
} else if (value.match(/^--no-.+/)) {
value = value.match(/^--no-(.+)/)[1]
value = !argv[value]
} else {
value = argv[value]
}
if (key && !value) {
implyFail.push(origKey)
}
})
})

if (implyFail.length) {
Expand Down
25 changes: 6 additions & 19 deletions test/validation.js
Expand Up @@ -39,32 +39,19 @@ describe('validation tests', () => {
})

it('fails if either implied argument is not set', (done) => {
let fail1 = false
let fail2 = false
yargs(['--foo', '-a'])
.boolean('foo')
.implies({
'foo': ['a', 'b']
})
yargs(['-f', '-b'])
.implies('f', ['b', 'c'])
.fail((msg1) => {
fail1 = true
yargs(['--foo', '-b'])
.boolean('foo')
.implies({
'foo': ['a', 'b']
})
yargs(['-f', '-c'])
.implies('f', ['b', 'c'])
.fail((msg2) => {
fail2 = true
msg1.should.match(/Implications failed/)
msg2.should.match(/Implications failed/)
msg1.should.match(/f -> b f -> c/)
msg2.should.match(/f -> b f -> c/)
return done()
})
.argv
})
.argv
// Prevent timeouts
fail1.should.be.true
fail2.should.be.true
})

it("fails if --no-foo's implied argument is not set", (done) => {
Expand Down

0 comments on commit c240661

Please sign in to comment.