Skip to content

Commit

Permalink
Merge pull request #48 from ctrf-io/feat/test-list
Browse files Browse the repository at this point in the history
Feat/test list
  • Loading branch information
Ma11hewThomas authored Nov 5, 2024
2 parents f983d78 + e91bfc9 commit 619df16
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/detailed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
run: npx tsc
- name: Test detailed with title
run: node dist/index.js tests ctrf-reports/ctrf-report.json --title "Detailed With Title"
- name: Test default no title
run: node dist/index.js ctrf-reports/ctrf-report.json --annotate false
- name: Test list
run: node dist/index.js test-list ctrf-reports/ctrf-report.json --annotate false --title "List With Title"
- name: Upload test results
uses: actions/upload-artifact@v4
with:
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ For a test details table, add the `tests` command to your workflow yaml:
if: always()
```

### Generating Test list

For a simple list of tests, add the `test-list` command to your workflow yaml:

```yaml
- name: Publish CTRF test list
run: npx github-actions-ctrf test-list path-to-your-ctrf-report.json
if: always()
```

### Generating Failed Test Details Table

For a failed test details table, add the `failed` command to your workflow yaml:
Expand Down Expand Up @@ -522,6 +532,10 @@ npm run report

![Tests](images/tests.png)

### Test list

![Tests](images/test-list.png)

### Failed details

![Failed](images/failed.png)
Expand Down
Binary file added images/test-list.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-actions-ctrf",
"version": "0.0.48",
"version": "0.0.49",
"description": "View test results directly within your GitHub workflow summary and Pull Requests",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 34 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { generateSkippedTestsDetailsTable } from './views/skipped'
import { generateFailedFoldedTable } from './views/failed-folded'
import { generateTestSuiteFoldedTable } from './views/suite-folded'
import { generateSuiteListView } from './views/suite-list'
import { generateTestListView } from './views/test-list'

interface Arguments {
_: Array<string | number>
Expand Down Expand Up @@ -75,6 +76,16 @@ const argv: Arguments = yargs(hideBin(process.argv))
})
}
)
.command(
'test-list <file>',
'Generate test list from a CTRF report',
(yargs) => {
return yargs.positional('file', {
describe: 'Path to the CTRF file',
type: 'string',
})
}
)
.command(
'failed <file>',
'Generate fail test report from a CTRF report',
Expand Down Expand Up @@ -391,6 +402,29 @@ if ((commandUsed === 'all' || commandUsed === '') && argv.file) {
} catch (error) {
console.error('Failed to read file:', error)
}
} else if (argv._.includes('test-list') && argv.file) {
try {
let report = validateCtrfFile(argv.file)
report = stripAnsiFromErrors(report)
if (report !== null) {
if (argv.title) {
addHeading(title)
}
generateTestListView(report.results.tests, useSuiteName)
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('failed') && argv.file) {
try {
let report = validateCtrfFile(argv.file)
Expand Down
2 changes: 1 addition & 1 deletion src/views/flaky-rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,4 @@ ${totalRunsMessage}
[Github Test Reporter CTRF](https://github.com/ctrf-io/github-test-reporter-ctrf)
`
core.summary.addRaw(summaryTable)
}
}
17 changes: 8 additions & 9 deletions src/views/suite-list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as core from '@actions/core'
import { CtrfTest } from '../../types/ctrf'
import { getEmojiForStatus } from './common'
import { stripAnsi } from '../common'

export function generateSuiteListView(tests: CtrfTest[], useSuite: boolean): void {
try {
Expand All @@ -11,8 +13,8 @@ export function generateSuiteListView(tests: CtrfTest[], useSuite: boolean): voi

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

if (!testResultsByGroup[groupKey]) {
testResultsByGroup[groupKey] = { tests: [], statusEmoji: '✅' }
Expand All @@ -33,18 +35,15 @@ export function generateSuiteListView(tests: CtrfTest[], useSuite: boolean): voi
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 statusEmoji = getEmojiForStatus(test.status)

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

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()
let message = stripAnsi(test.message || "No failure message")
message = message.replace(/\n{2,}/g, '\n').trim()

const escapedMessage = escapeMarkdown(message)

Expand Down
48 changes: 48 additions & 0 deletions src/views/test-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as core from '@actions/core'
import { CtrfTest } from '../../types/ctrf'
import { getTestName, stripAnsi } from '../common'
import { getEmojiForStatus } from './common'

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

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

tests.forEach((test) => {
const statusEmoji = getEmojiForStatus(test.status)

const testName = escapeMarkdown(getTestName(test, useSuiteName))

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

if (test.status === 'failed') {
let message = stripAnsi(test.message || "No failure message")
message = 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;${line}`)
.join('\n')

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

markdown += `\n[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 list: ${error.message}`)
} else {
core.setFailed('An unknown error occurred')
}
}
}

0 comments on commit 619df16

Please sign in to comment.