From cdc2f5f14ad13b0090535ec96808ba22d62164ed Mon Sep 17 00:00:00 2001 From: Luigi Bulanti Date: Fri, 2 Aug 2024 16:20:37 +0100 Subject: [PATCH] issue-315 - sorting validate command output to print errors first --- cli/src/commands/validate/validate.spec.ts | 34 +++++++++++++++++++++- cli/src/commands/validate/validate.ts | 13 +++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/cli/src/commands/validate/validate.spec.ts b/cli/src/commands/validate/validate.spec.ts index f92afcc2..adb999c5 100644 --- a/cli/src/commands/validate/validate.spec.ts +++ b/cli/src/commands/validate/validate.spec.ts @@ -352,9 +352,41 @@ describe('validate', () => { const { formatSpectralOutput, - formatJsonSchemaOutput + formatJsonSchemaOutput, + sortSpectralIssueBySeverity } = exportedForTesting; +describe('sortSpectralIssueBySeverity', () => { + + it('should sort the spectral issues based on the severity', () => { + const givenFirstError = buildISpectralDiagnostic('error-code-1', 'This is the first error', 0); + const givenFirstWarning = buildISpectralDiagnostic('warning-code-1', 'This is the first warning', 1); + const givenSecondWarning = buildISpectralDiagnostic('warning-code-2', 'This is the second warning', 1); + const givenSecondError = buildISpectralDiagnostic('error-code-2', 'This is the second error', 0); + const givenNotSortedSpectralIssues: ISpectralDiagnostic[] = [ givenFirstError, givenFirstWarning, givenSecondWarning, givenSecondError]; + sortSpectralIssueBySeverity(givenNotSortedSpectralIssues); + const expectedSortedSpectralIssue: ISpectralDiagnostic[] = [givenFirstError, givenSecondError, givenFirstWarning, givenSecondWarning]; + expect(givenNotSortedSpectralIssues).toStrictEqual(expectedSortedSpectralIssue); + }); +}); + +function buildISpectralDiagnostic(code: string, message: string, severity: number): ISpectralDiagnostic{ + return { + code: code, + message: message, + severity: severity, + path: [ + 'relationships', + '0', + 'relationship-type', + 'connects', + 'destination', + 'interface' + ], + range: { start: { line: 1, character: 1 }, end: { line: 2, character: 1 } } + }; +} + describe('formatSpectralOutput', () => { it('should convert the spectral output to the ValidationOutput format', () => { diff --git a/cli/src/commands/validate/validate.ts b/cli/src/commands/validate/validate.ts index 42d3d5f2..92b1962c 100644 --- a/cli/src/commands/validate/validate.ts +++ b/cli/src/commands/validate/validate.ts @@ -55,7 +55,7 @@ export default async function validate( logger.debug(`JSON Schema validation raw output: ${prettifyJson(validateSchema.errors)}`); errors = true; jsonSchemaValidations = formatJsonSchemaOutput(validateSchema.errors); - validations = validations.concat(jsonSchemaValidations); + validations = jsonSchemaValidations.concat(validations); } const validationsOutput = getFormattedOutput(validations, format, spectralRulesetForInstantiation, spectralRulesetForPattern, jsonSchemaValidations, spectralResult); @@ -158,6 +158,7 @@ async function runSpectralValidations( if (issues && issues.length > 0) { logger.debug(`Spectral raw output: ${prettifyJson(issues)}`); + sortSpectralIssueBySeverity(issues); spectralIssues = formatSpectralOutput(issues); if (issues.filter(issue => issue.severity === 0).length > 0) { logger.debug('Spectral output contains errors'); @@ -171,6 +172,13 @@ async function runSpectralValidations( return new SpectralResult(warnings, errors, spectralIssues); } +function sortSpectralIssueBySeverity(issues: ISpectralDiagnostic[]): void{ + logger.debug('Sorting the spectral issues by the severity'); + issues.sort((issue1: ISpectralDiagnostic, issue2: ISpectralDiagnostic) => + issue1.severity.valueOf() - issue2.severity.valueOf() + ); +} + function formatJsonSchemaOutput(jsonSchemaIssues: ErrorObject[]): ValidationOutput[]{ const validationOutput : ValidationOutput[] = []; @@ -265,5 +273,6 @@ function stripRefs(obj: object) : string { export const exportedForTesting = { formatSpectralOutput, formatJsonSchemaOutput, - stripRefs + stripRefs, + sortSpectralIssueBySeverity }; \ No newline at end of file