Skip to content

Commit

Permalink
tests: added 100% coverage for license file
Browse files Browse the repository at this point in the history
tests: added coverage check to fail if it drops
tests: added tests and ignore defensive code
  • Loading branch information
davglass committed Sep 4, 2015
1 parent a285796 commit ce8d661
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lib/index.js
Expand Up @@ -22,6 +22,7 @@ var flatten = function(options) {
unknown = options.unknown,
licenseData, files = [], licenseFile;

/*istanbul ignore next*/
if (colorize) {
moduleInfo = { licenses: chalk.bold.red(UNKNOWN) };
key = chalk.blue(json.name) + chalk.dim('@') + chalk.green(json.version);
Expand All @@ -30,33 +31,39 @@ var flatten = function(options) {
// If we have processed this key already, just return the data object.
// This was added so that we don't recurse forever if there was a circular
// dependency in the dependency tree.
/*istanbul ignore next*/
if (data[key]) {
return data;
}

data[key] = moduleInfo;

if (json.repository) {
/*istanbul ignore else*/
if (typeof json.repository === 'object' && typeof json.repository.url === 'string') {
moduleInfo.repository = json.repository.url.replace('git+ssh://git@', 'git://').replace('.git', '');
moduleInfo.repository = moduleInfo.repository.replace('git://github.com', 'https://github.com').replace('.git', '');
moduleInfo.repository = moduleInfo.repository.replace('git@github.com:', 'https://github.com/').replace('.git', '');
}
}
if (json.url) {
/*istanbul ignore next*/
if (typeof json.url === 'object') {
moduleInfo.url = json.url.web;
}
}

/*istanbul ignore next*/
if (unknown) {
moduleInfo.dependencyPath = json.path;
}

licenseData = json.license || json.licenses || undefined;
if (licenseData) {
/*istanbul ignore else*/
if (Array.isArray(licenseData) && licenseData.length > 0) {
moduleInfo.licenses = licenseData.map(function(license){
/*istanbul ignore else*/
if (typeof license === 'object') {
return license.type;
} else if (typeof license === 'string') {
Expand All @@ -72,6 +79,7 @@ var flatten = function(options) {
moduleInfo.licenses = license(json.readme);
}

/*istanbul ignore else*/
if (json.path && fs.existsSync(json.path)) {
files = fs.readdirSync(json.path).filter(function(filename) {
filename = filename.toUpperCase();
Expand All @@ -82,6 +90,7 @@ var flatten = function(options) {
files.forEach(function(filename) {
licenseFile = path.join(json.path, filename);
// Checking that the file is in fact a normal file and not a directory for example.
/*istanbul ignore else*/
if (fs.lstatSync(licenseFile).isFile()) {
if (!moduleInfo.licenses || moduleInfo.licenses.indexOf(UNKNOWN) > -1) {
//Only re-check the license if we didn't get it from elsewhere
Expand All @@ -92,11 +101,13 @@ var flatten = function(options) {
});

if (Array.isArray(moduleInfo.licenses)) {
/*istanbul ignore else*/
if (moduleInfo.licenses.length === 1) {
moduleInfo.licenses = moduleInfo.licenses[0];
}
}

/*istanbul ignore else*/
if (json.dependencies) {
Object.keys(json.dependencies).forEach(function(name) {
var childDependency = json.dependencies[name],
Expand Down Expand Up @@ -137,6 +148,7 @@ exports.init = function(options, callback) {
if (options.unknown) {
if (data[item].licenses && data[item].licenses !== UNKNOWN) {
if (data[item].licenses.indexOf('*') > -1) {
/*istanbul ignore if*/
if (colorize) {
data[item].licenses = chalk.bold.red(UNKNOWN);
} else {
Expand All @@ -145,6 +157,7 @@ exports.init = function(options, callback) {
}
}
}
/*istanbul ignore else*/
if (data[item]) {
if (options.onlyunknown) {
if (data[item].licenses.indexOf('*') > -1 ||
Expand All @@ -169,6 +182,7 @@ exports.init = function(options, callback) {
});
};

/*istanbul ignore next*/
exports.print = function(sorted) {
console.log(exports.asTree(sorted));
};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -31,7 +31,8 @@
},
"scripts": {
"pretest": "jshint --config ./node_modules/yui-lint/jshint.json ./lib/",
"test": "istanbul cover --print both vows -- --spec ./tests/*.js"
"test": "istanbul cover --print both vows -- --spec ./tests/*.js",
"posttest": "istanbul check-coverage --statements 95 --functions 100 --lines 95 --branches 90"
},
"preferGlobal": "true",
"bugs": {
Expand Down
72 changes: 72 additions & 0 deletions tests/license.js
@@ -0,0 +1,72 @@
var vows = require('vows'),
assert = require('assert'),
license = require('../lib/license');

var tests = {
loading: {
topic: function() {
return license;
},
'should be a function': function(topic) {
assert.isFunction(topic);
}
},
'undefined check': {
topic: function() {
return license(undefined);
},
'should return Undefined': function(data) {
assert.equal(data, 'Undefined');
}
},
'MIT check': {
topic: function() {
return license('asdf\nasdf\nasdf\nPermission is hereby granted, free of charge, to any');
},
'should return MIT': function(data) {
assert.equal(data, 'MIT*');
}
},
'MIT word check': {
topic: function() {
return license('asdf\nasdf\nMIT\nasdf\n');
},
'should return MIT': function(data) {
assert.equal(data, 'MIT*');
}
},
'BSD check': {
topic: function() {
return license('asdf\nRedistribution and use in source and binary forms, with or without\nasdf\n');
},
'should return BSD': function(data) {
assert.equal(data, 'BSD*');
}
},
'BSD word check': {
topic: function() {
return license('asdf\nasdf\nBSD\nasdf\n');
},
'should return BSD': function(data) {
assert.equal(data, 'BSD*');
}
},
'Apache word check': {
topic: function() {
return license('asdf\nasdf\nApache License\nasdf\n');
},
'should return Apache': function(data) {
assert.equal(data, 'Apache*');
}
},
'WTF check': {
topic: function() {
return license('DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE');
},
'should return WTF': function(data) {
assert.equal(data, 'WTF*');
}
}
};

vows.describe('licenses').addBatch(tests).export(module);
53 changes: 51 additions & 2 deletions tests/test.js
Expand Up @@ -57,7 +57,7 @@ var tests = {
}
}
},
'should parse local with unknown': {
'should parse local with unknown and excludes': {
topic: function () {
var self = this;

Expand Down Expand Up @@ -135,7 +135,56 @@ var tests = {
});
assert.equal(onlyStarsFound, false);
}
}
},
'should export a tree': {
topic: function() {
return checker.asTree([{}]);
},
'and format it': function(data) {
assert.ok(data);
assert.isTrue(data.indexOf('└─') > -1);
}
},
'should export as csv': {
topic: function() {
return checker.asCSV({
foo: {
licenses: 'MIT',
repository: '/path/to/foo'
}
});
},
'and format it': function(data) {
assert.ok(data);
assert.isTrue(data.indexOf('"foo","MIT","/path/to/foo"') > -1);
}
},
'should export as csv with partial data': {
topic: function() {
return checker.asCSV({
foo: {
}
});
},
'and format it': function(data) {
assert.ok(data);
assert.isTrue(data.indexOf('"foo","",""') > -1);
}
},
'should export as markdown': {
topic: function() {
return checker.asMarkDown({
foo: {
licenses: 'MIT',
repository: '/path/to/foo'
}
});
},
'and format it': function(data) {
assert.ok(data);
assert.isTrue(data.indexOf('[foo](/path/to/foo) - MIT') > -1);
}
},
};

vows.describe('license-checker').addBatch(tests).export(module);

0 comments on commit ce8d661

Please sign in to comment.