Skip to content

Commit

Permalink
fix: cache pkg lookups by path to avoid returning the wrong one (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew authored and bcoe committed Jul 14, 2016
1 parent eb1e185 commit fea7e0b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/package.json
@@ -1,5 +1,8 @@
{
"version": "9.9.9",
"repository": {
"type": "svn"
},
"yargs": {
"dot-notation": false
}
Expand Down
18 changes: 17 additions & 1 deletion test/yargs.js
Expand Up @@ -934,7 +934,7 @@ describe('yargs dsl tests', function () {

argv.b.should.equal(99)
argv.foo.should.equal('a')
expect(argv.git).to.equal(undefined)
expect(argv.type).to.equal(undefined)
})

it('allows an alternative cwd to be specified', function () {
Expand All @@ -946,6 +946,22 @@ describe('yargs dsl tests', function () {
argv.dotNotation.should.equal(false)
})

it('doesn\'t mess up other pkg lookups when cwd is specified', function () {
var r = checkOutput(function () {
return yargs('--version')
.pkgConf('repository', './test/fixtures')
.version()
.argv
})
const options = yargs.getOptions()

// assert pkgConf lookup (test/fixtures/package.json)
options.configObjects.should.deep.equal([{ type: 'svn' }])
// assert parseArgs and guessVersion lookup (package.json)
expect(options.configuration['dot-notation']).to.be.undefined
r.logs[0].should.not.equal('9.9.9') // breaks when yargs gets to this version
})

// see https://github.com/yargs/yargs/issues/485
it('handles an invalid package.json', function () {
var argv = yargs('--foo a')
Expand Down
9 changes: 5 additions & 4 deletions yargs.js
Expand Up @@ -355,9 +355,10 @@ function Yargs (processArgs, cwd, parentRequire) {
return self
}

var pkg = null
var pkgs = {}
function pkgUp (path) {
if (pkg && !path) return pkg
var npath = path || '*'
if (pkgs[npath]) return pkgs[npath]
const readPkgUp = require('read-pkg-up')

var obj = {}
Expand All @@ -367,8 +368,8 @@ function Yargs (processArgs, cwd, parentRequire) {
})
} catch (noop) {}

if (obj.pkg) pkg = obj.pkg
return pkg || {}
pkgs[npath] = obj.pkg || {}
return pkgs[npath]
}

self.parse = function (args, shortCircuit) {
Expand Down

0 comments on commit fea7e0b

Please sign in to comment.