Skip to content

Commit

Permalink
Input to truncate stack traces in annotations
Browse files Browse the repository at this point in the history
Updates parsing of JUnit tests to conditionally truncate stack traces based on input to the action. Uses the first line of the `message` when logging the path, line, and test duration, in the same manner as `annotateTestResult`. Maintains default behavior to truncate if input is not used.
  • Loading branch information
stackptr committed Jan 11, 2024
1 parent 0831a82 commit 529ff33
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ inputs:
annotations_limit:
description: 'Specify the limit for annotations. This will also interrupt parsing all test-suites if the limit is reached.'
required: false
truncate_stack_traces:
descrption: 'Truncate stack traces from test output to 2 lines in annotations'
required: false
default: true
runs:
using: 'node20'
main: 'dist/index.js'
34 changes: 24 additions & 10 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.

4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function run(): Promise<void> {
const transformers = readTransformers(core.getInput('transformers', {trimWhitespace: true}))
const followSymlink = core.getBooleanInput('follow_symlink')
const annotationsLimit = Number(core.getInput('annotations_limit') || -1)
const truncateStackTraces = core.getBooleanInput('truncate_stack_traces')

if (excludeSources.length === 0) {
excludeSources = ['/build/', '/__pycache__/']
Expand Down Expand Up @@ -73,7 +74,8 @@ export async function run(): Promise<void> {
retrieve('testFilesPrefix', testFilesPrefix, i, reportsCount),
transformers,
followSymlink,
annotationsLimit
annotationsLimit,
truncateStackTraces
)

mergedResult.totalCount += testResult.totalCount
Expand Down
24 changes: 16 additions & 8 deletions src/testParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export async function parseFile(
testFilesPrefix = '',
transformer: Transformer[] = [],
followSymlink = false,
annotationsLimit = -1
annotationsLimit = -1,
truncateStackTraces = true
): Promise<InternalTestResult> {
core.debug(`Parsing file ${file}`)

Expand Down Expand Up @@ -176,7 +177,8 @@ export async function parseFile(
testFilesPrefix,
transformer,
followSymlink,
annotationsLimit
annotationsLimit,
truncateStackTraces
)
}

Expand All @@ -196,7 +198,8 @@ async function parseSuite(
testFilesPrefix = '',
transformer: Transformer[],
followSymlink: boolean,
annotationsLimit: number
annotationsLimit: number,
truncateStackTraces: boolean
): Promise<InternalTestResult> {
let totalCount = 0
let skipped = 0
Expand Down Expand Up @@ -242,7 +245,8 @@ async function parseSuite(
testFilesPrefix,
transformer,
followSymlink,
annotationsLimit
annotationsLimit,
truncateStackTraces
)
totalCount += res.totalCount
skipped += res.skipped
Expand Down Expand Up @@ -318,10 +322,12 @@ async function parseSuite(
.toString()
.trim()

const stackTraceMessage = truncateStackTraces ? stackTrace.split('\n').slice(0, 2).join('\n') : stackTrace

const message: string = (
(failure && failure._attributes && failure._attributes.message) ||
(testcase.error && testcase.error._attributes && testcase.error._attributes.message) ||
stackTrace.split('\n').slice(0, 2).join('\n') ||
stackTraceMessage ||
testcase._attributes.name
).trim()

Expand Down Expand Up @@ -375,7 +381,7 @@ async function parseSuite(
// fish the time-taken out of the test case attributes, if present
const testTime = testcase._attributes.time === undefined ? '' : ` (${testcase._attributes.time}s)`

core.info(`${resolvedPath}:${pos.line} | ${message.replace(/\n/g, ' ')}${testTime}`)
core.info(`${resolvedPath}:${pos.line} | ${message.split('\n', 1)[0]}${testTime}`)

annotations.push({
path: resolvedPath,
Expand Down Expand Up @@ -420,7 +426,8 @@ export async function parseTestReports(
testFilesPrefix = '',
transformer: Transformer[] = [],
followSymlink = false,
annotationsLimit = -1
annotationsLimit = -1,
truncateStackTraces = true
): Promise<TestResult> {
core.debug(`Process test report for: ${reportPaths} (${checkName})`)
const globber = await glob.create(reportPaths, {followSymbolicLinks: followSymlink})
Expand All @@ -444,7 +451,8 @@ export async function parseTestReports(
testFilesPrefix,
transformer,
followSymlink,
annotationsLimit
annotationsLimit,
truncateStackTraces
)
if (c === 0) continue
totalCount += c
Expand Down

0 comments on commit 529ff33

Please sign in to comment.