Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve port from .rspec #242

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions lib/spork/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ module Spork
# This is used by bin/spork. It's wrapped in a class because it's easier to test that way.
class Runner
attr_reader :test_framework

def self.run(args, output, error)
self.new(args, output, error).run
end

def initialize(args, output, error)
raise ArgumentError, "expected array of args" unless args.is_a?(Array)
@output = output
@error = error
@options = {}
opt = OptionParser.new
opt.banner = "Usage: spork [test framework name] [options]\n\n"

opt.separator "Options:"
opt.on("-b", "--bootstrap") {|ignore| @options[:bootstrap] = true }
opt.on("-d", "--diagnose") {|ignore| @options[:diagnose] = true }
Expand All @@ -27,24 +27,40 @@ def initialize(args, output, error)
non_option_args = args.select { |arg| ! args[0].match(/^-/) }
@options[:server_matcher] = non_option_args[0]
opt.parse!(args)

if @options[:help]
@output.puts opt
@output.puts
@output.puts supported_test_frameworks_text
exit(0)
end

# If server is rspec and port isn't passed in, attempt to get it from .rspec
if !@options[:port] && [nil, 'rspec'].include?(@options[:server_matcher])
retrieve_port_from_dot_rspec
end
end


def retrieve_port_from_dot_rspec
File.open("./.rspec", "r") do |file|
file.each_line do |line|
next unless line =~ %r{drb-port\s*(\d+)}
@options[:port] = $1
end
end
rescue Errno::ENOENT
# Safely return. File not found.
end

def supported_test_frameworks_text
text = StringIO.new

text.puts "Supported test frameworks:"
text.puts Spork::TestFramework.supported_test_frameworks.sort { |a,b| a.short_name <=> b.short_name }.map { |s| (s.available? ? '(*) ' : '( ) ') + s.short_name }
text.puts "\nLegend: ( ) - not detected in project (*) - detected\n"
text.string
end

# Returns a server for the specified (or the detected default) testing framework. Returns nil if none detected, or if the specified is not supported or available.
def find_test_framework
Spork::TestFramework.factory(@output, @error, options[:server_matcher])
Expand All @@ -53,7 +69,7 @@ def find_test_framework
rescue Spork::TestFramework::FactoryException => e
@error.puts "#{e.message}\n\n#{supported_test_frameworks_text}"
end

def run
return false unless test_framework = find_test_framework
ENV["DRB"] = 'true'
Expand All @@ -65,7 +81,7 @@ def run
test_framework.bootstrap
when options[:diagnose]
require 'spork/diagnoser'

Spork::Diagnoser.install_hook!(test_framework.entry_point)
test_framework.preload
Spork::Diagnoser.output_results(@output)
Expand All @@ -77,9 +93,9 @@ def run
return true
end
end

private
attr_reader :options
attr_reader :options

end
end
Expand Down
30 changes: 23 additions & 7 deletions spec/spork/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,58 @@ def use_test_server(klass = Spork::TestFramework::RSpec)
before(:each) do
@out, @err = StringIO.new, StringIO.new
end

it "finds a matching server with a prefix" do
Spork::Runner.new(["rs"], @out, @err).find_test_framework.class.should == Spork::TestFramework::RSpec
end


it "attempts to get port number from .rspec if rspec is the desired framework" do
Spork::Runner.any_instance.should_receive(:retrieve_port_from_dot_rspec)
Spork::Runner.new(['rspec'], @out, @err)
end

it "gets the port number from .rspec if rspec and file present" do
File.should_receive(:open).and_yield(StringIO.new("--drb-port 1234"))
subject = Spork::Runner.new(['rspec'], @out, @err)
subject.instance_variable_get(:"@options")[:port] == "1234"
end

it "no error is raised if .rspec isn't present" do
File.should_receive(:open).and_raise(Errno::ENOENT)
expect{ subject = Spork::Runner.new(['rspec'], @out, @err) }.to_not raise_error
end

it "shows an error message if no matching server was found" do
Spork::Runner.new(["argle_bargle"], @out, @err).run.should == false
@err.string.should include(%(Couldn't find a supported test framework that begins with 'argle_bargle'))
end

it "bootstraps a server when -b is passed in" do
use_test_server
@test_framework.should_receive(:bootstrap)
Spork::Runner.new(['rspec', '-b'], @out, @err).run
end

it "aborts if it can't preload" do
use_test_server
@test_framework.should_receive(:preload).and_return(false)
Spork::Server.should_not_receive(:run)
Spork::Runner.new(['rspec'], @out, @err).run
end

it "runs the server if all is well" do
use_test_server
@test_framework.should_receive(:preload).and_return(true)
Spork::Server.should_receive(:run)
Spork::Runner.new(['rspec'], @out, @err).run
@err.string.should include("Using RSpec")
end

it "outputs a list of supported servers, along with supported asterisk" do
Spork::Server.stub!(:supported_test_frameworks).and_return([Spork::TestFramework::RSpec, Spork::TestFramework::Cucumber])
Spork::TestFramework::RSpec.stub!(:available?).and_return(true)
Spork::TestFramework::Cucumber.stub!(:available?).and_return(false)

Spork::Runner.new(['rspec'], @out, @err).supported_test_frameworks_text.should include("(*) RSpec")
end
end