Permalink
Browse files

Gracefully capturing errors from setup blocks

If an error occurs in a setup block, execution of a context is
immediately stopped. No other setup blocks are run; no hookups; no
assertions; no teardowns.

Sub-contexts that have a parent setup which is erroring will report the
same error (for now I guess).
  • Loading branch information...
gus committed May 3, 2012
1 parent 75b5ad8 commit 230e45f14b36674bf56b95ea99c96385819e801a
View
@@ -1,5 +1,6 @@
# @markup markdown
+* Errors that occur during a setup, hookup, or teardown are handled gracefully
* close #31 - RR seems to carry over some latent varification state between situations
# 0.12.5
View
@@ -98,7 +98,11 @@ def run(reporter)
# @param [Riot::Reporter] reporter the reporter to report results to
# @param [Riot::Situation] situation the situation to use for executing the context.
def local_run(reporter, situation)
- runnables.each { |runnable| reporter.report(runnable.to_s, runnable.run(situation)) }
+ runnables.each do |runnable|
+ code, response = *runnable.run(situation)
+ reporter.report(runnable.to_s, [code, response])
+ break if code == :setup_error
+ end
end
# Prints the full description from the context tree, grabbing the description from the parent and
View
@@ -69,7 +69,7 @@ def report(description, response)
@failures += 1
message, line, file = *response[1..-1]
fail(description, message, line, file)
- when :error then
+ when :error, :setup_error then
@errors += 1
error(description, result)
end
@@ -30,7 +30,6 @@ def error(description, e)
def simple_error(e)
format = []
filter_backtrace(e.backtrace) { |line| format << "at #{line}" }
-
format.join("\n")
end
end
View
@@ -48,6 +48,8 @@ def initialize(&definition)
def run(situation)
situation.setup(&definition)
[:setup]
+ rescue Exception => e
+ [:setup_error, e]
end
end # Setup
@@ -39,6 +39,11 @@ def results(time); end
topic.errors
end.equals(1)
+ asserts("error count increase when :setup_error sent to #report") do
+ topic.report("", [:setup_error, ""])
+ topic.errors
+ end.equals(2)
+
asserts("description sent to #error") do
topic.report("break it down", [:error, "error time"])
end.equals("errored(break it down, error time)")
@@ -20,20 +20,18 @@
context 'reporting on an empty context' do
setup do
- context = Riot::Context.new('empty context') do
+ context = Riot::Context.new('empty context') {
context("a nested empty context") {}
- end
- context.run(topic)
+ }.run(topic)
end
should("not output context name") { @out.string }.empty
end
context "reporting on a non-empty context" do
setup do
- context = Riot::Context.new('supercontext') do
+ Riot::Context.new('supercontext') {
asserts("truth") { true }
- end
- context.run(topic)
+ }.run(topic)
end
should('output context name') { @out.string }.matches(/supercontext/)
@@ -0,0 +1,47 @@
+require 'teststrap'
+
+context "Executing setup with an error" do
+ setup do
+ Riot::Setup.new { raise "Error in setup" }.run(Riot::Situation.new)
+ end
+
+ asserts("result") { topic[0] }.equals(:setup_error)
+ asserts("result object") { topic[1] }.kind_of(Exception)
+ asserts("error message") { topic[1].message }.equals("Error in setup")
+
+end # Executing setup with an error
+
+context "Executing a context" do
+ context "that errors during setup" do
+ setup do
+ Riot::Context.new("A") {
+ setup { raise "Whoopsie!" } # error
+ asserts("foo") { true } # success
+ asserts("bar") { false } # failure
+ }.run(Riot::SilentReporter.new)
+ end
+
+ asserts(:errors).equals(1)
+ asserts(:failures).equals(0)
+ asserts(:passes).equals(0)
+ end # that errors during setup
+
+ context "that errors in a parent setup" do
+ setup do
+ Riot::Context.new("A") {
+ setup { raise "Whoopsie!" } # error
+
+ context "B" do
+ asserts("foo") { true } # success
+ asserts("bar") { false } # failure
+ end
+ }.run(Riot::SilentReporter.new)
+ end
+
+ asserts(:errors).equals(2) # Same setup fails twice
+ asserts(:failures).equals(0)
+ asserts(:passes).equals(0)
+ end # that errors in a parent setup
+
+end # Executing a cotext
+
View
@@ -4,7 +4,6 @@
require 'riot'
Riot.verbose
-
Riot.pretty_dots if ENV["TM_MODE"]
module Riot

0 comments on commit 230e45f

Please sign in to comment.