Skip to content

Commit 3a27361

Browse files
committed
Adding exitOnNoFiles flag to fail (exit with a non-zero code) if no test files are discovered.
1 parent 646f98c commit 3a27361

File tree

10 files changed

+40
-5
lines changed

10 files changed

+40
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ For more advanced usage, there are several inputs available.
151151
title: '' # Set a custom title to display on the report.
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
154+
exitOnNoFiles: false # Exit the workflow with a failure status if no test files are found. Default is false
154155
exit-on-fail: false # Exit the workflow with a failure status if any tests fail. 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

__tests__/ctrf/report-preparation.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ function createSingleReportInputs(): Inputs {
501501
annotate: false,
502502
title: '',
503503
onFailOnly: false,
504+
exitOnNoFiles: false,
504505
exitOnFail: false,
505506
useSuiteName: false,
506507
previousResultsMax: 0,

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ inputs:
145145
description: 'Add a pull request comment only if tests fail.'
146146
required: false
147147
default: false
148+
exitOnNoFiles:
149+
description:
150+
'Exit the workflow with a failure status if no test files are found.'
151+
required: false
152+
default: false
148153
exit-on-fail:
149154
description: 'Exit the workflow with a failure status if any tests fail.'
150155
required: false

dist/index.js

Lines changed: 10 additions & 2 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/cli.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface Arguments {
3737
alwaysLatestComment?: boolean
3838
commentTag?: string
3939
results?: number
40+
exitOnNoFiles?: boolean
4041
exitOnFail?: boolean
4142
fetchPreviousResults?: boolean
4243
reportOrder?: string
@@ -316,6 +317,11 @@ async function main(): Promise<void> {
316317
description: 'Use suite name in the test name',
317318
default: false
318319
})
320+
.options('exit-on-no-files', {
321+
type: 'boolean',
322+
description: 'Fail action when if no test files are found',
323+
default: false
324+
})
319325
.options('exit-on-fail', {
320326
type: 'boolean',
321327
description: 'Fail action when if tests fail',

src/core/inputs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export function getCliInputs(args: Arguments): Inputs {
4444
annotate: args.annotate !== false,
4545
title: args.title || '',
4646
onFailOnly: args.onFailOnly || false,
47+
exitOnNoFiles: args.exitOnNoFiles || false,
4748
exitOnFail: args.exitOnFail || false,
4849
useSuiteName: args.useSuiteName || false,
4950
previousResultsMax: args.rows || 10,
@@ -130,6 +131,7 @@ export function getInputs(): Inputs {
130131
annotate: core.getInput('annotate').toLowerCase() === 'true',
131132
title: core.getInput('title') || '',
132133
onFailOnly: core.getInput('on-fail-only').toLowerCase() === 'true',
134+
exitOnNoFiles: core.getInput('exit-on-no-files').toLowerCase() === 'true',
133135
exitOnFail: core.getInput('exit-on-fail').toLowerCase() === 'true',
134136
useSuiteName: core.getInput('use-suite-name').toLowerCase() === 'true',
135137
previousResultsMax: parseInt(

src/ctrf/report-preparation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function prepareReport(
4545
throw new Error(`JUnit report not found at: ${inputs.ctrfPath}`)
4646
}
4747
} else {
48-
report = readCtrfReports(inputs.ctrfPath)
48+
report = readCtrfReports(inputs.ctrfPath, inputs.exitOnNoFiles)
4949
}
5050

5151
report = stripAnsiFromErrors(report)

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+
exitOnNoFiles: boolean
3637
exitOnFail: boolean
3738
useSuiteName: boolean
3839
previousResultsMax: number

src/utils/file-system.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,20 @@ export function readTemplate(filePath: string): string {
3333
* @returns A `Report` object containing the parsed report data.
3434
* @throws An error if the file does not exist, is not valid JSON, or does not contain CTRF results.
3535
*/
36-
export function readCtrfReports(pattern: string): Report {
36+
export function readCtrfReports(
37+
pattern: string,
38+
exitOnNoFiles: boolean
39+
): Report {
3740
core.info(`Reading CTRF reports from ${pattern}`)
41+
3842
try {
3943
const reports: Report[] = readReportsFromGlobPattern(pattern)
4044

4145
if (reports.length === 0) {
46+
if (exitOnNoFiles) {
47+
core.setFailed(`No CTRF reports found at: ${pattern}. Exiting action.`)
48+
}
49+
4250
core.warning(`CTRF report not found at: ${pattern}. Exiting action.`)
4351
process.exit(0)
4452
}
@@ -49,6 +57,9 @@ export function readCtrfReports(pattern: string): Report {
4957
return report
5058
} catch (error: unknown) {
5159
const errorMessage = error instanceof Error ? error.message : String(error)
60+
if (exitOnNoFiles) {
61+
core.setFailed(`No CTRF reports found at: ${pattern}. Exiting action.`)
62+
}
5263
core.warning(`${errorMessage}. Exiting action.`)
5364
process.exit(0)
5465
}

0 commit comments

Comments
 (0)