Skip to content

Commit

Permalink
fix: make -j output pure json (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
rdalton-va authored Aug 12, 2024
1 parent ea38c59 commit 79793c3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
41 changes: 11 additions & 30 deletions src/commands/suites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,23 @@ export default class Suites extends Command {
private logTestResults(
results: OASResult[],
isJsonOutput: boolean,
hasWarningFlag: boolean,
withoutWarnings: boolean,
): void {
results.forEach((result) => {
if (hasWarningFlag) this.noWarningsIncluded(result);
if (isJsonOutput) {
const output = JSONStructuredOutputFactory.buildFromOASResult(result);
this.log(JSON.stringify(output));
} else {
this.log(result.toString());
}

this.determineFailure(result);
});
}

determineFailure(result: OASResult): void {
const failedOperationCount = result.results
? result.results.filter((result) => result.failures.size > 0).length
: 0;

const totalOperationCount = result.results ? result.results.length : 0;

const passedOperationCount = totalOperationCount - failedOperationCount;
if (withoutWarnings) {
results.forEach((result) => this.removeWarnings(result));
}

if (failedOperationCount > 0) {
this.log(
`${failedOperationCount}/${totalOperationCount} operation${
totalOperationCount > 1 ? 's' : ''
} failed; ${passedOperationCount}/${totalOperationCount} operation${
totalOperationCount > 1 ? 's' : ''
} passed`,
if (isJsonOutput) {
const output = results.map((result) =>
JSONStructuredOutputFactory.buildFromOASResult(result),
);
this.log(JSON.stringify(output));
} else {
results.forEach((result) => this.log(result.toString()));
}
}

noWarningsIncluded(result: OASResult): OASResult {
removeWarnings(result: OASResult): OASResult {
result.results?.map((item) => {
delete item.warnings;
});
Expand Down
11 changes: 10 additions & 1 deletion src/validation/oas-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ export default class OASResult {
}

resultString += this.results.map((result) => result.toString()).join('');
}

resultString += '\n';
const passedOperationCount = totalOperationCount - failingOperationCount;
if (failingOperationCount > 0) {
resultString += `${failingOperationCount}/${totalOperationCount} operation${
totalOperationCount > 1 ? 's' : ''
} failed; ${passedOperationCount}/${totalOperationCount} operation${
totalOperationCount > 1 ? 's' : ''
} passed`;
}
}
return resultString;
}
}
20 changes: 12 additions & 8 deletions test/commands/suites.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
oasResultMixedResultsString,
oasResultError,
oasResultErrorJson,
oasResultMixedResultsStringWithoutWarnings,
} from '../fixtures/validation/oas-results';

const mockGetResults = jest.fn();
Expand Down Expand Up @@ -70,10 +71,7 @@ describe('Suites', () => {

await Suites.run(['pathDoesNotMatter.json']);

expect(result).toEqual([
`${oasResultFailureString}\n`,
`1/1 operation failed; 0/1 operation passed\n`,
]);
expect(result).toEqual([`${oasResultFailureString}\n`]);
});
});

Expand All @@ -83,10 +81,16 @@ describe('Suites', () => {

await Suites.run(['pathDoesNotMatter.json']);

expect(result).toEqual([
`${oasResultMixedResultsString}\n`,
`2/3 operations failed; 1/3 operations passed\n`,
]);
const string = `${oasResultMixedResultsString}\n`;
expect(result).toEqual([string]);
});
});

it('removeWarnings removes warnings from results', async () => {
mockGetResults.mockResolvedValue([oasResultMixedResults]);

await Suites.run(['pathDoesNotMatter.json', '-w']);

expect(result).toEqual([oasResultMixedResultsStringWithoutWarnings]);
});
});
21 changes: 16 additions & 5 deletions test/fixtures/validation/oas-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const oasResultSuccess = new OASResult(
);

export const oasResultSuccessString = `winterfell oas-ruleset: Succeeded
${operationExampleResultNoFailuresWarningsString}`;
${operationExampleResultNoFailuresWarningsString}\n`;

export const oasResultSuccessStructure: StructuredOutput = {
suiteId: oasResultSuccess.suiteId,
Expand All @@ -52,7 +52,7 @@ export const oasResultFailure = new OASResult(
);

export const oasResultFailureString = `riverrun oas-ruleset: 1/1 operation failed
${operationExampleResultFailuresWarningsString}`;
${operationExampleResultFailuresWarningsString}\n1/1 operation failed; 0/1 operation passed`;

export const oasResultFailureStructure: StructuredOutput = {
suiteId: oasResultSuccess.suiteId,
Expand Down Expand Up @@ -81,7 +81,18 @@ export const oasResultMixedResults = new OASResult(
);

export const oasResultMixedResultsString = `dragonstone oas-ruleset: 2/3 operations failed
${operationExampleResultFailuresWarningsString}${operationExampleResultFailuresNoWarningsString}${operationExampleResultNoFailuresWarningsString}`;
${operationExampleResultFailuresWarningsString}${operationExampleResultFailuresNoWarningsString}${operationExampleResultNoFailuresWarningsString}\n2/3 operations failed; 1/3 operations passed`;

export const oasResultMixedResultsStringWithoutWarnings = `dragonstone oas-ruleset: 2/3 operations failed
GET:/harryPotter - default: Failed
- Actual type did not match schema. Schema type: number. Actual type: string. Path: body -> age. Found 2 times
- Actual object missing required property. Required property: glasses. Path: body. Found 1 time
GET:/he-who-must-not-be-named - tomRiddle: Failed
- Actual object missing required property. Required property: house. Path: body. Found 1 time
GET:/he-who-must-not-be-named - voldermort: Succeeded
2/3 operations failed; 1/3 operations passed
`;

export const oasResultMixedResultsStructure: StructuredOutput = {
suiteId: oasResultSuccess.suiteId,
Expand Down Expand Up @@ -121,9 +132,9 @@ export const oasResultErrorStructure: StructuredOutput = {
};

export const oasResultErrorJson =
`{"suiteId":"oas-ruleset","id":"${oasResultError.testName}",` +
`[{"suiteId":"oas-ruleset","id":"${oasResultError.testName}",` +
`"config":{"oasPath":"${oasResultError.oasPath}","server":"${oasResultError.server}","authenticationType":[]},` +
`"error":"${oasResultError.error}"}`;
`"error":"${oasResultError.error}"}]`;

export const oasResultMissingPath = new OASResult(
'oas-ruleset',
Expand Down

0 comments on commit 79793c3

Please sign in to comment.