Skip to content

Commit

Permalink
feat: add suite list
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma11hewThomas committed Nov 5, 2024
1 parent 819820c commit 36d1a12
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { generateFlakyTestsDetailsTable } from './views/flaky'
import { generateSkippedTestsDetailsTable } from './views/skipped'
import { generateFailedFoldedTable } from './views/failed-folded'
import { generateTestSuiteFoldedTable } from './views/suite-folded'
import { generateSuiteListView } from './views/suite-list'

interface Arguments {
_: Array<string | number>
Expand Down Expand Up @@ -168,6 +169,20 @@ const argv: Arguments = yargs(hideBin(process.argv))
})
}
)
.command(
'suite-list <file>',
'Generate a test summary grouped by suite',
(yargs) => {
return yargs.positional('file', {
describe: 'Path to the CTRF file',
type: 'string',
})
.option('useSuite', {
type: 'boolean',
description: 'Use suite property, default is filePath',
})
}
)
.command(
'custom <file> <summary>',
'Generate a custom summary from a CTRF report',
Expand Down Expand Up @@ -562,7 +577,31 @@ if ((commandUsed === 'all' || commandUsed === '') && argv.file) {
} catch (error) {
console.error('Failed to read file:', error)
}
} else if (argv._.includes('custom') && argv.file) {
} else if (argv._.includes('suite-list') && argv.file) {
try {
let report = validateCtrfFile(argv.file)
report = stripAnsiFromErrors(report)
if (report !== null) {
if (argv.title) {
addHeading(title)
}
generateSuiteListView(report.results.tests, useSuite)
write()
if (argv.prComment) {
postPullRequestComment(report, apiUrl, baseUrl, onFailOnly, title, useSuiteName, prCommentMessage)
}
if (pullRequest) {
postPullRequestComment(report, apiUrl, baseUrl, onFailOnly, title, useSuiteName, core.summary.stringify())
}
if (exitOnFail) {
exitActionOnFail(report)
}
}
} catch (error) {
console.error('Failed to read file:', error)
}
}
else if (argv._.includes('custom') && argv.file) {
try {
if (argv.summary) {
if (path.extname(argv.summary) === '.hbs') {
Expand Down
75 changes: 75 additions & 0 deletions src/views/suite-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as core from '@actions/core'
import { CtrfTest } from '../../types/ctrf'

export function generateSuiteListView(tests: CtrfTest[], useSuite: boolean): void {
try {
let markdown = `\n`

const workspacePath = process.env.GITHUB_WORKSPACE || ''

const testResultsByGroup: Record<string, { tests: CtrfTest[], statusEmoji: string }> = {}

tests.forEach((test) => {
const groupKey = useSuite
? test.suite || 'Unknown Suite'
: (test.filePath || 'Unknown File').replace(workspacePath, '').replace(/^\//, '')

if (!testResultsByGroup[groupKey]) {
testResultsByGroup[groupKey] = { tests: [], statusEmoji: '✅' }
}

testResultsByGroup[groupKey].tests.push(test)

if (test.status === 'failed') {
testResultsByGroup[groupKey].statusEmoji = '❌'
}
})

function escapeMarkdown(text: string): string {
return text.replace(/([\\*_{}[\]()#+\-.!])/g, '\\$1')
}

Object.entries(testResultsByGroup).forEach(([groupKey, groupData]) => {
markdown += `## ${groupData.statusEmoji} ${escapeMarkdown(groupKey)}\n\n`

groupData.tests.forEach((test) => {
const statusEmoji =
test.status === 'passed' ? '✅' :
test.status === 'failed' ? '❌' :
test.status === 'skipped' ? '⏭️' :
test.status === 'pending' ? '⏳' : '❓'

const testName = escapeMarkdown(test.name || 'Unnamed Test')

markdown += `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;**${statusEmoji} ${testName}**\n`

if (test.status === 'failed' && test.message) {
const message = test.message.replace(/\n{2,}/g, '\n').trim()

const escapedMessage = escapeMarkdown(message)

const indentedMessage = escapedMessage
.split('\n')
.filter(line => line.trim() !== '')
.map(line => `&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${line}`)
.join('\n')

markdown += `${indentedMessage}\n`
}
})

markdown += `\n`
})

markdown += `[Github Test Reporter](https://github.com/ctrf-io/github-test-reporter)`

core.summary.addRaw(markdown)

} catch (error) {
if (error instanceof Error) {
core.setFailed(`Failed to display test suite list: ${error.message}`)
} else {
core.setFailed('An unknown error occurred')
}
}
}

0 comments on commit 36d1a12

Please sign in to comment.