Skip to content

Commit

Permalink
fix: Not outputing sarif file properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RotemS committed Sep 16, 2020
1 parent c3212af commit f063131
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/cli/commands/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,11 @@ async function test(...args: MethodArgs): Promise<TestCommandResult> {
if (options.json || options.sarif) {
// if all results are ok (.ok == true) then return the json
if (errorMappedResults.every((res) => res.ok)) {
return TestCommandResult.createJsonTestCommandResult(stringifiedData);
return TestCommandResult.createJsonTestCommandResult(
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
);
}

const err = new Error(stringifiedData) as any;
Expand All @@ -233,7 +237,11 @@ async function test(...args: MethodArgs): Promise<TestCommandResult> {
const fail = shouldFail(vulnerableResults, options.failOn);
if (!fail) {
// return here to prevent failure
return TestCommandResult.createJsonTestCommandResult(stringifiedData);
return TestCommandResult.createJsonTestCommandResult(
stringifiedData,
stringifiedJsonData,
stringifiedSarifData,
);
}
}
err.code = 'VULNS';
Expand Down
24 changes: 19 additions & 5 deletions src/cli/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ export abstract class TestCommandResult extends CommandResult {
}

public static createJsonTestCommandResult(
jsonResult: string,
stdout: string,
jsonResult?: string,
sarifResult?: string,
): JsonTestCommandResult {
return new JsonTestCommandResult(jsonResult);
return new JsonTestCommandResult(stdout, jsonResult, sarifResult);
}
}

Expand Down Expand Up @@ -72,11 +74,23 @@ class HumanReadableTestCommandResult extends TestCommandResult {
}

class JsonTestCommandResult extends TestCommandResult {
constructor(jsonResult: string) {
super(jsonResult);
constructor(stdout: string, jsonResult?: string, sarifResult?: string) {
super(stdout);
if (jsonResult) {
this.jsonResult = jsonResult;
}
if (sarifResult) {
this.sarifResult = sarifResult;
} else {
this.jsonResult = stdout;
}
}

public getJsonResult(): string {
return this.result;
return this.jsonResult;
}

public getSarifResult(): string {
return this.sarifResult;
}
}
5 changes: 3 additions & 2 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ async function handleError(args, error) {
}

saveResultsToFile(args.options, 'json', error.jsonStringifiedResults);
saveResultsToFile(args.options, 'sarif', error.jsonStringifiedResults);
saveResultsToFile(args.options, 'sarif', error.sarifStringifiedResults);

const analyticsError = vulnsFound
? {
Expand Down Expand Up @@ -416,7 +416,8 @@ function saveResultsToFile(
outputType: string,
jsonResults: string,
) {
const outputFile = options[`${outputType}-file-output`];
const flag = `${outputType}-file-output`;
const outputFile = options[flag];
if (outputFile && jsonResults) {
const outputFileStr = outputFile as string;
const fullOutputFilePath = getFullPath(outputFileStr);
Expand Down

0 comments on commit f063131

Please sign in to comment.