From 987b566f7126d3017897749cad3e6fe91e733bd9 Mon Sep 17 00:00:00 2001 From: Shaun Smith Date: Mon, 21 Aug 2017 12:12:00 +0100 Subject: [PATCH] fix: ignore non-jar items from jar list --- lib/{parse-gradle.js => gradle-dep-parser.js} | 25 ++++++++--------- lib/gradle-jar-parser.js | 22 +++++++++++++++ lib/index.js | 12 +++------ ...adle.test.js => gradle-dep-parser.test.js} | 2 +- test/functional/gradle-jar-parser.test.js | 27 +++++++++++++++++++ 5 files changed, 67 insertions(+), 21 deletions(-) rename lib/{parse-gradle.js => gradle-dep-parser.js} (94%) create mode 100644 lib/gradle-jar-parser.js rename test/functional/{parse-gradle.test.js => gradle-dep-parser.test.js} (97%) create mode 100644 test/functional/gradle-jar-parser.test.js diff --git a/lib/parse-gradle.js b/lib/gradle-dep-parser.js similarity index 94% rename from lib/parse-gradle.js rename to lib/gradle-dep-parser.js index e96ec56..a2c264e 100644 --- a/lib/parse-gradle.js +++ b/lib/gradle-dep-parser.js @@ -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') @@ -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; diff --git a/lib/gradle-jar-parser.js b/lib/gradle-jar-parser.js new file mode 100644 index 0000000..3d38970 --- /dev/null +++ b/lib/gradle-jar-parser.js @@ -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, ''); +} diff --git a/lib/index.js b/lib/index.js index b564d09..1a1b81e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 = { @@ -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, @@ -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) { diff --git a/test/functional/parse-gradle.test.js b/test/functional/gradle-dep-parser.test.js similarity index 97% rename from test/functional/parse-gradle.test.js rename to test/functional/gradle-dep-parser.test.js index a64ed96..59093c3 100644 --- a/test/functional/parse-gradle.test.js +++ b/test/functional/gradle-dep-parser.test.js @@ -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) { diff --git a/test/functional/gradle-jar-parser.test.js b/test/functional/gradle-jar-parser.test.js new file mode 100644 index 0000000..de2f6a4 --- /dev/null +++ b/test/functional/gradle-jar-parser.test.js @@ -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(); +});