Skip to content

Commit

Permalink
fix: monitor --json errors are valid json
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Jan 6, 2020
1 parent 70491c8 commit 97dd8d2
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/cli/commands/monitor/process-json-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ export function processJsonMonitorResponse(
});
// backwards compat - strip array if only one result
dataToSend = dataToSend.length === 1 ? dataToSend[0] : dataToSend;
const json = JSON.stringify(dataToSend, null, 2);
const stringifiedData = JSON.stringify(dataToSend, null, 2);

if (results.every((res) => res.ok)) {
return json;
return stringifiedData;
}

throw new Error(json);
const err = new Error(stringifiedData) as any;
err.json = dataToSend;
throw err;
}
37 changes: 37 additions & 0 deletions test/acceptance/cli-monitor/cli-monitor.all-projects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as sinon from 'sinon';
import * as _ from 'lodash';

interface AcceptanceTests {
language: string;
Expand Down Expand Up @@ -204,5 +205,41 @@ export const AllProjectsTests: AcceptanceTests = {
'Same body for --all-projects and --file=pom.xml',
);
},
'`monitor mono-repo-project with lockfiles --all-projects --json`': (
params,
utils,
) => async (t) => {
try {
utils.chdirWorkspaces();
const spyPlugin = sinon.spy(params.plugins, 'loadPlugin');
t.teardown(spyPlugin.restore);

const response = await params.cli.monitor('mono-repo-project', {
json: true,
allProjects: true,
});
JSON.parse(response).forEach((res) => {
if (_.isObject(res)) {
t.pass('monitor outputted JSON');
} else {
t.fail('Failed parsing monitor JSON output');
}

const keyList = [
'packageManager',
'manageUrl',
'id',
'projectName',
'isMonitored',
];

keyList.forEach((k) => {
!_.get(res, k) ? t.fail(k + ' not found') : t.pass(k + ' found');
});
});
} catch (error) {
t.fail('should have passed', error);
}
},
},
};
Empty file.
24 changes: 24 additions & 0 deletions test/system/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const server = require('../cli-server')(
// configure our fake configuration too
import * as cli from '../../src/cli/commands';
import { PolicyNotFoundError } from '../../src/lib/errors';
import { chdirWorkspaces } from '../acceptance/workspace-helper';

const before = test;
const after = test;
Expand Down Expand Up @@ -279,6 +280,29 @@ test('snyk policy', async (t) => {
}
});

test('monitor --json no supported target files', async (t) => {
try {
chdirWorkspaces();
await cli.monitor('no-supported-target-files', { json: true });
t.fail('should have thrown');
} catch (error) {
const jsonResponse = error.json;

if (_.isObject(jsonResponse)) {
t.pass('monitor outputted JSON');
} else {
t.fail('Failed parsing monitor JSON output');
}

const keyList = ['error', 'path'];
t.equals(jsonResponse.ok, false, 'result is an error');

keyList.forEach((k) => {
t.ok(_.get(jsonResponse, k, null), `${k} present`);
});
}
});

after('teardown', (t) => {
t.plan(4);

Expand Down

0 comments on commit 97dd8d2

Please sign in to comment.