Permalink
Browse files

Abort run on CTRL-C

- finishes current example
- runs after alls
- reports examples run up to and including the one that was running when
  the interrupt was received
- Closes #32.
  • Loading branch information...
1 parent bc22dd5 commit e5210135252c32be2bf629cf17cc118ae6457b20 @dchelimsky dchelimsky committed Sep 10, 2010
View
@@ -49,6 +49,10 @@ def self.world
end
+ class << self
+ attr_accessor :wants_to_quit
+ end
+
def self.world
@world ||= RSpec::Core::World.new
end
@@ -37,6 +37,7 @@ def pending?
end
def run(example_group_instance, reporter)
+ return if RSpec.wants_to_quit
@example_group_instance = example_group_instance
@example_group_instance.example = self
@@ -202,6 +202,7 @@ def self.around_hooks
end
def self.run(reporter)
+ return if RSpec.wants_to_quit
@reporter = reporter
example_group_instance = new
reporter.example_group_started(self)
@@ -216,6 +217,10 @@ def self.run(reporter)
ensure
eval_after_alls(example_group_instance)
reporter.example_group_finished(self)
+ if top_level? && RSpec.wants_to_quit
+ reporter.abort
+ exit!(1)
+ end
end
end
View
@@ -2,13 +2,20 @@ module RSpec::Core
class Reporter
def initialize(*formatters)
@formatters = formatters
- @failure_count = @pending_count = 0
+ @example_count = @failure_count = @pending_count = 0
end
def report(count)
start(count)
begin
yield self
+ ensure
+ conclude
+ end
+ end
+
+ def conclude
+ begin
stop
notify :start_dump
notify :dump_pending
@@ -19,10 +26,11 @@ def report(count)
end
end
- def start(example_count)
- @example_count = example_count
+ alias_method :abort, :conclude
+
+ def start(expected_example_count)
@start = Time.now
- notify :start, example_count
+ notify :start, expected_example_count
end
def message(message)
@@ -38,6 +46,7 @@ def example_group_finished(group)
end
def example_started(example)
+ @example_count += 1
notify :example_started, example
end
@@ -56,7 +65,7 @@ def example_pending(example)
end
def stop
- @duration = Time.now - @start
+ @duration = Time.now - @start if @start
notify :stop
end
View
@@ -27,7 +27,16 @@ def self.running_in_drb?
!!((DRb.current_server.uri) =~ /druby\:\/\/127.0.0.1\:/)
end
+ def self.trap_interrupt
+ trap('INT') do
+ exit!(1) if RSpec.wants_to_quit
+ RSpec.wants_to_quit = true
+ STDERR.puts "\nExiting... Interrupt again to exit immediately."
+ end
+ end
+
def self.run(args, err, out)
+ trap_interrupt
options = ConfigurationOptions.new(args)
options.parse_options
@@ -585,6 +585,15 @@ def subject; super().first; end
describe "#run" do
let(:reporter) { double("reporter").as_null_object }
+ context "with RSpec.wants_to_quit=true" do
+ it "returns without starting the group" do
+ RSpec.stub(:wants_to_quit) { true }
+ group = RSpec::Core::ExampleGroup.describe
+ reporter.should_not_receive(:example_group_started)
+ group.run(reporter)
+ end
+ end
+
context "with all examples passing" do
it "returns true" do
group = describe("something") do
@@ -40,6 +40,19 @@
end
describe "#run" do
+ context "with RSpec.wants_to_quit=true" do
+ it "returns without starting the example" do
+ RSpec.stub(:wants_to_quit) { true }
+ group = RSpec::Core::ExampleGroup.describe
+ example = group.example('example') {}
+
+ reporter = double('reporter').as_null_object
+ reporter.should_not_receive(:example_started)
+
+ example.run(group.new,reporter)
+ end
+ end
+
it "runs after(:each) when the example passes" do
after_run = false
group = RSpec::Core::ExampleGroup.describe do
@@ -2,6 +2,19 @@
module RSpec::Core
describe Reporter do
+ describe "abort" do
+ let(:formatter) { double("formatter").as_null_object }
+ let(:example) { double("example") }
+ let(:reporter) { Reporter.new(formatter) }
+
+ %w[start_dump dump_pending dump_failures dump_summary close].each do |message|
+ it "sends #{message} to the formatter(s)" do
+ formatter.should_receive(message)
+ reporter.abort
+ end
+ end
+ end
+
context "given one formatter" do
it "passes messages to that formatter" do
formatter = double("formatter")

0 comments on commit e521013

Please sign in to comment.