Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Handle errors in :suite hooks. #2316

Merged
merged 7 commits into from
Aug 29, 2016
Merged

Conversation

myronmarston
Copy link
Member

Previously, we just allowed the error to propagate to the user, which was a subpar experience for a few reasons:

  • The error would cause RSpec to crash, leading to a long stack trace containing lots of extraneous info the user did not see.
  • The error was not formatted nicely like other errors that happen while RSpec runs.
  • If the error happened in an after(:suite) hook, the test suite had finished running all specs but the summary of the results would not get printed since RSpec had crashed.

Now, we handle errors in :suite hooks and format the output the same way failures and errors are normally printed.

Notes/open questions:

  • The first 4 commits are refactoring commits that pave the way for this change. If desired, I can put those into a separate PR so it can be reviewed and merged separately, then this PR can be rebased on top. (Whoever reviews this, please let me know if you'd like me to do that).
  • There are a few other things I'm thinking of giving this kind of treatment:
    • Errors raised while loading spec files: instead of crashing RSpec, have it print a nice failure like we do here.
    • Errors raised in after(:context) hooks. Right now the output for these is not nice. If we have errors there notify via the new reporter method I've added in this PR, it'll solve Error in after(:all) hook doesn't fail rspec #2084 w/o the introduction of a config option as discussed there. It could be considered a breaking change, though, although it would be reasonable to consider it a bug that RSpec exits with 0 when all specs pass and an after(:context) hook fails.
  • I decided to hold off on these additional changes for now -- they can be in later PRs.
  • The changes here (and in any followup PRs for after(:context) and load-time errors) are a bit ambiguous in terms of SemVer. I could see an argument for treating them as bug fixes (e.g. RSpec didn't handle errors in these situations properly before, allowing them to crash RSpec) or as enhancements (we're improving error reporting in these situations) or as a breaking change (in the case of after(:context) -- it's a situation where the suite for some users currently passes but would be reported as failing if we make that change). For now I put this as a bug fix in the changelog.
  • This change was prompted by running into this for the new RSpec book I'm working on, and I need this in a release before the book is done (but not necessarily the changes for after(:context and load-time errors). It'd be nice to get this in 3.5.x so I can update the book to take advantage right away, instead of waiting for 3.6.

/cc @rspec/rspec

@myronmarston myronmarston force-pushed the myron/handle-errors-in-suite-hooks branch 4 times, most recently from 73af50a to 6c3b4c6 Compare August 25, 2016 02:31
Previously, we just allowed the error to propagate to
the user, which was a subpar experience for a few reasons:

- The error would cause RSpec to crash, leading to a long
  stack trace containing lots of extraneous info the user
  did not see.
- The error was not formatted nicely like other errors
  that happen while RSpec runs.
- If the error happened in an `after(:suite)` hook, the test
  suite had finished running all specs but the summary of the
  results would not get printed since RSpec had crashed.

Now, we handle errors in :suite hooks and format the output
the same way failures and errors are normally printed.
@myronmarston myronmarston force-pushed the myron/handle-errors-in-suite-hooks branch from 6c3b4c6 to 77f8b01 Compare August 25, 2016 03:04
@@ -152,6 +152,18 @@ def deprecation(hash)
end

# @private
# Provides a way to notify of an exception that is not tied to any
# particular exception (such as an exception encountered in a :suite hook).
Copy link
Member

Choose a reason for hiding this comment

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

First exception should read example I think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, fixed.

@xaviershay
Copy link
Member

LGTM, only comments were nits

- s/exception/example/ in a comment
- Do not run later `before(:suite)` hooks when an earlier
  one has failed. This aligns with `before(:example)` and
  `before(:context)` hooks.  However, `after(:suite)` hooks
  all get run even if one fails, which again aligns with
  `after(:example)` and `after(:suite)` hooks.
@myronmarston
Copy link
Member Author

LGTM, only comments were nits

Thanks for the review. Do you have thoughts with regard to the comments/questions I posed above about SemVer categorization?

@xaviershay
Copy link
Member

bugfix makes sense. Theory aside, I can't think of practical situations where anyone would be relying on this as a feature.

@xaviershay
Copy link
Member

I can't remember the specific issue, but I feel like we considered a similar situation in the past ("this is a bugfix that might break things relying on the bug") and ended up deciding to stick with bugfix.

@myronmarston
Copy link
Member Author

Cool. Good to merge?

@mrageh
Copy link
Contributor

mrageh commented Aug 28, 2016

The first bullet point in this commit message is a bit ambiguous to me.

The error would cause RSpec to crash, leading to a long
stack trace containing lots of extraneous info the user
did not see.

I think what you meant to say is:

The error would cause RSpec to crash, leading to a long
stack trace containing lots of extraneous info the user
did not need to see.

Otherwise LGTM 👍.

@JonRowe JonRowe merged commit d7ff6b3 into master Aug 29, 2016
@JonRowe
Copy link
Member

JonRowe commented Aug 29, 2016

Awesome work! :)

@JonRowe JonRowe deleted the myron/handle-errors-in-suite-hooks branch August 29, 2016 09:09
@myronmarston
Copy link
Member Author

Cherry picked to 3-5-maintenance, FWIW.

MatheusRich pushed a commit to MatheusRich/rspec-core that referenced this pull request Oct 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants