Skip to content

Commit

Permalink
Merge pull request #6 from snyk/fix/local-jars-filter
Browse files Browse the repository at this point in the history
fix: ignore non-jar items from jar list
  • Loading branch information
darscan authored Aug 21, 2017
2 parents a3d2bfb + 987b566 commit bee71af
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
25 changes: 13 additions & 12 deletions lib/parse-gradle.js → lib/gradle-dep-parser.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
var cloneDeep = require('clone-deep');

module.exports = {
parse: parse,
};

function parse(text, from) {
var data = processGradleOutput(text);
var depArray = createTree(
data.lines, data.omittedDeps, { from: [from] });
fillOmittedDependencies(depArray, data.omittedDeps);
var depTree = convertNodeArrayToObject(depArray);
return depTree;
}

function processGradleOutput(text) {
var omittedDeps = {};
var lines = text.split('\n')
Expand Down Expand Up @@ -189,15 +202,3 @@ function convertNodeArrayToObject(nodeArray) {
return acc;
}, {});
}

function parse(outputText, from) {
var processedData = processGradleOutput(outputText);
var depArray = createTree(processedData.lines,
processedData.omittedDeps,
{from: [from]});
fillOmittedDependencies(depArray, processedData.omittedDeps);
var depTree = convertNodeArrayToObject(depArray);
return depTree;
}

module.exports = parse;
22 changes: 22 additions & 0 deletions lib/gradle-jar-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
parse: parse,
};

function parse(text) {
var ext = '.jar';
if (text && text.length) {
return text.split('\n')
.map(trim)
.filter(function (line) {
return line && line.length > ext.length &&
line.substr(line.length - ext.length, ext.length) === ext;
});
}
return [];
}

function trim(text) {
// String.trim not available in es3
// see: https://goo.gl/EH6gh4
return text.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
}
12 changes: 4 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var os = require('os');
var fs = require('fs');
var path = require('path');
var subProcess = require('./sub-process');
var parse = require('./parse-gradle');
var depParser = require('./gradle-dep-parser');
var jarParser = require('./gradle-jar-parser');
var packageFormatVersion = 'mvn:0.0.1';

module.exports = {
Expand Down Expand Up @@ -43,7 +44,7 @@ function getPackage(root, targetFile, options) {
var packageName = path.basename(root);
var packageVersion = '0.0.0';
var from = packageName + '@' + packageVersion;
var depTree = parse(result, from);
var depTree = depParser.parse(result, from);
return {
dependencies: depTree,
name: packageName,
Expand All @@ -63,12 +64,7 @@ function getJarList(root, targetFile, options) {
getCommand(root, targetFile),
args,
{ cwd: root })
.then(function (result) {
if (result && result.length) {
return result.split('\n').filter(Boolean);
}
return [];
});
.then(jarParser.parse);
}

function getCommand(root, targetFile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs');
var path = require('path');
var test = require('tap-only');
var parse = require('../../lib/parse-gradle');
var parse = require('../../lib/gradle-dep-parser').parse;
var fixturePath = path.join(__dirname, '..', 'fixtures', 'no-wrapper');

test('compare full results', function (t) {
Expand Down
27 changes: 27 additions & 0 deletions test/functional/gradle-jar-parser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var test = require('tap-only');
var parse = require('../../lib/gradle-jar-parser').parse;

test('parse empty text', function (t) {
t.same(parse(''), [], 'empty list');
t.end();
});

test('parse normal text', function (t) {
var text = 'commons-cli-1.1.jar\ncommons-codec-1.6.jar\n';
var expected = [
'commons-cli-1.1.jar',
'commons-codec-1.6.jar',
];
t.same(parse(text), expected, 'jar list');
t.end();
});

test('parse badly formatted text', function (t) {
var text = 'Extra\nMore\n commons-cli-1.1.jar \n commons-codec-1.6.jar \n';
var expected = [
'commons-cli-1.1.jar',
'commons-codec-1.6.jar',
];
t.same(parse(text), expected, 'jar list');
t.end();
});

0 comments on commit bee71af

Please sign in to comment.