Skip to content

Commit

Permalink
fix: do not use cwd when resolving package.json for yargs parsing con…
Browse files Browse the repository at this point in the history
…fig (#726)
  • Loading branch information
nexdrew authored and bcoe committed Dec 6, 2016
1 parent 51af0a6 commit 9bdaab7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
10 changes: 2 additions & 8 deletions lib/validation.js
Expand Up @@ -223,12 +223,6 @@ module.exports = function (yargs, usage, y18n) {
const implyFail = []

Object.keys(implied).forEach(function (key) {
var booleanNegation
if (yargs.getOptions().configuration['boolean-negation'] === false) {
booleanNegation = false
} else {
booleanNegation = true
}
var num
const origKey = key
var value = implied[key]
Expand All @@ -240,7 +234,7 @@ module.exports = function (yargs, usage, y18n) {
if (typeof key === 'number') {
// check length of argv._
key = argv._.length >= key
} else if (key.match(/^--no-.+/) && booleanNegation) {
} else if (key.match(/^--no-.+/)) {
// check if key doesn't exist
key = key.match(/^--no-(.+)/)[1]
key = !argv[key]
Expand All @@ -254,7 +248,7 @@ module.exports = function (yargs, usage, y18n) {

if (typeof value === 'number') {
value = argv._.length >= value
} else if (value.match(/^--no-.+/) && booleanNegation) {
} else if (value.match(/^--no-.+/)) {
value = value.match(/^--no-(.+)/)[1]
value = !argv[value]
} else {
Expand Down
68 changes: 60 additions & 8 deletions test/validation.js
Expand Up @@ -12,7 +12,7 @@ describe('validation tests', function () {
it("fails if '_' populated, and implied argument not set", function (done) {
yargs(['cat'])
.implies({
1: 'foo'
1: 'foo' // 1 arg in _ means --foo is required
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
Expand All @@ -25,7 +25,7 @@ describe('validation tests', function () {
yargs(['--foo'])
.boolean('foo')
.implies({
'foo': 1
'foo': 1 // --foo means 1 arg in _ is required
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
Expand All @@ -37,7 +37,7 @@ describe('validation tests', function () {
it("fails if --no-foo's implied argument is not set", function (done) {
yargs([])
.implies({
'--no-bar': 'foo'
'--no-bar': 'foo' // when --bar is not given, --foo is required
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
Expand All @@ -49,7 +49,7 @@ describe('validation tests', function () {
it('fails if a key is set, along with a key that it implies should not be set', function (done) {
yargs(['--bar', '--foo'])
.implies({
'bar': '--no-foo'
'bar': '--no-foo' // --bar means --foo cannot be given
})
.fail(function (msg) {
msg.should.match(/Implications failed/)
Expand All @@ -58,16 +58,68 @@ describe('validation tests', function () {
.argv
})

it('does not treat --no- as a special case if boolean negation is disabled', function (done) {
yargs(['--foo'], './test/fixtures')
it('fails if implied key (with "no" in the name) is not set', function () {
var failCalled = false
yargs('--bar')
.implies({
'foo': '--no-foo'
'bar': 'noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) is required
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
msg.should.match(/Implications failed/)
return done()
})
.argv
failCalled.should.be.true
})

it('doesn\'t fail if implied key (with "no" in the name) is set', function () {
var failCalled = false
const argv = yargs('--bar --noFoo')
.implies({
'bar': 'noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) is required
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
})
.argv
failCalled.should.be.false
expect(argv.bar).to.be.true
expect(argv.noFoo).to.be.true
expect(argv.foo).to.not.exist
})

it('fails if implied key (with "no" in the name) is given when it should not', function () {
var failCalled = false
yargs('--bar --noFoo')
.implies({
'bar': '--no-noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) cannot be given
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
msg.should.match(/Implications failed/)
})
.argv
failCalled.should.be.true
})

it('doesn\'t fail if implied key (with "no" in the name) that should not be given is not set', function () {
var failCalled = false
const argv = yargs('--bar')
.implies({
'bar': '--no-noFoo' // --bar means --noFoo (or --no-foo with boolean-negation disabled) cannot be given
// note that this has nothing to do with --foo
})
.fail(function (msg) {
failCalled = true
})
.argv
failCalled.should.be.false
expect(argv.bar).to.be.true
expect(argv.noFoo).to.not.exist
expect(argv.foo).to.not.exist
})
})

Expand Down
2 changes: 1 addition & 1 deletion yargs.js
Expand Up @@ -818,7 +818,7 @@ function Yargs (processArgs, cwd, parentRequire) {

function parseArgs (args, shortCircuit) {
options.__ = y18n.__
options.configuration = pkgUp(cwd)['yargs'] || {}
options.configuration = pkgUp()['yargs'] || {}
const parsed = Parser.detailed(args, options)
var argv = parsed.argv
if (parseContext) argv = assign(parseContext, argv)
Expand Down

0 comments on commit 9bdaab7

Please sign in to comment.