Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

Signed-off-by: Rémy Coutable <rymai@rymai.me>
  • Loading branch information...
commit 4af9541bc1093d3a00d3e89445ba39d756a92608 1 parent 1be98b4
Rémy Coutable rymai authored
2  Guardfile
... ... @@ -1,4 +1,4 @@
1   -guard 'rspec', :version => 2, :formatter => "instafail" do
  1 +guard 'rspec', :version => 2, :cli => "-f doc" do
2 2 watch(%r{^spec/(.*)_spec.rb})
3 3 watch(%r{^lib/(.*).rb}) { |m| "spec/#{m[1]}_spec.rb" }
4 4 watch('spec/spec_helper.rb') { "spec" }
2  guard-spork.gemspec
@@ -25,4 +25,4 @@ Gem::Specification.new do |s|
25 25
26 26 s.files = Dir.glob('{lib}/**/*') + %w[LICENSE README.rdoc]
27 27 s.require_path = 'lib'
28   -end
  28 +end
5 lib/guard/spork.rb
@@ -7,10 +7,9 @@ class Spork < Guard
7 7 autoload :Runner, 'guard/spork/runner'
8 8 attr_accessor :runner
9 9
10   - def initialize(watchers = [], options = {})
  10 + def initialize(watchers=[], options={})
11 11 super
12 12 @runner = Runner.new(options)
13   -
14 13 end
15 14
16 15 def start
@@ -33,4 +32,4 @@ def stop
33 32 end
34 33
35 34 end
36   -end
  35 +end
53 lib/guard/spork/runner.rb
@@ -5,13 +5,13 @@ class Spork
5 5 class Runner
6 6 attr_accessor :options
7 7
8   - def initialize(options = {})
  8 + def initialize(options={})
9 9 options[:wait] ||= 20 # seconds
10 10 options[:rspec_port] ||= 8989
11 11 options[:cucumber_port] ||= 8990
12   - options[:rspec_env]
13   - options[:cucumber_env]
14   - @options = options
  12 + options[:rspec_env] ||= nil
  13 + options[:cucumber_env] ||= nil
  14 + @options = options
15 15 @children = {}
16 16
17 17 Signal.trap('CHLD', self.method(:reap_children))
@@ -19,18 +19,19 @@ def initialize(options = {})
19 19
20 20 def launch_sporks(action)
21 21 UI.info "#{action.capitalize}ing Spork for #{sporked_gems} ", :reset => true
22   - spawn_child(spork_env("rspec"),spork_command("rspec")) if rspec?
23   - spawn_child(spork_env("cucumber"),spork_command("cucumber")) if cucumber?
  22 + spawn_child(options[:rspec_env], spork_command("rspec")) if rspec?
  23 + spawn_child(options[:cucumber_env], spork_command("cucumber")) if cucumber?
24 24 verify_launches(action)
25 25 end
26 26
27 27 def kill_sporks
28   - UI.debug "Killing sporks #{spork_pids.join(', ')}"
  28 + UI.debug "Killing Spork servers with PID: #{spork_pids.join(', ')}"
29 29 spork_pids.each { |pid| Process.kill("KILL", pid) }
30 30 end
31 31
32 32 private
33   - def spawn_child(env,cmd)
  33 +
  34 + def spawn_child(env, cmd)
34 35 pid = fork
35 36 raise "Fork failed." if pid == -1
36 37
@@ -38,17 +39,17 @@ def spawn_child(env,cmd)
38 39 ignore_control_signals
39 40 if env
40 41 if RUBY_VERSION > "1.9"
41   - exec(env,cmd)
  42 + exec(env, cmd)
42 43 else
43   - env.each {|key, value| ENV[key]=value }
44   - exec(cmd)
  44 + env.each { |key, value| ENV[key] = value }
  45 + exec(cmd)
45 46 end
46 47 else
47 48 exec(cmd)
48 49 end
49 50 end
50 51
51   - UI.debug "Spawned spork #{pid} ('#{cmd}')"
  52 + UI.debug "Spawned Spork server #{pid} ('#{cmd}')"
52 53 @children[pid] = cmd
53 54 pid
54 55 end
@@ -81,31 +82,19 @@ def terminated_children
81 82 end
82 83 stats
83 84 end
84   -
85   - def spork_env(type)
86   - case type
87   - when "rspec"
88   - options[:rspec_env]
89   - when "cucumber"
90   - options[:cucumber_env]
91   - end
92   - end
93   -
94   - def spork_command(type)
95 85
96   - cmd_parts = []
  86 + def spork_command(type)
  87 + cmd_parts = []
97 88 cmd_parts << "bundle exec" if bundler?
98 89 cmd_parts << "spork"
99 90
100   - case type
101   -
102   - when "rspec"
  91 + case type
  92 + when "rspec"
103 93 cmd_parts << "-p #{options[:rspec_port]}"
104 94 when "cucumber"
105   - cmd_parts << "cu"
106   - cmd_parts << "-p #{options[:cucumber_port]}"
  95 + cmd_parts << "cu -p #{options[:cucumber_port]}"
107 96 end
108   -
  97 +
109 98 cmd_parts.join(" ")
110 99 end
111 100
@@ -118,12 +107,12 @@ def verify_launches(action)
118 107 rescue Errno::ECONNREFUSED
119 108 next
120 109 end
121   - UI.info "Spork for #{sporked_gems} successfully #{action}ed", :reset => true
  110 + UI.info "Spork server for #{sporked_gems} successfully #{action}ed", :reset => true
122 111 Notifier.notify "#{sporked_gems} successfully #{action}ed", :title => "Spork", :image => :success
123 112 return true
124 113 end
125 114 UI.reset_line # workaround before Guard::UI update
126   - UI.error "Could not #{action} Spork for #{sporked_gems}. Make sure you can use it manually first."
  115 + UI.error "Could not #{action} Spork server for #{sporked_gems}. Make sure you can use it manually first."
127 116 Notifier.notify "#{sporked_gems} NOT #{action}ed", :title => "Spork", :image => :failed
128 117 end
129 118
6 lib/guard/spork/templates/Guardfile
... ... @@ -1,7 +1,7 @@
1   -guard 'spork', :cucumber_env => {'RAILS_ENV' => 'cucumber'}, :rspec_env => {'RAILS_ENV' => 'test'} do
  1 +guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'cucumber' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
2 2 watch('config/application.rb')
3 3 watch('config/environment.rb')
4   - watch(%r{^config/environments/.*\.rb$})
5   - watch(%r{^config/initializers/.*\.rb$})
  4 + watch(%r{^config/environments/.+\.rb$})
  5 + watch(%r{^config/initializers/.+\.rb$})
6 6 watch('spec/spec_helper.rb')
7 7 end
2  lib/guard/spork/version.rb
... ... @@ -1,5 +1,5 @@
1 1 module Guard
2 2 module SporkVersion
3   - VERSION = "0.1.8.dev"
  3 + VERSION = "0.1.7"
4 4 end
5 5 end
212 spec/guard/spork/runner_spec.rb
... ... @@ -1,152 +1,162 @@
1 1 require 'spec_helper'
2 2
3 3 describe Guard::Spork::Runner do
4   - subject { Guard::Spork::Runner.new(:wait => 1) }
  4 + subject { Guard::Spork::Runner.new }
  5 +
  6 + describe "#initialize" do
  7 + it "default options are { :cucumber_port => 8990, :rspec_port => 8989, :wait => 20, :rspec_env => nil, :cucumber_env => nil }" do
  8 + subject.options.should == {
  9 + :cucumber_port => 8990,
  10 + :rspec_port => 8989,
  11 + :wait => 20,
  12 + :rspec_env => nil,
  13 + :cucumber_env => nil
  14 + }
  15 + end
  16 + end
5 17
6   - its(:options) { should == { :cucumber_port => 8990, :rspec_port => 8989, :wait => 1 } }
7   -
8 18 describe "#launch_sporks" do
9   -
10 19 before(:each) do
11 20 subject.stub(:spawn_child) { true }
12 21 Dir.stub(:pwd) { "" }
13 22 end
14 23
15   - context "with rspec" do
  24 + context "with RSpec only" do
16 25 before(:each) do
17   - File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
18   - File.should_receive(:exist?).any_number_of_times.with('/features') { false }
19   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
  26 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
  27 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(false)
  28 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
20 29 TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
21 30 end
22 31
23   - it "should launch rspec spork server" do
24   - subject.should_receive(:spawn_child).with(nil,"spork -p 8989")
  32 + it "launches Spork server for RSpec" do
  33 + subject.should_receive(:spawn_child).with(nil, "spork -p 8989")
25 34 subject.launch_sporks("start")
26 35 end
27 36 end
28 37
29   - context "with cucumber" do
  38 + context "with Cucumber only" do
30 39 before(:each) do
31   - File.should_receive(:exist?).any_number_of_times.with('/spec') { false }
32   - File.should_receive(:exist?).any_number_of_times.with('/features') { true }
33   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
  40 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(false)
  41 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
  42 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
34 43 TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
35 44 end
36 45
37   - it "should launch cucumber spork server" do
38   - subject.should_receive(:spawn_child).with(nil,"spork cu -p 8990")
  46 + it "launches Spork server for Cucumber" do
  47 + subject.should_receive(:spawn_child).with(nil, "spork cu -p 8990")
39 48 subject.launch_sporks("start")
40 49 end
41 50 end
42 51
43   - context "with rspec & cucumber" do
  52 + context "with RSpec & Cucumber" do
44 53 before(:each) do
45   - File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
46   - File.should_receive(:exist?).any_number_of_times.with('/features') { true }
47   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
  54 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
  55 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
  56 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
48 57 TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
49 58 TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
50 59 end
51 60
52   - it "should launch rspec & cucumber spork server" do
53   - subject.should_receive(:spawn_child).with(nil,"spork -p 8989")
54   - subject.should_receive(:spawn_child).with(nil,"spork cu -p 8990")
  61 + it "launches Spork servers for RSpec & Cucumber" do
  62 + subject.should_receive(:spawn_child).with(nil, "spork -p 8989")
  63 + subject.should_receive(:spawn_child).with(nil, "spork cu -p 8990")
55 64 subject.launch_sporks("start")
56 65 end
57 66 end
58 67
59   - context "with rspec, cucumber & bundler" do
  68 + context "with RSpec, Cucumber & Bundler" do
60 69 before(:each) do
61   - File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
62   - File.should_receive(:exist?).any_number_of_times.with('/features') { true }
63   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { true }
  70 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
  71 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
  72 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(true)
64 73 TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
65 74 TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
66 75 end
67 76
68   - it "should launch rspec & cucumber spork server" do
69   - subject.should_receive(:spawn_child).with(nil,"bundle exec spork -p 8989")
70   - subject.should_receive(:spawn_child).with(nil,"bundle exec spork cu -p 8990")
  77 + it "launches Spork servers for RSpec & Cucumber with 'bundle exec'" do
  78 + subject.should_receive(:spawn_child).with(nil, "bundle exec spork -p 8989")
  79 + subject.should_receive(:spawn_child).with(nil, "bundle exec spork cu -p 8990")
71 80 subject.launch_sporks("start")
72 81 end
73 82 end
74 83
75   - end
76   -
77   - describe "#launch_sporks with env" do
78   - before(:each) do
  84 + describe ":rspec_env & :cucumber_env options" do
  85 + before(:each) do
79 86 subject.options = {:cucumber_port => 8990, :rspec_port => 8989, :wait => 1, :cucumber_env => {'RAILS_ENV' => 'cucumber'}, :rspec_env => {'RAILS_ENV' => 'test'}}
80 87 subject.stub(:spawn_child) { true }
81 88 Dir.stub(:pwd) { "" }
82   - end
83   -
84   - context "with rspec" do
85   - before(:each) do
86   - File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
87   - File.should_receive(:exist?).any_number_of_times.with('/features') { false }
88   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
89   - TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
90   - end
91   -
92   - it "should launch rspec spork server" do
93   - subject.should_receive(:spawn_child).with({"RAILS_ENV"=>"test"},"spork -p 8989")
94   - subject.launch_sporks("start")
95   - end
96   - end
97   -
98   - context "with cucumber" do
99   - before(:each) do
100   - File.should_receive(:exist?).any_number_of_times.with('/spec') { false }
101   - File.should_receive(:exist?).any_number_of_times.with('/features') { true }
102   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
103   - TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
104   - end
105   -
106   - it "should launch cucumber spork server" do
107   - subject.should_receive(:spawn_child).with({'RAILS_ENV' => 'cucumber'},"spork cu -p 8990")
108   - subject.launch_sporks("start")
109   - end
110   - end
111   -
112   - context "with rspec & cucumber" do
113   - before(:each) do
114   - File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
115   - File.should_receive(:exist?).any_number_of_times.with('/features') { true }
116   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { false }
117   - TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
118   - TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
119   - end
120   -
121   - it "should launch rspec & cucumber spork server" do
122   - subject.should_receive(:spawn_child).with({"RAILS_ENV"=>"test"},"spork -p 8989")
123   - subject.should_receive(:spawn_child).with({'RAILS_ENV' => 'cucumber'},"spork cu -p 8990")
124   - subject.launch_sporks("start")
125   - end
126   - end
127   -
128   - context "with rspec, cucumber & bundler" do
129   - before(:each) do
130   - File.should_receive(:exist?).any_number_of_times.with('/spec') { true }
131   - File.should_receive(:exist?).any_number_of_times.with('/features') { true }
132   - File.should_receive(:exist?).any_number_of_times.with('/Gemfile') { true }
133   - TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
134   - TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
135   - end
136   -
137   - it "should launch rspec & cucumber spork server" do
138   - subject.should_receive(:spawn_child).with({"RAILS_ENV"=>"test"},"bundle exec spork -p 8989")
139   - subject.should_receive(:spawn_child).with({'RAILS_ENV' => 'cucumber'},"bundle exec spork cu -p 8990")
140   - subject.launch_sporks("start")
141   - end
142   - end
143   -
144   - end
145   -
  89 + end
  90 +
  91 + context "with RSpec only" do
  92 + before(:each) do
  93 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
  94 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(false)
  95 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
  96 + TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
  97 + end
  98 +
  99 + it "launches Spork server for RSpec" do
  100 + subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'test' }, "spork -p 8989")
  101 + subject.launch_sporks("start")
  102 + end
  103 + end
  104 +
  105 + context "with Cucumber only" do
  106 + before(:each) do
  107 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(false)
  108 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
  109 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
  110 + TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
  111 + end
  112 +
  113 + it "launches Spork server for Cucumber" do
  114 + subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'cucumber' }, "spork cu -p 8990")
  115 + subject.launch_sporks("start")
  116 + end
  117 + end
  118 +
  119 + context "with RSpec & Cucumber" do
  120 + before(:each) do
  121 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
  122 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
  123 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(false)
  124 + TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
  125 + TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
  126 + end
  127 +
  128 + it "launches Spork servers for RSpec & Cucumber" do
  129 + subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'test' }, "spork -p 8989")
  130 + subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'cucumber' }, "spork cu -p 8990")
  131 + subject.launch_sporks("start")
  132 + end
  133 + end
  134 +
  135 + context "with RSpec, Cucumber & Bundler" do
  136 + before(:each) do
  137 + File.should_receive(:exist?).any_number_of_times.with('/spec').and_return(true)
  138 + File.should_receive(:exist?).any_number_of_times.with('/features').and_return(true)
  139 + File.should_receive(:exist?).any_number_of_times.with('/Gemfile').and_return(true)
  140 + TCPSocket.should_receive(:new).with('localhost', 8989) { socket_mock }
  141 + TCPSocket.should_receive(:new).with('localhost', 8990) { socket_mock }
  142 + end
  143 +
  144 + it "launches Spork servers for RSpec & Cucumber with 'bundle exec'" do
  145 + subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'test' }, "bundle exec spork -p 8989")
  146 + subject.should_receive(:spawn_child).with({ 'RAILS_ENV' => 'cucumber' }, "bundle exec spork cu -p 8990")
  147 + subject.launch_sporks("start")
  148 + end
  149 + end
  150 +
  151 + end
  152 + end
  153 +
146 154 describe "#kill_sporks" do
147   - it "should kill command" do
148   - subject.should_receive(:spork_pids).twice { [999] }
149   - Process.should_receive(:kill).with("KILL",999)
  155 + it "calls a KILL command for each Spork server" do
  156 + subject.should_receive(:spork_pids).twice.and_return([666, 999])
  157 + Guard::UI.should_receive(:debug).with("Killing Spork servers with PID: 666, 999")
  158 + Process.should_receive(:kill).with("KILL", 666)
  159 + Process.should_receive(:kill).with("KILL", 999)
150 160 subject.kill_sporks
151 161 end
152 162 end
18 spec/guard/spork_spec.rb
@@ -4,38 +4,38 @@
4 4 subject { Guard::Spork.new }
5 5
6 6 describe '#initialize' do
7   - it 'should instanciate runner with option' do
  7 + it "instantiates Runner with the given options" do
8 8 Guard::Spork::Runner.should_receive(:new).with(:bundler => false)
9 9 Guard::Spork.new [], { :bundler => false }
10 10 end
11 11 end
12 12
13   - describe "start" do
14   - it "should start sporks" do
  13 + describe "#start" do
  14 + it "calls Runner#kill_sporks and Runner#launch_sporks with 'start'" do
15 15 subject.runner.should_receive(:kill_sporks)
16 16 subject.runner.should_receive(:launch_sporks).with("start")
17 17 subject.start
18 18 end
19 19 end
20 20
21   - describe "reload" do
22   - it "should kill & relaund sporks'" do
  21 + describe "#reload" do
  22 + it "calls Runner#kill_sporks and Runner#launch_sporks with 'reload'" do
23 23 subject.runner.should_receive(:kill_sporks)
24 24 subject.runner.should_receive(:launch_sporks).with("reload")
25 25 subject.reload
26 26 end
27 27 end
28 28
29   - describe "run_on_change" do
30   - it "should kill & relaund sporks'" do
  29 + describe "#run_on_change" do
  30 + it "calls Runner#kill_sporks and Runner#launch_sporks with 'reload'" do
31 31 subject.runner.should_receive(:kill_sporks)
32 32 subject.runner.should_receive(:launch_sporks).with("reload")
33 33 subject.run_on_change(["spec/spec_helper.rb"])
34 34 end
35 35 end
36 36
37   - describe "start" do
38   - it "should kill sporks'" do
  37 + describe "#stop" do
  38 + it "calls Runner#kill_sporks" do
39 39 subject.runner.should_receive(:kill_sporks)
40 40 subject.stop
41 41 end
14 spec/spec_helper.rb
... ... @@ -1,18 +1,10 @@
1 1 require 'rspec'
2 2 require 'guard/spork'
3 3
  4 +ENV["GUARD_ENV"] = 'test'
  5 +
4 6 RSpec.configure do |config|
5 7 config.color_enabled = true
6 8 config.filter_run :focus => true
7 9 config.run_all_when_everything_filtered = true
8   -
9   - config.before(:each) do
10   - ENV["GUARD_ENV"] = 'test'
11   - @lib_path = Pathname.new(File.expand_path('../../lib/', __FILE__))
12   - end
13   -
14   - config.after(:each) do
15   - ENV["GUARD_ENV"] = nil
16   - end
17   -
18   -end
  10 +end

0 comments on commit 4af9541

Please sign in to comment.
Something went wrong with that request. Please try again.