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

Commit 1c5818d

Browse files
Extract runner callables to factory methods
It was suggested that the callables used in the OptionParser could be defined as methods on the parser, and set in the options as follows: options[:runner] = method(:bisect_and_exit) Doing this I was unable to capture extra info in the callables, however (for example, the `#print_help_and_exit` method needs access to the parser and some invalid options). Instead, I've tried out adding callable factory methods. This means the factory methods can each have different signatures, and be passed whatever information they need, while the resulting callables can all maintain a consistent interface.
1 parent e855b2d commit 1c5818d

File tree

1 file changed

+62
-49
lines changed

1 file changed

+62
-49
lines changed

lib/rspec/core/option_parser.rb

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,8 @@ def parser(options)
6868

6969
parser.on('--bisect[=verbose]', 'Repeatedly runs the suite in order to isolate the failures to the ',
7070
' smallest reproducible case.') do |argument|
71-
options[:runner] = -> (opts, err, out) do
72-
RSpec::Support.require_rspec_core "bisect/coordinator"
73-
74-
success = Bisect::Coordinator.bisect_with(
75-
opts.args,
76-
RSpec.configuration,
77-
bisect_formatter_for(argument)
78-
)
79-
80-
exit(success ? 0 : 1)
81-
end
71+
options[:bisect] = argument
72+
options[:runner] = Callables.bisect_and_exit
8273
end
8374

8475
parser.on('--[no-]fail-fast[=COUNT]', 'Abort the run after a certain number of failures (1 by default).') do |argument|
@@ -106,28 +97,16 @@ def parser(options)
10697
options[:dry_run] = true
10798
end
10899

109-
parser.on('-X', '--[no-]drb', 'Run examples via DRb.') do |o|
110-
if o
111-
options[:runner] = -> (opts, err, out) do
112-
require 'rspec/core/drb'
113-
begin
114-
DRbRunner.new(opts).run(err, out)
115-
exit
116-
rescue DRb::DRbConnError
117-
err.puts "No DRb server is running. Running in local process instead ..."
118-
end
119-
end
120-
end
100+
parser.on('-X', '--[no-]drb', 'Run examples via DRb.') do |use_drb|
101+
options[:runner] = Callables.try_drb_runner if use_drb
121102
end
122103

123104
parser.on('--drb-port PORT', 'Port to connect to the DRb server.') do |o|
124105
options[:drb_port] = o.to_i
125106
end
126107

127108
parser.on('--init', 'Initialize your project with RSpec.') do |_cmd|
128-
options[:runner] = -> (*_args) do
129-
initialize_project_and_exit
130-
end
109+
options[:runner] = Callables.initialize_project_and_exit
131110
end
132111

133112
parser.separator("\n **** Output ****\n\n")
@@ -264,9 +243,7 @@ def parser(options)
264243
parser.separator("\n **** Utility ****\n\n")
265244

266245
parser.on('-v', '--version', 'Display the version.') do
267-
options[:runner] = -> (*_args) do
268-
print_version_and_exit
269-
end
246+
options[:runner] = Callables.print_version_and_exit
270247
end
271248

272249
# These options would otherwise be confusing to users, so we forcibly
@@ -278,9 +255,7 @@ def parser(options)
278255
invalid_options = %w[-d --I]
279256

280257
parser.on_tail('-h', '--help', "You're looking at it.") do
281-
options[:runner] = -> (*_args) do
282-
print_help_and_exit(parser, invalid_options)
283-
end
258+
options[:runner] = Callables.print_help_and_exit(parser, invalid_options)
284259
end
285260

286261
# This prevents usage of the invalid_options.
@@ -308,26 +283,64 @@ def configure_only_failures(options)
308283
add_tag_filter(options, :inclusion_filter, :last_run_status, 'failed')
309284
end
310285

311-
def initialize_project_and_exit
312-
RSpec::Support.require_rspec_core "project_initializer"
313-
ProjectInitializer.new.run
314-
exit
315-
end
286+
module Callables
287+
class << self
288+
def initialize_project_and_exit
289+
lambda do |*_args|
290+
RSpec::Support.require_rspec_core "project_initializer"
291+
ProjectInitializer.new.run
292+
exit
293+
end
294+
end
316295

317-
def bisect_formatter_for(argument)
318-
return Formatters::BisectDebugFormatter if argument == "verbose"
319-
Formatters::BisectProgressFormatter
320-
end
296+
def try_drb_runner
297+
lambda do |options, err, out|
298+
require 'rspec/core/drb'
299+
begin
300+
DRbRunner.new(options).run(err, out)
301+
exit
302+
rescue DRb::DRbConnError
303+
err.puts "No DRb server is running. Running in local process instead ..."
304+
end
305+
end
306+
end
321307

322-
def print_version_and_exit
323-
puts RSpec::Core::Version::STRING
324-
exit
325-
end
308+
def bisect_and_exit
309+
lambda do |options, _err, _out|
310+
RSpec::Support.require_rspec_core "bisect/coordinator"
311+
312+
success = Bisect::Coordinator.bisect_with(
313+
options.args,
314+
RSpec.configuration,
315+
bisect_formatter_for(options.options[:bisect])
316+
)
317+
318+
exit(success ? 0 : 1)
319+
end
320+
end
321+
322+
def print_version_and_exit
323+
lambda do |*_args|
324+
puts RSpec::Core::Version::STRING
325+
exit
326+
end
327+
end
328+
329+
def print_help_and_exit(parser, invalid_options)
330+
lambda do |*_args|
331+
# Removing the blank invalid options from the output.
332+
puts parser.to_s.gsub(/^\s+(#{invalid_options.join('|')})\s*$\n/, '')
333+
exit
334+
end
335+
end
336+
337+
private
326338

327-
def print_help_and_exit(parser, invalid_options)
328-
# Removing the blank invalid options from the output.
329-
puts parser.to_s.gsub(/^\s+(#{invalid_options.join('|')})\s*$\n/, '')
330-
exit
339+
def bisect_formatter_for(argument)
340+
return Formatters::BisectDebugFormatter if argument == "verbose"
341+
Formatters::BisectProgressFormatter
342+
end
343+
end
331344
end
332345
end
333346
end

0 commit comments

Comments
 (0)