Skip to content

Commit

Permalink
fix: json obj for snyk test error
Browse files Browse the repository at this point in the history
  • Loading branch information
aryehbeitz authored and Anton Drukh committed Jan 16, 2018
1 parent 1c579e1 commit cdd907c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
12 changes: 10 additions & 2 deletions cli/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function test() {
}
})
.then(function (res) {
results.push(res);
results.push(_.assign(res, {path: path}));
});
});
}, Promise.resolve());
Expand All @@ -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);

Expand Down
26 changes: 23 additions & 3 deletions test/acceptance/cli.acceptance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
Expand Down Expand Up @@ -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');
});
});

Expand Down
17 changes: 15 additions & 2 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit cdd907c

Please sign in to comment.