Skip to content

Commit

Permalink
Merge pull request #30 from taskworld/master
Browse files Browse the repository at this point in the history
Allow duplications to be identified by a hash key
  • Loading branch information
danielstjules committed Dec 1, 2015
2 parents a093af7 + 53a913e commit dcb25bc
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Inspector.prototype._analyze = function() {
for (i = 0; i < nodes.length; i++) {
if (nodes[i].length < 2) continue;

match = new Match(nodes[i]);
match = new Match(nodes[i], key);
if (this._diff) {
match.generateDiffs(this._fileContents);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/match.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var jsdiff = require('diff');
var strip = require('strip-indent');
var crypto = require('crypto');

/**
* Creates a new Match, consisting of an array of nodes. If generated, an
Expand All @@ -9,9 +10,11 @@ var strip = require('strip-indent');
*
* @param {Node[]} nodes An array of matching nodes
*/
function Match(nodes) {
function Match(nodes, key) {
this.nodes = nodes;
this.diffs = [];
this._key = key || '';
this.hash = crypto.createHash('sha1').update(this._key).digest('hex');
}

module.exports = Match;
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ JSONReporter.prototype._getOutput = function(match) {

self = this;
output = '';
formatted = {instances: []};
formatted = {id: match.hash, instances: []};

nodes = match.nodes;
nodes.forEach(function(node) {
Expand Down
2 changes: 1 addition & 1 deletion lib/reporters/pmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ PMDReporter.prototype._getOutput = function(match) {
output += '\n';
}

output += '<duplication lines="' + this._getTotalLines(nodes) + '">\n';
output += '<duplication lines="' + this._getTotalLines(nodes) + '" id="' + match.hash + '">\n';
nodes.forEach(function(node) {
output += self._getFile(node);
});
Expand Down
8 changes: 8 additions & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ module.exports = {
};
},

collectMatches: function(inspector) {
var array = [];
inspector.on('match', function(match) {
array.push(match);
});
return array;
},

getOutput: function() {
return output;
},
Expand Down
17 changes: 15 additions & 2 deletions spec/matchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@ describe('Match', function() {
{type: 'Literal'}
];

var match = new Match(mockNodes);
var match = new Match(mockNodes, 'key');
expect(match.nodes).to.be(mockNodes);
});

it('initializes the object with an empty array for match.diffs', function() {
var match = new Match([]);
var match = new Match([], '');
expect(match.diffs).to.eql([]);
});
});

describe('hash', function() {
it('returns a different hash for different key', function() {
var match1 = new Match([], 'a');
var match2 = new Match([], 'b');
expect(match1.hash).not.to.equal(match2.hash);
});
it('returns a same hash for same key', function() {
var match1 = new Match([], 'a');
var match2 = new Match([], 'a');
expect(match1.hash).to.equal(match2.hash);
});
});

function getFixture(fixtureName){
var ast, contents, content;

Expand Down
4 changes: 4 additions & 0 deletions spec/reporters/jsonSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('JSONReporter', function() {
threshold: 1
});
var reporter = new JSONReporter(inspector);
var matches = helpers.collectMatches(inspector);

inspector.removeAllListeners('start');
inspector.removeAllListeners('end');
Expand All @@ -48,6 +49,7 @@ describe('JSONReporter', function() {

var parsedOutput = JSON.parse(helpers.getOutput());
expect(parsedOutput).to.eql({
id: matches[0].hash,
instances: [
{path: 'spec/fixtures/smallDiffs.js', lines: [1,1]},
{path: 'spec/fixtures/smallDiffs.js', lines: [2,2]},
Expand Down Expand Up @@ -97,11 +99,13 @@ describe('JSONReporter', function() {
var reporter = new JSONReporter(inspector, {
writableStream: concatStream
});
var matches = helpers.collectMatches(inspector);

inspector.run();

function onFinish(data) {
expect(JSON.parse(data)).to.eql([{
id: matches[0].hash,
instances: [
{path: 'spec/fixtures/smallDiffs.js', lines: [1,1]},
{path: 'spec/fixtures/smallDiffs.js', lines: [2,2]},
Expand Down
10 changes: 6 additions & 4 deletions spec/reporters/pmdSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ describe('PMDReporter', function() {
});

it('prints paths and line numbers in a duplication element', function() {
var inspector, reporter;
var inspector, reporter, matches;

inspector = new Inspector([fixtures.smallDiffs], {threshold: 1});
reporter = new PMDReporter(inspector);
matches = helpers.collectMatches(inspector);

inspector.removeAllListeners('start');
inspector.removeAllListeners('end');
Expand All @@ -35,7 +36,7 @@ describe('PMDReporter', function() {
helpers.restoreOutput();

expect(helpers.getOutput()).to.eql(
'<duplication lines=\"3\">\n' +
'<duplication lines=\"3\" id="' + matches[0].hash + '">\n' +
'<file path=\"' + fixtures.smallDiffs + '\" line=\"1\"/>\n' +
'<file path=\"' + fixtures.smallDiffs + '\" line=\"2\"/>\n' +
'<file path=\"' + fixtures.smallDiffs + '\" line=\"3\"/>\n' +
Expand All @@ -45,13 +46,14 @@ describe('PMDReporter', function() {
});

it('prints diffs if enabled, within a codefragment element', function() {
var inspector, reporter, absolutePath;
var inspector, reporter, absolutePath, matches;

inspector = new Inspector([fixtures.smallDiffs], {
diff: true,
threshold: 1
});
reporter = new PMDReporter(inspector, {diff: true});
matches = helpers.collectMatches(inspector);

inspector.removeAllListeners('start');
inspector.removeAllListeners('end');
Expand All @@ -60,7 +62,7 @@ describe('PMDReporter', function() {
helpers.restoreOutput();

expect(helpers.getOutput()).to.eql(
'<duplication lines=\"3\">\n' +
'<duplication lines=\"3\" id="' + matches[0].hash + '">\n' +
'<file path=\"' + fixtures.smallDiffs + '\" line=\"1\"/>\n' +
'<file path=\"' + fixtures.smallDiffs + '\" line=\"2\"/>\n' +
'<file path=\"' + fixtures.smallDiffs + '\" line=\"3\"/>\n' +
Expand Down

0 comments on commit dcb25bc

Please sign in to comment.