-
Notifications
You must be signed in to change notification settings - Fork 214
feat: add junit output support to scorecard-classic #2727
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6265584
Add junit output for scorecard-classic
RomanHotsiy 48cf9fa
Refine scorecard formatter helpers
RomanHotsiy 005242a
Tighten junit scorecard output
RomanHotsiy f6a272d
Simplify scorecard output format type
RomanHotsiy 0dcc462
Align junit formatter with json output
RomanHotsiy 8a3e863
Trim redundant junit formatter tests
RomanHotsiy 70905e8
Fix packaged smoke regression and junit metadata
RomanHotsiy 405fcfc
Revert scorecard config fallback workaround
RomanHotsiy a5624a8
Merge branch 'main' into codex/scorecard-classic-junit-output
RomanHotsiy d1cad80
chore: move strip-ansi-codes to utils
RomanHotsiy de1255b
Merge branch 'main' into codex/scorecard-classic-junit-output
RomanHotsiy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@redocly/cli": minor | ||
| --- | ||
|
|
||
| Added support for `junit` output in the `scorecard-classic` command. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
packages/cli/src/commands/scorecard-classic/__tests__/junit-formatter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import * as openapiCore from '@redocly/openapi-core'; | ||
|
|
||
| import { printScorecardResultsAsJunit } from '../formatters/junit-formatter.js'; | ||
| import type { ScorecardProblem } from '../types.js'; | ||
|
|
||
| const createMockSource = (absoluteRef: string) => ({ | ||
| absoluteRef, | ||
| getAst: () => ({}), | ||
| getRootAst: () => ({}), | ||
| getLineColLocation: () => ({ line: 1, col: 1 }), | ||
| }); | ||
|
|
||
| describe('printScorecardResultsAsJunit', () => { | ||
| beforeEach(() => { | ||
| vi.spyOn(openapiCore.logger, 'output').mockImplementation(() => {}); | ||
| vi.spyOn(openapiCore.logger, 'info').mockImplementation(() => {}); | ||
| }); | ||
|
|
||
| const getOutput = () => | ||
| (openapiCore.logger.output as any).mock.calls.map((call: any) => call[0]).join(''); | ||
|
|
||
| it('outputs an empty junit suite when there are no problems', () => { | ||
| printScorecardResultsAsJunit('/api/openapi.yaml', [], 'Gold', true); | ||
|
|
||
| expect(getOutput()).toMatchInlineSnapshot(` | ||
| "<?xml version="1.0" encoding="UTF-8"?> | ||
| <testsuites name="redocly scorecard-classic" tests="0" failures="0" errors="0" skipped="0"> | ||
| <testsuite name="scorecard-classic" tests="0" failures="0" errors="0" skipped="0"> | ||
| <properties> | ||
| <property name="api" value="/api/openapi.yaml" /> | ||
| <property name="achievedLevel" value="Gold" /> | ||
| </properties> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| " | ||
| `); | ||
| }); | ||
|
|
||
| it('maps errors to failures and warnings to skipped test cases', () => { | ||
| const problems: ScorecardProblem[] = [ | ||
| { | ||
| message: 'Missing summary', | ||
| ruleId: 'operation-summary', | ||
| severity: 'error', | ||
| suggest: [], | ||
| location: [ | ||
| { | ||
| source: createMockSource('/api/openapi.yaml') as any, | ||
| pointer: '#/paths/~1pets/get/summary', | ||
| reportOnKey: false, | ||
| }, | ||
| ], | ||
| scorecardLevel: 'Gold', | ||
| }, | ||
| { | ||
| message: 'Missing description', | ||
| ruleId: 'operation-description', | ||
| severity: 'warn', | ||
| suggest: [], | ||
| location: [ | ||
| { | ||
| source: createMockSource('/api/openapi.yaml') as any, | ||
| pointer: '#/info', | ||
| reportOnKey: false, | ||
| }, | ||
| ], | ||
| scorecardLevel: 'Silver', | ||
| }, | ||
| ]; | ||
|
|
||
| printScorecardResultsAsJunit('/api/openapi.yaml', problems, 'Silver', false); | ||
|
|
||
| expect(getOutput()).toMatchInlineSnapshot(` | ||
| "<?xml version="1.0" encoding="UTF-8"?> | ||
| <testsuites name="redocly scorecard-classic" tests="2" failures="1" errors="0" skipped="1"> | ||
| <testsuite name="scorecard-classic" tests="2" failures="1" errors="0" skipped="1"> | ||
| <properties> | ||
| <property name="api" value="/api/openapi.yaml" /> | ||
| </properties> | ||
| <testcase classname="Gold" name="operation-summary" file="/api/openapi.yaml" line="1"> | ||
| <failure message="Missing summary" type="operation-summary">Level: Gold | ||
| Rule: operation-summary | ||
| Severity: error | ||
| File: /api/openapi.yaml | ||
| Line: 1 | ||
| Column: 1 | ||
| Pointer: #/paths/~1pets/get/summary | ||
| Message: Missing summary</failure> | ||
| </testcase> | ||
| <testcase classname="Silver" name="operation-description" file="/api/openapi.yaml" line="1"> | ||
| <skipped message="Missing description">Level: Silver | ||
| Rule: operation-description | ||
| Severity: warn | ||
| File: /api/openapi.yaml | ||
| Line: 1 | ||
| Column: 1 | ||
| Pointer: #/info | ||
| Message: Missing description</skipped> | ||
| </testcase> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| " | ||
| `); | ||
| }); | ||
|
|
||
| it('XML-escapes details and strips ANSI sequences from messages', () => { | ||
| const problems: ScorecardProblem[] = [ | ||
| { | ||
| message: '\u001b[1;31mValue must be < 5 & > 0\u001b[0m', | ||
| ruleId: 'custom/my-rule', | ||
| severity: 'error', | ||
| suggest: [], | ||
| location: [ | ||
| { | ||
| source: createMockSource('/api/openapi.yaml') as any, | ||
| pointer: '#/info', | ||
| reportOnKey: false, | ||
| }, | ||
| ], | ||
| scorecardLevel: 'Gold', | ||
| }, | ||
| ]; | ||
|
|
||
| printScorecardResultsAsJunit('/api/openapi.yaml', problems, 'Gold', false); | ||
|
|
||
| expect(getOutput()).toMatchInlineSnapshot(` | ||
| "<?xml version="1.0" encoding="UTF-8"?> | ||
| <testsuites name="redocly scorecard-classic" tests="1" failures="1" errors="0" skipped="0"> | ||
| <testsuite name="scorecard-classic" tests="1" failures="1" errors="0" skipped="0"> | ||
| <properties> | ||
| <property name="api" value="/api/openapi.yaml" /> | ||
| </properties> | ||
| <testcase classname="Gold" name="custom/my-rule" file="/api/openapi.yaml" line="1"> | ||
| <failure message="Value must be < 5 & > 0" type="custom/my-rule">Level: Gold | ||
| Rule: custom/my-rule | ||
| Severity: error | ||
| File: /api/openapi.yaml | ||
| Line: 1 | ||
| Column: 1 | ||
| Pointer: #/info | ||
| Message: Value must be < 5 & > 0</failure> | ||
| </testcase> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| " | ||
| `); | ||
| }); | ||
|
|
||
| it('falls back to the api path and zero coordinates when a problem has no location', () => { | ||
| const problems: ScorecardProblem[] = [ | ||
| { | ||
| message: 'No location error', | ||
| ruleId: 'some-rule', | ||
| severity: 'error', | ||
| suggest: [], | ||
| location: [], | ||
| scorecardLevel: undefined, | ||
| }, | ||
| ]; | ||
|
|
||
| printScorecardResultsAsJunit('/api/openapi.yaml', problems, 'Non Conformant', false); | ||
|
|
||
| expect(getOutput()).toMatchInlineSnapshot(` | ||
| "<?xml version="1.0" encoding="UTF-8"?> | ||
| <testsuites name="redocly scorecard-classic" tests="1" failures="1" errors="0" skipped="0"> | ||
| <testsuite name="scorecard-classic" tests="1" failures="1" errors="0" skipped="0"> | ||
| <properties> | ||
| <property name="api" value="/api/openapi.yaml" /> | ||
| </properties> | ||
| <testcase classname="Unknown" name="some-rule" file="/api/openapi.yaml" line="0"> | ||
| <failure message="No location error" type="some-rule">Level: Unknown | ||
| Rule: some-rule | ||
| Severity: error | ||
| File: /api/openapi.yaml | ||
| Line: 0 | ||
| Column: 0 | ||
| Message: No location error</failure> | ||
| </testcase> | ||
| </testsuite> | ||
| </testsuites> | ||
|
|
||
| " | ||
| `); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
packages/cli/src/commands/scorecard-classic/formatters/junit-formatter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { getLineColLocation, logger, xmlEscape } from '@redocly/openapi-core'; | ||
|
|
||
| import { stripAnsiCodes } from '../../../utils/strip-ansi-codes.js'; | ||
| import type { ScorecardProblem } from '../types.js'; | ||
|
|
||
| type ProblemLocation = { | ||
| file: string; | ||
| line: number; | ||
| column: number; | ||
| pointer?: string; | ||
| }; | ||
|
|
||
| function formatDetail(label: string, value: string | number): string { | ||
| return `${label}: ${xmlEscape(String(value))}`; | ||
| } | ||
|
|
||
| function getProblemLocation(problem: ScorecardProblem, apiPath: string): ProblemLocation { | ||
| const location = problem.location[0]; | ||
|
|
||
| if (!location) { | ||
| return { | ||
| file: apiPath, | ||
| line: 0, | ||
| column: 0, | ||
| }; | ||
| } | ||
|
|
||
| const lineColLocation = getLineColLocation(location); | ||
|
|
||
| return { | ||
| file: location.source.absoluteRef, | ||
| line: lineColLocation.start.line, | ||
| column: lineColLocation.start.col, | ||
| pointer: location.pointer, | ||
| }; | ||
| } | ||
|
|
||
| function formatProblemDetails(problem: ScorecardProblem, location: ProblemLocation): string { | ||
| const details = [ | ||
| formatDetail('Level', problem.scorecardLevel || 'Unknown'), | ||
| formatDetail('Rule', problem.ruleId), | ||
| formatDetail('Severity', problem.severity), | ||
| formatDetail('File', location.file), | ||
| formatDetail('Line', location.line), | ||
| formatDetail('Column', location.column), | ||
| ]; | ||
|
|
||
| if (location.pointer) { | ||
| details.push(formatDetail('Pointer', location.pointer)); | ||
| } | ||
|
|
||
| details.push(formatDetail('Message', stripAnsiCodes(problem.message))); | ||
|
|
||
| return details.join('\n'); | ||
| } | ||
|
|
||
| export function printScorecardResultsAsJunit( | ||
| path: string, | ||
| problems: ScorecardProblem[], | ||
| achievedLevel: string, | ||
| targetLevelAchieved: boolean | ||
| ): void { | ||
| const failures = problems.filter((problem) => problem.severity === 'error').length; | ||
| const skipped = problems.filter((problem) => problem.severity === 'warn').length; | ||
|
|
||
| logger.output('<?xml version="1.0" encoding="UTF-8"?>\n'); | ||
| logger.output( | ||
| `<testsuites name="redocly scorecard-classic" tests="${problems.length}" failures="${failures}" errors="0" skipped="${skipped}">\n` | ||
| ); | ||
| logger.output( | ||
| `<testsuite name="scorecard-classic" tests="${problems.length}" failures="${failures}" errors="0" skipped="${skipped}">\n` | ||
| ); | ||
| logger.output('<properties>\n'); | ||
| logger.output(`<property name="api" value="${xmlEscape(path)}" />\n`); | ||
| if (targetLevelAchieved) { | ||
| logger.output(`<property name="achievedLevel" value="${xmlEscape(achievedLevel)}" />\n`); | ||
| } | ||
| logger.output('</properties>\n'); | ||
|
|
||
| for (const problem of problems) { | ||
| const location = getProblemLocation(problem, path); | ||
| const level = problem.scorecardLevel || 'Unknown'; | ||
| const message = stripAnsiCodes(problem.message); | ||
| const details = formatProblemDetails(problem, location); | ||
|
|
||
| logger.output( | ||
| `<testcase classname="${xmlEscape(level)}" name="${xmlEscape( | ||
| problem.ruleId | ||
| )}" file="${xmlEscape(location.file)}" line="${location.line}">\n` | ||
| ); | ||
|
|
||
| if (problem.severity === 'warn') { | ||
| logger.output(`<skipped message="${xmlEscape(message)}">${details}</skipped>\n`); | ||
| } else { | ||
| logger.output( | ||
| `<failure message="${xmlEscape(message)}" type="${xmlEscape( | ||
| problem.ruleId | ||
| )}">${details}</failure>\n` | ||
| ); | ||
| } | ||
|
|
||
| logger.output('</testcase>\n'); | ||
| } | ||
|
|
||
| logger.output('</testsuite>\n'); | ||
| logger.output('</testsuites>\n\n'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.