Skip to content

Commit

Permalink
Added more unit tests for utils.js #11
Browse files Browse the repository at this point in the history
  • Loading branch information
sthzg authored and sthzg committed Jul 8, 2016
1 parent 1aecea6 commit 7ae5dff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
6 changes: 5 additions & 1 deletion test/_helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ const mockedDirsForSearchPaths = {
'/tmp/searchPath1': {
'sp1dir1': {
'foo': {},
'bar': {}
'bar': {},
'package.json': JSON.stringify({
name: 'sp1dir1pkg',
version: '1.0.0'
})
},
'sp1dir2': {},
'sp1dir3': {}
Expand Down
23 changes: 23 additions & 0 deletions test/utils/utilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
3 changes: 2 additions & 1 deletion utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var readFileSync = require('fs').readFileSync;
var existsSync = require('fs').existsSync;
var globby = require('globby');
var path = require('path');
Expand Down Expand Up @@ -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'));
}


Expand Down

0 comments on commit 7ae5dff

Please sign in to comment.