Skip to content

Commit fb366c4

Browse files
committed
Allow tests to fail based on a exitOnEmpty flag being set.
1 parent 646f98c commit fb366c4

File tree

9 files changed

+63
-3
lines changed

9 files changed

+63
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ For more advanced usage, there are several inputs available.
152152
annotate: true # Add failed test annotations. Default is true
153153
on-fail-only: false # Add a pull request comment only if tests fail. Default is false
154154
exit-on-fail: false # Exit the workflow with a failure status if any tests fail. Default is false
155+
exit-on-empty: false # Exit the workflow with a failure status if no tests are found. Default is false
155156
use-suite-name: false # Prefix test names with the suite name for better grouping. Default is false
156157
collapse-large-reports: false # Collapse large reports (test-table and test-list) for better readability. Default is false
157158
update-comment: false # Update existing Pull Request comment. Default is false

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ inputs:
149149
description: 'Exit the workflow with a failure status if any tests fail.'
150150
required: false
151151
default: false
152+
exit-on-empty:
153+
description:
154+
'Exit the workflow with a failure status if no tests are found.'
155+
required: false
156+
default: false
152157
use-suite-name:
153158
description: 'Prefix test names with the suite name for better grouping.'
154159
required: false

dist/index.js

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/action-handler.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
process.env.RUN_MODE = 'action'
22

3-
import { exitActionOnFail, getAllGitHubContext, handleError } from '../github'
3+
import {
4+
exitActionOnFail,
5+
exitActionOnEmpty,
6+
getAllGitHubContext,
7+
handleError
8+
} from '../github'
49
import { getInputs } from './inputs'
510
import { prepareReport } from '../ctrf'
611
import { handleViewsAndComments, handleAnnotations } from '../github/handler'
@@ -17,6 +22,9 @@ export async function runAction(): Promise<void> {
1722
await handleViewsAndComments(inputs, report)
1823
handleAnnotations(inputs, report)
1924

25+
if (inputs.exitOnEmpty) {
26+
exitActionOnEmpty(report)
27+
}
2028
if (inputs.exitOnFail) {
2129
exitActionOnFail(report)
2230
}

src/core/cli.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ process.env.RUN_MODE = 'cli'
33

44
import yargs from 'yargs/yargs'
55
import { hideBin } from 'yargs/helpers'
6-
import { exitActionOnFail, getAllGitHubContext, handleError } from '../github'
6+
import {
7+
exitActionOnFail,
8+
exitActionOnEmpty,
9+
getAllGitHubContext,
10+
handleError
11+
} from '../github'
712
import { prepareReport } from '../ctrf'
813
import { handleViewsAndComments, handleAnnotations } from '../github/handler'
914
import { getCliInputs } from '../core/inputs'
@@ -38,6 +43,7 @@ export interface Arguments {
3843
commentTag?: string
3944
results?: number
4045
exitOnFail?: boolean
46+
exitOnEmpty?: boolean
4147
fetchPreviousResults?: boolean
4248
reportOrder?: string
4349
maxWorkflowRunsToCheck?: number
@@ -321,6 +327,11 @@ async function main(): Promise<void> {
321327
description: 'Fail action when if tests fail',
322328
default: false
323329
})
330+
.options('exit-on-empty', {
331+
type: 'boolean',
332+
description: 'Fail action when if no tests are found',
333+
default: false
334+
})
324335
.options('update-comment', {
325336
type: 'boolean',
326337
description: 'Updates existing Pull Request comment',
@@ -379,6 +390,9 @@ async function main(): Promise<void> {
379390

380391
await processPrComment(argv, report, inputs)
381392

393+
if (inputs.exitOnEmpty) {
394+
exitActionOnEmpty(report)
395+
}
382396
if (inputs.exitOnFail) {
383397
exitActionOnFail(report)
384398
}

src/core/inputs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export function getCliInputs(args: Arguments): Inputs {
4545
title: args.title || '',
4646
onFailOnly: args.onFailOnly || false,
4747
exitOnFail: args.exitOnFail || false,
48+
exitOnEmpty: args.exitOnEmpty || false,
4849
useSuiteName: args.useSuiteName || false,
4950
previousResultsMax: args.rows || 10,
5051
metricsReportsMax: args.results || 100,
@@ -131,6 +132,7 @@ export function getInputs(): Inputs {
131132
title: core.getInput('title') || '',
132133
onFailOnly: core.getInput('on-fail-only').toLowerCase() === 'true',
133134
exitOnFail: core.getInput('exit-on-fail').toLowerCase() === 'true',
135+
exitOnEmpty: core.getInput('exit-on-empty').toLowerCase() === 'true',
134136
useSuiteName: core.getInput('use-suite-name').toLowerCase() === 'true',
135137
previousResultsMax: parseInt(
136138
core.getInput('previous-results-max') || '10',

src/github/core.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,19 @@ export function exitActionOnFail(report: Report): void {
396396
}
397397
}
398398

399+
/**
400+
* Exits the GitHub Action with a failure status if the CTRF report does not contains any tests.
401+
*
402+
* @param report - The CTRF report containing the summary of test results.
403+
*/
404+
export function exitActionOnEmpty(report: Report): void {
405+
if (report.results.summary.tests === 0) {
406+
core.setFailed(
407+
`Github Test Reporter: ${report.results.summary.tests} tests found in the report, exiting as per configuration`
408+
)
409+
}
410+
}
411+
399412
/**
400413
* Handles errors that occur during the action, setting the GitHub Action status to failed.
401414
*

src/types/reporter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export interface Inputs {
3333
annotate: boolean
3434
title: string
3535
onFailOnly: boolean
36+
exitOnEmpty: boolean
3637
exitOnFail: boolean
3738
useSuiteName: boolean
3839
previousResultsMax: number

0 commit comments

Comments
 (0)