Add support for rspec

This commit is contained in:
oscar mampel 2024-03-14 00:40:50 +01:00
parent 7e5f292040
commit 1a3cfe6b48
11 changed files with 450 additions and 0 deletions

View file

@ -15,6 +15,7 @@ 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'
@ -223,6 +224,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

@ -0,0 +1,113 @@
import { Console } from 'console'
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
}