From badb56fdb9fda2a3aa07f09180756b18c2b8780b Mon Sep 17 00:00:00 2001 From: James Netherton Date: Mon, 29 Apr 2024 13:30:54 +0100 Subject: [PATCH] WIP --- .github/actions/test-summary/action.yaml | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/.github/actions/test-summary/action.yaml b/.github/actions/test-summary/action.yaml index e0fea7cb9b2..a28553cce64 100644 --- a/.github/actions/test-summary/action.yaml +++ b/.github/actions/test-summary/action.yaml @@ -16,8 +16,65 @@ inputs: runs: using: "composite" steps: + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: 20 - name: Install script dependencies shell: bash run: | npm install junit2json + - name: Generate test summary + uses: actions/github-script@v7 + with: + script: | + const fs = require('fs'); + const junit = require('junit2json'); + const testReportGlobPattern = "${{ inputs.test-report-xml-base-dir }}/${{ inputs.test-report-xml-includes }}" + const summaryData = []; + summaryData.push([{data: 'Test Class', header: true}, {data: 'Test Name', header: true}, {data: 'Failure', header: true}, {data: 'Details', header: true}]); + // Determine the report heading. Use the base path of where to search for JUnit reports if not test-report-summary-heading is not provided + let testReportHeading = "${{ inputs.test-report-summary-heading }}" + if (!testReportHeading) { + testReportHeading = testReportHeading.replace(/-/g, ' ').replace(/(^\w{1})|(\s+\w{1})/g, wordFirstLetter => wordFirstLetter.toUpperCase()); + } + + if (!testReportHeading.endsWith(" Test Results")) { + testReportHeading += " Test Results"; + } + + // Iterate and parse surefire / failsafe reports and use the info to build the summary + const globber = await glob.create(testReportGlobPattern); + for await (const reportFile of globber.globGenerator()) { + const file = fs.readFileSync(reportFile); + const report = junit.parse(file).then((report) => { + if (report.errors > 0 || report.failures > 0) { + report.testcase.forEach((testCase) => { + if (testCase.failure !== undefined) { + const shortClassName = `${testCase.classname.substring(testCase.classname.lastIndexOf('.') + 1)}`; + const className = `
${shortClassName}\n${testCase.classname}
`; + const details = `
View\n${testCase.failure[0].inner}
`; + let testName = `<${testCase.name}`; + if (testName.length > 25) { + testName = `
View\n<${testCase.name}
`; + } + + let message = `testCase.failure[0].message`; + if (message.length > 50) { + message = `
View\n${message}
`; + } + summaryData.push([className, testName, message, details]); + } + }); + } + }); + } + + // Write the summary data if there were test failures + if (summaryData.length > 1) { + await core.summary + .addHeading(testReportHeading) + .addTable(summaryData) + .write(); + }