Skip to content

Commit

Permalink
Merge pull request #60 from dorny/reduce-report-size
Browse files Browse the repository at this point in the history
Set listTests and listSuites to lower detail if report is too big
  • Loading branch information
dorny authored Feb 20, 2021
2 parents 603e845 + 7faeced commit 85275e4
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 38 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Changelog

## v1.2.0
- [Set `listTests` and `listSuites` to lower detail if report is too big](https://github.com/dorny/test-reporter/pull/60)

## v1.1.0
- [Support public repo PR workflow](https://github.com/dorny/test-reporter/pull/56)

# v1.0.0
## v1.0.0
Supported languages / frameworks:
- .NET / xUnit / NUnit / MSTest
- Dart / test
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dotnet-trx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('dotnet-trx tests', () => {
const result = await parser.parse(filePath, fileContent)
expect(result).toMatchSnapshot()

const report = getReport([result], {listTests: 'failed'})
const report = getReport([result])
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
fs.writeFileSync(outputPath, report)
})
Expand Down
2 changes: 1 addition & 1 deletion __tests__/jest-junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('jest-junit tests', () => {
const result = await parser.parse(filePath, fileContent)
expect(result).toMatchSnapshot()

const report = getReport([result], {listTests: 'failed'})
const report = getReport([result])
fs.mkdirSync(path.dirname(outputPath), {recursive: true})
fs.writeFileSync(outputPath, report)
})
Expand Down
61 changes: 46 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Large diffs are not rendered by default.

73 changes: 54 additions & 19 deletions src/report/get-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,58 @@ import {Align, formatTime, Icon, link, table} from '../utils/markdown-utils'
import {slug} from '../utils/slugger'

export interface ReportOptions {
listSuites?: 'all' | 'failed'
listTests?: 'all' | 'failed' | 'none'
listSuites: 'all' | 'failed'
listTests: 'all' | 'failed' | 'none'
}

export function getReport(results: TestRunResult[], options: ReportOptions = {}): string {
const defaultOptions: ReportOptions = {
listSuites: 'all',
listTests: 'all'
}

export function getReport(results: TestRunResult[], options: ReportOptions = defaultOptions): string {
core.info('Generating check run summary')

const maxReportLength = 65535
const sections: string[] = []

applySort(results)

const badge = getReportBadge(results)
sections.push(badge)

const runs = getTestRunsReport(results, options)
sections.push(...runs)
const opts = {...options}
let report = renderReport(results, opts)
if (getByteLength(report) <= maxReportLength) {
return report
}

const report = sections.join('\n')
if (report.length > maxReportLength) {
let msg = `**Check Run summary limit of ${maxReportLength} chars was exceed**`
if (options.listTests !== 'all') {
msg += '\n- Consider setting `list-tests` option to `only-failed` or `none`'
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) {
return report
}
if (options.listSuites !== 'all') {
msg += '\n- Consider setting `list-suites` option to `only-failed`'
}

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
}
}

return `${badge}\n${msg}`
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
}
}

return report
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}`
}

function applySort(results: TestRunResult[]): void {
Expand All @@ -45,6 +65,21 @@ function applySort(results: TestRunResult[]): void {
}
}

function getByteLength(text: string): number {
return Buffer.byteLength(text, 'utf8')
}

function renderReport(results: TestRunResult[], options: ReportOptions): string {
const sections: string[] = []
const badge = getReportBadge(results)
sections.push(badge)

const runs = getTestRunsReport(results, options)
sections.push(...runs)

return sections.join('\n')
}

function getReportBadge(results: TestRunResult[]): string {
const passed = results.reduce((sum, tr) => sum + tr.passed, 0)
const skipped = results.reduce((sum, tr) => sum + tr.skipped, 0)
Expand Down

0 comments on commit 85275e4

Please sign in to comment.