Skip to content

Commit

Permalink
fix: throw proper error on failing sbt execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Drukh committed Sep 8, 2018
1 parent 8243aba commit 676bd14
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 45 deletions.
23 changes: 10 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var path = require('path');
var subProcess = require('./sub-process');
var parser = require('./parse-sbt');
var packageFormatVersion = 'mvn:0.0.1';
var debug = require('debug')('snyk');

module.exports = {
inspect: inspect,
Expand All @@ -16,9 +15,8 @@ function inspect(root, targetFile, options) {
if (!options) {
options = {dev: false};
}
return subProcess.execute('sbt',
buildArgs(root, targetFile, options.args),
{cwd: root})
var sbtArgs = buildArgs(root, targetFile, options.args);
return subProcess.execute('sbt', sbtArgs, {cwd: root})
.then(function (result) {
var packageName = path.basename(root);
var packageVersion = '0.0.0';
Expand All @@ -34,15 +32,14 @@ function inspect(root, targetFile, options) {
};
})
.catch(function (error) {
debug(error);
if (typeof error == 'string') {
var thrownError = parser.parseError(error);
if (thrownError) {
throw thrownError;
}
throw new Error('snyk-sbt-plugin error: ' + error);
}

error.message = error.message + '\n\n' +
'Please make sure that the `sbt-dependency-graph` plugin ' +
'(https://github.com/jrudolph/sbt-dependency-graph) is installed ' +
'globally or on the current project, and that ' +
'`sbt ' + sbtArgs.join(' ') + '` executes successfully ' +
'on this project.\n\n' +
'If the problem persists, collect the output of ' +
'`sbt ' + sbtArgs.join(' ') + '` and contact support@snyk.io\n';
throw error;
});
}
Expand Down
9 changes: 0 additions & 9 deletions lib/parse-sbt.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,6 @@ function convertDepArrayToObject(depsArr) {
}, {});
}

function parseError(text) {
if (text.indexOf('Not a valid command: dependencyTree') !== -1) {
return new Error('Error: Missing plugin `sbt-dependency-graph` ' +
'(https://github.com/jrudolph/sbt-dependency-graph).\n' +
'Please install it globally or on the current project and try again.');
}
}

function parse(text, name, version) {
var rootTree = convertStrToTree(text);
var snykTree;
Expand Down Expand Up @@ -119,7 +111,6 @@ function parse(text, name, version) {

module.exports = {
parse: parse,
parseError: parseError,
};

function getKeys(obj) {
Expand Down
2 changes: 1 addition & 1 deletion lib/sub-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports.execute = function (command, args, options) {

proc.on('close', function (code) {
if (code !== 0) {
return reject(stdout || stderr);
return reject(new Error(stdout || stderr));
}
resolve(stdout || stderr);
});
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
},
"author": "snyk.io",
"license": "Apache-2.0",
"dependencies": {
"debug": "^3.1.0"
},
"devDependencies": {
"eslint": "^4.11.0",
"semantic-release": "^15",
Expand Down
8 changes: 0 additions & 8 deletions test/fixtures/sbt-no-plugin-output.txt

This file was deleted.

9 changes: 0 additions & 9 deletions test/functional/parse-sbt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,3 @@ test('parse `sbt dependencies` output: single configuration', function (t) {
.dependencies['joda-time:joda-time'].version,
'2.5', 'transient dependency');
});


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');
});
28 changes: 26 additions & 2 deletions test/system/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var path = require('path');
var test = require('tap-only');
var sinon = require('sinon');
var plugin = require('../../lib');
var subProcess = require('../../lib/sub-process');

test('run inspect()', function (t) {
return plugin.inspect(path.join(
Expand All @@ -23,8 +25,30 @@ test('run inspect() with no sbt plugin', function (t) {
.then(function () {
t.fail('should not be reached');
})
.catch(function (result) {
t.match(result.message, 'Missing plugin `sbt-dependency-graph`');
.catch(function (error) {
t.match(error.message, 'the `sbt-dependency-graph` plugin');
t.pass('Error thrown correctly');
});
});

test('run inspect() with failing `sbt` execution', function (t) {
stubSubProcessExec(t);
return plugin.inspect(path.join(__dirname, '..', 'fixtures', 'testproj'),
'build.sbt')
.then(function () {
t.fail('should not be reached');
})
.catch(function (error) {
t.match(error.message, 'abort');
t.pass('Error thrown correctly');
});
});


function stubSubProcessExec(t) {
sinon.stub(subProcess, 'execute')
.callsFake(function () {
return Promise.reject(new Error('abort'));
});
t.teardown(subProcess.execute.restore);
}

0 comments on commit 676bd14

Please sign in to comment.