Skip to content

Commit

Permalink
refactor: Strip json result array after formatting
Browse files Browse the repository at this point in the history
This refactor is done to not have a special case for
array vs single result when formatting the results
for JSON output.
Note: dataToSend will now take the jsonData after formatting
rather than the data before formatting. Since the formatting
only changes the vulnerbilities and removes the unwanted
scanResult, and since the vulnerbilities are removed from
dataToSend before it is used, this change will not affect the
CLI bahvior except for removing the unwanted scanResult.
  • Loading branch information
snaftaly committed Jan 5, 2022
1 parent 5d29d7f commit 8c93f31
Show file tree
Hide file tree
Showing 6 changed files with 2,449 additions and 2,452 deletions.
8 changes: 1 addition & 7 deletions src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,12 @@ export default async function test(
? createErrorMappedResultsForJsonOutput(results)
: results.map(mapIacTestResult);

// backwards compat - strip array IFF only one result
const jsonData =
errorMappedResults.length === 1
? errorMappedResults[0]
: errorMappedResults;

const {
stdout: dataToSend,
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
} = extractDataToSendFromResults(results, jsonData, options);
} = extractDataToSendFromResults(results, errorMappedResults, options);

if (options.json || options.sarif) {
// if all results are ok (.ok == true)
Expand Down
22 changes: 10 additions & 12 deletions src/lib/formatters/test/format-test-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function formatJsonVulnerabilityStructure(jsonResult, options: Options) {

export function extractDataToSendFromResults(
results,
jsonData,
errorMappedResults,
options: Options,
): OutputDataTypes {
let sarifData = {};
Expand All @@ -85,18 +85,16 @@ export function extractDataToSendFromResults(
stringifiedSarifData = jsonStringifyLargeObject(sarifData);
}

const jsonResults = errorMappedResults.map((res) =>
createJsonResultOutput(res, options),
);

// backwards compat - strip array IFF only one result
const jsonData = jsonResults.length === 1 ? jsonResults[0] : jsonResults;

let stringifiedJsonData = '';
if (options.json || options['json-file-output']) {
if (Array.isArray(jsonData)) {
const jsonResult = jsonData.map((res) =>
createJsonResultOutput(res, options),
);
stringifiedJsonData = jsonStringifyLargeObject(jsonResult);
} else {
stringifiedJsonData = jsonStringifyLargeObject(
createJsonResultOutput(jsonData, options),
);
}
stringifiedJsonData = jsonStringifyLargeObject(jsonData);
}

const dataToSend = options.sarif ? sarifData : jsonData;
Expand All @@ -105,7 +103,7 @@ export function extractDataToSendFromResults(
: stringifiedJsonData;

return {
stdout: dataToSend, // this is for the human-readable stdout output and is set (but not used) even if --json or --sarif is not set
stdout: dataToSend, // this is for the human-readable stdout output and is set even if --json or --sarif is set
stringifiedData, // this will be used to display either the Snyk or SARIF format JSON to stdout if --json or --sarif is set
stringifiedJsonData, // this will be used for the --json-file-output=<file.json> option
stringifiedSarifData, // this will be used for the --sarif-file-output=<file.json> option
Expand Down
Loading

0 comments on commit 8c93f31

Please sign in to comment.