Merge branch 'upstream-main' into ritchxu/support-actions-summary

This commit is contained in:
Ray Xu 2024-06-25 10:26:24 -07:00
commit 84e60bad69
37 changed files with 2141 additions and 1348 deletions

View file

@ -12,10 +12,12 @@ import {getAnnotations} from './report/get-annotations'
import {getReport} from './report/get-report'
import {DartJsonParser} from './parsers/dart-json/dart-json-parser'
import {DotnetNunitParser} from './parsers/dotnet-nunit/dotnet-nunit-parser'
import {DotnetTrxParser} from './parsers/dotnet-trx/dotnet-trx-parser'
import {JavaJunitParser} from './parsers/java-junit/java-junit-parser'
import {JestJunitParser} from './parsers/jest-junit/jest-junit-parser'
import {MochaJsonParser} from './parsers/mocha-json/mocha-json-parser'
import {RspecJsonParser} from './parsers/rspec-json/rspec-json-parser'
import {SwiftXunitParser} from './parsers/swift-xunit/swift-xunit-parser'
import {normalizeDirPath, normalizeFilePath} from './utils/path-utils'
@ -226,6 +228,8 @@ class TestReporter {
switch (reporter) {
case 'dart-json':
return new DartJsonParser(options, 'dart')
case 'dotnet-nunit':
return new DotnetNunitParser(options)
case 'dotnet-trx':
return new DotnetTrxParser(options)
case 'flutter-json':
@ -236,6 +240,8 @@ class TestReporter {
return new JestJunitParser(options)
case 'mocha-json':
return new MochaJsonParser(options)
case 'rspec-json':
return new RspecJsonParser(options)
case 'swift-xunit':
return new SwiftXunitParser(options)
default:

View file

@ -123,7 +123,7 @@ export class DartJsonParser implements TestParser {
const group = suite.groups[evt.test.groupIDs[evt.test.groupIDs.length - 1]]
group.tests.push(test)
tests[evt.test.id] = test
} else if (isTestDoneEvent(evt) && !evt.hidden && tests[evt.testID]) {
} else if (isTestDoneEvent(evt) && tests[evt.testID]) {
tests[evt.testID].testDone = evt
} else if (isErrorEvent(evt) && tests[evt.testID]) {
tests[evt.testID].error = evt
@ -152,14 +152,16 @@ export class DartJsonParser implements TestParser {
return groups.map(group => {
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)
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)
})
const tests = group.tests
.filter(tc => !tc.testDone?.hidden)
.map(tc => {
const error = this.getError(suite, tc)
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)
})
}

View file

@ -0,0 +1,151 @@
import {ParseOptions, TestParser} from '../../test-parser'
import {parseStringPromise} from 'xml2js'
import {NunitReport, TestCase, TestSuite} from './dotnet-nunit-types'
import {getExceptionSource} from '../../utils/node-utils'
import {getBasePath, normalizeFilePath} from '../../utils/path-utils'
import {
TestExecutionResult,
TestRunResult,
TestSuiteResult,
TestGroupResult,
TestCaseResult,
TestCaseError
} from '../../test-results'
export class DotnetNunitParser implements TestParser {
assumedWorkDir: string | undefined
constructor(readonly options: ParseOptions) {}
async parse(path: string, content: string): Promise<TestRunResult> {
const ju = await this.getNunitReport(path, content)
return this.getTestRunResult(path, ju)
}
private async getNunitReport(path: string, content: string): Promise<NunitReport> {
try {
return (await parseStringPromise(content)) as NunitReport
} catch (e) {
throw new Error(`Invalid XML at ${path}\n\n${e}`)
}
}
private getTestRunResult(path: string, nunit: NunitReport): TestRunResult {
const suites: TestSuiteResult[] = []
const time = parseFloat(nunit['test-run'].$.duration) * 1000
this.populateTestCasesRecursive(suites, [], nunit['test-run']['test-suite'])
return new TestRunResult(path, suites, time)
}
private populateTestCasesRecursive(
result: TestSuiteResult[],
suitePath: TestSuite[],
testSuites: TestSuite[] | undefined
): void {
if (testSuites === undefined) {
return
}
for (const suite of testSuites) {
suitePath.push(suite)
this.populateTestCasesRecursive(result, suitePath, suite['test-suite'])
const testcases = suite['test-case']
if (testcases !== undefined) {
for (const testcase of testcases) {
this.addTestCase(result, suitePath, testcase)
}
}
suitePath.pop()
}
}
private addTestCase(result: TestSuiteResult[], suitePath: TestSuite[], testCase: TestCase): void {
// The last suite in the suite path is the "group".
// The rest are concatenated together to form the "suite".
// But ignore "Theory" suites.
const suitesWithoutTheories = suitePath.filter(suite => suite.$.type !== 'Theory')
const suiteName = suitesWithoutTheories
.slice(0, suitesWithoutTheories.length - 1)
.map(suite => suite.$.name)
.join('.')
const groupName = suitesWithoutTheories[suitesWithoutTheories.length - 1].$.name
let existingSuite = result.find(existingSuite => existingSuite.name === suiteName)
if (existingSuite === undefined) {
existingSuite = new TestSuiteResult(suiteName, [])
result.push(existingSuite)
}
let existingGroup = existingSuite.groups.find(existingGroup => existingGroup.name === groupName)
if (existingGroup === undefined) {
existingGroup = new TestGroupResult(groupName, [])
existingSuite.groups.push(existingGroup)
}
existingGroup.tests.push(
new TestCaseResult(
testCase.$.name,
this.getTestExecutionResult(testCase),
parseFloat(testCase.$.duration) * 1000,
this.getTestCaseError(testCase)
)
)
}
private getTestExecutionResult(test: TestCase): TestExecutionResult {
if (test.$.result === 'Failed' || test.failure) return 'failed'
if (test.$.result === 'Skipped') return 'skipped'
return 'success'
}
private getTestCaseError(tc: TestCase): TestCaseError | undefined {
if (!this.options.parseErrors || !tc.failure || tc.failure.length === 0) {
return undefined
}
const details = tc.failure[0]
let path
let line
if (details['stack-trace'] !== undefined && details['stack-trace'].length > 0) {
const src = getExceptionSource(details['stack-trace'][0], this.options.trackedFiles, file =>
this.getRelativePath(file)
)
if (src) {
path = src.path
line = src.line
}
}
return {
path,
line,
message: details.message && details.message.length > 0 ? details.message[0] : '',
details: details['stack-trace'] && details['stack-trace'].length > 0 ? details['stack-trace'][0] : ''
}
}
private getRelativePath(path: string): string {
path = normalizeFilePath(path)
const workDir = this.getWorkDir(path)
if (workDir !== undefined && path.startsWith(workDir)) {
path = path.substr(workDir.length)
}
return path
}
private getWorkDir(path: string): string | undefined {
return (
this.options.workDir ??
this.assumedWorkDir ??
(this.assumedWorkDir = getBasePath(path, this.options.trackedFiles))
)
}
}

View file

@ -0,0 +1,57 @@
export interface NunitReport {
'test-run': TestRun
}
export interface TestRun {
$: {
id: string
runstate: string
testcasecount: string
result: string
total: string
passed: string
failed: string
inconclusive: string
skipped: string
asserts: string
'engine-version': string
'clr-version': string
'start-time': string
'end-time': string
duration: string
}
'test-suite'?: TestSuite[]
}
export interface TestSuite {
$: {
name: string
type: string
}
'test-case'?: TestCase[]
'test-suite'?: TestSuite[]
}
export interface TestCase {
$: {
id: string
name: string
fullname: string
methodname: string
classname: string
runstate: string
seed: string
result: string
label: string
'start-time': string
'end-time': string
duration: string
asserts: string
}
failure?: TestFailure[]
}
export interface TestFailure {
message?: string[]
'stack-trace'?: string[]
}

View file

@ -137,11 +137,18 @@ export class JavaJunitParser implements TestParser {
}
}
let message
if (typeof failure === 'object') {
message = failure.$.message
if (failure.$?.type) {
message = failure.$.type + ': ' + message
}
}
return {
path: filePath,
line,
details,
message: typeof failure === 'object' ? failure.message : undefined
message
}
}

View file

@ -40,6 +40,8 @@ export interface TestCase {
export interface Failure {
_: string
type: string
message: string
$: {
type?: string
message: string
}
}

View file

@ -0,0 +1,112 @@
import {ParseOptions, TestParser} from '../../test-parser'
import {
TestCaseError,
TestCaseResult,
TestExecutionResult,
TestGroupResult,
TestRunResult,
TestSuiteResult
} from '../../test-results'
import {RspecJson, RspecExample} from './rspec-json-types'
export class RspecJsonParser implements TestParser {
assumedWorkDir: string | undefined
constructor(readonly options: ParseOptions) {}
async parse(path: string, content: string): Promise<TestRunResult> {
const mocha = this.getRspecJson(path, content)
const result = this.getTestRunResult(path, mocha)
result.sort(true)
return Promise.resolve(result)
}
private getRspecJson(path: string, content: string): RspecJson {
try {
return JSON.parse(content)
} catch (e) {
throw new Error(`Invalid JSON at ${path}\n\n${e}`)
}
}
private getTestRunResult(resultsPath: string, rspec: RspecJson): TestRunResult {
const suitesMap: {[path: string]: TestSuiteResult} = {}
const getSuite = (test: RspecExample): TestSuiteResult => {
const path = test.file_path
return suitesMap[path] ?? (suitesMap[path] = new TestSuiteResult(path, []))
}
for (const test of rspec.examples) {
const suite = getSuite(test)
if (test.status === 'failed') {
this.processTest(suite, test, 'failed')
} else if (test.status === 'passed') {
this.processTest(suite, test, 'success')
} else if (test.status === 'pending') {
this.processTest(suite, test, 'skipped')
}
}
const suites = Object.values(suitesMap)
return new TestRunResult(resultsPath, suites, rspec.summary.duration)
}
private processTest(suite: TestSuiteResult, test: RspecExample, result: TestExecutionResult): void {
const groupName =
test.full_description !== test.description
? test.full_description.substr(0, test.full_description.length - test.description.length).trimEnd()
: null
let group = suite.groups.find(grp => grp.name === groupName)
if (group === undefined) {
group = new TestGroupResult(groupName, [])
suite.groups.push(group)
}
const error = this.getTestCaseError(test)
const testCase = new TestCaseResult(test.full_description, result, test.run_time ?? 0, error)
group.tests.push(testCase)
}
private getTestCaseError(test: RspecExample): TestCaseError | undefined {
const backtrace = test.exception?.backtrace
const message = test.exception?.message
if (backtrace === undefined) {
return undefined
}
let path
let line
const details = backtrace.join('\n')
const src = this.getExceptionSource(backtrace)
if (src) {
path = src.path
line = src.line
}
return {
path,
line,
message,
details
}
}
private getExceptionSource(backtrace: string[]): {path: string; line: number} | undefined {
const re = /^(.*?):(\d+):/
for (const str of backtrace) {
const match = str.match(re)
if (match !== null) {
const [_, path, lineStr] = match
if (path.startsWith('./')) {
const line = parseInt(lineStr)
return {path, line}
}
}
}
return undefined
}
}

View file

@ -0,0 +1,34 @@
export interface RspecJson {
version: number
examples: RspecExample[]
summary: RspecSummary
summary_line: string
}
export interface RspecExample {
id: string
description: string
full_description: string
status: TestStatus
file_path: string
line_number: number
run_time: number
pending_message: string | null
exception?: RspecException
}
type TestStatus = 'passed' | 'failed' | 'pending'
export interface RspecException {
class: string
message: string
backtrace: string[]
}
export interface RspecSummary {
duration: number
example_count: number
failure_count: number
pending_count: number
errors_outside_of_examples_count: number
}

View file

@ -50,33 +50,17 @@ export async function downloadArtifact(
const headers = {
Authorization: `Bearer ${token}`
}
const resp = await got(req.url, {
headers,
followRedirect: false
})
core.info(`Fetch artifact URL: ${resp.statusCode} ${resp.statusMessage}`)
if (resp.statusCode !== 302) {
throw new Error('Fetch artifact URL failed: received unexpected status code')
}
const url = resp.headers.location
if (url === undefined) {
const receivedHeaders = Object.keys(resp.headers)
core.info(`Received headers: ${receivedHeaders.join(', ')}`)
throw new Error('Location header was not found in API response')
}
if (typeof url !== 'string') {
throw new Error(`Location header has unexpected value: ${url}`)
}
const downloadStream = got.stream(url, {headers})
const downloadStream = got.stream(req.url, {headers})
const fileWriterStream = createWriteStream(fileName)
core.info(`Downloading ${url}`)
downloadStream.on('redirect', response => {
core.info(`Downloading ${response.headers.location}`)
})
downloadStream.on('downloadProgress', ({transferred}) => {
core.info(`Progress: ${transferred} B`)
})
await asyncStream(downloadStream, fileWriterStream)
} finally {
core.endGroup()