Skip to content

Commit 861db83

Browse files
Merge pull request #222 from kevbite/issue221
Add exitOnNoFiles argument to fail action when no test files are found
2 parents 195d91e + d581f3a commit 861db83

File tree

10 files changed

+48
-9
lines changed

10 files changed

+48
-9
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
exit-on-empty: false # Exit the workflow with a failure status if no tests are found. Default is false
156157
use-suite-name: false # Prefix test names with the suite name for better grouping. 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
exitOnEmpty: false,
506507
useSuiteName: false,

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+
exit-on-no-files:
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: 14 additions & 4 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
@@ -42,6 +42,7 @@ export interface Arguments {
4242
alwaysLatestComment?: boolean
4343
commentTag?: string
4444
results?: number
45+
exitOnNoFiles?: boolean
4546
exitOnFail?: boolean
4647
exitOnEmpty?: boolean
4748
fetchPreviousResults?: boolean
@@ -322,6 +323,11 @@ async function main(): Promise<void> {
322323
description: 'Use suite name in the test name',
323324
default: false
324325
})
326+
.options('exit-on-no-files', {
327+
type: 'boolean',
328+
description: 'Fail action when if no test files are found',
329+
default: false
330+
})
325331
.options('exit-on-fail', {
326332
type: 'boolean',
327333
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
exitOnEmpty: args.exitOnEmpty || false,
4950
useSuiteName: args.useSuiteName || false,
@@ -131,6 +132,7 @@ export function getInputs(): Inputs {
131132
annotate: core.getInput('annotate').toLowerCase() === 'true',
132133
title: core.getInput('title') || '',
133134
onFailOnly: core.getInput('on-fail-only').toLowerCase() === 'true',
135+
exitOnNoFiles: core.getInput('exit-on-no-files').toLowerCase() === 'true',
134136
exitOnFail: core.getInput('exit-on-fail').toLowerCase() === 'true',
135137
exitOnEmpty: core.getInput('exit-on-empty').toLowerCase() === 'true',
136138
useSuiteName: core.getInput('use-suite-name').toLowerCase() === 'true',

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
exitOnEmpty: boolean
3738
exitOnFail: boolean
3839
useSuiteName: boolean

src/utils/file-system.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,23 @@ 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+
process.exit(core.ExitCode.Failure)
49+
}
50+
4251
core.warning(`CTRF report not found at: ${pattern}. Exiting action.`)
43-
process.exit(0)
52+
process.exit(core.ExitCode.Success)
4453
}
4554

4655
const report: Report =
@@ -49,8 +58,12 @@ export function readCtrfReports(pattern: string): Report {
4958
return report
5059
} catch (error: unknown) {
5160
const errorMessage = error instanceof Error ? error.message : String(error)
61+
if (exitOnNoFiles) {
62+
core.setFailed(`No CTRF reports found at: ${pattern}. Exiting action.`)
63+
process.exit(core.ExitCode.Failure)
64+
}
5265
core.warning(`${errorMessage}. Exiting action.`)
53-
process.exit(0)
66+
process.exit(core.ExitCode.Success)
5467
}
5568
}
5669

0 commit comments

Comments
 (0)