Merge branch 'dev' into mocha-json

This commit is contained in:
Michal Dorner 2021-04-01 00:05:41 +02:00
commit ee126813a2
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
24 changed files with 3653 additions and 1304 deletions

View file

@ -147,9 +147,22 @@ class TestReporter {
results.push(tr)
}
core.info(`Creating check run ${name}`)
const createResp = await this.octokit.checks.create({
head_sha: this.context.sha,
name,
status: 'in_progress',
output: {
title: name,
summary: ''
},
...github.context.repo
})
core.info('Creating report summary')
const {listSuites, listTests} = this
const summary = getReport(results, {listSuites, listTests})
const baseUrl = createResp.data.html_url
const summary = getReport(results, {listSuites, listTests, baseUrl})
core.info('Creating annotations')
const annotations = getAnnotations(results, this.maxAnnotations)
@ -158,10 +171,9 @@ class TestReporter {
const conclusion = isFailed ? 'failure' : 'success'
const icon = isFailed ? Icon.fail : Icon.success
core.info(`Creating check run with conclusion ${conclusion}`)
const resp = await this.octokit.checks.create({
head_sha: this.context.sha,
name,
core.info(`Updating check run conclusion (${conclusion}) and output`)
const resp = await this.octokit.checks.update({
check_run_id: createResp.data.id,
conclusion,
status: 'completed',
output: {

View file

@ -145,7 +145,11 @@ export class DartJsonParser implements TestParser {
group.tests.sort((a, b) => (a.testStart.test.line ?? 0) - (b.testStart.test.line ?? 0))
const tests = group.tests.map(tc => {
const error = this.getError(suite, tc)
return new TestCaseResult(tc.testStart.test.name, tc.result, tc.time, error)
const testName =
group.group.name !== undefined && tc.testStart.test.name.startsWith(group.group.name)
? tc.testStart.test.name.slice(group.group.name.length).trim()
: tc.testStart.test.name.trim()
return new TestCaseResult(testName, tc.result, tc.time, error)
})
return new TestGroupResult(group.group.name, tests)
})
@ -157,7 +161,6 @@ export class DartJsonParser implements TestParser {
}
const {trackedFiles} = this.options
const message = test.error?.error ?? ''
const stackTrace = test.error?.stackTrace ?? ''
const print = test.print
.filter(p => p.messageType === 'print')
@ -165,6 +168,7 @@ export class DartJsonParser implements TestParser {
.join('\n')
const details = [print, stackTrace].filter(str => str !== '').join('\n')
const src = this.exceptionThrowSource(details, trackedFiles)
const message = this.getErrorMessage(test.error?.error ?? '', print)
let path
let line
@ -187,6 +191,21 @@ export class DartJsonParser implements TestParser {
}
}
private getErrorMessage(message: string, print: string): string {
if (this.sdk === 'flutter') {
const uselessMessageRe = /^Test failed\. See exception logs above\.\nThe test description was:/m
const flutterPrintRe = /^ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK +\s+(.*)\s+When the exception was thrown, this was the stack:/ms
if (uselessMessageRe.test(message)) {
const match = print.match(flutterPrintRe)
if (match !== null) {
return match[1]
}
}
}
return message || print
}
private exceptionThrowSource(ex: string, trackedFiles: string[]): {path: string; line: number} | undefined {
const lines = ex.split(/\r?\n/g)

View file

@ -87,7 +87,9 @@ export class DotnetTrxParser implements TestParser {
}
const output = r.unitTestResult.Output
const error = output?.length > 0 && output[0].ErrorInfo?.length > 0 ? output[0].ErrorInfo[0] : undefined
const duration = parseNetDuration(r.unitTestResult.$.duration)
const durationAttr = r.unitTestResult.$.duration
const duration = durationAttr ? parseNetDuration(durationAttr) : 0
const test = new Test(r.testMethod.$.name, r.unitTestResult.$.outcome, duration, error)
tc.tests.push(test)
}

View file

@ -43,7 +43,7 @@ export interface UnitTestResult {
$: {
testId: string
testName: string
duration: string
duration?: string
outcome: Outcome
}
Output: Output[]

View file

@ -1,5 +1,6 @@
import {ellipsis, fixEol} from '../utils/markdown-utils'
import {TestRunResult} from '../test-results'
import {getFirstNonEmptyLine} from '../utils/parse-utils'
type Annotation = {
path: string
@ -98,11 +99,6 @@ function enforceCheckRunLimits(err: Annotation): Annotation {
return err
}
function getFirstNonEmptyLine(stackTrace: string): string | undefined {
const lines = stackTrace.split(/\r?\n/g)
return lines.find(str => !/^\s*$/.test(str))
}
function ident(text: string, prefix: string): string {
return text
.split(/\n/g)

View file

@ -1,61 +1,79 @@
import * as core from '@actions/core'
import {TestExecutionResult, TestRunResult, TestSuiteResult} from '../test-results'
import {Align, formatTime, Icon, link, table} from '../utils/markdown-utils'
import {getFirstNonEmptyLine} from '../utils/parse-utils'
import {slug} from '../utils/slugger'
const MAX_REPORT_LENGTH = 65535
export interface ReportOptions {
listSuites: 'all' | 'failed'
listTests: 'all' | 'failed' | 'none'
baseUrl: string
}
const defaultOptions: ReportOptions = {
listSuites: 'all',
listTests: 'all'
listTests: 'all',
baseUrl: ''
}
export function getReport(results: TestRunResult[], options: ReportOptions = defaultOptions): string {
core.info('Generating check run summary')
const maxReportLength = 65535
applySort(results)
const opts = {...options}
let report = renderReport(results, opts)
if (getByteLength(report) <= maxReportLength) {
let lines = renderReport(results, opts)
let report = lines.join('\n')
if (getByteLength(report) <= MAX_REPORT_LENGTH) {
return report
}
if (opts.listTests === 'all') {
core.info("Test report summary is too big - setting 'listTests' to 'failed'")
opts.listTests = 'failed'
report = renderReport(results, opts)
if (getByteLength(report) <= maxReportLength) {
lines = renderReport(results, opts)
report = lines.join('\n')
if (getByteLength(report) <= MAX_REPORT_LENGTH) {
return report
}
}
if (opts.listSuites === 'all') {
core.info("Test report summary is too big - setting 'listSuites' to 'failed'")
opts.listSuites = 'failed'
report = renderReport(results, opts)
if (getByteLength(report) <= maxReportLength) {
return report
core.warning(`Test report summary exceeded limit of ${MAX_REPORT_LENGTH} bytes and will be trimmed`)
return trimReport(lines)
}
function trimReport(lines: string[]): string {
const closingBlock = '```'
const errorMsg = `**Report exceeded GitHub limit of ${MAX_REPORT_LENGTH} bytes and has been trimmed**`
const maxErrorMsgLength = closingBlock.length + errorMsg.length + 2
const maxReportLength = MAX_REPORT_LENGTH - maxErrorMsgLength
let reportLength = 0
let codeBlock = false
let endLineIndex = 0
for (endLineIndex = 0; endLineIndex < lines.length; endLineIndex++) {
const line = lines[endLineIndex]
const lineLength = getByteLength(line)
reportLength += lineLength + 1
if (reportLength > maxReportLength) {
break
}
if (line === '```') {
codeBlock = !codeBlock
}
}
if (opts.listTests !== 'none') {
core.info("Test report summary is too big - setting 'listTests' to 'none'")
opts.listTests = 'none'
report = renderReport(results, opts)
if (getByteLength(report) <= maxReportLength) {
return report
}
const reportLines = lines.slice(0, endLineIndex)
if (codeBlock) {
reportLines.push('```')
}
core.warning(`Test report summary exceeded limit of ${maxReportLength} bytes`)
const badge = getReportBadge(results)
const msg = `**Test report summary exceeded limit of ${maxReportLength} bytes and was removed**`
return `${badge}\n${msg}`
reportLines.push(errorMsg)
return reportLines.join('\n')
}
function applySort(results: TestRunResult[]): void {
@ -69,7 +87,7 @@ function getByteLength(text: string): number {
return Buffer.byteLength(text, 'utf8')
}
function renderReport(results: TestRunResult[], options: ReportOptions): string {
function renderReport(results: TestRunResult[], options: ReportOptions): string[] {
const sections: string[] = []
const badge = getReportBadge(results)
sections.push(badge)
@ -77,7 +95,7 @@ function renderReport(results: TestRunResult[], options: ReportOptions): string
const runs = getTestRunsReport(results, options)
sections.push(...runs)
return sections.join('\n')
return sections
}
function getReportBadge(results: TestRunResult[]): string {
@ -118,7 +136,7 @@ function getTestRunsReport(testRuns: TestRunResult[], options: ReportOptions): s
const tableData = testRuns.map((tr, runIndex) => {
const time = formatTime(tr.time)
const name = tr.path
const addr = makeRunSlug(runIndex).link
const addr = options.baseUrl + makeRunSlug(runIndex).link
const nameLink = link(name, addr)
const passed = tr.passed > 0 ? `${tr.passed}${Icon.success}` : ''
const failed = tr.failed > 0 ? `${tr.failed}${Icon.fail}` : ''
@ -143,9 +161,9 @@ function getSuitesReport(tr: TestRunResult, runIndex: number, options: ReportOpt
const sections: string[] = []
const trSlug = makeRunSlug(runIndex)
const nameLink = `<a id="${trSlug.id}" href="${trSlug.link}">${tr.path}</a>`
const nameLink = `<a id="${trSlug.id}" href="${options.baseUrl + trSlug.link}">${tr.path}</a>`
const icon = getResultIcon(tr.result)
sections.push(`## ${nameLink} ${icon}`)
sections.push(`## ${icon}\xa0${nameLink}`)
const time = formatTime(tr.time)
const headingLine2 =
@ -163,7 +181,7 @@ function getSuitesReport(tr: TestRunResult, runIndex: number, options: ReportOpt
const tsTime = formatTime(s.time)
const tsName = s.name
const skipLink = options.listTests === 'none' || (options.listTests === 'failed' && s.result !== 'failed')
const tsAddr = makeSuiteSlug(runIndex, suiteIndex).link
const tsAddr = options.baseUrl + makeSuiteSlug(runIndex, suiteIndex).link
const tsNameLink = skipLink ? tsName : link(tsName, tsAddr)
const passed = s.passed > 0 ? `${s.passed}${Icon.success}` : ''
const failed = s.failed > 0 ? `${s.failed}${Icon.fail}` : ''
@ -186,7 +204,10 @@ function getSuitesReport(tr: TestRunResult, runIndex: number, options: ReportOpt
}
function getTestsReport(ts: TestSuiteResult, runIndex: number, suiteIndex: number, options: ReportOptions): string[] {
const groups = options.listTests === 'failed' ? ts.failedGroups : ts.groups
if (options.listTests === 'failed' && ts.result !== 'failed') {
return []
}
const groups = ts.groups
if (groups.length === 0) {
return []
}
@ -195,33 +216,30 @@ function getTestsReport(ts: TestSuiteResult, runIndex: number, suiteIndex: numbe
const tsName = ts.name
const tsSlug = makeSuiteSlug(runIndex, suiteIndex)
const tsNameLink = `<a id="${tsSlug.id}" href="${tsSlug.link}">${tsName}</a>`
const tsNameLink = `<a id="${tsSlug.id}" href="${options.baseUrl + tsSlug.link}">${tsName}</a>`
const icon = getResultIcon(ts.result)
sections.push(`### ${tsNameLink} ${icon}`)
const tsTime = formatTime(ts.time)
const headingLine2 = `**${ts.tests}** tests were completed in **${tsTime}** with **${ts.passed}** passed, **${ts.failed}** failed and **${ts.skipped}** skipped.`
sections.push(headingLine2)
sections.push(`### ${icon}\xa0${tsNameLink}`)
sections.push('```')
for (const grp of groups) {
const tests = options.listTests === 'failed' ? grp.failedTests : grp.tests
if (tests.length === 0) {
continue
if (grp.name) {
sections.push(grp.name)
}
const space = grp.name ? ' ' : ''
for (const tc of grp.tests) {
const result = getResultIcon(tc.result)
sections.push(`${space}${result} ${tc.name}`)
if (tc.error) {
const lines = (tc.error.message ?? getFirstNonEmptyLine(tc.error.details)?.trim())
?.split(/\r?\n/g)
.map(l => '\t' + l)
if (lines) {
sections.push(...lines)
}
}
}
const grpHeader = grp.name ? `\n**${grp.name}**` : ''
const testsTable = table(
['Result', 'Test', 'Time'],
[Align.Center, Align.Left, Align.Right],
...grp.tests.map(tc => {
const name = tc.name
const time = formatTime(tc.time)
const result = getResultIcon(tc.result)
return [result, name, time]
})
)
sections.push(grpHeader, testsTable)
}
sections.push('```')
return sections
}

View file

@ -41,7 +41,7 @@ export function ellipsis(text: string, maxLength: number): string {
export function formatTime(ms: number): string {
if (ms > 1000) {
return `${(ms / 1000).toFixed(3)}s`
return `${Math.round(ms / 1000)}s`
}
return `${Math.round(ms)}ms`

View file

@ -1,6 +1,5 @@
export function parseNetDuration(str: string): number {
// matches dotnet duration: 00:00:00.0010000
const durationRe = /^(\d\d):(\d\d):(\d\d\.\d+)$/
const durationRe = /^(\d\d):(\d\d):(\d\d(?:\.\d+)?)$/
const durationMatch = str.match(durationRe)
if (durationMatch === null) {
throw new Error(`Invalid format: "${str}" is not NET duration`)
@ -18,3 +17,8 @@ export function parseIsoDate(str: string): Date {
return new Date(str)
}
export function getFirstNonEmptyLine(stackTrace: string): string | undefined {
const lines = stackTrace.split(/\r?\n/g)
return lines.find(str => !/^\s*$/.test(str))
}