spork 0.4.2 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/spork.rb CHANGED
@@ -1,17 +1,23 @@
1
1
  $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
2
2
  module Spork
3
- SPEC_HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")
4
-
5
3
  class << self
4
+ def already_preforked
5
+ @already_preforked ||= []
6
+ end
7
+
8
+ def already_run
9
+ @already_run ||= []
10
+ end
11
+
6
12
  def prefork(&block)
7
- return if @already_preforked
8
- @already_preforked = true
13
+ return if already_preforked.include?(expanded_caller(caller.first))
14
+ already_preforked << expanded_caller(caller.first)
9
15
  yield
10
16
  end
11
17
 
12
18
  def each_run(&block)
13
- return if @state == :preforking || (@state != :not_using_spork && @already_run)
14
- @already_run = true
19
+ return if @state == :preforking || (@state != :not_using_spork && already_run.include?(expanded_caller(caller.first)))
20
+ already_run << expanded_caller(caller.first)
15
21
  yield
16
22
  end
17
23
 
@@ -36,5 +42,11 @@ module Spork
36
42
  running!
37
43
  load(helper_file)
38
44
  end
45
+
46
+ def expanded_caller(caller_line)
47
+ file, line = caller_line.split(":")
48
+ line.gsub(/:.+/, '')
49
+ File.expand_path(Dir.pwd, file) + ":" + line
50
+ end
39
51
  end
40
52
  end
@@ -0,0 +1,54 @@
1
+ class Spork::Forker
2
+ class ForkDiedException < Exception; end
3
+ def initialize(&block)
4
+ return unless block_given?
5
+ @child_io, @server_io = UNIXSocket.socketpair
6
+ @child_pid = Kernel.fork do
7
+ @server_io.close
8
+ Marshal.dump(yield, @child_io)
9
+ # wait for the parent to acknowledge receipt of the result.
10
+ master_response =
11
+ begin
12
+ Marshal.load(@child_io)
13
+ rescue EOFError
14
+ nil
15
+ end
16
+
17
+ # terminate, skipping any at_exit blocks.
18
+ exit!(0)
19
+ end
20
+ @child_io.close
21
+ end
22
+
23
+ def result
24
+ return unless running?
25
+ result_thread = Thread.new do
26
+ begin
27
+ @result = Marshal.load(@server_io)
28
+ Marshal.dump('ACK', @server_io)
29
+ rescue ForkDiedException
30
+ @result = nil
31
+ end
32
+ end
33
+ Process.wait(@child_pid)
34
+ result_thread.raise(ForkDiedException) if @result.nil?
35
+ @child_pid = nil
36
+ @result
37
+ end
38
+
39
+ def abort
40
+ if running?
41
+ Process.kill(Signal.list['TERM'], @child_pid)
42
+ @child_pid = nil
43
+ true
44
+ end
45
+ end
46
+
47
+ def running?
48
+ return false unless @child_pid
49
+ Process.getpgid(@child_pid)
50
+ true
51
+ rescue Errno::ESRCH
52
+ false
53
+ end
54
+ end
data/lib/spork/server.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'drb/drb'
2
2
  require 'rbconfig'
3
+ require 'spork/forker.rb'
3
4
 
4
5
  # This is based off of spec_server.rb from rspec-rails (David Chelimsky), which was based on Florian Weber's TDDMate
5
6
  class Spork::Server
@@ -93,19 +94,16 @@ class Spork::Server
93
94
 
94
95
  def run(argv, stderr, stdout)
95
96
  return false if running?
96
- $stdout = stdout
97
- $stderr = stderr
98
- @child_pid = Kernel.fork do
97
+ @child = ::Spork::Forker.new do
98
+ $stdout, $stderr = stdout, stderr
99
99
  Spork.exec_each_run(helper_file)
100
100
  run_tests(argv, stderr, stdout)
101
101
  end
102
- Process.wait(@child_pid)
103
- @child_pid = nil
104
- true
102
+ @child.result
105
103
  end
106
104
 
107
105
  def running?
108
- !! @child_pid
106
+ @child && @child.running?
109
107
  end
110
108
 
111
109
  private
@@ -140,16 +138,13 @@ class Spork::Server
140
138
  end
141
139
 
142
140
  def abort
143
- if running?
144
- Process.kill(Signal.list['TERM'], @child_pid)
145
- true
146
- end
141
+ @child && @child.abort
147
142
  end
148
143
 
149
144
  def sig_int_received
150
145
  if running?
151
146
  abort
152
- puts "Running specs stopped. Press CTRL-C again to stop the server."
147
+ puts "Running tests stopped. Press CTRL-C again to stop the server."
153
148
  else
154
149
  exit!(0)
155
150
  end
@@ -2,23 +2,27 @@ class Spork::Server::Cucumber < Spork::Server
2
2
  CUCUMBER_PORT = 8990
3
3
  CUCUMBER_HELPER_FILE = File.join(Dir.pwd, "features/support/env.rb")
4
4
 
5
- def self.port
6
- CUCUMBER_PORT
7
- end
8
-
9
- def self.helper_file
10
- CUCUMBER_HELPER_FILE
5
+ class << self
6
+ def port
7
+ CUCUMBER_PORT
8
+ end
9
+
10
+ def helper_file
11
+ CUCUMBER_HELPER_FILE
12
+ end
13
+
14
+ attr_accessor :step_mother
11
15
  end
12
16
 
13
- def self.step_mother=(value)
14
- @step_mother = value
17
+ def step_mother
18
+ self.class.step_mother
15
19
  end
16
20
 
17
21
  def run_tests(argv, stderr, stdout)
18
22
  require 'cucumber/cli/main'
19
- ::Cucumber::Cli::Main.step_mother = @step_mother
20
- ::Cucumber::Cli::Main.new(argv, stderr, stdout).execute!(@step_mother)
23
+ ::Cucumber::Cli::Main.step_mother = step_mother
24
+ ::Cucumber::Cli::Main.new(argv, stdout, stderr).execute!(step_mother)
21
25
  end
22
26
  end
23
27
 
24
- Spork::Server::Cucumber.step_mother = self
28
+ Spork::Server::Cucumber.step_mother = self
data/spec/spec_helper.rb CHANGED
@@ -1,32 +1,3 @@
1
- require 'rubygems'
2
- require 'spork'
3
-
4
- Spork.prefork do
5
- # Loading more in this block will cause your specs to run faster. However,
6
- # if you change any configuration or code from libraries loaded here, you'll
7
- # need to restart spork for it take effect.
8
-
9
- end
10
-
11
- Spork.each_run do
12
- # This code will be run each time you run your specs.
13
-
14
- end
15
-
16
- # --- Instructions ---
17
- # - Sort through your spec_helper file. Place as much environment loading
18
- # code that you don't normally modify during development in the
19
- # Spork.prefork block.
20
- # - Place the rest under Spork.each_run block
21
- # - Any code that is left outside of the blocks will be ran during preforking
22
- # and during each_run!
23
- # - These instructions should self-destruct in 10 seconds. If they don't,
24
- # feel free to delete them.
25
- #
26
-
27
-
28
-
29
-
30
1
  require 'rubygems'
31
2
  require 'spec'
32
3
 
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Spork::Forker do
4
+ it "runs a block in a fork" do
5
+ @var = "hello world"
6
+ Spork::Forker.new { @var = "booyah" }.result
7
+ @var.should == "hello world"
8
+ end
9
+
10
+ it "returns the result" do
11
+ Spork::Forker.new { "results" }.result.should == "results"
12
+ end
13
+
14
+ it "reports when the fork is running" do
15
+ forker = Spork::Forker.new { sleep 0.1 }
16
+ forker.running?.should == true
17
+ forker.result
18
+ sleep 0.1
19
+ forker.running?.should == false
20
+ end
21
+
22
+ it "aborts a fork and returns nil for the result" do
23
+ started_at = Time.now
24
+ ended_at = nil
25
+ forker = Spork::Forker.new { sleep 5 }
26
+ Thread.new do
27
+ forker.result.should == nil
28
+ ended_at = Time.now
29
+ end
30
+ sleep 0.5
31
+ forker.abort
32
+ sleep 0.1
33
+ (ended_at - started_at).should be_close(0.5, 0.1)
34
+ forker.running?.should == false
35
+ end
36
+ end
@@ -2,10 +2,14 @@ require File.dirname(__FILE__) + '/../../spec_helper'
2
2
 
3
3
  describe Spork::Server::RSpec do
4
4
  before(:each) do
5
- @adapter = Spork::Adapter::RSpec.new
5
+ @server = Spork::Server::RSpec.new
6
6
  end
7
7
 
8
8
  it "uses the RSPEC_PORT for it's port" do
9
- @adapter.port.should == Spork::Adapter::RSpec::RSPEC_PORT
9
+ @server.port.should == Spork::Server::RSpec::RSPEC_PORT
10
+ end
11
+
12
+ it "uses the RSPEC_HELPER_FILE for it's helper_file" do
13
+ @server.helper_file.should == Spork::Server::RSpec::RSPEC_HELPER_FILE
10
14
  end
11
15
  end
@@ -20,6 +20,7 @@ class FakeServer < Spork::Server
20
20
 
21
21
  def run_tests(argv, input, output)
22
22
  sleep(@wait_time || 0.5)
23
+ true
23
24
  end
24
25
  end
25
26
 
@@ -119,8 +120,14 @@ describe Spork::Server do
119
120
  sleep(0.05)
120
121
  @fake.send(:abort)
121
122
  sleep(0.01) while @fake.running?
122
-
123
+
123
124
  (Time.now - started_at).should < @fake.wait_time
124
125
  end
126
+
127
+ it "returns the result of the run_tests method from the forked child" do
128
+ create_helper_file
129
+ @fake.stub!(:run_tests).and_return("tests were ran")
130
+ @fake.run("test", STDOUT, STDIN).should == "tests were ran"
131
+ end
125
132
  end
126
133
  end
data/spec/spork_spec.rb CHANGED
@@ -41,4 +41,15 @@ describe Spork do
41
41
  it "runs both blocks when Spork not activated" do
42
42
  spec_helper_simulator.should == [:prefork, :each_run]
43
43
  end
44
+
45
+ it "prevents blocks from being ran twice" do
46
+ spec_helper_simulator.should == [:prefork, :each_run]
47
+ spec_helper_simulator.should == []
48
+ end
49
+
50
+ it "runs multiple prefork and each_run blocks at different locations" do
51
+ Spork.prefork { }
52
+ Spork.each_run { }
53
+ spec_helper_simulator.should == [:prefork, :each_run]
54
+ end
44
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spork
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Harper
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-30 00:00:00 -06:00
12
+ date: 2009-06-01 00:00:00 -06:00
13
13
  default_executable: spork
14
14
  dependencies: []
15
15
 
@@ -28,11 +28,13 @@ files:
28
28
  - README.rdoc
29
29
  - assets/bootstrap.rb
30
30
  - lib/spork.rb
31
+ - lib/spork/forker.rb
31
32
  - lib/spork/runner.rb
32
33
  - lib/spork/server.rb
33
34
  - lib/spork/server/cucumber.rb
34
35
  - lib/spork/server/rspec.rb
35
36
  - spec/spec_helper.rb
37
+ - spec/spork/forker_spec.rb
36
38
  - spec/spork/runner_spec.rb
37
39
  - spec/spork/server/rspec_spec.rb
38
40
  - spec/spork/server_spec.rb
@@ -66,6 +68,7 @@ specification_version: 2
66
68
  summary: spork
67
69
  test_files:
68
70
  - spec/spec_helper.rb
71
+ - spec/spork/forker_spec.rb
69
72
  - spec/spork/runner_spec.rb
70
73
  - spec/spork/server/rspec_spec.rb
71
74
  - spec/spork/server_spec.rb