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

Ensure ExecutionResult timing is always populated #2144

Merged
merged 3 commits into from
Dec 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### Development
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.4.1...master)

Bug Fixes:

* Fix `rspec --profile` when an example calls `abort` or `exit`.
(Bradley Schaefer, #2144)

### 3.4.1 / 2015-11-18
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.4.0...v3.4.1)

Expand Down
19 changes: 15 additions & 4 deletions lib/rspec/core/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Core
#
# This allows us to provide rich metadata about each individual
# example without adding tons of methods directly to the ExampleGroup
# that users may inadvertantly redefine.
# that users may inadvertently redefine.
#
# Useful for configuring logging and/or taking some action based
# on the state of an example's metadata.
Expand Down Expand Up @@ -260,6 +260,7 @@ def run(example_group_instance, reporter)

finish(reporter)
ensure
execution_result.ensure_timing_set(clock)
RSpec.current_example = nil
end

Expand Down Expand Up @@ -565,13 +566,23 @@ def example_skipped?
# @api private
# Records the finished status of the example.
def record_finished(status, finished_at)
self.status = status
self.finished_at = finished_at
self.run_time = (finished_at - started_at).to_f
self.status = status
calculate_run_time(finished_at)
end

# @api private
# Populates finished_at and run_time if it has not yet been set
def ensure_timing_set(clock)
calculate_run_time(clock.now) unless finished_at
end

private

def calculate_run_time(finished_at)
self.finished_at = finished_at
self.run_time = (finished_at - started_at).to_f
end

# For backwards compatibility we present `status` as a string
# when presenting the legacy hash interface.
def hash_for_delegation
Expand Down
14 changes: 13 additions & 1 deletion spec/rspec/core/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ def metadata_hash(*args)
end
end

it "captures example timing even for exceptions unhandled by RSpec" do
unhandled = RSpec::Support::AllExceptionsExceptOnesWeMustNotRescue::AVOID_RESCUING.first
example = example_group.example { raise unhandled }

begin
example_group.run
rescue unhandled
# no-op, prevent from bubbling up
end
expect(example.execution_result.finished_at).not_to be_nil
end

describe "#exception" do
it "supplies the exception raised, if there is one" do
example = example_group.example { raise "first" }
Expand Down Expand Up @@ -784,7 +796,7 @@ def expect_pending_result(example)
end

describe "exposing the examples reporter" do
it "returns a null reporter when the example hasnt run yet" do
it "returns a null reporter when the example has not run yet" do
example = RSpec.describe.example
expect(example.reporter).to be RSpec::Core::NullReporter
end
Expand Down