diff --git a/.gitignore b/.gitignore index 620fc0fc..118a3dea 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ __pycache__/ test/fixtures/testproj/project/project/ test/fixtures/testproj/project/target/ test/fixtures/testproj/target/ +test/fixtures/testproj-noplugin/project/project/ +test/fixtures/testproj-noplugin/project/target/ +test/fixtures/testproj-noplugin/target/ diff --git a/lib/index.js b/lib/index.js index e54ca81c..458931d0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,6 @@ var path = require('path'); var subProcess = require('./sub-process'); -var parse = require('./parse-sbt'); +var parser = require('./parse-sbt'); var packageFormatVersion = 'mvn:0.0.1'; module.exports = { @@ -15,7 +15,7 @@ function inspect(root, targetFile, options) { .then(function (result) { var packageName = path.basename(root); var packageVersion = '0.0.0'; - var depTree = parse(result, packageName, packageVersion); + var depTree = parser.parse(result, packageName, packageVersion); depTree.packageFormatVersion = packageFormatVersion; return { @@ -25,6 +25,13 @@ function inspect(root, targetFile, options) { }, package: depTree, }; + }) + .catch(function (error) { + var thrownError = parser.parseError(error); + if (thrownError) { + throw thrownError; + } + throw new Error('An unknown error occurred.'); }); } diff --git a/lib/parse-sbt.js b/lib/parse-sbt.js index 884aa433..3fb75677 100644 --- a/lib/parse-sbt.js +++ b/lib/parse-sbt.js @@ -93,6 +93,14 @@ function convertDepArrayToObject(depsArr) { }, {}); } +function parseError(text) { + if (text.indexOf('Not a valid command: dependency-tree') !== -1) { + return new Error('Plugin missing `sbt-dependency-graph` ' + + '(https://github.com/jrudolph/sbt-dependency-graph). ' + + 'Please install it globally or on the current project and try again.'); + } +} + function parse(text, name, version) { var tree = converStrToTree(text); var snykTree = { @@ -105,4 +113,7 @@ function parse(text, name, version) { return snykTree; } -module.exports = parse; +module.exports = { + parse: parse, + parseError: parseError, +}; diff --git a/package.json b/package.json index 85ea0338..d6524c22 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "test": "npm run lint && npm run test-functional", "lint": "jscs `find ./lib -name '*.js'` -v && jscs `find ./test -name '*.js'` -v", "test-functional": "tap `find ./test/functional -name '*.test.js'`", - "test-system": "tap `find ./test/system -name '*.test.js'`", + "test-system": "tap --timeout=300 `find ./test/system -name '*.test.js'`", "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, "author": "snyk.io", diff --git a/test/fixtures/sbt-no-plugin-output.txt b/test/fixtures/sbt-no-plugin-output.txt new file mode 100644 index 00000000..915b3a86 --- /dev/null +++ b/test/fixtures/sbt-no-plugin-output.txt @@ -0,0 +1,8 @@ +[info] Loading global plugins from /Users/dror/.sbt/0.13/plugins/project +[info] Loading global plugins from /Users/dror/.sbt/0.13/plugins +[info] Loading project definition from /Users/dror/work/snyk/snyk-sbt-plugin/test/fixtures/testproj-noplugin/project +[info] Set current project to Hello (in build file:/Users/dror/work/snyk/snyk-sbt-plugin/test/fixtures/testproj-noplugin/) +[error] Not a valid command: dependency-tree +[error] Not a valid key: dependency-tree (similar: dependency-overrides, dependencyOverrides, dependency-classpath) +[error] dependency-tree +[error] ^ diff --git a/test/fixtures/testproj-noplugin/build.sbt b/test/fixtures/testproj-noplugin/build.sbt new file mode 100644 index 00000000..a35abef2 --- /dev/null +++ b/test/fixtures/testproj-noplugin/build.sbt @@ -0,0 +1,10 @@ +lazy val root = (project in file(".")). + settings( + inThisBuild(List( + organization := "com.example", + scalaVersion := "2.12.1", + version := "0.1.0-SNAPSHOT" + )), + name := "Hello", + libraryDependencies += "axis" % "axis" % "1.4" + ) diff --git a/test/fixtures/testproj-noplugin/project/build.properties b/test/fixtures/testproj-noplugin/project/build.properties new file mode 100644 index 00000000..27e88aa1 --- /dev/null +++ b/test/fixtures/testproj-noplugin/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.13 diff --git a/test/fixtures/testproj-noplugin/project/site.sbt b/test/fixtures/testproj-noplugin/project/site.sbt new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/testproj-noplugin/src/main/scala/example/Hello.scala b/test/fixtures/testproj-noplugin/src/main/scala/example/Hello.scala new file mode 100644 index 00000000..80ea40a9 --- /dev/null +++ b/test/fixtures/testproj-noplugin/src/main/scala/example/Hello.scala @@ -0,0 +1,9 @@ +package example + +object Hello extends Greeting with App { + println(greeting) +} + +trait Greeting { + lazy val greeting: String = "hello" +} diff --git a/test/functional/parse-sbt.test.js b/test/functional/parse-sbt.test.js index fe418d7e..526d481a 100644 --- a/test/functional/parse-sbt.test.js +++ b/test/functional/parse-sbt.test.js @@ -1,12 +1,13 @@ -var test = require('tap-only'); -var parse = require('../../lib/parse-sbt'); var fs = require('fs'); +var path = require('path'); +var test = require('tap-only'); +var parser = require('../../lib/parse-sbt'); test('parse a `sbt dependencies` output', function (t) { t.plan(4); - var sbtOutput = fs.readFileSync( - __dirname + '/../fixtures/sbt-dependency-output.txt', 'utf8'); - var depTree = parse(sbtOutput, 'testApp', '1.0.1'); + var sbtOutput = fs.readFileSync(path.join( + __dirname, '..', 'fixtures', 'sbt-dependency-output.txt'), 'utf8'); + var depTree = parser.parse(sbtOutput, 'testApp', '1.0.1'); t.equal(depTree .dependencies['myproject-common:myproject-common_2.11'] @@ -44,3 +45,12 @@ test('parse a `sbt dependencies` output', function (t) { ], '`from` array is good'); }); + + +test('parse an error output', function (t) { + t.plan(1); + var sbtOutput = fs.readFileSync(path.join( + __dirname, '..', 'fixtures', 'sbt-no-plugin-output.txt'), 'utf8'); + var error = parser.parseError(sbtOutput, 'testApp', '1.0.1'); + t.type(error, 'object', 'Error thrown correctly'); +}); diff --git a/test/system/plugin.test.js b/test/system/plugin.test.js index 1ed46d71..1e50b804 100644 --- a/test/system/plugin.test.js +++ b/test/system/plugin.test.js @@ -1,11 +1,12 @@ +var fs = require('fs'); +var path = require('path'); var test = require('tap-only'); var plugin = require('../../lib'); -var fs = require('fs'); test('run inspect()', function (t) { t.plan(1); - return plugin.inspect( - __dirname + '/../fixtures/testproj/', + return plugin.inspect(path.join( + __dirname, '..', 'fixtures', 'testproj'), 'build.sbt') .then(function (result) { t.equal(result.package @@ -17,3 +18,13 @@ test('run inspect()', function (t) { 'correct version found'); }); }); + +test('run inspect() with no sbt plugin', function (t) { + t.plan(1); + return plugin.inspect(path.join( + __dirname, '..', 'fixtures', 'testproj-noplugin'), + 'build.sbt') + .catch(function (result) { + t.pass('Error thrown correctly'); + }); +});