Skip to content

Return error when rspec exits with non-zero but no failure#436

Draft
nprizal wants to merge 2 commits intomainfrom
te-5202-return-error-when-rspec-exits-with-non-zero-but-no-failure
Draft

Return error when rspec exits with non-zero but no failure#436
nprizal wants to merge 2 commits intomainfrom
te-5202-return-error-when-rspec-exits-with-non-zero-but-no-failure

Conversation

@nprizal
Copy link
Contributor

@nprizal nprizal commented Feb 5, 2026

Description

There’s a scenario where rspec exits with a non-zero code but no test failures or errors are reported. This can occur if there’s an explicit exit call in the source code that isn’t caught within the specs (as shown in the example below). Since no failures are reported, bktec treats it as a “pass”. In this case we should return an error from bktec.

# specs/my_spec.rb
RSpec.describe "test"
  def exits
    exit 6
  end

  it "exits" do
    exits
  end
end
$ rspec specs/my_spec.rb

Finished in 1.26 seconds (files took 6.3 seconds to load)
1 example, 0 failures

$ echo $?
6

@nprizal nprizal requested a review from a team as a code owner February 5, 2026 01:01
@nprizal nprizal force-pushed the te-5202-return-error-when-rspec-exits-with-non-zero-but-no-failure branch from c066961 to 9fd6566 Compare February 5, 2026 01:06
@nprizal nprizal marked this pull request as draft February 5, 2026 01:09
@nprizal nprizal force-pushed the te-5202-return-error-when-rspec-exits-with-non-zero-but-no-failure branch from 9fd6566 to d572914 Compare February 5, 2026 01:10
}

if exitError := new(exec.ExitError); errors.As(err, &exitError) {
if report.Summary.FailureCount == 0 && report.Summary.ErrorsOutsideOfExamplesCount == 0 {
Copy link
Contributor

@malclocke malclocke Feb 5, 2026

Choose a reason for hiding this comment

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

Is there a way we can invert the logic here? We have if ExitError and some inclusion condition and I think this would be a lot better as if ExitError and not some exclusion condition. I.e. change this to "any failure status from the runner is considered an error except for when we explicitly allow it".

This change is good but it leaves us in a similar situation to where we are now, where any unexpected failure state is a pass by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point. But it's more complicated than that. We don't return error from this function when RSpec succesfully run and finished because we want to print the result at the end of the run. Instead, we build a RunResult from the test runner json output, that decided whether bktec will exit with 0 or 1.

I can revert the logic, but we can't simply make "any unexpected failure state is a failed by default", unless we are okay with skipping report printing by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the logic, let me know what you think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants