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

Commit 965e2d1

Browse files
Move --drb runner specs into OptionParser specs with other callables
Previously the logic these tests exercise was contained in RSpec::Core::Runner.run. Now it's been moved into a callable set by the OptionParser, I've chosen to test it as part of the OptionParser tests. This feels like an intermediate step, and I think I will likely end up extracting the runner callables and testing them directly, rather than via the OptionParser.
1 parent 80b6be1 commit 965e2d1

File tree

3 files changed

+97
-51
lines changed

3 files changed

+97
-51
lines changed

lib/rspec/core/option_parser.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ def self.drb_with_fallback
300300
lambda do |options, err, out|
301301
require 'rspec/core/drb'
302302
begin
303-
DRbRunner.new(options).run(err, out)
304-
return
303+
return DRbRunner.new(options).run(err, out)
305304
rescue DRb::DRbConnError
306305
err.puts "No DRb server is running. Running in local process instead ..."
307306
end

spec/rspec/core/option_parser_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'rspec/core/drb'
12
require 'rspec/core/project_initializer'
23

34
module RSpec::Core
@@ -93,6 +94,76 @@ def generate_help_text
9394
end
9495
end
9596

97+
%w[ -X --drb ].each do |option|
98+
describe option do
99+
let(:parser) { Parser.new([option]) }
100+
let(:options) { double(:options) }
101+
let(:err) { double(:stderr, :puts => nil) }
102+
let(:out) { double(:stderr, :puts => nil) }
103+
104+
it 'sets the `:drb` option to true' do
105+
options = parser.parse
106+
107+
expect(options[:drb]).to be_truthy
108+
end
109+
110+
context 'when a DRb server is running' do
111+
it "builds a DRbRunner and runs the specs" do
112+
drb_proxy = double(RSpec::Core::DRbRunner, :run => 0)
113+
allow(RSpec::Core::DRbRunner).to receive(:new).and_return(drb_proxy)
114+
115+
options = parser.parse
116+
exit_code = options[:runner].call(options, err, out)
117+
118+
expect(drb_proxy).to have_received(:run).with(err, out)
119+
expect(exit_code).to eq(0)
120+
end
121+
end
122+
123+
context 'when a DRb server is not running' do
124+
let(:runner) { instance_double(RSpec::Core::Runner, :run => 0) }
125+
126+
before(:each) do
127+
allow(RSpec::Core::Runner).to receive(:new).and_return(runner)
128+
allow(RSpec::Core::DRbRunner).to receive(:new).and_raise(DRb::DRbConnError)
129+
end
130+
131+
it "outputs a message" do
132+
options = parser.parse
133+
options[:runner].call(options, err, out)
134+
135+
expect(err).to have_received(:puts).with(
136+
"No DRb server is running. Running in local process instead ..."
137+
)
138+
end
139+
140+
it "builds a runner instance and runs the specs" do
141+
options = parser.parse
142+
options[:runner].call(options, err, out)
143+
144+
expect(RSpec::Core::Runner).to have_received(:new).with(options)
145+
expect(runner).to have_received(:run).with(err, out)
146+
end
147+
148+
if RSpec::Support::RubyFeatures.supports_exception_cause?
149+
it "prevents the DRb error from being listed as the cause of expectation failures" do
150+
allow(RSpec::Core::Runner).to receive(:new) do |options|
151+
raise RSpec::Expectations::ExpectationNotMetError
152+
end
153+
154+
expect {
155+
options = parser.parse
156+
options[:runner].call(options, err, out)
157+
158+
}.to raise_error(RSpec::Expectations::ExpectationNotMetError) do |e|
159+
expect(e.cause).to be_nil
160+
end
161+
end
162+
end
163+
end
164+
end
165+
end
166+
96167
describe "--init" do
97168
it "initializes a project and returns a 0 exit code" do
98169
project_init = instance_double(ProjectInitializer)
@@ -320,6 +391,10 @@ def generate_help_text
320391
end
321392
end
322393

394+
describe '--bisect' do
395+
396+
end
397+
323398
describe '--profile' do
324399
it 'sets profile_examples to true by default' do
325400
options = Parser.parse(%w[--profile])

spec/rspec/core/runner_spec.rb

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -255,67 +255,39 @@ def interrupt
255255
end
256256

257257
describe ".run" do
258+
let(:args) { double(:args) }
258259
let(:err) { StringIO.new }
259260
let(:out) { StringIO.new }
261+
let(:options) { { } }
262+
let(:configuration_options) { double(:configuration_options, :options => options) }
260263

261-
context "with --drb or -X" do
262-
before(:each) do
263-
@options = RSpec::Core::ConfigurationOptions.new(%w[--drb --drb-port 8181 --color])
264-
allow(RSpec::Core::ConfigurationOptions).to receive(:new) { @options }
265-
end
266-
267-
def run_specs
268-
RSpec::Core::Runner.run(%w[ --drb ], err, out)
269-
end
264+
before(:each) do
265+
allow(RSpec::Core::ConfigurationOptions).to receive(:new).and_return(configuration_options)
266+
end
270267

271-
context 'and a DRb server is running' do
272-
it "builds a DRbRunner and runs the specs" do
273-
drb_proxy = double(RSpec::Core::DRbRunner, :run => true)
274-
expect(drb_proxy).to receive(:run).with(err, out)
268+
context 'when the options contain a runner callable' do
269+
let(:runner) { double(:runner, :call => nil) }
270+
let(:options) { { :runner => runner } }
275271

276-
expect(RSpec::Core::DRbRunner).to receive(:new).and_return(drb_proxy)
272+
it 'invokes the runner callable' do
273+
RSpec::Core::Runner.run([], err, out)
277274

278-
run_specs
279-
end
275+
expect(runner).to have_received(:call).with(configuration_options, err, out)
280276
end
277+
end
281278

282-
context 'and a DRb server is not running' do
283-
before(:each) do
284-
expect(RSpec::Core::DRbRunner).to receive(:new).and_raise(DRb::DRbConnError)
285-
end
286-
287-
it "outputs a message" do
288-
allow(RSpec.configuration).to receive(:files_to_run) { [] }
289-
expect(err).to receive(:puts).with(
290-
"No DRb server is running. Running in local process instead ..."
291-
)
292-
run_specs
293-
end
294-
295-
it "builds a runner instance and runs the specs" do
296-
process_proxy = double(RSpec::Core::Runner, :run => 0)
297-
expect(process_proxy).to receive(:run).with(err, out)
298-
299-
expect(RSpec::Core::Runner).to receive(:new).and_return(process_proxy)
279+
context 'when no runner callable is set' do
280+
it 'instantiates a Runner instance and runs it' do
281+
process_proxy = double(RSpec::Core::Runner, :run => 0)
282+
allow(RSpec::Core::Runner).to receive(:new).and_return(process_proxy)
300283

301-
run_specs
302-
end
284+
RSpec::Core::Runner.run([], err, out)
303285

304-
if RSpec::Support::RubyFeatures.supports_exception_cause?
305-
it "prevents the DRb error from being listed as the cause of expectation failures" do
306-
allow(RSpec::Core::Runner).to receive(:new) do |options|
307-
raise RSpec::Expectations::ExpectationNotMetError
308-
end
309-
310-
expect {
311-
Runner.run([], StringIO.new, StringIO.new)
312-
}.to raise_error(RSpec::Expectations::ExpectationNotMetError) do |e|
313-
expect(e.cause).to be_nil
314-
end
315-
end
316-
end
286+
expect(RSpec::Core::Runner).to have_received(:new)
287+
expect(process_proxy).to have_received(:run).with(err, out)
317288
end
318289
end
290+
319291
end
320292

321293
context "when run" do

0 commit comments

Comments
 (0)