diff --git a/lib/spec/runner/drb_command_line.rb b/lib/spec/runner/drb_command_line.rb index 058a8e1df..6c2b54d63 100644 --- a/lib/spec/runner/drb_command_line.rb +++ b/lib/spec/runner/drb_command_line.rb @@ -4,6 +4,12 @@ module Spec module Runner # Facade to run specs by connecting to a DRB server class DrbCommandLine + # port to run against + def self.port(options) + (options.drb_port || ENV["RSPEC_DRB"] || 8989).to_i + end + + # Runs specs on a DRB server. Note that this API is similar to that of # CommandLine - making it possible for clients to use both interchangeably. def self.run(options) @@ -13,7 +19,7 @@ def self.run(options) rescue SocketError; \ DRb.start_service("druby://:0"); \ end - spec_server = DRbObject.new_with_uri("druby://127.0.0.1:8989") + spec_server = DRbObject.new_with_uri("druby://127.0.0.1:#{port(options)}") spec_server.run(options.argv, options.error_stream, options.output_stream) true rescue DRb::DRbConnError @@ -21,6 +27,7 @@ def self.run(options) false end end + end end end diff --git a/lib/spec/runner/option_parser.rb b/lib/spec/runner/option_parser.rb index fbbf444a9..87c9522c2 100644 --- a/lib/spec/runner/option_parser.rb +++ b/lib/spec/runner/option_parser.rb @@ -80,6 +80,7 @@ def spec_command? :runner => ["-U", "--runner RUNNER", "Use a custom Runner."], :debug => ["-u", "--debugger", "Enable ruby-debugging."], :drb => ["-X", "--drb", "Run examples via DRb. (For example against script/spec_server)"], + :drb_port => ["--port PORT", "Port for DRb server. (Ignored without --drb)"], :version => ["-v", "--version", "Show version"], :help => ["-h", "--help", "You're looking at it"] } @@ -113,6 +114,7 @@ def initialize(err, out) on(*OPTIONS[:runner]) {|runner| @options.user_input_for_runner = runner} on(*OPTIONS[:debug]) {@options.debug = true} on(*OPTIONS[:drb]) {} + on(*OPTIONS[:drb_port]) {|port| @options.drb_port = port} on(*OPTIONS[:version]) {parse_version} on("--autospec") {@options.autospec = true} on_tail(*OPTIONS[:help]) {parse_help} diff --git a/lib/spec/runner/options.rb b/lib/spec/runner/options.rb index e0179e80e..585b984ef 100644 --- a/lib/spec/runner/options.rb +++ b/lib/spec/runner/options.rb @@ -50,7 +50,8 @@ class Options :argv ) attr_reader :colour, :differ_class, :files, :examples, :example_groups - + attr_writer :drb_port + def initialize(error_stream, output_stream) @error_stream = error_stream @output_stream = output_stream @@ -289,6 +290,10 @@ def dry_run? @dry_run == true end + def drb_port + @drb_port.to_i if @drb_port + end + protected def define_predicate_matchers diff --git a/spec/spec/runner/drb_command_line_spec.rb b/spec/spec/runner/drb_command_line_spec.rb index 0c17a7f68..5d058513e 100644 --- a/spec/spec/runner/drb_command_line_spec.rb +++ b/spec/spec/runner/drb_command_line_spec.rb @@ -101,6 +101,47 @@ def tty?; true end end end + context "port" do + before do + @options = stub("options", :drb_port => nil) + end + + + it "should default to 8989" do + Spec::Runner::DrbCommandLine.port(@options).should == 8989 + end + + it "should pull default value from RSPEC_DRB environment" do + original = ENV['RSPEC_DRB'] + begin + ENV['RSPEC_DRB'] = '9000' + Spec::Runner::DrbCommandLine.port(@options).should == 9000 + ensure + ENV['RSPEC_DRB'] = original + end + end + + it "should pull configured value" do + @options.stub(:drb_port => '5000') + + Spec::Runner::DrbCommandLine.port(@options).should == 5000 + end + + it 'should prefer configured value to environment' do + @options.stub(:drb_port => '5000') + original = ENV['RSPEC_DRB'] + begin + ENV['RSPEC_DRB'] = '9000' + Spec::Runner::DrbCommandLine.port(@options).should == 5000 + ensure + ENV['RSPEC_DRB'] = original + end + end + + + end + + end end end diff --git a/spec/spec/runner/option_parser_spec.rb b/spec/spec/runner/option_parser_spec.rb index a516bc686..bac01c908 100644 --- a/spec/spec/runner/option_parser_spec.rb +++ b/spec/spec/runner/option_parser_spec.rb @@ -63,6 +63,15 @@ def parse(args) options = parse(["--drb", "-u"]) options.debug.should be_false end + + it "should accept port option" do + options = parse(["--port", "9000"]) + options.drb_port.should == 9000 + end + + it 'should require argument to port option' do + lambda { parse(["--port"]) }.should raise_error(OptionParser::MissingArgument) + end it "should accept dry run option" do options = parse(["--dry-run"]) diff --git a/spec/spec/runner/options_spec.rb b/spec/spec/runner/options_spec.rb index bb1e0e8c3..63f9fa43d 100644 --- a/spec/spec/runner/options_spec.rb +++ b/spec/spec/runner/options_spec.rb @@ -266,6 +266,13 @@ module Runner end end + describe "#drb_port" do + it "returns a number" do + @options.drb_port = "400" + @options.drb_port.should == 400 + end + end + describe "#number_of_examples" do context "when --example is parsed" do it "provides the number of examples parsed instead of the total number of examples collected" do