From cdd907c88ba6cfe641a6558b3a788be73252f54c Mon Sep 17 00:00:00 2001 From: Aryeh Beitz Date: Mon, 15 Jan 2018 14:20:13 +0200 Subject: [PATCH] fix: json obj for snyk test error --- cli/commands/test.js | 12 ++++++++++-- test/acceptance/cli.acceptance.test.js | 26 +++++++++++++++++++++++--- test/cli.test.js | 17 +++++++++++++++-- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/cli/commands/test.js b/cli/commands/test.js index 572fcf5d3b..6259527b8d 100644 --- a/cli/commands/test.js +++ b/cli/commands/test.js @@ -58,7 +58,7 @@ function test() { } }) .then(function (res) { - results.push(res); + results.push(_.assign(res, {path: path})); }); }); }, Promise.resolve()); @@ -69,7 +69,15 @@ function test() { // results is now an array of 1 or more test results // values depend on `options.json` value - string or object if (options.json) { - // backwards compat - strip array iff only one result + results = results.map(function (result) { + // add json for when thrown exception + if (result instanceof Error) { + return {ok: false, error: result.message, path: result.path}; + } + return result; + }); + + // backwards compat - strip array IFF only one result var dataToSend = results.length === 1 ? results[0] : results; var json = JSON.stringify(dataToSend, '', 2); diff --git a/test/acceptance/cli.acceptance.test.js b/test/acceptance/cli.acceptance.test.js index 77110000d5..c23ced8e31 100644 --- a/test/acceptance/cli.acceptance.test.js +++ b/test/acceptance/cli.acceptance.test.js @@ -57,6 +57,22 @@ before('prime config', function (t) { }); +test('test cli with multiple params: good and bad', function (t) { + t.plan(6); + return cli.test('/', 'semver', {registry: 'npm', org: 'EFF', json: true}) + .then(function () { + t.fail('expect to error'); + }).catch(function (error) { + errObj = JSON.parse(error.message); + t.ok(errObj.length == 2, 'expecting two results'); + t.notOk(errObj[0].ok, 'first object shouldnt be ok'); + t.ok(errObj[1].ok, 'second object should be ok'); + t.ok(errObj[0].path.length > 0, 'should have path'); + t.ok(errObj[1].path.length > 0, 'should have path'); + t.pass('info on both objects'); + }); +}); + /** * Remote package `test` */ @@ -975,16 +991,20 @@ test('`monitor --policy-path`', function (t) { }); }); - -test('`monitor non-existing`', function (t) { +test('`monitor non-existing --json`', function (t) { chdirWorkspaces(); return cli.monitor('non-existing', {json: true}) .then(function () { t.fail('should have failed'); }) .catch(function (error) { + var errObj = JSON.parse(error.message); + t.notOk(errObj.ok, 'ok object should be false'); + t.match(errObj.error, + 'snyk monitor should be pointed at an existing project', + 'show err message'); + t.match(errObj.path, 'non-existing', 'should show specified path'); t.pass('throws error'); - t.match(error.message, 'pointed at an existing project', 'shows error'); }); }); diff --git a/test/cli.test.js b/test/cli.test.js index 482bb85d75..fd2e10d7b1 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -55,7 +55,7 @@ before('prime config', function (t) { }).catch(t.bailout).then(t.end); }); -test('cli', function (t) { +test('cli tests for online repos', function (t) { t.plan(2); cli.test('semver@2').then(function (res) { @@ -76,7 +76,20 @@ test('cli', function (t) { t.equal(vuln.id, 'npm:semver:20150403', 'correctly found vulnerability: ' + vuln.id); }); - +}); + +test('cli tests erroring paths', {timeout: 3000}, function (t) { + t.plan(3); + + cli.test('/', {json: true}).then(function (res) { + t.fail(res); + }).catch(function (error) { + var errObj = JSON.parse(error.message); + t.ok(errObj.error.length > 1, 'should display error message'); + t.match(errObj.path, '/', 'path property should be populated') + t.pass('error json with correct output when one bad project specified'); + t.end(); + }); }); test('monitor', function (t) {