Skip to content

Commit

Permalink
Fix issue with root modules that don't have good names / versions
Browse files Browse the repository at this point in the history
  • Loading branch information
iandotkelly committed May 10, 2015
1 parent 3ab45de commit f41cebf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lib/nlf.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,19 @@ function addPackageJson(moduleData, module) {
}
}

/**
* Creates an ID for a module
* @param {Object} moduleData read-installed module data
*/
function createId(moduleData) {

if (!moduleData._id || moduleData._id === '@') {
return 'unknown(' + moduleData.path + ')@0.0.0';
}

return moduleData._id;
}

/**
* Create a module object from a record in readInstalled
*
Expand All @@ -294,10 +307,12 @@ function addPackageJson(moduleData, module) {
*/
function createModule(moduleData, callback) {

var repository = (moduleData.repository || {}).url || '(none)',
directory = moduleData.path,
module = new Module(moduleData._id, moduleData.name,
moduleData.version, directory, repository);
var repository = (moduleData.repository || {}).url || '(none)';
var directory = moduleData.path;
var id = createId(moduleData);
var name = moduleData.name || id;
var version = moduleData.version || '0.0.0';
var module = new Module(id, name, version, directory, repository);

// glob for license files
findPotentialLicenseFiles(directory, '*license*',
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/missing-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"title": "NLF Test Project",
"description": "Test project for unit tests with missing name and version",
"author": "Ian Kelly <iandotkelly@gmail.com>",
"license": "MIT"
}
27 changes: 27 additions & 0 deletions test/unit/nlf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var path = require('path');
var fixturedir = path.join(__dirname, '../fixtures/test-project');
var licensesArrayDir = path.join(__dirname, '../fixtures/licenses-array');
var licensesStringDir = path.join(__dirname, '../fixtures/licenses-string');
var missingName = path.join(__dirname, '../fixtures/missing-name');

describe('nlf', function () {

Expand Down Expand Up @@ -242,6 +243,32 @@ describe('nlf', function () {
});

});

describe('with a project without name or version', function () {

it('should correctly get the license', function(done) {

nlf.find(
{
directory: missingName
},
function (err, results) {
if (err) {
throw err;
}
results.length.should.be.equal(1);
var result = results[0];
result.name.should.be.equal('unknown(' + missingName + ')@0.0.0');
result.id.should.be.equal('unknown(' + missingName + ')@0.0.0');
result.version.should.be.equal('0.0.0');
var sources = result.licenseSources.package.sources;
sources.length.should.eql(1);
sources[0].license.should.eql('MIT');
done();
});
});
});

});

});

0 comments on commit f41cebf

Please sign in to comment.