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

Commit 9d2893f

Browse files
committed
Extract bisect formatter base class.
Also, rename the bisect formatter to indicate it communicates over DRb.
1 parent 9811a38 commit 9d2893f

File tree

12 files changed

+99
-66
lines changed

12 files changed

+99
-66
lines changed

lib/rspec/core/bisect/server.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'drb/drb'
22
require 'drb/acl'
3+
RSpec::Support.require_rspec_core "bisect/utilities"
34

45
module RSpec
56
module Core
@@ -49,10 +50,10 @@ def drb_port
4950
@drb_port ||= Integer(@drb.uri[/\d+$/])
5051
end
5152

52-
# Fetched via DRb by the BisectFormatter to determine when to abort.
53+
# Fetched via DRb by the BisectDRbFormatter to determine when to abort.
5354
attr_accessor :expected_failures
5455

55-
# Set via DRb by the BisectFormatter with the results of the run.
56+
# Set via DRb by the BisectDRbFormatter with the results of the run.
5657
attr_accessor :latest_run_results
5758

5859
# Fetched via DRb to tell clients which files to run

lib/rspec/core/bisect/shell_command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def command_for(locations, server)
2020
parts << RUBY << load_path
2121
parts << open3_safe_escape(RSpec::Core.path_to_executable)
2222

23-
parts << "--format" << "bisect"
23+
parts << "--format" << "bisect-drb"
2424
parts << "--drb-port" << server.drb_port
2525

2626
parts.concat(reusable_cli_options)

lib/rspec/core/bisect/utilities.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module RSpec
2+
module Core
3+
module Bisect
4+
# @private
5+
ExampleSetDescriptor = Struct.new(:all_example_ids, :failed_example_ids)
6+
end
7+
end
8+
end

lib/rspec/core/formatters.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module RSpec::Core::Formatters
7272
autoload :ProgressFormatter, 'rspec/core/formatters/progress_formatter'
7373
autoload :ProfileFormatter, 'rspec/core/formatters/profile_formatter'
7474
autoload :JsonFormatter, 'rspec/core/formatters/json_formatter'
75-
autoload :BisectFormatter, 'rspec/core/formatters/bisect_formatter'
75+
autoload :BisectDRbFormatter, 'rspec/core/formatters/bisect_drb_formatter'
7676
autoload :ExceptionPresenter, 'rspec/core/formatters/exception_presenter'
7777

7878
# Register the formatter class
@@ -203,8 +203,8 @@ def built_in_formatter(key)
203203
ProgressFormatter
204204
when 'j', 'json'
205205
JsonFormatter
206-
when 'bisect'
207-
BisectFormatter
206+
when 'bisect-drb'
207+
BisectDRbFormatter
208208
end
209209
end
210210

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
RSpec::Support.require_rspec_core "bisect/utilities"
2+
3+
module RSpec
4+
module Core
5+
module Formatters
6+
# Contains common logic for formatters used by `--bisect` to communicate results
7+
# back to the bisect runner.
8+
#
9+
# Subclasses must define a `notify_results(all_example_ids, failed_example_ids)`
10+
# method.
11+
# @private
12+
class BaseBisectFormatter
13+
def self.inherited(formatter)
14+
Formatters.register formatter, :start_dump, :example_failed, :example_finished
15+
end
16+
17+
def initialize(expected_failures)
18+
@all_example_ids = []
19+
@failed_example_ids = []
20+
@remaining_failures = expected_failures
21+
end
22+
23+
def example_failed(notification)
24+
@failed_example_ids << notification.example.id
25+
end
26+
27+
def example_finished(notification)
28+
@all_example_ids << notification.example.id
29+
return unless @remaining_failures.include?(notification.example.id)
30+
@remaining_failures.delete(notification.example.id)
31+
32+
status = notification.example.execution_result.status
33+
return if status == :failed && !@remaining_failures.empty?
34+
RSpec.world.wants_to_quit = true
35+
end
36+
37+
def start_dump(_notification)
38+
# `notify_results` is defined in the subclass
39+
notify_results(Bisect::ExampleSetDescriptor.new(
40+
@all_example_ids, @failed_example_ids))
41+
end
42+
end
43+
end
44+
end
45+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'drb/drb'
2+
RSpec::Support.require_rspec_core "formatters/base_bisect_formatter"
3+
4+
module RSpec
5+
module Core
6+
module Formatters
7+
# Used by `--bisect`. When it shells out and runs a portion of the suite, it uses
8+
# this formatter as a means to have the status reported back to it, via DRb.
9+
#
10+
# Note that since DRb calls carry considerable overhead compared to normal
11+
# method calls, we try to minimize the number of DRb calls for perf reasons,
12+
# opting to communicate only at the start and the end of the run, rather than
13+
# after each example.
14+
# @private
15+
class BisectDRbFormatter < BaseBisectFormatter
16+
def initialize(_output)
17+
drb_uri = "druby://localhost:#{RSpec.configuration.drb_port}"
18+
@bisect_server = DRbObject.new_with_uri(drb_uri)
19+
RSpec.configuration.files_or_directories_to_run = @bisect_server.files_or_directories_to_run
20+
super(Set.new(@bisect_server.expected_failures))
21+
end
22+
23+
def notify_results(results)
24+
@bisect_server.latest_run_results = results
25+
end
26+
end
27+
end
28+
end
29+
end

lib/rspec/core/formatters/bisect_formatter.rb

Lines changed: 0 additions & 51 deletions
This file was deleted.

spec/rspec/core/bisect/example_minimizer_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'rspec/core/bisect/example_minimizer'
2-
require 'rspec/core/formatters/bisect_formatter'
32
require 'rspec/core/bisect/server'
43
require 'rspec/core/bisect/shell_command'
54
require 'support/fake_bisect_runner'

spec/rspec/core/bisect/server_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module RSpec::Core
4141
end
4242
end
4343

44-
context "when used in combination with the BisectFormatter", :slow do
44+
context "when used in combination with the BisectDRbFormatter", :slow do
4545
include FormatterSupport
4646

4747
attr_reader :server
@@ -55,7 +55,7 @@ module RSpec::Core
5555

5656
def run_formatter_specs
5757
RSpec.configuration.drb_port = server.drb_port
58-
run_rspec_with_formatter("bisect")
58+
run_rspec_with_formatter("bisect-drb")
5959
end
6060

6161
it 'receives suite results' do

spec/rspec/core/bisect/shell_command_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require 'rspec/core/bisect/shell_command'
2-
require 'rspec/core/formatters/bisect_formatter'
2+
require 'rspec/core/formatters/bisect_drb_formatter'
33

44
module RSpec::Core
55
RSpec.describe Bisect::ShellCommand do

spec/rspec/core/bisect/shell_runner_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require 'rspec/core/bisect/shell_runner'
21
require 'rspec/core/bisect/shell_command'
3-
require 'rspec/core/formatters/bisect_formatter'
2+
require 'rspec/core/bisect/shell_runner'
3+
require 'rspec/core/bisect/utilities'
44

55
module RSpec::Core
66
RSpec.describe Bisect::ShellRunner do
@@ -13,7 +13,7 @@ module RSpec::Core
1313
let(:target_specs) { %w[ spec/1_spec.rb[1:1] spec/1_spec.rb[1:2] ] }
1414

1515
it "passes the failed examples from the original run as the expected failures so the runs can abort early" do
16-
original_results = Formatters::BisectFormatter::RunResults.new(
16+
original_results = Bisect::ExampleSetDescriptor.new(
1717
[], %w[ spec/failure_spec.rb[1:1] spec/failure_spec.rb[1:2] ]
1818
)
1919

spec/support/fake_bisect_runner.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
require 'rspec/core/bisect/utilities'
2+
13
FakeBisectRunner = Struct.new(:all_ids, :always_failures, :dependent_failures) do
24
def original_results
35
failures = always_failures | dependent_failures.keys
4-
RSpec::Core::Formatters::BisectFormatter::RunResults.new(all_ids, failures.sort)
6+
RSpec::Core::Bisect::ExampleSetDescriptor.new(all_ids, failures.sort)
57
end
68

79
def run(ids)
@@ -10,7 +12,7 @@ def run(ids)
1012
failures << failing_example if dependency_satisfied?(depends_upon, ids)
1113
end
1214

13-
RSpec::Core::Formatters::BisectFormatter::RunResults.new(ids.sort, failures.sort)
15+
RSpec::Core::Bisect::ExampleSetDescriptor.new(ids.sort, failures.sort)
1416
end
1517

1618
private

0 commit comments

Comments
 (0)