mirror of
https://github.com/dorny/test-reporter.git
synced 2025-12-16 06:17:10 +01:00
Add list-suites and list-tests options to limit report size
This commit is contained in:
parent
0919385c06
commit
3744805866
20 changed files with 28593 additions and 18534 deletions
33
src/main.ts
33
src/main.ts
|
|
@ -5,10 +5,12 @@ import glob from 'fast-glob'
|
|||
import {parseDartJson} from './parsers/dart-json/dart-json-parser'
|
||||
import {parseDotnetTrx} from './parsers/dotnet-trx/dotnet-trx-parser'
|
||||
import {parseJestJunit} from './parsers/jest-junit/jest-junit-parser'
|
||||
import {getReport} from './report/get-report'
|
||||
import {FileContent, ParseOptions, ParseTestResult} from './parsers/parser-types'
|
||||
import {normalizeDirPath} from './utils/file-utils'
|
||||
import {listFiles} from './utils/git'
|
||||
import {enforceCheckRunLimits, getCheckRunSha} from './utils/github-utils'
|
||||
import {Icon} from './utils/markdown-utils'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
|
|
@ -19,13 +21,25 @@ async function run(): Promise<void> {
|
|||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const maxAnnotations = parseInt(core.getInput('max-annotations', {required: true}))
|
||||
const failOnError = core.getInput('fail-on-error', {required: true}) === 'true'
|
||||
const name = core.getInput('name', {required: true})
|
||||
const path = core.getInput('path', {required: true})
|
||||
const reporter = core.getInput('reporter', {required: true})
|
||||
const token = core.getInput('token', {required: true})
|
||||
const listSuites = core.getInput('list-suites', {required: true})
|
||||
const listTests = core.getInput('list-tests', {required: true})
|
||||
const maxAnnotations = parseInt(core.getInput('max-annotations', {required: true}))
|
||||
const failOnError = core.getInput('fail-on-error', {required: true}) === 'true'
|
||||
const workDirInput = core.getInput('working-directory', {required: false})
|
||||
const token = core.getInput('token', {required: true})
|
||||
|
||||
if (listSuites !== 'all' && listSuites !== 'only-failed') {
|
||||
core.setFailed(`Input parameter 'list-suites' has invalid value`)
|
||||
return
|
||||
}
|
||||
|
||||
if (listTests !== 'all' && listTests !== 'only-failed' && listTests !== 'none') {
|
||||
core.setFailed(`Input parameter 'list-tests' has invalid value`)
|
||||
return
|
||||
}
|
||||
|
||||
if (isNaN(maxAnnotations) || maxAnnotations < 0 || maxAnnotations > 50) {
|
||||
core.setFailed(`Input parameter 'max-annotations' has invalid value`)
|
||||
|
|
@ -46,7 +60,6 @@ async function main(): Promise<void> {
|
|||
const trackedFiles = annotations ? await listFiles() : []
|
||||
|
||||
const opts: ParseOptions = {
|
||||
name,
|
||||
trackedFiles,
|
||||
workDir,
|
||||
annotations
|
||||
|
|
@ -62,9 +75,11 @@ async function main(): Promise<void> {
|
|||
|
||||
core.info(`Using test report parser '${reporter}'`)
|
||||
const result = await parser(files, opts)
|
||||
const conclusion = result.success ? 'success' : 'failure'
|
||||
|
||||
enforceCheckRunLimits(result, maxAnnotations)
|
||||
const isFailed = result.testRuns.some(tr => tr.result === 'failed')
|
||||
const conclusion = isFailed ? 'failure' : 'success'
|
||||
const icon = isFailed ? Icon.fail : Icon.success
|
||||
|
||||
core.info(`Creating check run '${name}' with conclusion '${conclusion}'`)
|
||||
await octokit.checks.create({
|
||||
|
|
@ -72,12 +87,16 @@ async function main(): Promise<void> {
|
|||
name,
|
||||
conclusion,
|
||||
status: 'completed',
|
||||
output: result.output,
|
||||
output: {
|
||||
title: `${name} ${icon}`,
|
||||
summary: getReport(result.testRuns, {listSuites, listTests}),
|
||||
annotations: result.annotations
|
||||
},
|
||||
...github.context.repo
|
||||
})
|
||||
|
||||
core.setOutput('conclusion', conclusion)
|
||||
if (failOnError && !result.success) {
|
||||
if (failOnError && isFailed) {
|
||||
core.setFailed(`Failed test has been found and 'fail-on-error' option is set to ${failOnError}`)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
import * as core from '@actions/core'
|
||||
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
|
||||
|
||||
import getReport from '../../report/get-report'
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
import {Icon, fixEol} from '../../utils/markdown-utils'
|
||||
import {fixEol} from '../../utils/markdown-utils'
|
||||
|
||||
import {
|
||||
ReportEvent,
|
||||
|
|
@ -72,16 +71,10 @@ class TestCase {
|
|||
export async function parseDartJson(files: FileContent[], options: ParseOptions): Promise<TestResult> {
|
||||
const testRuns = files.map(f => getTestRun(f.path, f.content))
|
||||
const testRunsResults = testRuns.map(getTestRunResult)
|
||||
const success = testRuns.every(tr => tr.success)
|
||||
const icon = success ? Icon.success : Icon.fail
|
||||
|
||||
return {
|
||||
success,
|
||||
output: {
|
||||
title: `${options.name.trim()} ${icon}`,
|
||||
summary: getReport(testRunsResults),
|
||||
annotations: options.annotations ? getAnnotations(testRuns, options.workDir, options.trackedFiles) : undefined
|
||||
}
|
||||
testRuns: testRunsResults,
|
||||
annotations: options.annotations ? getAnnotations(testRuns, options.workDir, options.trackedFiles) : []
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types
|
|||
import {parseStringPromise} from 'xml2js'
|
||||
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
import {Icon, fixEol} from '../../utils/markdown-utils'
|
||||
import {fixEol} from '../../utils/markdown-utils'
|
||||
import {parseIsoDate, parseNetDuration} from '../../utils/parse-utils'
|
||||
|
||||
import {
|
||||
|
|
@ -15,7 +15,6 @@ import {
|
|||
TestGroupResult,
|
||||
TestCaseResult
|
||||
} from '../../report/test-results'
|
||||
import getReport from '../../report/get-report'
|
||||
|
||||
class TestClass {
|
||||
constructor(readonly name: string) {}
|
||||
|
|
@ -54,16 +53,9 @@ export async function parseDotnetTrx(files: FileContent[], options: ParseOptions
|
|||
testClasses.push(...tc)
|
||||
}
|
||||
|
||||
const success = testRuns.every(tr => tr.result === 'success')
|
||||
const icon = success ? Icon.success : Icon.fail
|
||||
|
||||
return {
|
||||
success,
|
||||
output: {
|
||||
title: `${options.name.trim()} ${icon}`,
|
||||
summary: getReport(testRuns),
|
||||
annotations: options.annotations ? getAnnotations(testClasses, options.workDir, options.trackedFiles) : undefined
|
||||
}
|
||||
testRuns,
|
||||
annotations: options.annotations ? getAnnotations(testClasses, options.workDir, options.trackedFiles) : []
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types
|
|||
import {parseStringPromise} from 'xml2js'
|
||||
|
||||
import {JunitReport, TestCase, TestSuite} from './jest-junit-types'
|
||||
import {fixEol, Icon} from '../../utils/markdown-utils'
|
||||
import {fixEol} from '../../utils/markdown-utils'
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
|
||||
import {
|
||||
|
|
@ -13,7 +13,6 @@ import {
|
|||
TestGroupResult,
|
||||
TestCaseResult
|
||||
} from '../../report/test-results'
|
||||
import getReport from '../../report/get-report'
|
||||
|
||||
export async function parseJestJunit(files: FileContent[], options: ParseOptions): Promise<TestResult> {
|
||||
const junit: JunitReport[] = []
|
||||
|
|
@ -26,16 +25,9 @@ export async function parseJestJunit(files: FileContent[], options: ParseOptions
|
|||
testRuns.push(tr)
|
||||
}
|
||||
|
||||
const success = testRuns.every(tr => tr.result === 'success')
|
||||
const icon = success ? Icon.success : Icon.fail
|
||||
|
||||
return {
|
||||
success,
|
||||
output: {
|
||||
title: `${options.name.trim()} ${icon}`,
|
||||
summary: getReport(testRuns),
|
||||
annotations: options.annotations ? getAnnotations(junit, options.workDir, options.trackedFiles) : undefined
|
||||
}
|
||||
testRuns,
|
||||
annotations: options.annotations ? getAnnotations(junit, options.workDir, options.trackedFiles) : []
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import {Endpoints} from '@octokit/types'
|
||||
import {TestRunResult} from '../report/test-results'
|
||||
|
||||
export type OutputParameters = Endpoints['POST /repos/{owner}/{repo}/check-runs']['parameters']['output']
|
||||
export type Annotation = {
|
||||
path: string
|
||||
start_line: number
|
||||
|
|
@ -18,13 +17,12 @@ export type ParseTestResult = (files: FileContent[], options: ParseOptions) => P
|
|||
export type FileContent = {path: string; content: string}
|
||||
|
||||
export interface ParseOptions {
|
||||
name: string
|
||||
annotations: boolean
|
||||
workDir: string
|
||||
trackedFiles: string[]
|
||||
}
|
||||
|
||||
export interface TestResult {
|
||||
success: boolean
|
||||
output: OutputParameters
|
||||
testRuns: TestRunResult[]
|
||||
annotations: Annotation[]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 ``
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,10 @@ export class TestRunResult {
|
|||
get result(): TestExecutionResult {
|
||||
return this.suites.some(t => t.result === 'failed') ? 'failed' : 'success'
|
||||
}
|
||||
|
||||
get failedSuites(): TestSuiteResult[] {
|
||||
return this.suites.filter(s => s.result === 'failed')
|
||||
}
|
||||
}
|
||||
|
||||
export class TestSuiteResult {
|
||||
|
|
@ -47,6 +51,10 @@ export class TestSuiteResult {
|
|||
get result(): TestExecutionResult {
|
||||
return this.groups.some(t => t.result === 'failed') ? 'failed' : 'success'
|
||||
}
|
||||
|
||||
get failedGroups(): TestGroupResult[] {
|
||||
return this.groups.filter(grp => grp.result === 'failed')
|
||||
}
|
||||
}
|
||||
|
||||
export class TestGroupResult {
|
||||
|
|
@ -68,6 +76,10 @@ export class TestGroupResult {
|
|||
get result(): TestExecutionResult {
|
||||
return this.tests.some(t => t.result === 'failed') ? 'failed' : 'success'
|
||||
}
|
||||
|
||||
get failedTests(): TestCaseResult[] {
|
||||
return this.tests.filter(tc => tc.result === 'failed')
|
||||
}
|
||||
}
|
||||
|
||||
export class TestCaseResult {
|
||||
|
|
|
|||
|
|
@ -14,16 +14,11 @@ export function getCheckRunSha(): string {
|
|||
}
|
||||
|
||||
export function enforceCheckRunLimits(result: TestResult, maxAnnotations: number): void {
|
||||
const output = result.output
|
||||
if (!output) {
|
||||
return
|
||||
}
|
||||
|
||||
// Limit number of created annotations
|
||||
output.annotations?.splice(maxAnnotations + 1)
|
||||
result.annotations.splice(maxAnnotations + 1)
|
||||
|
||||
// Limit number of characters in annotation fields
|
||||
for (const err of output.annotations ?? []) {
|
||||
for (const err of result.annotations) {
|
||||
err.title = ellipsis(err.title || '', 255)
|
||||
err.message = ellipsis(err.message, 65535)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue