Permalink
Browse files

Cleaned up code and specs. Ready for a new release.

Signed-off-by: Rémy Coutable <rymai@rymai.me>
  • Loading branch information...
1 parent 1be98b4 commit 4af9541bc1093d3a00d3e89445ba39d756a92608 Rémy Coutable committed Apr 17, 2011
View
2 Guardfile
@@ -1,4 +1,4 @@
-guard 'rspec', :version => 2, :formatter => "instafail" do
+guard 'rspec', :version => 2, :cli => "-f doc" do
watch(%r{^spec/(.*)_spec.rb})
watch(%r{^lib/(.*).rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
View
2 guard-spork.gemspec
@@ -25,4 +25,4 @@ Gem::Specification.new do |s|
s.files = Dir.glob('{lib}/**/*') + %w[LICENSE README.rdoc]
s.require_path = 'lib'
-end
+end
View
5 lib/guard/spork.rb
@@ -7,10 +7,9 @@ class Spork < Guard
autoload :Runner, 'guard/spork/runner'
attr_accessor :runner
- def initialize(watchers = [], options = {})
+ def initialize(watchers=[], options={})
super
@runner = Runner.new(options)
-
end
def start
@@ -33,4 +32,4 @@ def stop
end
end
-end
+end
View
53 lib/guard/spork/runner.rb
@@ -5,50 +5,51 @@ class Spork
class Runner
attr_accessor :options
- def initialize(options = {})
+ def initialize(options={})
options[:wait] ||= 20 # seconds
options[:rspec_port] ||= 8989
options[:cucumber_port] ||= 8990
- options[:rspec_env]
- options[:cucumber_env]
- @options = options
+ options[:rspec_env] ||= nil
+ options[:cucumber_env] ||= nil
+ @options = options
@children = {}
Signal.trap('CHLD', self.method(:reap_children))
end
def launch_sporks(action)
UI.info "#{action.capitalize}ing Spork for #{sporked_gems} ", :reset => true
- spawn_child(spork_env("rspec"),spork_command("rspec")) if rspec?
- spawn_child(spork_env("cucumber"),spork_command("cucumber")) if cucumber?
+ spawn_child(options[:rspec_env], spork_command("rspec")) if rspec?
+ spawn_child(options[:cucumber_env], spork_command("cucumber")) if cucumber?
verify_launches(action)
end
def kill_sporks
- UI.debug "Killing sporks #{spork_pids.join(', ')}"
+ UI.debug "Killing Spork servers with PID: #{spork_pids.join(', ')}"
spork_pids.each { |pid| Process.kill("KILL", pid) }
end
private
- def spawn_child(env,cmd)
+
+ def spawn_child(env, cmd)
pid = fork
raise "Fork failed." if pid == -1
unless pid
ignore_control_signals
if env
if RUBY_VERSION > "1.9"
- exec(env,cmd)
+ exec(env, cmd)
else
- env.each {|key, value| ENV[key]=value }
- exec(cmd)
+ env.each { |key, value| ENV[key] = value }
+ exec(cmd)
end
else
exec(cmd)
end
end
- UI.debug "Spawned spork #{pid} ('#{cmd}')"
+ UI.debug "Spawned Spork server #{pid} ('#{cmd}')"
@children[pid] = cmd
pid
end
@@ -81,31 +82,19 @@ def terminated_children
end
stats
end
-
- def spork_env(type)
- case type
- when "rspec"
- options[:rspec_env]
- when "cucumber"
- options[:cucumber_env]
- end
- end
-
- def spork_command(type)
- cmd_parts = []
+ def spork_command(type)
+ cmd_parts = []
cmd_parts << "bundle exec" if bundler?
cmd_parts << "spork"
- case type
-
- when "rspec"
+ case type
+ when "rspec"
cmd_parts << "-p #{options[:rspec_port]}"
when "cucumber"
- cmd_parts << "cu"
- cmd_parts << "-p #{options[:cucumber_port]}"
+ cmd_parts << "cu -p #{options[:cucumber_port]}"
end
-
+
cmd_parts.join(" ")
end
@@ -118,12 +107,12 @@ def verify_launches(action)
rescue Errno::ECONNREFUSED
next
end
- UI.info "Spork for #{sporked_gems} successfully #{action}ed", :reset => true
+ UI.info "Spork server for #{sporked_gems} successfully #{action}ed", :reset => true
Notifier.notify "#{sporked_gems} successfully #{action}ed", :title => "Spork", :image => :success
return true
end
UI.reset_line # workaround before Guard::UI update
- UI.error "Could not #{action} Spork for #{sporked_gems}. Make sure you can use it manually first."
+ UI.error "Could not #{action} Spork server for #{sporked_gems}. Make sure you can use it manually first."
Notifier.notify "#{sporked_gems} NOT #{action}ed", :title => "Spork", :image => :failed
end
View
6 lib/guard/spork/templates/Guardfile
@@ -1,7 +1,7 @@
-guard 'spork', :cucumber_env => {'RAILS_ENV' => 'cucumber'}, :rspec_env => {'RAILS_ENV' => 'test'} do
+guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'cucumber' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
- watch(%r{^config/environments/.*\.rb$})
- watch(%r{^config/initializers/.*\.rb$})
+ watch(%r{^config/environments/.+\.rb$})
+ watch(%r{^config/initializers/.+\.rb$})
watch('spec/spec_helper.rb')
end
View
2 lib/guard/spork/version.rb
@@ -1,5 +1,5 @@
module Guard
module SporkVersion
- VERSION = "0.1.8.dev"
+ VERSION = "0.1.7"
end
end
View
212 spec/guard/spork/runner_spec.rb
@@ -1,152 +1,162 @@
require 'spec_helper'
describe Guard::Spork::Runner do
- subject { Guard::Spork::Runner.new(:wait => 1) }
+ subject { Guard::Spork::Runner.new }
+
+ describe "#initialize" do
+ it "default options are { :cucumber_port => 8990, :rspec_port => 8989, :wait => 20, :rspec_env => nil, :cucumber_env => nil }" do
+ subject.options.should == {
+ :cucumber_port => 8990,
+ :rspec_port => 8989,
+ :wait => 20,
+ :rspec_env => nil,
+ :cucumber_env => nil
+ }
+ end
+ end
- its(:options) { should == { :cucumber_port => 8990, :rspec_port => 8989, :wait => 1 } }
-
describe "#launch_sporks" do
-
before(:each) do
subject.stub(:spawn_child) { true }
Dir.stub(:pwd) { "" }
end
- context "with rspec" do
+ context "with RSpec only" do
before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
- File.should_receive(:exist?).any_number_of_times.with('/features') { false }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(false)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
end
- it "should launch rspec spork server" do
- subject.should_receive(:spawn_child).with(nil,"spork -p 8989")
+ it "launches Spork server for RSpec" do
+ subject.should_receive(:spawn_child).with(nil, "spork -p 8989")
subject.launch_sporks("start")
end
end
- context "with cucumber" do
+ context "with Cucumber only" do
before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { false }
- File.should_receive(:exist?).any_number_of_times.with('/features') { true }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(false)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
end
- it "should launch cucumber spork server" do
- subject.should_receive(:spawn_child).with(nil,"spork cu -p 8990")
+ it "launches Spork server for Cucumber" do
+ subject.should_receive(:spawn_child).with(nil, "spork cu -p 8990")
subject.launch_sporks("start")
end
end
- context "with rspec & cucumber" do
+ context "with RSpec & Cucumber" do
before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
- File.should_receive(:exist?).any_number_of_times.with('/features') { true }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
end
- it "should launch rspec & cucumber spork server" do
- subject.should_receive(:spawn_child).with(nil,"spork -p 8989")
- subject.should_receive(:spawn_child).with(nil,"spork cu -p 8990")
+ it "launches Spork servers for RSpec & Cucumber" do
+ subject.should_receive(:spawn_child).with(nil, "spork -p 8989")
+ subject.should_receive(:spawn_child).with(nil, "spork cu -p 8990")
subject.launch_sporks("start")
end
end
- context "with rspec, cucumber & bundler" do
+ context "with RSpec, Cucumber & Bundler" do
before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
- File.should_receive(:exist?).any_number_of_times.with('/features') { true }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { true }
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(true)
TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
end
- it "should launch rspec & cucumber spork server" do
- subject.should_receive(:spawn_child).with(nil,"bundle exec spork -p 8989")
- subject.should_receive(:spawn_child).with(nil,"bundle exec spork cu -p 8990")
+ it "launches Spork servers for RSpec & Cucumber with 'bundle exec'" do
+ subject.should_receive(:spawn_child).with(nil, "bundle exec spork -p 8989")
+ subject.should_receive(:spawn_child).with(nil, "bundle exec spork cu -p 8990")
subject.launch_sporks("start")
end
end
- end
-
- describe "#launch_sporks with env" do
- before(:each) do
+ describe ":rspec_env & :cucumber_env options" do
+ before(:each) do
subject.options = {:cucumber_port => 8990, :rspec_port => 8989, :wait => 1, :cucumber_env => {'RAILS_ENV' => 'cucumber'}, :rspec_env => {'RAILS_ENV' => 'test'}}
subject.stub(:spawn_child) { true }
Dir.stub(:pwd) { "" }
- end
-
- context "with rspec" do
- before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
- File.should_receive(:exist?).any_number_of_times.with('/features') { false }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
- TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
- end
-
- it "should launch rspec spork server" do
- subject.should_receive(:spawn_child).with({"RAILS_ENV"=>"test"},"spork -p 8989")
- subject.launch_sporks("start")
- end
- end
-
- context "with cucumber" do
- before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { false }
- File.should_receive(:exist?).any_number_of_times.with('/features') { true }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
- TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
- end
-
- it "should launch cucumber spork server" do
- subject.should_receive(:spawn_child).with({'RAILS_ENV' => 'cucumber'},"spork cu -p 8990")
- subject.launch_sporks("start")
- end
- end
-
- context "with rspec & cucumber" do
- before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
- File.should_receive(:exist?).any_number_of_times.with('/features') { true }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
- TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
- TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
- end
-
- it "should launch rspec & cucumber spork server" do
- subject.should_receive(:spawn_child).with({"RAILS_ENV"=>"test"},"spork -p 8989")
- subject.should_receive(:spawn_child).with({'RAILS_ENV' => 'cucumber'},"spork cu -p 8990")
- subject.launch_sporks("start")
- end
- end
-
- context "with rspec, cucumber & bundler" do
- before(:each) do
- File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
- File.should_receive(:exist?).any_number_of_times.with('/features') { true }
- File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { true }
- TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
- TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
- end
-
- it "should launch rspec & cucumber spork server" do
- subject.should_receive(:spawn_child).with({"RAILS_ENV"=>"test"},"bundle exec spork -p 8989")
- subject.should_receive(:spawn_child).with({'RAILS_ENV' => 'cucumber'},"bundle exec spork cu -p 8990")
- subject.launch_sporks("start")
- end
- end
-
- end
-
+ end
+
+ context "with RSpec only" do
+ before(:each) do
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(false)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
+ TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
+ end
+
+ it "launches Spork server for RSpec" do
+ subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'test' }, "spork -p 8989")
+ subject.launch_sporks("start")
+ end
+ end
+
+ context "with Cucumber only" do
+ before(:each) do
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(false)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
+ TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
+ end
+
+ it "launches Spork server for Cucumber" do
+ subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'cucumber' }, "spork cu -p 8990")
+ subject.launch_sporks("start")
+ end
+ end
+
+ context "with RSpec & Cucumber" do
+ before(:each) do
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
+ TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
+ TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
+ end
+
+ it "launches Spork servers for RSpec & Cucumber" do
+ subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'test' }, "spork -p 8989")
+ subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'cucumber' }, "spork cu -p 8990")
+ subject.launch_sporks("start")
+ end
+ end
+
+ context "with RSpec, Cucumber & Bundler" do
+ before(:each) do
+ File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
+ File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(true)
+ TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
+ TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
+ end
+
+ it "launches Spork servers for RSpec & Cucumber with 'bundle exec'" do
+ subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'test' }, "bundle exec spork -p 8989")
+ subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'cucumber' }, "bundle exec spork cu -p 8990")
+ subject.launch_sporks("start")
+ end
+ end
+
+ end
+ end
+
describe "#kill_sporks" do
- it "should kill command" do
- subject.should_receive(:spork_pids).twice { [999] }
- Process.should_receive(:kill).with("KILL",999)
+ it "calls a KILL command for each Spork server" do
+ subject.should_receive(:spork_pids).twice.and_return([666, 999])
+ Guard::UI.should_receive(:debug).with("Killing Spork servers with PID: 666, 999")
+ Process.should_receive(:kill).with("KILL", 666)
+ Process.should_receive(:kill).with("KILL", 999)
subject.kill_sporks
end
end
View
18 spec/guard/spork_spec.rb
@@ -4,38 +4,38 @@
subject { Guard::Spork.new }
describe '#initialize' do
- it 'should instanciate runner with option' do
+ it "instantiates Runner with the given options" do
Guard::Spork::Runner.should_receive(:new).with(:bundler => false)
Guard::Spork.new [], { :bundler => false }
end
end
- describe "start" do
- it "should start sporks" do
+ describe "#start" do
+ it "calls Runner#kill_sporks and Runner#launch_sporks with 'start'" do
subject.runner.should_receive(:kill_sporks)
subject.runner.should_receive(:launch_sporks).with("start")
subject.start
end
end
- describe "reload" do
- it "should kill & relaund sporks'" do
+ describe "#reload" do
+ it "calls Runner#kill_sporks and Runner#launch_sporks with 'reload'" do
subject.runner.should_receive(:kill_sporks)
subject.runner.should_receive(:launch_sporks).with("reload")
subject.reload
end
end
- describe "run_on_change" do
- it "should kill & relaund sporks'" do
+ describe "#run_on_change" do
+ it "calls Runner#kill_sporks and Runner#launch_sporks with 'reload'" do
subject.runner.should_receive(:kill_sporks)
subject.runner.should_receive(:launch_sporks).with("reload")
subject.run_on_change(["spec/spec_helper.rb"])
end
end
- describe "start" do
- it "should kill sporks'" do
+ describe "#stop" do
+ it "calls Runner#kill_sporks" do
subject.runner.should_receive(:kill_sporks)
subject.stop
end
View
14 spec/spec_helper.rb
@@ -1,18 +1,10 @@
require 'rspec'
require 'guard/spork'
+ENV["GUARD_ENV"] = 'test'
+
RSpec.configure do |config|
config.color_enabled = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
-
- config.before(:each) do
- ENV["GUARD_ENV"] = 'test'
- @lib_path = Pathname.new(File.expand_path('../../lib/', __FILE__))
- end
-
- config.after(:each) do
- ENV["GUARD_ENV"] = nil
- end
-
-end
+end

0 comments on commit 4af9541

Please sign in to comment.