mirror of
https://github.com/dorny/test-reporter.git
synced 2025-12-16 06:17:10 +01:00
Create annotations where exceptions were thrown
This commit is contained in:
parent
6750c31e23
commit
fc8cfe0f32
15 changed files with 1606 additions and 19 deletions
23
src/main.ts
23
src/main.ts
|
|
@ -1,8 +1,9 @@
|
|||
import * as core from '@actions/core'
|
||||
import * as github from '@actions/github'
|
||||
import {parseJestJunit} from './parsers/jest-junit/jest-junit-parser'
|
||||
import {ParseTestResult} from './parsers/test-parser'
|
||||
import {getFileContent} from './utils/file-utils'
|
||||
import {ParseOptions, ParseTestResult} from './parsers/test-parser'
|
||||
import {getFileContent, normalizeDirPath} from './utils/file-utils'
|
||||
import {listFiles} from './utils/git'
|
||||
import {getCheckRunSha} from './utils/github-utils'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
|
|
@ -14,18 +15,34 @@ async function run(): Promise<void> {
|
|||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const annotations = core.getInput('annotations', {required: true}) === '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 workDirInput = core.getInput('working-directory', {required: false})
|
||||
|
||||
if (workDirInput) {
|
||||
process.chdir(workDirInput)
|
||||
}
|
||||
|
||||
const workDir = normalizeDirPath(workDirInput || process.cwd(), true)
|
||||
const octokit = github.getOctokit(token)
|
||||
const sha = getCheckRunSha()
|
||||
|
||||
// We won't need tracked files if we are not going to create annotations
|
||||
const trackedFiles = annotations ? await listFiles() : []
|
||||
|
||||
const opts: ParseOptions = {
|
||||
annotations,
|
||||
trackedFiles,
|
||||
workDir
|
||||
}
|
||||
|
||||
const parser = getParser(reporter)
|
||||
const content = getFileContent(path)
|
||||
const result = await parser(content)
|
||||
const result = await parser(content, opts)
|
||||
const conclusion = result.success ? 'success' : 'failure'
|
||||
|
||||
await octokit.checks.create({
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
import {TestResult} from '../test-parser'
|
||||
import {Annotation, ParseOptions, TestResult} from '../test-parser'
|
||||
import {parseStringPromise} from 'xml2js'
|
||||
|
||||
import {JunitReport, TestCase, TestSuite, TestSuites} from './jest-junit-types'
|
||||
import {Align, Icon, link, table, exceptionCell} from '../../utils/markdown-utils'
|
||||
import {normalizeFilePath} from '../../utils/file-utils'
|
||||
import {slug} from '../../utils/slugger'
|
||||
import {parseAttribute} from '../../utils/xml-utils'
|
||||
|
||||
export async function parseJestJunit(content: string): Promise<TestResult> {
|
||||
export async function parseJestJunit(content: string, options: ParseOptions): Promise<TestResult> {
|
||||
const junit = (await parseStringPromise(content, {
|
||||
attrValueProcessors: [parseAttribute]
|
||||
})) as JunitReport
|
||||
|
|
@ -17,7 +18,8 @@ export async function parseJestJunit(content: string): Promise<TestResult> {
|
|||
success,
|
||||
output: {
|
||||
title: junit.testsuites.$.name,
|
||||
summary: getSummary(success, junit)
|
||||
summary: getSummary(success, junit),
|
||||
annotations: options.annotations ? getAnnotations(junit, options.workDir, options.trackedFiles) : undefined
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -125,3 +127,55 @@ 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 getAnnotations(junit: JunitReport, workDir: string, trackedFiles: string[]): Annotation[] {
|
||||
const annotations: Annotation[] = []
|
||||
for (const suite of junit.testsuites.testsuite) {
|
||||
for (const tc of suite.testcase) {
|
||||
if (!tc.failure) {
|
||||
continue
|
||||
}
|
||||
for (const ex of tc.failure) {
|
||||
const src = exceptionThrowSource(ex, workDir, trackedFiles)
|
||||
if (src === null) {
|
||||
continue
|
||||
}
|
||||
annotations.push({
|
||||
annotation_level: 'failure',
|
||||
start_line: src.line,
|
||||
end_line: src.line,
|
||||
start_column: src.column,
|
||||
path: src.file,
|
||||
message: ex,
|
||||
title: 'Exception was thrown here'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
return annotations
|
||||
}
|
||||
|
||||
export function exceptionThrowSource(
|
||||
ex: string,
|
||||
workDir: string,
|
||||
trackedFiles: string[]
|
||||
): {file: string; line: number; column: number} | null {
|
||||
const lines = ex.split(/\r?\n/)
|
||||
const re = /\((.*):(\d+):(\d+)\)$/
|
||||
|
||||
for (const str of lines) {
|
||||
const match = str.match(re)
|
||||
if (match !== null) {
|
||||
const [_, fileStr, lineStr, colStr] = match
|
||||
const filePath = normalizeFilePath(fileStr)
|
||||
const file = filePath.startsWith(workDir) ? filePath.substr(workDir.length) : filePath
|
||||
if (trackedFiles.includes(file)) {
|
||||
const line = parseInt(lineStr)
|
||||
const column = parseInt(colStr)
|
||||
return {file, line, column}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,25 @@
|
|||
import {Endpoints} from '@octokit/types'
|
||||
|
||||
type OutputParameters = Endpoints['POST /repos/:owner/:repo/check-runs']['parameters']['output']
|
||||
export type OutputParameters = Endpoints['POST /repos/:owner/:repo/check-runs']['parameters']['output']
|
||||
export type Annotation = {
|
||||
path: string
|
||||
start_line: number
|
||||
end_line: number
|
||||
start_column?: number
|
||||
end_column?: number
|
||||
annotation_level: 'notice' | 'warning' | 'failure'
|
||||
message: string
|
||||
title?: string
|
||||
raw_details?: string
|
||||
}
|
||||
|
||||
export type ParseTestResult = (content: string) => Promise<TestResult>
|
||||
export type ParseTestResult = (content: string, options: ParseOptions) => Promise<TestResult>
|
||||
|
||||
export interface ParseOptions {
|
||||
annotations: boolean
|
||||
workDir: string
|
||||
trackedFiles: string[]
|
||||
}
|
||||
|
||||
export interface TestResult {
|
||||
success: boolean
|
||||
|
|
|
|||
21
src/utils/exec.ts
Normal file
21
src/utils/exec.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import {exec as execImpl, ExecOptions} from '@actions/exec'
|
||||
|
||||
// Wraps original exec() function
|
||||
// Returns exit code and whole stdout/stderr
|
||||
export default async function exec(commandLine: string, args?: string[], options?: ExecOptions): Promise<ExecResult> {
|
||||
options = options || {}
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
options.listeners = {
|
||||
stdout: (data: Buffer) => (stdout += data.toString()),
|
||||
stderr: (data: Buffer) => (stderr += data.toString())
|
||||
}
|
||||
const code = await execImpl(commandLine, args, options)
|
||||
return {code, stdout, stderr}
|
||||
}
|
||||
|
||||
export interface ExecResult {
|
||||
code: number
|
||||
stdout: string
|
||||
stderr: string
|
||||
}
|
||||
|
|
@ -11,3 +11,23 @@ export function getFileContent(path: string): string {
|
|||
|
||||
return fs.readFileSync(path, {encoding: 'utf8'})
|
||||
}
|
||||
|
||||
export function normalizeDirPath(path: string, trailingSeparator: boolean): string {
|
||||
if (!path) {
|
||||
return path
|
||||
}
|
||||
|
||||
path = normalizeFilePath(path)
|
||||
if (trailingSeparator && !path.endsWith('/')) {
|
||||
path += '/'
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
export function normalizeFilePath(path: string): string {
|
||||
if (!path) {
|
||||
return path
|
||||
}
|
||||
|
||||
return path.trim().replace(/\\/g, '/')
|
||||
}
|
||||
|
|
|
|||
22
src/utils/git.ts
Normal file
22
src/utils/git.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import * as core from '@actions/core'
|
||||
import exec from './exec'
|
||||
|
||||
export async function listFiles(): Promise<string[]> {
|
||||
core.startGroup('Listing all files tracked by git')
|
||||
let output = ''
|
||||
try {
|
||||
output = (await exec('git', ['ls-files', '-z'])).stdout
|
||||
} finally {
|
||||
fixStdOutNullTermination()
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
return output.split('\u0000').filter(s => s.length > 0)
|
||||
}
|
||||
|
||||
function fixStdOutNullTermination(): void {
|
||||
// Previous command uses NULL as delimiters and output is printed to stdout.
|
||||
// We have to make sure next thing written to stdout will start on new line.
|
||||
// Otherwise things like ::set-output wouldn't work.
|
||||
core.info('')
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue