diff --git a/test/_helpers/index.js b/test/_helpers/index.js index 1706dfe..092b027 100644 --- a/test/_helpers/index.js +++ b/test/_helpers/index.js @@ -47,7 +47,11 @@ const mockedDirsForSearchPaths = { '/tmp/searchPath1': { 'sp1dir1': { 'foo': {}, - 'bar': {} + 'bar': {}, + 'package.json': JSON.stringify({ + name: 'sp1dir1pkg', + version: '1.0.0' + }) }, 'sp1dir2': {}, 'sp1dir3': {} diff --git a/test/utils/utilsTest.js b/test/utils/utilsTest.js index d6bf017..47ee470 100644 --- a/test/utils/utilsTest.js +++ b/test/utils/utilsTest.js @@ -52,3 +52,26 @@ describe('populatePkgStoreFromPaths()', () => { }); }); }); + + +describe('getPkgInfo()', () => { + describe('getPkgInfo()', function () { + before(function () { + mockfs(tHelpers.mockedDirsForSearchPaths.structure); + const pkgPaths = utils.getInstalledPkgPaths(tHelpers.mockedDirsForSearchPaths.roots); + this.store = utils.populatePkgStoreFromPaths(pkgPaths); + }); + + it('returns data from package.json', function () { + const pkg = utils.getPkgInfo('sp1dir1', this.store); + chai.equal(pkg.hasError, false); + chai.equal(pkg.pkg.version, '1.0.0'); + chai.equal(pkg.pkg.name, 'sp1dir1pkg'); + chai.equal(pkg.pkg.path, '/tmp/searchPath1/sp1dir1'); + }); + + after(function () { + mockfs.restore(); + }); + }); +}); diff --git a/utils/utils.js b/utils/utils.js index cf3e5d6..1faca21 100644 --- a/utils/utils.js +++ b/utils/utils.js @@ -1,5 +1,6 @@ 'use strict'; +var readFileSync = require('fs').readFileSync; var existsSync = require('fs').existsSync; var globby = require('globby'); var path = require('path'); @@ -188,7 +189,7 @@ function findExternalSubgens(prefixes, host, installed) { * @returns {*} */ function loadPkgJsonFromPkgPath(dir) { - return require(path.join(dir, 'package.json')); + return JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8')); }