bashrspeccontinuous-integrationruby-on-rails-5rspec3

Catch RSpec "error occured outside of example"


I have a custom script that lets me retry failing examples if there are few failing examples.

It is based on scanning the list of failed specs to extract the number of missing specs. However, I just discovered that I have a big false positive when the specs did not even start because of "error occured outside of example"

Here s what my script looks like

bundle exec rspec [...] --failure-exit-code 0 # I use 0 exit code to check whether specs all passed in one go or not.
failures_count=$(grep -o "failed" rspec_persistence.txt | wc -l)
if [ "$failures_count" -eq "0" ]
then
  exit 0
fi
# Last part of the code retries failed specs

The thing is, RSpec error exit code is 1 whether this is due to failed specs or "errors occured outside of examples"


Solution

  • I realized an error outside of examples meant all examples would have a failure status of "unknown" and none with "passed"

    cat rspec_persistence.txt
    example_id                                                                               | status  | run_time     |
    ---------------------------------------------------------------------------------------- | ------- | ------------ |
      ./spec/services/foo_spec.rb[1:1:1]               | unknown |              |
    

    So the following code would work

    # Avoid passing if no specs were ran (most likely indicate a "errors occured outside of examples")
    passed_count=$(grep -o "passed" rspec_persistence.txt | wc -l)
    if [ "$passed_count" -eq "0" ]
    then
      exit 1
    fi