milhouse-spork 0.7.5.1 → 0.7.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +19 -19
- data/README.rdoc +99 -99
- data/assets/bootstrap.rb +29 -29
- data/bin/spork +20 -20
- data/features/cucumber_rails_integration.feature +118 -118
- data/features/diagnostic_mode.feature +40 -40
- data/features/rails_delayed_loading_workarounds.feature +115 -115
- data/features/rspec_rails_integration.feature +93 -93
- data/features/spork_debugger.feature +108 -108
- data/features/steps/general_steps.rb +3 -3
- data/features/steps/rails_steps.rb +52 -52
- data/features/steps/sandbox_steps.rb +115 -115
- data/features/support/background_job.rb +63 -63
- data/features/support/env.rb +111 -111
- data/features/unknown_app_framework.feature +41 -41
- data/geminstaller.yml +9 -9
- data/lib/spork/app_framework/rails.rb +157 -157
- data/lib/spork/app_framework/rails_stub_files/application.rb +1 -1
- data/lib/spork/app_framework/rails_stub_files/application_controller.rb +22 -22
- data/lib/spork/app_framework/rails_stub_files/application_helper.rb +2 -2
- data/lib/spork/app_framework/unknown.rb +5 -5
- data/lib/spork/app_framework.rb +73 -73
- data/lib/spork/custom_io_streams.rb +24 -24
- data/lib/spork/diagnoser.rb +103 -103
- data/lib/spork/ext/ruby-debug.rb +150 -150
- data/lib/spork/forker.rb +70 -70
- data/lib/spork/run_strategy/forking.rb +29 -29
- data/lib/spork/run_strategy/magazine/magazine_slave.rb +0 -0
- data/lib/spork/run_strategy/magazine/magazine_slave_provider.rb +0 -0
- data/lib/spork/run_strategy/magazine/ring_server.rb +0 -0
- data/lib/spork/run_strategy/magazine.rb +2 -2
- data/lib/spork/run_strategy.rb +44 -44
- data/lib/spork/runner.rb +90 -90
- data/lib/spork/server.rb +74 -74
- data/lib/spork/test_framework/cucumber.rb +24 -24
- data/lib/spork/test_framework/rspec.rb +14 -14
- data/lib/spork/test_framework.rb +167 -167
- data/lib/spork.rb +126 -126
- data/spec/spec_helper.rb +108 -108
- data/spec/spork/app_framework/rails_spec.rb +22 -22
- data/spec/spork/app_framework/unknown_spec.rb +12 -12
- data/spec/spork/app_framework_spec.rb +16 -16
- data/spec/spork/diagnoser_spec.rb +105 -105
- data/spec/spork/forker_spec.rb +44 -44
- data/spec/spork/run_strategy/forking_spec.rb +38 -38
- data/spec/spork/runner_spec.rb +50 -50
- data/spec/spork/server_spec.rb +15 -15
- data/spec/spork/test_framework/cucumber_spec.rb +11 -11
- data/spec/spork/test_framework/rspec_spec.rb +10 -10
- data/spec/spork/test_framework_spec.rb +114 -114
- data/spec/spork_spec.rb +151 -151
- data/spec/support/fake_framework.rb +15 -15
- data/spec/support/fake_run_strategy.rb +21 -21
- metadata +7 -15
data/lib/spork/test_framework.rb
CHANGED
@@ -1,167 +1,167 @@
|
|
1
|
-
class Spork::TestFramework
|
2
|
-
LOAD_PREFERENCE = ['RSpec', 'Cucumber']
|
3
|
-
BOOTSTRAP_FILE = File.dirname(__FILE__) + "/../../assets/bootstrap.rb"
|
4
|
-
|
5
|
-
@@supported_test_frameworks = []
|
6
|
-
attr_reader :stdout, :stderr
|
7
|
-
|
8
|
-
class FactoryException < Exception; end
|
9
|
-
|
10
|
-
class NoFrameworksAvailable < FactoryException
|
11
|
-
def message
|
12
|
-
"I can't find any testing frameworks to use. Are you running me from a project directory?"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class FrameworkNotAvailable < FactoryException
|
17
|
-
def initialize(framework)
|
18
|
-
@framework = framework
|
19
|
-
end
|
20
|
-
|
21
|
-
def message
|
22
|
-
"I can't find the file #{@framework.helper_file} for the #{@framework.short_name} testing framework.\nAre you running me from the project directory?"
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class NoFrameworkMatched < FactoryException
|
27
|
-
def initialize(beginning_with)
|
28
|
-
@beginning_with = beginning_with
|
29
|
-
end
|
30
|
-
|
31
|
-
def message
|
32
|
-
"Couldn't find a supported test framework that begins with '#{@beginning_with}'"
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def initialize(stdout = STDOUT, stderr = STDERR)
|
37
|
-
@stdout, @stderr = stdout, stderr
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.factory(output = STDOUT, error = STDERR, beginning_with = nil)
|
41
|
-
if beginning_with
|
42
|
-
@klass = supported_test_frameworks(beginning_with).first
|
43
|
-
raise(NoFrameworkMatched.new(beginning_with)) if @klass.nil?
|
44
|
-
raise(FrameworkNotAvailable.new(@klass)) unless @klass.available?
|
45
|
-
else
|
46
|
-
@klass = available_test_frameworks.first
|
47
|
-
raise(NoFrameworksAvailable.new) unless @klass
|
48
|
-
end
|
49
|
-
@klass.new(output, error)
|
50
|
-
end
|
51
|
-
|
52
|
-
def self.helper_file
|
53
|
-
self::HELPER_FILE
|
54
|
-
end
|
55
|
-
|
56
|
-
def self.default_port
|
57
|
-
(ENV["#{short_name.upcase}_DRB"] || self::DEFAULT_PORT).to_i
|
58
|
-
end
|
59
|
-
|
60
|
-
def self.short_name
|
61
|
-
self.name.gsub('Spork::TestFramework::', '')
|
62
|
-
end
|
63
|
-
|
64
|
-
# Returns a list of all testing servers that have detected their testing framework being used in the project.
|
65
|
-
def self.available_test_frameworks
|
66
|
-
supported_test_frameworks.select { |s| s.available? }
|
67
|
-
end
|
68
|
-
|
69
|
-
# Returns a list of all servers that have been implemented (it keeps track of them automatically via Class.inherited)
|
70
|
-
def self.supported_test_frameworks(starting_with = nil)
|
71
|
-
@@supported_test_frameworks.sort! { |a,b| a.load_preference_index <=> b.load_preference_index }
|
72
|
-
return @@supported_test_frameworks if starting_with.nil?
|
73
|
-
@@supported_test_frameworks.select do |s|
|
74
|
-
s.short_name.match(/^#{Regexp.escape(starting_with)}/i)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def short_name
|
79
|
-
self.class.short_name
|
80
|
-
end
|
81
|
-
|
82
|
-
def helper_file
|
83
|
-
self.class.helper_file
|
84
|
-
end
|
85
|
-
|
86
|
-
# Detects if the test helper has been bootstrapped.
|
87
|
-
def bootstrapped?
|
88
|
-
File.read(helper_file).include?("Spork.prefork")
|
89
|
-
end
|
90
|
-
|
91
|
-
# Bootstraps the current test helper file by prepending a Spork.prefork and Spork.each_run block at the beginning.
|
92
|
-
def bootstrap
|
93
|
-
if bootstrapped?
|
94
|
-
stderr.puts "Already bootstrapped!"
|
95
|
-
return
|
96
|
-
end
|
97
|
-
stderr.puts "Bootstrapping #{helper_file}."
|
98
|
-
contents = File.read(helper_file)
|
99
|
-
bootstrap_code = File.read(BOOTSTRAP_FILE)
|
100
|
-
File.open(helper_file, "wb") do |f|
|
101
|
-
f.puts bootstrap_code
|
102
|
-
f.puts contents
|
103
|
-
end
|
104
|
-
|
105
|
-
stderr.puts "Done. Edit #{helper_file} now with your favorite text editor and follow the instructions."
|
106
|
-
true
|
107
|
-
end
|
108
|
-
|
109
|
-
# Returns true if the testing frameworks helper file exists. Override if this is not sufficient to detect your testing framework.
|
110
|
-
def self.available?
|
111
|
-
File.exist?(helper_file)
|
112
|
-
end
|
113
|
-
|
114
|
-
# Used to specify
|
115
|
-
def self.load_preference_index
|
116
|
-
LOAD_PREFERENCE.index(short_name) || LOAD_PREFERENCE.length
|
117
|
-
end
|
118
|
-
|
119
|
-
def preload
|
120
|
-
Spork.exec_prefork do
|
121
|
-
unless bootstrapped?
|
122
|
-
stderr.puts "#{helper_file} has not been bootstrapped. Run spork --bootstrap to do so."
|
123
|
-
stderr.flush
|
124
|
-
|
125
|
-
if framework.bootstrap_required?
|
126
|
-
stderr.puts "I can't do anything for you by default for the framework your using: #{framework.short_name}.\nYou must bootstrap #{helper_file} to continue."
|
127
|
-
stderr.flush
|
128
|
-
return false
|
129
|
-
else
|
130
|
-
load(framework.entry_point)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
framework.preload do
|
135
|
-
if bootstrapped?
|
136
|
-
stderr.puts "Loading Spork.prefork block..."
|
137
|
-
stderr.flush
|
138
|
-
load(helper_file)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
true
|
143
|
-
end
|
144
|
-
|
145
|
-
def run_tests(argv, stderr, stdout)
|
146
|
-
raise NotImplementedError
|
147
|
-
end
|
148
|
-
|
149
|
-
def entry_point
|
150
|
-
bootstrapped? ? helper_file : framework.entry_point
|
151
|
-
end
|
152
|
-
|
153
|
-
def default_port
|
154
|
-
self.class.default_port
|
155
|
-
end
|
156
|
-
|
157
|
-
protected
|
158
|
-
def self.inherited(subclass)
|
159
|
-
@@supported_test_frameworks << subclass
|
160
|
-
end
|
161
|
-
|
162
|
-
def framework
|
163
|
-
@framework ||= Spork::AppFramework.detect_framework
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
Spork.detect_and_require('spork/test_framework/*.rb')
|
1
|
+
class Spork::TestFramework
|
2
|
+
LOAD_PREFERENCE = ['RSpec', 'Cucumber']
|
3
|
+
BOOTSTRAP_FILE = File.dirname(__FILE__) + "/../../assets/bootstrap.rb"
|
4
|
+
|
5
|
+
@@supported_test_frameworks = []
|
6
|
+
attr_reader :stdout, :stderr
|
7
|
+
|
8
|
+
class FactoryException < Exception; end
|
9
|
+
|
10
|
+
class NoFrameworksAvailable < FactoryException
|
11
|
+
def message
|
12
|
+
"I can't find any testing frameworks to use. Are you running me from a project directory?"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class FrameworkNotAvailable < FactoryException
|
17
|
+
def initialize(framework)
|
18
|
+
@framework = framework
|
19
|
+
end
|
20
|
+
|
21
|
+
def message
|
22
|
+
"I can't find the file #{@framework.helper_file} for the #{@framework.short_name} testing framework.\nAre you running me from the project directory?"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class NoFrameworkMatched < FactoryException
|
27
|
+
def initialize(beginning_with)
|
28
|
+
@beginning_with = beginning_with
|
29
|
+
end
|
30
|
+
|
31
|
+
def message
|
32
|
+
"Couldn't find a supported test framework that begins with '#{@beginning_with}'"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(stdout = STDOUT, stderr = STDERR)
|
37
|
+
@stdout, @stderr = stdout, stderr
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.factory(output = STDOUT, error = STDERR, beginning_with = nil)
|
41
|
+
if beginning_with
|
42
|
+
@klass = supported_test_frameworks(beginning_with).first
|
43
|
+
raise(NoFrameworkMatched.new(beginning_with)) if @klass.nil?
|
44
|
+
raise(FrameworkNotAvailable.new(@klass)) unless @klass.available?
|
45
|
+
else
|
46
|
+
@klass = available_test_frameworks.first
|
47
|
+
raise(NoFrameworksAvailable.new) unless @klass
|
48
|
+
end
|
49
|
+
@klass.new(output, error)
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.helper_file
|
53
|
+
self::HELPER_FILE
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.default_port
|
57
|
+
(ENV["#{short_name.upcase}_DRB"] || self::DEFAULT_PORT).to_i
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.short_name
|
61
|
+
self.name.gsub('Spork::TestFramework::', '')
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns a list of all testing servers that have detected their testing framework being used in the project.
|
65
|
+
def self.available_test_frameworks
|
66
|
+
supported_test_frameworks.select { |s| s.available? }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns a list of all servers that have been implemented (it keeps track of them automatically via Class.inherited)
|
70
|
+
def self.supported_test_frameworks(starting_with = nil)
|
71
|
+
@@supported_test_frameworks.sort! { |a,b| a.load_preference_index <=> b.load_preference_index }
|
72
|
+
return @@supported_test_frameworks if starting_with.nil?
|
73
|
+
@@supported_test_frameworks.select do |s|
|
74
|
+
s.short_name.match(/^#{Regexp.escape(starting_with)}/i)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def short_name
|
79
|
+
self.class.short_name
|
80
|
+
end
|
81
|
+
|
82
|
+
def helper_file
|
83
|
+
self.class.helper_file
|
84
|
+
end
|
85
|
+
|
86
|
+
# Detects if the test helper has been bootstrapped.
|
87
|
+
def bootstrapped?
|
88
|
+
File.read(helper_file).include?("Spork.prefork")
|
89
|
+
end
|
90
|
+
|
91
|
+
# Bootstraps the current test helper file by prepending a Spork.prefork and Spork.each_run block at the beginning.
|
92
|
+
def bootstrap
|
93
|
+
if bootstrapped?
|
94
|
+
stderr.puts "Already bootstrapped!"
|
95
|
+
return
|
96
|
+
end
|
97
|
+
stderr.puts "Bootstrapping #{helper_file}."
|
98
|
+
contents = File.read(helper_file)
|
99
|
+
bootstrap_code = File.read(BOOTSTRAP_FILE)
|
100
|
+
File.open(helper_file, "wb") do |f|
|
101
|
+
f.puts bootstrap_code
|
102
|
+
f.puts contents
|
103
|
+
end
|
104
|
+
|
105
|
+
stderr.puts "Done. Edit #{helper_file} now with your favorite text editor and follow the instructions."
|
106
|
+
true
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns true if the testing frameworks helper file exists. Override if this is not sufficient to detect your testing framework.
|
110
|
+
def self.available?
|
111
|
+
File.exist?(helper_file)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Used to specify
|
115
|
+
def self.load_preference_index
|
116
|
+
LOAD_PREFERENCE.index(short_name) || LOAD_PREFERENCE.length
|
117
|
+
end
|
118
|
+
|
119
|
+
def preload
|
120
|
+
Spork.exec_prefork do
|
121
|
+
unless bootstrapped?
|
122
|
+
stderr.puts "#{helper_file} has not been bootstrapped. Run spork --bootstrap to do so."
|
123
|
+
stderr.flush
|
124
|
+
|
125
|
+
if framework.bootstrap_required?
|
126
|
+
stderr.puts "I can't do anything for you by default for the framework your using: #{framework.short_name}.\nYou must bootstrap #{helper_file} to continue."
|
127
|
+
stderr.flush
|
128
|
+
return false
|
129
|
+
else
|
130
|
+
load(framework.entry_point)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
framework.preload do
|
135
|
+
if bootstrapped?
|
136
|
+
stderr.puts "Loading Spork.prefork block..."
|
137
|
+
stderr.flush
|
138
|
+
load(helper_file)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
true
|
143
|
+
end
|
144
|
+
|
145
|
+
def run_tests(argv, stderr, stdout)
|
146
|
+
raise NotImplementedError
|
147
|
+
end
|
148
|
+
|
149
|
+
def entry_point
|
150
|
+
bootstrapped? ? helper_file : framework.entry_point
|
151
|
+
end
|
152
|
+
|
153
|
+
def default_port
|
154
|
+
self.class.default_port
|
155
|
+
end
|
156
|
+
|
157
|
+
protected
|
158
|
+
def self.inherited(subclass)
|
159
|
+
@@supported_test_frameworks << subclass
|
160
|
+
end
|
161
|
+
|
162
|
+
def framework
|
163
|
+
@framework ||= Spork::AppFramework.detect_framework
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
Spork.detect_and_require('spork/test_framework/*.rb')
|
data/lib/spork.rb
CHANGED
@@ -1,126 +1,126 @@
|
|
1
|
-
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
-
require 'pathname'
|
3
|
-
module Spork
|
4
|
-
BINARY = File.expand_path(File.dirname(__FILE__) + '/../bin/spork')
|
5
|
-
LIBDIR = Pathname.new(File.expand_path(File.dirname(__FILE__)))
|
6
|
-
|
7
|
-
autoload :Server, (LIBDIR + 'spork/server').to_s
|
8
|
-
autoload :TestFramework, (LIBDIR + 'spork/test_framework').to_s
|
9
|
-
autoload :AppFramework, (LIBDIR + 'spork/app_framework').to_s
|
10
|
-
autoload :RunStrategy, (LIBDIR + 'spork/run_strategy').to_s
|
11
|
-
autoload :Runner, (LIBDIR + 'spork/runner').to_s
|
12
|
-
autoload :Forker, (LIBDIR + 'spork/forker').to_s
|
13
|
-
autoload :Diagnoser, (LIBDIR + 'spork/diagnoser').to_s
|
14
|
-
|
15
|
-
class << self
|
16
|
-
# Run a block, during prefork mode. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
|
17
|
-
#
|
18
|
-
# == Parameters
|
19
|
-
#
|
20
|
-
# * +prevent_double_run+ - Pass false to disable double run prevention
|
21
|
-
def prefork(prevent_double_run = true, &block)
|
22
|
-
return if prevent_double_run && already_ran?(caller.first)
|
23
|
-
yield
|
24
|
-
end
|
25
|
-
|
26
|
-
# Run a block AFTER the fork occurs. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
|
27
|
-
#
|
28
|
-
# == Parameters
|
29
|
-
#
|
30
|
-
# * +prevent_double_run+ - Pass false to disable double run prevention
|
31
|
-
def each_run(prevent_double_run = true, &block)
|
32
|
-
return if prevent_double_run && already_ran?(caller.first)
|
33
|
-
if @state == :using_spork
|
34
|
-
each_run_procs << block
|
35
|
-
else
|
36
|
-
yield
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
# Used by the server. Sets the state to activate spork. Otherwise, prefork and each_run are run in passive mode, allowing specs without a Spork server.
|
41
|
-
def using_spork!
|
42
|
-
@state = :using_spork
|
43
|
-
end
|
44
|
-
|
45
|
-
def using_spork?
|
46
|
-
@state == :using_spork
|
47
|
-
end
|
48
|
-
|
49
|
-
# Used by the server. Returns the current state of Spork.
|
50
|
-
def state
|
51
|
-
@state ||= :not_using_spork
|
52
|
-
end
|
53
|
-
|
54
|
-
# Used by the server. Called when loading the prefork blocks of the code.
|
55
|
-
def exec_prefork(&block)
|
56
|
-
using_spork!
|
57
|
-
yield
|
58
|
-
end
|
59
|
-
|
60
|
-
# Used by the server. Called to run all of the prefork blocks.
|
61
|
-
def exec_each_run(&block)
|
62
|
-
each_run_procs.each { |p| p.call }
|
63
|
-
each_run_procs.clear
|
64
|
-
yield if block_given?
|
65
|
-
end
|
66
|
-
|
67
|
-
# Traps an instance method of a class (or module) so any calls to it don't actually run until Spork.exec_each_run
|
68
|
-
def trap_method(klass, method_name)
|
69
|
-
method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork)
|
70
|
-
|
71
|
-
klass.class_eval <<-EOF, __FILE__, __LINE__ + 1
|
72
|
-
alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork})
|
73
|
-
def #{method_name}(*args)
|
74
|
-
Spork.each_run(false) do
|
75
|
-
#{method_name_without_spork}(*args)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
EOF
|
79
|
-
end
|
80
|
-
|
81
|
-
# Same as trap_method, but for class methods instead
|
82
|
-
def trap_class_method(klass, method_name)
|
83
|
-
trap_method((class << klass; self; end), method_name)
|
84
|
-
end
|
85
|
-
|
86
|
-
def detect_and_require(subfolder)
|
87
|
-
([LIBDIR.to_s] + other_spork_gem_load_paths).uniq.each do |gem_path|
|
88
|
-
Dir.glob(File.join(gem_path, subfolder)).each { |file| require file }
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def other_spork_gem_load_paths
|
93
|
-
@other_spork_gem_load_paths ||= (
|
94
|
-
Gem.latest_load_paths.grep(/spork/).select do |g|
|
95
|
-
not g.match(%r{/spork-[0-9\-.]+/lib}) # don't include other versions of spork
|
96
|
-
end
|
97
|
-
)
|
98
|
-
end
|
99
|
-
|
100
|
-
private
|
101
|
-
def alias_method_names(method_name, feature)
|
102
|
-
/^(.+?)([\?\!]{0,1})$/.match(method_name.to_s)
|
103
|
-
["#{$1}_without_spork#{$2}", "#{$1}_with_spork#{$2}"]
|
104
|
-
end
|
105
|
-
|
106
|
-
def already_ran
|
107
|
-
@already_ran ||= []
|
108
|
-
end
|
109
|
-
|
110
|
-
def expanded_caller(caller_line)
|
111
|
-
file, line = caller_line.split(":")
|
112
|
-
line.gsub(/:.+/, '')
|
113
|
-
File.expand_path(file, Dir.pwd) + ":" + line
|
114
|
-
end
|
115
|
-
|
116
|
-
def already_ran?(caller_script_and_line)
|
117
|
-
return true if already_ran.include?(expanded_caller(caller_script_and_line))
|
118
|
-
already_ran << expanded_caller(caller_script_and_line)
|
119
|
-
false
|
120
|
-
end
|
121
|
-
|
122
|
-
def each_run_procs
|
123
|
-
@each_run_procs ||= []
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
1
|
+
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
require 'pathname'
|
3
|
+
module Spork
|
4
|
+
BINARY = File.expand_path(File.dirname(__FILE__) + '/../bin/spork')
|
5
|
+
LIBDIR = Pathname.new(File.expand_path(File.dirname(__FILE__)))
|
6
|
+
|
7
|
+
autoload :Server, (LIBDIR + 'spork/server').to_s
|
8
|
+
autoload :TestFramework, (LIBDIR + 'spork/test_framework').to_s
|
9
|
+
autoload :AppFramework, (LIBDIR + 'spork/app_framework').to_s
|
10
|
+
autoload :RunStrategy, (LIBDIR + 'spork/run_strategy').to_s
|
11
|
+
autoload :Runner, (LIBDIR + 'spork/runner').to_s
|
12
|
+
autoload :Forker, (LIBDIR + 'spork/forker').to_s
|
13
|
+
autoload :Diagnoser, (LIBDIR + 'spork/diagnoser').to_s
|
14
|
+
|
15
|
+
class << self
|
16
|
+
# Run a block, during prefork mode. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
|
17
|
+
#
|
18
|
+
# == Parameters
|
19
|
+
#
|
20
|
+
# * +prevent_double_run+ - Pass false to disable double run prevention
|
21
|
+
def prefork(prevent_double_run = true, &block)
|
22
|
+
return if prevent_double_run && already_ran?(caller.first)
|
23
|
+
yield
|
24
|
+
end
|
25
|
+
|
26
|
+
# Run a block AFTER the fork occurs. By default, if prefork is called twice in the same file and line number, the supplied block will only be ran once.
|
27
|
+
#
|
28
|
+
# == Parameters
|
29
|
+
#
|
30
|
+
# * +prevent_double_run+ - Pass false to disable double run prevention
|
31
|
+
def each_run(prevent_double_run = true, &block)
|
32
|
+
return if prevent_double_run && already_ran?(caller.first)
|
33
|
+
if @state == :using_spork
|
34
|
+
each_run_procs << block
|
35
|
+
else
|
36
|
+
yield
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Used by the server. Sets the state to activate spork. Otherwise, prefork and each_run are run in passive mode, allowing specs without a Spork server.
|
41
|
+
def using_spork!
|
42
|
+
@state = :using_spork
|
43
|
+
end
|
44
|
+
|
45
|
+
def using_spork?
|
46
|
+
@state == :using_spork
|
47
|
+
end
|
48
|
+
|
49
|
+
# Used by the server. Returns the current state of Spork.
|
50
|
+
def state
|
51
|
+
@state ||= :not_using_spork
|
52
|
+
end
|
53
|
+
|
54
|
+
# Used by the server. Called when loading the prefork blocks of the code.
|
55
|
+
def exec_prefork(&block)
|
56
|
+
using_spork!
|
57
|
+
yield
|
58
|
+
end
|
59
|
+
|
60
|
+
# Used by the server. Called to run all of the prefork blocks.
|
61
|
+
def exec_each_run(&block)
|
62
|
+
each_run_procs.each { |p| p.call }
|
63
|
+
each_run_procs.clear
|
64
|
+
yield if block_given?
|
65
|
+
end
|
66
|
+
|
67
|
+
# Traps an instance method of a class (or module) so any calls to it don't actually run until Spork.exec_each_run
|
68
|
+
def trap_method(klass, method_name)
|
69
|
+
method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork)
|
70
|
+
|
71
|
+
klass.class_eval <<-EOF, __FILE__, __LINE__ + 1
|
72
|
+
alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork})
|
73
|
+
def #{method_name}(*args)
|
74
|
+
Spork.each_run(false) do
|
75
|
+
#{method_name_without_spork}(*args)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
EOF
|
79
|
+
end
|
80
|
+
|
81
|
+
# Same as trap_method, but for class methods instead
|
82
|
+
def trap_class_method(klass, method_name)
|
83
|
+
trap_method((class << klass; self; end), method_name)
|
84
|
+
end
|
85
|
+
|
86
|
+
def detect_and_require(subfolder)
|
87
|
+
([LIBDIR.to_s] + other_spork_gem_load_paths).uniq.each do |gem_path|
|
88
|
+
Dir.glob(File.join(gem_path, subfolder)).each { |file| require file }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def other_spork_gem_load_paths
|
93
|
+
@other_spork_gem_load_paths ||= (
|
94
|
+
Gem.latest_load_paths.grep(/spork/).select do |g|
|
95
|
+
not g.match(%r{/spork-[0-9\-.]+/lib}) # don't include other versions of spork
|
96
|
+
end
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
def alias_method_names(method_name, feature)
|
102
|
+
/^(.+?)([\?\!]{0,1})$/.match(method_name.to_s)
|
103
|
+
["#{$1}_without_spork#{$2}", "#{$1}_with_spork#{$2}"]
|
104
|
+
end
|
105
|
+
|
106
|
+
def already_ran
|
107
|
+
@already_ran ||= []
|
108
|
+
end
|
109
|
+
|
110
|
+
def expanded_caller(caller_line)
|
111
|
+
file, line = caller_line.split(":")
|
112
|
+
line.gsub(/:.+/, '')
|
113
|
+
File.expand_path(file, Dir.pwd) + ":" + line
|
114
|
+
end
|
115
|
+
|
116
|
+
def already_ran?(caller_script_and_line)
|
117
|
+
return true if already_ran.include?(expanded_caller(caller_script_and_line))
|
118
|
+
already_ran << expanded_caller(caller_script_and_line)
|
119
|
+
false
|
120
|
+
end
|
121
|
+
|
122
|
+
def each_run_procs
|
123
|
+
@each_run_procs ||= []
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|