Skip to content

Commit

Permalink
Add tests for customFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Tusch committed Sep 8, 2015
1 parent c9948c4 commit 96c2d6f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
19 changes: 11 additions & 8 deletions lib/index.js
Expand Up @@ -58,8 +58,8 @@ var flatten = function(options) {
moduleInfo.dependencyPath = json.path;
}

//Build custom format output
if (options.customFormat !== undefined) {
/*istanbul ignore next*/
if (options.customFormat) {
Object.keys(options.customFormat).forEach(function forEachCallback(item) {
if (json[item]) {
//For now, we only support strings, not JSON objects
Expand All @@ -70,7 +70,7 @@ var flatten = function(options) {
moduleInfo[item] = options.customFormat[item];
}
});
}
}

licenseData = json.license || json.licenses || undefined;
if (licenseData) {
Expand Down Expand Up @@ -288,18 +288,21 @@ exports.asMarkDown = function(sorted, customFormat) {
return text;
};

/*istanbul ignore next*/
exports.parseJson = function(jsonPath) {
if (typeof jsonPath !== 'string') {
return new Error('did not specify a path');
}

var jsonData = '';
var jsonFileContents = '';
var result = { };

try {
jsonData = fs.readFileSync(jsonPath, {encoding: 'utf8'});
jsonFileContents = fs.readFileSync(jsonPath, {encoding: 'utf8'});
result = JSON.parse(jsonFileContents);
} catch (err) {
return err;
result = err;
} finally {
return result;
}

return JSON.parse(jsonData);
};
41 changes: 35 additions & 6 deletions tests/test.js
Expand Up @@ -41,6 +41,26 @@ var tests = {
assert.equal('[abbrev@1.0.7](https://github.com/isaacs/abbrev-js) - ISC', str.split('\n')[0]);
}
},
'should parse local with unknown and custom format': {
topic: function () {
var self = this;

checker.init({
start: path.join(__dirname, '../'),
customFormat: {
'name': '<<Default Name>>',
'description': '<<Default Description>>',
'pewpew': '<<Should Never be set>>'
}
}, function (sorted) {
self.callback(null, sorted);
});
},
'and give us results': function (d) {
assert.isTrue(Object.keys(d).length > 70);
assert.equal(d['abbrev@1.0.7'].description, 'Like ruby\'s abbrev module, but in js');
}
},
'should parse local without unknown': {
topic: function () {
var self = this;
Expand Down Expand Up @@ -88,13 +108,12 @@ var tests = {
assert.equal(d, 'Undefined');
}
},
'should create a custom format using customFormat': {
'should create a custom format using customFormat successfully': {
topic: function () {
var self = this;

checker.init({
start: path.join(__dirname, '../'),
exclude: "MIT, ISC",
customFormat: {
'name': '<<Default Name>>',
'description': '<<Default Description>>',
Expand Down Expand Up @@ -141,7 +160,7 @@ var tests = {
//Test dynamically with the file directly
Object.keys(d).forEach(function(licenseItem) {
Object.keys(customJson).forEach(function(definedItem) {
assert.notEqual(d[licenseItem][definedItem], undefined);
assert.notEqual(d[licenseItem][definedItem], 'undefined');
});
});
}
Expand Down Expand Up @@ -245,18 +264,28 @@ var tests = {
assert.isTrue(data.indexOf('[foo](/path/to/foo) - MIT') > -1);
}
},
'should parse json successfully': {
'should parse json successfully (File exists + was json)': {
topic: function() {
var path = './customFormatExample.json';
var path = './tests/config/custom_format_correct.json';
return path;
},
'and check it': function(path) {
var json = checker.parseJson(path);
assert.notEqual(json, undefined);
assert.notEqual(json, null);
assert.equal(json.licenseModified, 'no');
}
},
'should parse json with errors (File exists + no json)': {
topic: function() {
var path = './tests/config/custom_format_broken.json';
return path;
},
'and check it': function(path) {
assert.throws(checker.parseJson(path));
}
},
'should parse json with errors': {
'should parse json with errors (File not found)': {
topic: function() {
var path = './NotExitingFile.json';
return path;
Expand Down

0 comments on commit 96c2d6f

Please sign in to comment.