Skip to content

Commit

Permalink
Store instances in an array instead of Hash
Browse files Browse the repository at this point in the history
I should have listened to the code when it kept
telling me that hashes was a bad idea.
  • Loading branch information
Mange committed Dec 5, 2011
1 parent 5fcd02e commit 44059e8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 48 deletions.
8 changes: 4 additions & 4 deletions lib/guard/spork/runner.rb
Expand Up @@ -51,10 +51,10 @@ def kill_orphan_sporks

private
def initialize_spork_instances
@spork_instances = {}
@spork_instances = []
[:rspec, :cucumber, :test_unit].each do |type|
port, env = options[:"#{type}_port"], options[:"#{type}_env"]
spork_instances[type] = SporkInstance.new(type, port, env, :bundler => should_use?(:bundler)) if should_use?(type)
spork_instances << SporkInstance.new(type, port, env, :bundler => should_use?(:bundler)) if should_use?(type)
end
end

Expand All @@ -65,9 +65,9 @@ def kill_pids(pids)

def find_instances(type = nil)
if type.nil?
spork_instances.values
spork_instances
else
[spork_instances[type]]
spork_instances.select { |instance| instance.type == type }
end
end

Expand Down
94 changes: 50 additions & 44 deletions spec/guard/spork/runner_spec.rb
Expand Up @@ -33,14 +33,18 @@ def file_existance(files)
end
end

def instance(type, runner = runner)
runner.spork_instances.find { |instance| instance.type == type }
end

it "has a spork instance for :rspec when configured" do
runner = Guard::Spork::Runner.new({
:rspec => true,
:rspec_port => 2,
:rspec_env => {'spec' => 'yes'},
})

runner.spork_instances[:rspec].tap do |instance|
instance(:rspec, runner).tap do |instance|
instance.port.should == 2
instance.env.should == {'spec' => 'yes'}
end
Expand All @@ -53,7 +57,7 @@ def file_existance(files)
:cucumber_env => {'cuke' => 'yes'},
})

runner.spork_instances[:cucumber].tap do |instance|
instance(:cucumber, runner).tap do |instance|
instance.port.should == 2
instance.env.should == {'cuke' => 'yes'}
end
Expand All @@ -66,7 +70,7 @@ def file_existance(files)
:test_unit_env => {'unit' => 'yes'},
})

runner.spork_instances[:test_unit].tap do |instance|
instance(:test_unit, runner).tap do |instance|
instance.port.should == 2
instance.env.should == {'unit' => 'yes'}
end
Expand All @@ -83,19 +87,19 @@ def file_existance(files)
end

it "has a spork instance for :test_unit" do
runner.spork_instances[:test_unit].should be_instance_of(Guard::Spork::SporkInstance)
instance(:test_unit).should be_instance_of(Guard::Spork::SporkInstance)
end

it "does not have bundler enabled for the test_unit instance" do
runner.spork_instances[:test_unit].options.should include(:bundler => false)
instance(:test_unit).options.should include(:bundler => false)
end

it "does not have a spork instance for :rspec" do
runner.spork_instances[:rspec].should be_nil
instance(:rspec).should be_nil
end

it "does not have a spork instance for :cucumber" do
runner.spork_instances[:cucumber].should be_nil
instance(:cucumber).should be_nil
end
end

Expand All @@ -110,19 +114,19 @@ def file_existance(files)
end

it "has a spork instance for :rspec" do
runner.spork_instances[:rspec].should be_instance_of(Guard::Spork::SporkInstance)
instance(:rspec).should be_instance_of(Guard::Spork::SporkInstance)
end

it "does not have bundler enabled for the rspec instance" do
runner.spork_instances[:rspec].options.should include(:bundler => false)
instance(:rspec).options.should include(:bundler => false)
end

it "does not have a spork instance for :test_unit" do
runner.spork_instances[:test_unit].should be_nil
instance(:test_unit).should be_nil
end

it "does not have a spork instance for :cucumber" do
runner.spork_instances[:cucumber].should be_nil
instance(:cucumber).should be_nil
end
end

Expand All @@ -137,19 +141,19 @@ def file_existance(files)
end

it "has a spork instance for :cucumber" do
runner.spork_instances[:cucumber].should be_instance_of(Guard::Spork::SporkInstance)
instance(:cucumber).should be_instance_of(Guard::Spork::SporkInstance)
end

it "does not have bundler enabled for the cucumber instance" do
runner.spork_instances[:cucumber].options.should include(:bundler => false)
instance(:cucumber).options.should include(:bundler => false)
end

it "does not have a spork instance for :test_unit" do
runner.spork_instances[:test_unit].should be_nil
instance(:test_unit).should be_nil
end

it "does not have a spork instance for :rspec" do
runner.spork_instances[:rspec].should be_nil
instance(:rspec).should be_nil
end
end

Expand All @@ -164,23 +168,23 @@ def file_existance(files)
end

it "has a spork instance for :rspec" do
runner.spork_instances[:rspec].should be_instance_of(Guard::Spork::SporkInstance)
instance(:rspec).should be_instance_of(Guard::Spork::SporkInstance)
end

it "has a spork instance for :cucumber" do
runner.spork_instances[:cucumber].should be_instance_of(Guard::Spork::SporkInstance)
instance(:cucumber).should be_instance_of(Guard::Spork::SporkInstance)
end

it "has bundler enabled for the rspec instance" do
runner.spork_instances[:rspec].options.should include(:bundler => true)
instance(:rspec).options.should include(:bundler => true)
end

it "has bundler enabled for the cucumber instance" do
runner.spork_instances[:cucumber].options.should include(:bundler => true)
instance(:cucumber).options.should include(:bundler => true)
end

it "does not have a spork instance for :test_unit" do
runner.spork_instances[:test_unit].should be_nil
instance(:test_unit).should be_nil
end
end
end
Expand All @@ -190,32 +194,34 @@ def file_existance(files)
let(:cucumber_instance) { fake_instance(:cucumber) }

def fake_instance(type)
double("fake instance", :start => nil, :running? => true, :type => type).tap do |mock|
def mock.to_s() type.to_s end
# This one is needed to get this to work in 1.8.7
# The effect only shows up during tests; Array#join uses #to_s in both
# 1.8.7 and 1.9.2
# It's probably something related to RSpec
def mock.inspect() type.to_s end
end
fake = Object.new
fake.instance_eval do
def start() nil end
def running?() true end
def type() @type end
def to_s() type.to_s end
def inspect() to_s end
end
fake.instance_variable_set('@type', type)
fake
end

around(:each) do |example|
Timeout.timeout(2) { example.run }
end

before(:each) do
runner.stub(:spork_instances => {:rspec => rspec_instance, :cucumber => cucumber_instance})
runner.stub(:spork_instances => [rspec_instance, cucumber_instance])
runner.stub(:sleep)
end

context "with no type specified" do
it "outputs an info message" do
runner.stub(:spork_instances => {
"a" => fake_instance("one"),
"b" => fake_instance("two"),
"c" => fake_instance("three")
})
runner.stub(:spork_instances => [
fake_instance("one"),
fake_instance("two"),
fake_instance("three"),
])
Guard::UI.should_receive(:info).with("Kissing Spork for one, two, three", :reset => true)
runner.launch_sporks("kiss")
end
Expand Down Expand Up @@ -249,13 +255,13 @@ def mock.inspect() type.to_s end

context "with a type specified" do
it "outputs an info message" do
runner.stub(:spork_instances => {
"a" => fake_instance("one"),
"b" => fake_instance("two"),
"c" => fake_instance("three")
})
runner.stub(:spork_instances => [
fake_instance("one"),
fake_instance("two"),
fake_instance("three"),
])
Guard::UI.should_receive(:info).with("Kissing Spork for two", :reset => true)
runner.launch_sporks("kiss", "b")
runner.launch_sporks("kiss", "two")
end

it "starts the matching spork instance" do
Expand Down Expand Up @@ -291,7 +297,7 @@ def mock.inspect() type.to_s end
it "kills all alive spork instances" do
alive = double("alive instance", :alive? => true, :pid => 111)
dead = double("dead instance", :alive? => false, :pid => 222)
runner.stub(:spork_instances => {'a' => alive, 'b' => dead})
runner.stub(:spork_instances => [alive, dead])

Guard::UI.should_receive(:debug).with(/111/)
alive.should_receive(:kill)
Expand All @@ -303,9 +309,9 @@ def mock.inspect() type.to_s end

context "with a given type" do
it "kills the matching spork instance" do
matching = double("alive instance", :alive? => true, :pid => 111)
other = double("dead instance", :alive? => true, :pid => 222)
runner.stub(:spork_instances => {:matching => matching, :other => other})
matching = double("alive instance", :alive? => true, :pid => 111, :type => :matching)
other = double("dead instance", :alive? => true, :pid => 222, :type => :other)
runner.stub(:spork_instances => [matching, other])

Guard::UI.should_receive(:debug).with(/111/)
matching.should_receive(:kill)
Expand Down

0 comments on commit 44059e8

Please sign in to comment.