Skip to content

Commit

Permalink
Merge pull request #2 from snyk/fix/check-plugin
Browse files Browse the repository at this point in the history
fix: check whether the sbt dependency plugin exists
  • Loading branch information
deebugger committed Jun 21, 2017
2 parents 838302d + c7fd1ee commit 0801e0d
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
11 changes: 9 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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 {
Expand All @@ -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.');
});
}

Expand Down
13 changes: 12 additions & 1 deletion lib/parse-sbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -105,4 +113,7 @@ function parse(text, name, version) {
return snykTree;
}

module.exports = parse;
module.exports = {
parse: parse,
parseError: parseError,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/sbt-no-plugin-output.txt
Original file line number Diff line number Diff line change
@@ -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] ^
10 changes: 10 additions & 0 deletions test/fixtures/testproj-noplugin/build.sbt
Original file line number Diff line number Diff line change
@@ -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"
)
1 change: 1 addition & 0 deletions test/fixtures/testproj-noplugin/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.13
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example

object Hello extends Greeting with App {
println(greeting)
}

trait Greeting {
lazy val greeting: String = "hello"
}
20 changes: 15 additions & 5 deletions test/functional/parse-sbt.test.js
Original file line number Diff line number Diff line change
@@ -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']
Expand Down Expand Up @@ -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');
});
17 changes: 14 additions & 3 deletions test/system/plugin.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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');
});
});

0 comments on commit 0801e0d

Please sign in to comment.