Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --failure-exit-code option #949

Merged
merged 3 commits into from Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Breaking Changes

### Added
- Added `--failure-exit-code [INT]` flag to specify a custom exit code when tests fail. This option allows users to define a specific exit code that the test suite should return if any tests fail.

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions Readme.md
Expand Up @@ -252,6 +252,8 @@ Options are:
-i, --isolate Do not run any other tests in the group used by --single(-s)
--isolate-n [PROCESSES] Use 'isolate' singles with number of processes, default: 1.
--highest-exit-status Exit with the highest exit status provided by test run(s)
If failure-exit-code is specified, that value takes priority.
--failure-exit-code [INT] Specify the exit code to use when tests fail.
--specify-groups [SPECS] Use 'specify-groups' if you want to specify multiple specs running in multiple
processes in a specific formation. Commas indicate specs in the same process,
pipes indicate specs in a new process. Cannot use with --single, --isolate, or
Expand Down
21 changes: 16 additions & 5 deletions lib/parallel_tests/cli.rb
Expand Up @@ -96,8 +96,9 @@ def run_tests_in_parallel(num_processes, options)
if any_test_failed?(test_results)
warn final_fail_message

# return the highest exit status to allow sub-processes to send things other than 1
exit_status = if options[:highest_exit_status]
exit_status = if options[:failure_exit_code]
options[:failure_exit_code]
elsif options[:highest_exit_status]
test_results.map { |data| data.fetch(:exit_status) }.max
else
1
Expand Down Expand Up @@ -226,9 +227,19 @@ def parse_options!(argv)
"Use 'isolate' singles with number of processes, default: 1."
) { |n| options[:isolate_count] = n }

opts.on("--highest-exit-status", "Exit with the highest exit status provided by test run(s)") do
options[:highest_exit_status] = true
end
opts.on(
"--highest-exit-status",
<<~TEXT.rstrip.split("\n").join("\n#{newline_padding}")
Exit with the highest exit status provided by test run(s)
If failure-exit-code is specified, that value takes priority.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just fail if both are given

TEXT
) { options[:highest_exit_status] = true }

opts.on(
"--failure-exit-code [INT]",
Integer,
"Specify the exit code to use when tests fail"
) { |code| options[:failure_exit_code] = code }

opts.on(
"--specify-groups [SPECS]",
Expand Down
4 changes: 4 additions & 0 deletions spec/parallel_tests/cli_spec.rb
Expand Up @@ -61,6 +61,10 @@ def call(*args)
)
end

it "parses --failure-exit-code" do
expect(call(["test", "--failure-exit-code", "42"])).to eq(defaults.merge(failure_exit_code: 42))
end

it "parses --quiet" do
expect(call(["test", "--quiet"])).to eq(defaults.merge(quiet: true))
end
Expand Down