Skip to content

Commit

Permalink
feat: add --json-file-output support to code test
Browse files Browse the repository at this point in the history
Since --json produces the same output as --sarif, the same applies here.
We also support both --sarif-file-output and --json-file-output at the
same time, so as to be consistent with other commands.

[ZEN-128]
  • Loading branch information
strassl-snyk committed Apr 21, 2022
1 parent 2873a3a commit b97d0e6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 23 deletions.
23 changes: 17 additions & 6 deletions src/lib/plugins/sast/index.ts
Expand Up @@ -56,20 +56,24 @@ export const codePlugin: EcosystemPlugin = {
meta,
prefix,
);
let sarifResult;
if (numOfIssues > 0 && options['no-markdown']) {
sarifTypedResult.runs?.[0].results?.forEach(({ message }) => {
delete message.markdown;
});
}
let sarifResult: string | undefined;
if (options['sarif-file-output']) {
sarifResult = jsonStringifyLargeObject(sarifTypedResult);
}
let jsonResult: string | undefined;
if (options['json-file-output']) {
jsonResult = jsonStringifyLargeObject(sarifTypedResult);
}
if (options.sarif || options.json) {
readableResult = jsonStringifyLargeObject(sarifTypedResult);
}
if (numOfIssues > 0) {
hasIssues(readableResult, sarifResult);
throwIssuesError({ readableResult, sarifResult, jsonResult });
}
return sarifResult ? { readableResult, sarifResult } : { readableResult };
} catch (error) {
Expand Down Expand Up @@ -117,11 +121,18 @@ function isUnauthorizedError(error: any): boolean {
);
}

function hasIssues(readableResult: string, sarifResult?: string): Error {
const err = new Error(readableResult) as any;
function throwIssuesError(args: {
readableResult: string;
sarifResult: string | undefined;
jsonResult: string | undefined;
}): Error {
const err = new Error(args.readableResult) as any;
err.code = 'VULNS';
if (sarifResult !== undefined) {
err.sarifStringifiedResults = sarifResult;
if (args.sarifResult !== undefined) {
err.sarifStringifiedResults = args.sarifResult;
}
if (args.jsonResult !== undefined) {
err.jsonStringifiedResults = args.jsonResult;
}
throw err;
}
57 changes: 40 additions & 17 deletions test/jest/unit/snyk-code/snyk-code-test.spec.ts
Expand Up @@ -258,15 +258,31 @@ describe('Test snyk code', () => {
).rejects.toHaveProperty('userMessage', 'Test limit reached!');
});

it('should create sarif result when `--sarif-file-output` is used', async () => {
it.each([
{
name:
'should write only sarif result to file when only `--sarif-file-output` is used',
options: { 'sarif-file-output': true, 'json-file-output': false },
},
{
name:
'should write only json result to file when only `--json-file-output` is used',
options: { 'sarif-file-output': false, 'json-file-output': true },
},
{
name:
'should write sarif and json results to file when `--sarif-file-output` and `--json-file-output` are used',
options: { 'sarif-file-output': true, 'json-file-output': true },
},
])('$name', async (args) => {
const options: ArgsOptions = {
path: '',
traverseNodeModules: false,
showVulnPaths: 'none',
code: true,
_: [],
_doubleDashArgs: [],
'sarif-file-output': 'test.json',
...args.options,
};

analyzeFoldersMock.mockResolvedValue(sampleAnalyzeFoldersResponse);
Expand All @@ -278,24 +294,31 @@ describe('Test snyk code', () => {
});
trackUsageSpy.mockResolvedValue({});

let error: any;
try {
await snykTest('some/path', options);
} catch (error) {
// check if stringified sarif result exists
expect(error.sarifStringifiedResults).toBeTruthy();

const errSarifResult = error.sarifStringifiedResults.trim();
const expectedSarifOutput = jsonStringifyLargeObject(
sampleSarifResponse,
).trim();
const errMessage = stripAscii(stripAnsi(error.message.trim()));
const expectedOutput = stripAscii(stripAnsi(testOutput.trim()));

// check if error code and message is correct and sarif result is as expected
expect(error.code).toBe('VULNS');
expect(errMessage).toBe(expectedOutput);
expect(errSarifResult).toBe(expectedSarifOutput);
} catch (err) {
error = err;
}
expect(error).toBeDefined();

// Currently json and sarif output are exactly the same, but can be requested independently
const expectedSarifOutput = args.options['sarif-file-output']
? jsonStringifyLargeObject(sampleSarifResponse).trim()
: undefined;
const expectedJsonOutput = args.options['json-file-output']
? jsonStringifyLargeObject(sampleSarifResponse).trim()
: undefined;
const expectedOutput = stripAscii(stripAnsi(testOutput.trim()));

const errMessage = stripAscii(stripAnsi(error.message.trim()));
const errSarifResult = error.sarifStringifiedResults?.trim();
const errJsonResult = error.jsonStringifiedResults?.trim();

expect(error.code).toStrictEqual('VULNS');
expect(errMessage).toStrictEqual(expectedOutput);
expect(errSarifResult).toStrictEqual(expectedSarifOutput);
expect(errJsonResult).toStrictEqual(expectedJsonOutput);
});

describe('Default org test in CLI output', () => {
Expand Down

0 comments on commit b97d0e6

Please sign in to comment.