Skip to content

Commit

Permalink
Merge pull request #58 from briandunn/fix/56-marshalable
Browse files Browse the repository at this point in the history
Use serialized exception in execution result
  • Loading branch information
briandunn authored Mar 15, 2021
2 parents 0da55c5 + 94a82fd commit 5e70faf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/flatware/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ def run(job, _options = {})
runner = ::RSpec::Core::Runner
def runner.trap_interrupt() end

profile_examples = ::RSpec.configuration.profile_examples
::RSpec.reset
::RSpec.configuration.profile_examples = profile_examples
::RSpec.configuration.deprecation_stream = StringIO.new
::RSpec.configuration.output_stream = StringIO.new
::RSpec.configuration.add_formatter(Flatware::RSpec::Formatter)
Expand Down
7 changes: 6 additions & 1 deletion lib/flatware/rspec/marshalable/example.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Flatware
module RSpec
module Marshalable
require 'flatware/rspec/marshalable/execution_result'
require 'flatware/rspec/marshalable/shared_group_inclusion_backtrace'

##
Expand All @@ -14,7 +15,7 @@ module Marshalable
]
) do
def initialize(rspec_example)
super(*members.map do |attribute|
super(marshalable_execution_result(rspec_example.execution_result), *members[1..].map do |attribute|
rspec_example.public_send(attribute)
end)

Expand All @@ -25,6 +26,10 @@ def initialize(rspec_example)

private

def marshalable_execution_result(execution_result)
ExecutionResult.from_rspec(execution_result)
end

def marshalable_metadata(rspec_metadata)
rspec_metadata.slice(:extra_failure_lines).tap do |metadata|
if (backtraces = rspec_metadata[:shared_group_inclusion_backtrace])
Expand Down
18 changes: 18 additions & 0 deletions lib/flatware/rspec/marshalable/execution_result.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Flatware
module RSpec
module Marshalable
require 'flatware/serialized_exception'
class ExecutionResult < ::RSpec::Core::Example::ExecutionResult
def self.from_rspec(result)
new.tap do |marshalable|
marshalable.exception = result.exception && SerializedException.from(result.exception)

%i[finished_at run_time started_at status].each do |member|
marshalable.public_send(:"#{member}=", result.public_send(member))
end
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/flatware/rspec/checkpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
failure_notification = instance_double(
::RSpec::Core::Example,
full_description: 'bad news',
execution_result: nil,
execution_result: instance_double(::RSpec::Core::Example::ExecutionResult, exception: nil).as_null_object,
location: nil,
location_rerun_argument: nil,
metadata: {}
Expand Down
33 changes: 31 additions & 2 deletions spec/flatware/rspec/marshalable/example_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
require 'spec_helper'
require 'flatware/rspec/marshalable/example'
describe Flatware::RSpec::Marshalable::Example do
def stub_execution_result(exception)
instance_double(
::RSpec::Core::Example::ExecutionResult,
exception: exception,
finished_at: Time.now,
run_time: 0,
started_at: Time.now,
status: :failed
)
end

it 'caries what is needed to format a backtrace' do
exception = Exception.new
::RSpec::Core::Formatters::ExceptionPresenter.new(
Exception.new,
exception,
described_class.new(
instance_double(
::RSpec::Core::Example,
execution_result: nil,
execution_result: stub_execution_result(exception),
full_description: nil,
location: nil,
location_rerun_argument: nil,
Expand All @@ -16,4 +28,21 @@
)
).fully_formatted(nil)
end

it 'does not cary constant references in exceptions' do
const = stub_const('A::Constant::Not::Likely::Loaded::In::Sink', Class.new(RuntimeError))
message = described_class.new(
instance_double(
::RSpec::Core::Example,
execution_result: stub_execution_result(const.new),
full_description: nil,
location: nil,
location_rerun_argument: nil,
metadata: {}
)
)

expect(message.execution_result.exception.class.to_s).to eq(const.to_s)
expect(message.execution_result.exception.class).to_not be_an_instance_of(const)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it 'can be added together' do
failed_example = instance_double(
::RSpec::Core::Example,
execution_result: nil,
execution_result: instance_double(::RSpec::Core::Example::ExecutionResult, exception: nil).as_null_object,
full_description: 'the example',
location_rerun_argument: nil,
location: nil,
Expand Down

0 comments on commit 5e70faf

Please sign in to comment.