Add list-suites and list-tests options to limit report size

This commit is contained in:
Michal Dorner 2021-01-25 12:53:45 +01:00
parent 0919385c06
commit 3744805866
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
20 changed files with 28593 additions and 18534 deletions

View file

@ -3,16 +3,50 @@ import {TestExecutionResult, TestRunResult, TestSuiteResult} from './test-result
import {Align, Icon, link, table} from '../utils/markdown-utils'
import {slug} from '../utils/slugger'
export default function getReport(results: TestRunResult[]): string {
const badge = getBadge(results)
const runsSummary = results.map(getRunSummary).join('\n\n')
const suites = results
.flatMap(tr => tr.suites)
.map((ts, i) => getSuiteSummary(ts, i))
.join('\n')
export interface ReportOptions {
listSuites?: 'all' | 'only-failed'
listTests?: 'all' | 'only-failed' | 'none'
}
const suitesSection = `# Test Suites\n\n${suites}`
return [badge, runsSummary, suitesSection].join('\n\n')
export function getReport(results: TestRunResult[], options: ReportOptions = {}): string {
const maxReportLength = 65535
const sections: string[] = []
const badge = getBadge(results)
sections.push(badge)
const runsSummary = results.map((tr, i) => getRunSummary(tr, i, options)).join('\n\n')
sections.push(runsSummary)
if (options.listTests !== 'none') {
const suitesSummary = results
.map((tr, runIndex) => {
const suites = options.listSuites === 'only-failed' ? tr.failedSuites : tr.suites
return suites
.map((ts, suiteIndex) => getSuiteSummary(ts, runIndex, suiteIndex, options))
.filter(str => str !== '')
})
.flat()
.join('\n')
const suitesSection = `# Test Suites\n\n${suitesSummary}`
sections.push(suitesSection)
}
const report = sections.join('\n\n')
if (report.length > maxReportLength) {
let msg = `**Check Run summary limit of ${maxReportLength} chars was exceed**`
if (options.listTests !== 'all') {
msg += '\n- Consider setting `list-tests` option to `only-failed` or `none`'
}
if (options.listSuites !== 'all') {
msg += '\n- Consider setting `list-suites` option to `only-failed`'
}
return `${badge}\n\n${msg}`
}
return report
}
function getBadge(results: TestRunResult[]): string {
@ -36,36 +70,49 @@ function getBadge(results: TestRunResult[]): string {
return `![${text}](https://img.shields.io/badge/${uri})`
}
function getRunSummary(tr: TestRunResult): string {
function getRunSummary(tr: TestRunResult, runIndex: number, options: ReportOptions): string {
core.info('Generating check run summary')
const time = `${(tr.time / 1000).toFixed(3)}s`
const headingLine1 = `### ${tr.path}`
const headingLine2 = `**${tr.tests}** tests were completed in **${time}** with **${tr.passed}** passed, **${tr.skipped}** skipped and **${tr.failed}** failed.`
const suitesSummary = tr.suites.map((s, i) => {
const suites = options.listSuites === 'only-failed' ? tr.failedSuites : tr.suites
const suitesSummary = suites.map((s, suiteIndex) => {
const icon = getResultIcon(s.result)
const tsTime = `${s.time}ms`
const tsName = s.name
const tsAddr = makeSuiteSlug(i, tsName).link
const tsAddr = makeSuiteSlug(runIndex, suiteIndex, tsName).link
const tsNameLink = link(tsName, tsAddr)
return [icon, tsNameLink, s.tests, tsTime, s.passed, s.skipped, s.failed]
})
const summary = table(
['Result', 'Suite', 'Tests', 'Time', `Passed ${Icon.success}`, `Skipped ${Icon.skip}`, `Failed ${Icon.fail}`],
[Align.Center, Align.Left, Align.Right, Align.Right, Align.Right, Align.Right, Align.Right],
...suitesSummary
)
const summary =
suites.length === 0
? ''
: table(
['Result', 'Suite', 'Tests', 'Time', `Passed ${Icon.success}`, `Skipped ${Icon.skip}`, `Failed ${Icon.fail}`],
[Align.Center, Align.Left, Align.Right, Align.Right, Align.Right, Align.Right, Align.Right],
...suitesSummary
)
return [headingLine1, headingLine2, summary].join('\n\n')
}
function getSuiteSummary(ts: TestSuiteResult, index: number): string {
function getSuiteSummary(ts: TestSuiteResult, runIndex: number, suiteIndex: number, options: ReportOptions): string {
const groups = options.listTests === 'only-failed' ? ts.failedGroups : ts.groups
if (groups.length === 0) {
return ''
}
const icon = getResultIcon(ts.result)
const content = ts.groups
const content = groups
.map(grp => {
const tests = options.listTests === 'only-failed' ? grp.failedTests : grp.tests
if (tests.length === 0) {
return ''
}
const header = grp.name ? `### ${grp.name}\n\n` : ''
const tests = table(
const testsTable = table(
['Result', 'Test', 'Time'],
[Align.Center, Align.Left, Align.Right],
...grp.tests.map(tc => {
@ -76,19 +123,19 @@ function getSuiteSummary(ts: TestSuiteResult, index: number): string {
})
)
return `${header}${tests}\n`
return `${header}${testsTable}\n`
})
.join('\n')
const tsName = ts.name
const tsSlug = makeSuiteSlug(index, tsName)
const tsSlug = makeSuiteSlug(runIndex, suiteIndex, tsName)
const tsNameLink = `<a id="${tsSlug.id}" href="${tsSlug.link}">${tsName}</a>`
return `## ${tsNameLink} ${icon}\n\n${content}`
}
function makeSuiteSlug(index: number, name: string): {id: string; link: string} {
// use "ts-$index-" as prefix to avoid slug conflicts after escaping the paths
return slug(`ts-${index}-${name}`)
function makeSuiteSlug(runIndex: number, suiteIndex: number, name: string): {id: string; link: string} {
// use prefix to avoid slug conflicts after escaping the paths
return slug(`r${runIndex}s${suiteIndex}-${name}`)
}
function getResultIcon(result: TestExecutionResult): string {