Skip to content

Commit

Permalink
Merge branch 'master' into ft_custom_format
Browse files Browse the repository at this point in the history
Conflicts:
	README.md
	lib/index.js
	tests/test.js
  • Loading branch information
Philipp Tusch committed Sep 7, 2015
2 parents 4aa7065 + 3fbba5b commit 7eac458
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Options
-------

* `--unknown` report guessed licenses as unknown licenses.
* `--onlyunknown` only list packages with unknown or guessed licenses.
* `--json` output in json format.
* `--csv` output in csv format.
* `--out [filepath]` write the data to a specific file.
Expand All @@ -72,6 +73,7 @@ license-checker --csv --out /path/to/licenses.csv
license-checker --unknown
license-checker --customPath customFormatExample.js
license-checker --exclude 'MIT, MIT/X11, BSD, ISC'
license-checker --onlyunknown
```

Requiring
Expand Down
1 change: 1 addition & 0 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var nopt = require('nopt'),
markdown: Boolean,
out: require('path'),
unknown: Boolean,
onlyunknown: Boolean,
version: Boolean,
color: Boolean,
start: String,
Expand Down
25 changes: 23 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
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,27 +31,31 @@ 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;
moduleInfo.dependencyPath = json.path;
}

//Build custom format output
Expand All @@ -69,8 +74,10 @@ var flatten = function(options) {

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 @@ -86,6 +93,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 @@ -96,6 +104,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 @@ -106,11 +115,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 @@ -157,6 +168,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 @@ -165,8 +177,16 @@ exports.init = function(options, callback) {
}
}
}
/*istanbul ignore else*/
if (data[item]) {
sorted[item] = data[item];
if (options.onlyunknown) {
if (data[item].licenses.indexOf('*') > -1 ||
data[item].licenses.indexOf('UNKNOWN') > -1) {
sorted[item] = data[item];
}
} else {
sorted[item] = data[item];
}
}
});
if (exclude) {
Expand All @@ -182,6 +202,7 @@ exports.init = function(options, callback) {
});
};

/*istanbul ignore next*/
exports.print = function(sorted) {
console.log(exports.asTree(sorted));
};
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "license-checker",
"description": "Check license info for a pacakge",
"author": "Dav Glass <davglass@gmail.com>",
"version": "3.1.0",
"version": "4.0.0",
"dependencies": {
"chalk": "~0.5.1",
"mkdirp": "^0.3.5",
Expand Down 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
Original file line number Diff line number Diff line change
@@ -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);
98 changes: 96 additions & 2 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var tests = {
}
}
},
'should parse local with unknown': {
'should parse local with unknown and excludes': {
topic: function () {
var self = this;

Expand Down Expand Up @@ -146,8 +146,102 @@ var tests = {
});
}
}
};

checker.init({
start: path.join(__dirname, '../'),
onlyunknown: true
}, function (sorted) {
self.callback(null, sorted);
});
},
'so we check if there is no license with a star or UNKNOWN found': function(d) {
var onlyStarsFound = true;
Object.keys(d).forEach(function(item) {
if (d[item].licenses && d[item].licenses.indexOf('UNKNOWN') !== -1) {
//Okay
} else if (d[item].licenses && d[item].licenses.indexOf('*') !== -1) {
//Okay
} else {
onlyStarsFound = false;
}
});

assert.ok(onlyStarsFound);
}
},
'should only list UNKNOWN or guessed licenses with errors (argument missing)': {
topic: function () {
var self = this;

checker.init({
start: path.join(__dirname, '../')
}, function (sorted) {
self.callback(null, sorted);
});
},
'so we check if there is no license with a star or UNKNOWN found': function(d) {
var onlyStarsFound = true;
Object.keys(d).forEach(function(item) {
if (d[item].licenses && d[item].licenses.indexOf('UNKNOWN') !== -1) {
//Okay
} else if (d[item].licenses && d[item].licenses.indexOf('*') !== -1) {
//Okay
} else {
onlyStarsFound = false;
}
});
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 7eac458

Please sign in to comment.