Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add --json-file-output support to code test #3161

Merged
merged 1 commit into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/lib/plugins/sast/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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