Skip to content

Commit

Permalink
Merge pull request #45 from dorny/logs-and-error-handling
Browse files Browse the repository at this point in the history
Improve logging and error handling
  • Loading branch information
Michal Dorner authored and GitHub committed Jan 18, 2021
2 parents 967dbab + 42dbc1f commit 8fdf2ae
Show file tree
Hide file tree
Showing 10 changed files with 5,085 additions and 7,801 deletions.
1,118 changes: 614 additions & 504 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

3,911 changes: 1 addition & 3,910 deletions dist/sourcemap-register.js

Large diffs are not rendered by default.

7,788 changes: 4,420 additions & 3,368 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
"eslint": "^7.17.0",
"eslint-plugin-github": "^4.1.1",
"eslint-plugin-jest": "^24.1.3",
"jest": "^24.9.0",
"jest": "^26.6.3",
"jest-circus": "^26.6.3",
"jest-junit": "^12.0.0",
"js-yaml": "^4.0.0",
"prettier": "2.2.1",
"ts-jest": "^24.3.0",
"ts-jest": "^26.4.4",
"typescript": "^4.1.2"
},
"jest-junit": {
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ async function main(): Promise<void> {
const files = await getFiles(path)

if (files.length === 0) {
core.setFailed(`No file matches path ${path}`)
core.setFailed(`No file matches path '${path}'`)
return
}

core.info(`Using test report parser '${reporter}'`)
const result = await parser(files, opts)
const conclusion = result.success ? 'success' : 'failure'

core.info(`Creating check run '${name}' with conclusion '${conclusion}'`)
await octokit.checks.create({
head_sha: sha,
name,
Expand All @@ -68,7 +70,7 @@ async function main(): Promise<void> {

core.setOutput('conclusion', conclusion)
if (failOnError && !result.success) {
core.setFailed(`Failed test has been found and 'fail-on-error' option is set to ${failOnError}.`)
core.setFailed(`Failed test has been found and 'fail-on-error' option is set to ${failOnError}`)
}
}

Expand All @@ -83,14 +85,15 @@ function getParser(reporter: string): ParseTestResult {
case 'jest-junit':
return parseJestJunit
default:
throw new Error(`Input parameter 'reporter' is set to invalid value '${reporter}'`)
throw new Error(`Input variable 'reporter' is set to invalid value '${reporter}'`)
}
}

export async function getFiles(pattern: string): Promise<FileContent[]> {
const paths = await glob(pattern, {dot: true})
return Promise.all(
paths.map(async path => {
core.info(`Reading test report '${path}'`)
const content = await fs.promises.readFile(path, {encoding: 'utf8'})
return {path, content}
})
Expand Down
18 changes: 16 additions & 2 deletions src/parsers/dart-json/dart-json-parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'

import getReport from '../../report/get-report'
Expand Down Expand Up @@ -85,8 +86,21 @@ export async function parseDartJson(files: FileContent[], options: ParseOptions)
}

function getTestRun(path: string, content: string): TestRun {
const lines = content.split(/\n\r?/g).filter(line => line !== '')
const events = lines.map(str => JSON.parse(str)) as ReportEvent[]
core.info(`Parsing content of '${path}'`)
const lines = content.split(/\n\r?/g)
const events = lines
.map((str, i) => {
if (str.trim() === '') {
return null
}
try {
return JSON.parse(str)
} catch (e) {
const col = e.columnNumber !== undefined ? `:${e.columnNumber}` : ''
new Error(`Invalid JSON at ${path}:${i + 1}${col}\n\n${e}`)
}
})
.filter(evt => evt != null) as ReportEvent[]

let success = false
let totalTime = 0
Expand Down
18 changes: 12 additions & 6 deletions src/parsers/dotnet-trx/dotnet-trx-parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {ErrorInfo, Outcome, TestMethod, TrxReport} from './dotnet-trx-types'

import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
Expand Down Expand Up @@ -29,7 +30,7 @@ class Test {
readonly error?: ErrorInfo
) {}

get result(): TestExecutionResult {
get result(): TestExecutionResult | undefined {
switch (this.outcome) {
case 'Passed':
return 'success'
Expand All @@ -46,7 +47,7 @@ export async function parseDotnetTrx(files: FileContent[], options: ParseOptions
const testClasses: TestClass[] = []

for (const file of files) {
const trx = await getTrxReport(file.content)
const trx = await getTrxReport(file)
const tc = getTestClasses(trx)
const tr = getTestRunResult(file.path, trx, tc)
testRuns.push(tr)
Expand All @@ -66,10 +67,15 @@ export async function parseDotnetTrx(files: FileContent[], options: ParseOptions
}
}

async function getTrxReport(content: string): Promise<TrxReport> {
return (await parseStringPromise(content, {
attrValueProcessors: [parseAttribute]
})) as TrxReport
async function getTrxReport(file: FileContent): Promise<TrxReport> {
core.info(`Parsing content of '${file.path}'`)
try {
return (await parseStringPromise(file.content, {
attrValueProcessors: [parseAttribute]
})) as TrxReport
} catch (e) {
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
}
}

function getTestRunResult(path: string, trx: TrxReport, testClasses: TestClass[]): TestRunResult {
Expand Down
16 changes: 11 additions & 5 deletions src/parsers/jest-junit/jest-junit-parser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {Annotation, FileContent, ParseOptions, TestResult} from '../parser-types'
import {parseStringPromise} from 'xml2js'

Expand All @@ -20,7 +21,7 @@ export async function parseJestJunit(files: FileContent[], options: ParseOptions
const testRuns: TestRunResult[] = []

for (const file of files) {
const ju = await getJunitReport(file.content)
const ju = await getJunitReport(file)
const tr = getTestRunResult(file.path, ju)
junit.push(ju)
testRuns.push(tr)
Expand All @@ -39,10 +40,15 @@ export async function parseJestJunit(files: FileContent[], options: ParseOptions
}
}

async function getJunitReport(content: string): Promise<JunitReport> {
return (await parseStringPromise(content, {
attrValueProcessors: [parseAttribute]
})) as JunitReport
async function getJunitReport(file: FileContent): Promise<JunitReport> {
core.info(`Parsing content of '${file.path}'`)
try {
return (await parseStringPromise(file.content, {
attrValueProcessors: [parseAttribute]
})) as JunitReport
} catch (e) {
throw new Error(`Invalid XML at ${file.path}\n\n${e}`)
}
}

function getTestRunResult(path: string, junit: JunitReport): TestRunResult {
Expand Down
2 changes: 2 additions & 0 deletions src/report/get-report.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as core from '@actions/core'
import {TestExecutionResult, TestRunResult, TestSuiteResult} from './test-results'
import {Align, Icon, link, table} from '../utils/markdown-utils'
import {slug} from '../utils/slugger'
Expand All @@ -14,6 +15,7 @@ export default function getReport(results: TestRunResult[]): string {
}

function getRunSummary(tr: TestRunResult): 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.`
Expand Down

0 comments on commit 8fdf2ae

Please sign in to comment.