wakiki-spork 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/Gemfile +6 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +127 -0
  4. data/assets/bootstrap.rb +29 -0
  5. data/bin/spork +20 -0
  6. data/ext/mkrf_conf.rb +26 -0
  7. data/features/at_exit_during_each_run.feature +35 -0
  8. data/features/cucumber_rails_integration.feature +111 -0
  9. data/features/diagnostic_mode.feature +41 -0
  10. data/features/gemfiles/rails3.0/Gemfile +10 -0
  11. data/features/gemfiles/rails3.0/Gemfile.lock +135 -0
  12. data/features/rails_delayed_loading_workarounds.feature +115 -0
  13. data/features/rspec_rails_integration.feature +92 -0
  14. data/features/spork_debugger.feature +108 -0
  15. data/features/steps/general_steps.rb +3 -0
  16. data/features/steps/rails_steps.rb +63 -0
  17. data/features/steps/sandbox_steps.rb +115 -0
  18. data/features/support/background_job.rb +63 -0
  19. data/features/support/bundler_helpers.rb +42 -0
  20. data/features/support/env.rb +117 -0
  21. data/features/unknown_app_framework.feature +42 -0
  22. data/lib/spork.rb +156 -0
  23. data/lib/spork/app_framework.rb +80 -0
  24. data/lib/spork/app_framework/padrino.rb +22 -0
  25. data/lib/spork/app_framework/rails.rb +167 -0
  26. data/lib/spork/app_framework/rails_stub_files/application.rb +1 -0
  27. data/lib/spork/app_framework/rails_stub_files/application_controller.rb +22 -0
  28. data/lib/spork/app_framework/rails_stub_files/application_helper.rb +3 -0
  29. data/lib/spork/app_framework/unknown.rb +6 -0
  30. data/lib/spork/custom_io_streams.rb +25 -0
  31. data/lib/spork/diagnoser.rb +105 -0
  32. data/lib/spork/ext/rails-reloader.rb +14 -0
  33. data/lib/spork/ext/ruby-debug.rb +150 -0
  34. data/lib/spork/forker.rb +71 -0
  35. data/lib/spork/run_strategy.rb +44 -0
  36. data/lib/spork/run_strategy/forking.rb +32 -0
  37. data/lib/spork/run_strategy/magazine.rb +121 -0
  38. data/lib/spork/run_strategy/magazine/magazine_slave.rb +30 -0
  39. data/lib/spork/run_strategy/magazine/magazine_slave_provider.rb +27 -0
  40. data/lib/spork/run_strategy/magazine/ring_server.rb +10 -0
  41. data/lib/spork/runner.rb +91 -0
  42. data/lib/spork/server.rb +74 -0
  43. data/lib/spork/test_framework.rb +167 -0
  44. data/lib/spork/test_framework/cucumber.rb +24 -0
  45. data/lib/spork/test_framework/rspec.rb +14 -0
  46. data/spec/spec_helper.rb +108 -0
  47. data/spec/spork/app_framework/rails_spec.rb +22 -0
  48. data/spec/spork/app_framework/unknown_spec.rb +12 -0
  49. data/spec/spork/app_framework_spec.rb +16 -0
  50. data/spec/spork/diagnoser_spec.rb +105 -0
  51. data/spec/spork/forker_spec.rb +44 -0
  52. data/spec/spork/run_strategy/forking_spec.rb +38 -0
  53. data/spec/spork/runner_spec.rb +50 -0
  54. data/spec/spork/server_spec.rb +15 -0
  55. data/spec/spork/test_framework/cucumber_spec.rb +11 -0
  56. data/spec/spork/test_framework/rspec_spec.rb +10 -0
  57. data/spec/spork/test_framework_spec.rb +114 -0
  58. data/spec/spork_spec.rb +151 -0
  59. data/spec/support/fake_framework.rb +15 -0
  60. data/spec/support/fake_run_strategy.rb +21 -0
  61. metadata +159 -0
@@ -0,0 +1,22 @@
1
+ class Spork::AppFramework::Padrino < Spork::AppFramework
2
+
3
+ def preload(&block)
4
+ STDERR.puts "Preloading Padrino environment"
5
+ STDERR.flush
6
+ ENV["PADRINO_ENV"] ||= "test"
7
+ require boot_file
8
+ # Make it so that we don't have to restart Spork if we change, say, a model or routes
9
+ Spork.each_run { ::Padrino.reload! }
10
+ yield
11
+ end
12
+
13
+ def entry_point
14
+ @entry_point ||= File.expand_path("config/boot.rb", Dir.pwd)
15
+ end
16
+ alias :boot_file :entry_point
17
+
18
+ def boot_contents
19
+ @boot_contents ||= File.read(boot_file)
20
+ end
21
+
22
+ end
@@ -0,0 +1,167 @@
1
+ class Spork::AppFramework::Rails < Spork::AppFramework
2
+
3
+ # TODO - subclass this out to handle different versions of rails
4
+ # Also... this is the nastiest duck punch ever. Clean this up.
5
+ # module NinjaPatcher
6
+ # def self.included(klass)
7
+ # klass.class_eval do
8
+ # unless method_defined?(:load_environment_without_spork)
9
+ # alias :load_environment_without_spork :load_environment
10
+ # alias :load_environment :load_environment_with_spork
11
+ # end
12
+ #
13
+ # def self.run_with_spork(*args, &block) # it's all fun and games until someone gets an eye poked out
14
+ # if ENV['RAILS_ENV']
15
+ # Object.send(:remove_const, :RAILS_ENV)
16
+ # Object.const_set(:RAILS_ENV, ENV['RAILS_ENV'].dup)
17
+ # end
18
+ # run_without_spork(*args, &block)
19
+ # end
20
+ #
21
+ # class << self
22
+ # unless method_defined?(:run_without_spork)
23
+ # alias :run_without_spork :run
24
+ # alias :run :run_with_spork
25
+ # end
26
+ # end
27
+ # end
28
+ # end
29
+ #
30
+ # def load_environment_with_spork
31
+ # result = load_environment_without_spork
32
+ # install_hooks
33
+ # result
34
+ # end
35
+ #
36
+ # def install_hooks
37
+ # auto_reestablish_db_connection
38
+ # delay_observer_loading
39
+ # delay_app_preload
40
+ # delay_application_controller_loading
41
+ # delay_route_loading
42
+ # delay_eager_view_loading
43
+ # end
44
+ #
45
+ # def reset_rails_env
46
+ # return unless ENV['RAILS_ENV']
47
+ # Object.send(:remove_const, :RAILS_ENV)
48
+ # Object.const_set(:RAILS_ENV, ENV['RAILS_ENV'].dup)
49
+ # end
50
+ #
51
+ # def delay_observer_loading
52
+ # if ::Rails::Initializer.instance_methods.map(&:to_sym).include?(:load_observers)
53
+ # Spork.trap_method(::Rails::Initializer, :load_observers)
54
+ # end
55
+ # if Object.const_defined?(:ActionController)
56
+ # require "action_controller/dispatcher.rb"
57
+ # Spork.trap_class_method(::ActionController::Dispatcher, :define_dispatcher_callbacks) if ActionController::Dispatcher.respond_to?(:define_dispatcher_callbacks)
58
+ # end
59
+ # end
60
+ #
61
+ # def delay_app_preload
62
+ # if ::Rails::Initializer.instance_methods.map(&:to_sym).include?(:load_application_classes)
63
+ # Spork.trap_method(::Rails::Initializer, :load_application_classes)
64
+ # end
65
+ # end
66
+ #
67
+ # def delay_application_controller_loading
68
+ # if application_controller_source = ["#{Dir.pwd}/app/controllers/application.rb", "#{Dir.pwd}/app/controllers/application_controller.rb"].find { |f| File.exist?(f) }
69
+ # application_helper_source = "#{Dir.pwd}/app/helpers/application_helper.rb"
70
+ # load_paths = (::ActiveSupport.const_defined?(:Dependencies) ? ::ActiveSupport::Dependencies : ::Dependencies).load_paths
71
+ # load_paths.unshift(File.expand_path('rails_stub_files', File.dirname(__FILE__)))
72
+ # Spork.each_run do
73
+ # require application_controller_source
74
+ # require application_helper_source if File.exist?(application_helper_source)
75
+ # # update the rails magic to refresh the module
76
+ # ApplicationController.send(:helper, ApplicationHelper)
77
+ # end
78
+ # end
79
+ # end
80
+ #
81
+ # def auto_reestablish_db_connection
82
+ # if Object.const_defined?(:ActiveRecord)
83
+ # Spork.each_run do
84
+ # # rails lib/test_help.rb is very aggressive about overriding RAILS_ENV and will switch it back to test after the cucumber env was loaded
85
+ # reset_rails_env
86
+ # ActiveRecord::Base.establish_connection
87
+ # end
88
+ # end
89
+ # end
90
+ #
91
+ # def delay_route_loading
92
+ # require 'ruby-debug'; require "/Users/timcharper/Library/Application Support/TextMate/Bundles/RubyAMP.tmbundle/Support/ext/debugger_extension.rb"; Debugger.start; Debugger.start_control; debugger
93
+ #
94
+ # if ::Rails::Initializer.instance_methods.map(&:to_sym).include?(:initialize_routing)
95
+ # Spork.trap_method(::Rails::Initializer, :initialize_routing)
96
+ # end
97
+ # end
98
+ #
99
+ # def delay_eager_view_loading
100
+ # # So, in testing mode it seems it would be optimal to not eager load
101
+ # # views (as your may only run a test that uses one or two views).
102
+ # # However, I decided to delay eager loading rather than force it to
103
+ # # disable because you may wish to eager load your views (I.E. you're
104
+ # # testing concurrency)
105
+ #
106
+ # # Rails 2.3.x +
107
+ # if defined?(::ActionView::Template::EagerPath)
108
+ # Spork.trap_method(::ActionView::Template::EagerPath, :load!)
109
+ # else
110
+ # raise "NOCOMMIT"
111
+ # end
112
+ # end
113
+ # end
114
+
115
+ def preload(&block)
116
+ STDERR.puts "Preloading Rails environment"
117
+ STDERR.flush
118
+ ENV["RAILS_ENV"] ||= 'test'
119
+ preload_rails
120
+ yield
121
+ end
122
+
123
+ def entry_point
124
+ @entry_point ||= File.expand_path("config/environment.rb", Dir.pwd)
125
+ end
126
+
127
+ alias :environment_file :entry_point
128
+
129
+ def boot_file
130
+ @boot_file ||= File.join(File.dirname(environment_file), 'boot')
131
+ end
132
+
133
+ def application_file
134
+ @application_file ||= File.join(File.dirname(environment_file), 'application')
135
+ end
136
+
137
+ def environment_contents
138
+ @environment_contents ||= File.read(environment_file)
139
+ end
140
+
141
+ def vendor
142
+ @vendor ||= File.expand_path("vendor/rails", Dir.pwd)
143
+ end
144
+
145
+ def deprecated_version
146
+ @version ||= (
147
+ if /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/.match(environment_contents)
148
+ $1
149
+ else
150
+ nil
151
+ end
152
+ )
153
+ end
154
+
155
+ def preload_rails
156
+ if deprecated_version && (not deprecated_version.match?(/^3/))
157
+ puts "This version of spork only supports Rails 3.0. To use spork with rails 2.3.x, downgrade to spork 0.8.x."
158
+ exit 1
159
+ end
160
+ require application_file
161
+ ::Rails.application
162
+
163
+ # ::Rails::Initializer.send(:include, Spork::AppFramework::Rails::NinjaPatcher)
164
+ 1
165
+ end
166
+
167
+ end
@@ -0,0 +1 @@
1
+ load(File.dirname(__FILE__) + "/application_controller.rb")
@@ -0,0 +1,22 @@
1
+ # This is a stub used to help Spork delay the loading of the real ApplicationController
2
+ class ::ApplicationController < ActionController::Base
3
+ @@preloading = true
4
+ class << self
5
+ def inherited(klass)
6
+ (@_descendants ||= []) << klass if @@preloading
7
+ super
8
+ end
9
+
10
+ def reapply_inheritance!
11
+ @@preloading = false
12
+ Array(@_descendants).each do |descendant|
13
+ descendant.master_helper_module.send(:include, master_helper_module)
14
+ descendant.send(:default_helper_module!)
15
+
16
+ descendant.respond_to?(:reapply_inheritance!) && descendant.reapply_inheritance!
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ Spork.each_run { ApplicationController.reapply_inheritance! }
@@ -0,0 +1,3 @@
1
+ # This is a stub used to help Spork delay the loading of the real ApplicationHelper
2
+ module ::ApplicationHelper
3
+ end
@@ -0,0 +1,6 @@
1
+ # This is used if no supported appliction framework is detected
2
+ class Spork::AppFramework::Unknown < Spork::AppFramework
3
+ def entry_point
4
+ nil
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ # This class is mainly used for testing.
2
+ # When included (and used), it gives us an opportunity to stub out the output streams used for a given class
3
+ module Spork::CustomIOStreams
4
+ def self.included(klass)
5
+ klass.send(:extend, ::Spork::CustomIOStreams::ClassMethods)
6
+ end
7
+
8
+ def stderr
9
+ self.class.stderr
10
+ end
11
+
12
+ def stdout
13
+ self.class.stdout
14
+ end
15
+
16
+ module ClassMethods
17
+ def stderr
18
+ $stderr
19
+ end
20
+
21
+ def stdout
22
+ $stdout
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,105 @@
1
+ # The Diagnoser hooks into load and require and keeps track of when files are required / loaded, and who loaded them.
2
+ # It's used when you run spork --diagnose
3
+ #
4
+ # = Example
5
+ #
6
+ # Spork::Diagnoser.install_hook!('/path/env.rb', '/path')
7
+ # require '/path/to/env.rb'
8
+ # Spork::Diagnoser.output_results(STDOUT)
9
+ class Spork::Diagnoser
10
+ class << self
11
+ def loaded_files
12
+ @loaded_files ||= {}
13
+ end
14
+
15
+ # Installs the diagnoser hook into Kernel#require and Kernel#load
16
+ #
17
+ # == Parameters
18
+ #
19
+ # * +entry_file+ - The file that is used to load the project. Used to filter the backtrace so anything that happens after it is hidden.
20
+ # * +dir+ - The project directory. Any file loaded outside of this directory will not be logged.
21
+ def install_hook!(entry_file = nil, dir = Dir.pwd)
22
+ @dir = File.expand_path(Dir.pwd, dir)
23
+ @entry_file = entry_file
24
+
25
+ Kernel.class_eval do
26
+ alias :require_without_diagnoser :require
27
+ alias :load_without_diagnoser :load
28
+
29
+ def require(string)
30
+ ::Spork::Diagnoser.add_included_file(string, caller)
31
+ require_without_diagnoser(string)
32
+ end
33
+ private :require
34
+
35
+ def load(string)
36
+ ::Spork::Diagnoser.add_included_file(string, caller)
37
+ load_without_diagnoser(string)
38
+ end
39
+ private :load
40
+ end
41
+ end
42
+
43
+ def add_included_file(filename, callstack)
44
+ filename = expand_filename(filename)
45
+ return unless File.exist?(filename)
46
+ loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename)
47
+ end
48
+
49
+ # Uninstall the hook. Generally useful only for testing the Diagnoser.
50
+ def remove_hook!
51
+ return unless Kernel.private_instance_methods.include?('require_without_diagnoser')
52
+ Kernel.class_eval do
53
+ alias :require :require_without_diagnoser
54
+ alias :load :load_without_diagnoser
55
+
56
+ undef_method(:require_without_diagnoser)
57
+ undef_method(:load_without_diagnoser)
58
+ end
59
+ true
60
+ end
61
+
62
+ # output the results of a diagnostic run.
63
+ #
64
+ # == Parameters
65
+ #
66
+ # * +stdout+ - An IO stream to output the results to.
67
+ def output_results(stdout)
68
+ project_prefix = Dir.pwd + "/"
69
+ minimify = lambda { |f| f.gsub(project_prefix, '')}
70
+ stdout.puts "- Spork Diagnosis -\n"
71
+ stdout.puts "-- Summary --"
72
+ stdout.puts loaded_files.keys.sort.map(&minimify)
73
+ stdout.puts "\n\n\n"
74
+ stdout.puts "-- Detail --"
75
+ loaded_files.keys.sort.each do |file|
76
+ stdout.puts "\n\n\n--- #{minimify.call(file)} ---\n"
77
+ stdout.puts loaded_files[file].map(&minimify)
78
+ end
79
+ end
80
+
81
+ private
82
+ def filter_callstack(callstack, entry_file = @entry_file)
83
+ callstack.pop until callstack.empty? || callstack.last.include?(@entry_file) if @entry_file
84
+ callstack.map do |line|
85
+ next if line.include?('lib/spork/diagnoser.rb')
86
+ line.gsub!('require_without_diagnoser', 'require')
87
+ line
88
+ end.compact
89
+ end
90
+
91
+ def expand_filename(filename)
92
+ ([Dir.pwd] + $:).each do |attempted_path|
93
+ attempted_filename = File.expand_path(filename, attempted_path)
94
+ return attempted_filename if File.file?(attempted_filename)
95
+ attempted_filename = attempted_filename + ".rb"
96
+ return attempted_filename if File.file?(attempted_filename)
97
+ end
98
+ filename
99
+ end
100
+
101
+ def subdirectory?(directory)
102
+ File.expand_path(directory, Dir.pwd).include?(@dir)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,14 @@
1
+ Spork.each_run do
2
+ ::ActiveSupport.const_defined?(:Dependencies) ?
3
+ ::ActiveSupport::Dependencies.mechanism = :load :
4
+ ::Dependencies.mechanism = :load
5
+
6
+ require 'action_controller/dispatcher'
7
+ dispatcher = ::ActionController::Dispatcher.new($stdout)
8
+
9
+ if ::ActionController::Dispatcher.respond_to?(:reload_application)
10
+ ::ActionController::Dispatcher.reload_application
11
+ else
12
+ dispatcher.reload_application
13
+ end
14
+ end if Spork.using_spork?
@@ -0,0 +1,150 @@
1
+ require 'socket'
2
+ require 'forwardable'
3
+
4
+ begin
5
+ require 'ruby-debug'
6
+
7
+ # Experimental!
8
+
9
+ class SporkDebugger
10
+ DEFAULT_PORT = 10_123
11
+ HOST = '127.0.0.1'
12
+
13
+ extend Forwardable
14
+ def_delegators :state, *[:prepare_debugger, :initialize]
15
+ attr_reader :state
16
+
17
+ class << self
18
+ attr_reader :instance
19
+ def run
20
+ @instance ||= new
21
+ end
22
+ end
23
+
24
+ def initialize
25
+ @state = SporkDebugger::PreloadState.new
26
+ Spork.send(:each_run_procs).unshift(lambda do
27
+ @state = @state.transition_to_each_run_state
28
+ end)
29
+ end
30
+
31
+ module NetworkHelpers
32
+ def find_port(starting_with)
33
+ port = starting_with
34
+ begin
35
+ server = TCPServer.new(HOST, port)
36
+ server.close
37
+ rescue Errno::EADDRINUSE
38
+ port += 1
39
+ retry
40
+ end
41
+
42
+ port
43
+ end
44
+ end
45
+
46
+ class PreloadState
47
+ include NetworkHelpers
48
+ def initialize
49
+ Spork.each_run { install_hook }
50
+ listen_for_connection_signals
51
+ end
52
+
53
+ def finish
54
+ @tcp_service.close; @tcp_service = nil;
55
+ end
56
+
57
+ def transition_to_each_run_state
58
+ finish
59
+ SporkDebugger::EachRunState.new(@port)
60
+ end
61
+
62
+ protected
63
+ def install_hook
64
+ Kernel.class_eval do
65
+ alias :debugger_without_spork_hook :debugger
66
+ def debugger(steps = 1)
67
+ SporkDebugger.instance.prepare_debugger
68
+ debugger_without_spork_hook
69
+ end
70
+ end
71
+ end
72
+
73
+ def listen_for_connection_signals
74
+ @port = SporkDebugger::DEFAULT_PORT
75
+ begin
76
+ @tcp_service = TCPServer.new(SporkDebugger::HOST, @port)
77
+ rescue Errno::EADDRINUSE
78
+ @port += 1
79
+ retry
80
+ end
81
+ Thread.new { main_loop }
82
+ end
83
+
84
+ def main_loop
85
+ while @tcp_service do
86
+ socket = @tcp_service.accept
87
+ port = Marshal.load(socket)
88
+ Marshal.dump(true, socket)
89
+ connect_rdebug_client(port)
90
+ socket.close
91
+ end
92
+ rescue => e
93
+ puts "error: #{e.class} - #{e}"
94
+ end
95
+
96
+ def connect_rdebug_client(port = Debugger::PORT)
97
+ puts "\n\n - Debug Session Started - \n\n\n"
98
+ begin
99
+ Debugger.start_client(SporkDebugger::HOST, port)
100
+ rescue Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED
101
+ end
102
+ puts "\n\n - Debug Session Terminated - \n\n\n"
103
+ end
104
+ end
105
+
106
+ class EachRunState
107
+ include NetworkHelpers
108
+ def initialize(connection_request_port)
109
+ @connection_request_port = connection_request_port
110
+ end
111
+
112
+ def prepare_debugger
113
+ return if @debugger_prepared
114
+ @debugger_prepared = true
115
+ port, cport = start_rdebug_server
116
+ signal_spork_server_to_connect_to_rdebug_server(port)
117
+ wait_for_connection
118
+ puts "\n\n - breakpoint - see your spork server for the debug terminal - \n\n"
119
+ end
120
+
121
+ def signal_spork_server_to_connect_to_rdebug_server(rdebug_server_port)
122
+ socket = TCPSocket.new(SporkDebugger::HOST, @connection_request_port)
123
+ Marshal.dump(rdebug_server_port, socket)
124
+ response = Marshal.load(socket)
125
+ socket.close
126
+ end
127
+
128
+ def start_rdebug_server
129
+ Debugger.stop if Debugger.started?
130
+ port = find_port(Debugger::PORT)
131
+ cport = find_port(port + 1)
132
+ Debugger.start_remote(SporkDebugger::HOST, [port, cport]) do
133
+ Debugger.run_init_script(StringIO.new)
134
+ end
135
+ Debugger.start
136
+ [port, cport]
137
+ end
138
+
139
+ protected
140
+ def wait_for_connection
141
+ while Debugger.handler.interface.nil?; sleep 0.10; end
142
+ end
143
+ end
144
+ end
145
+
146
+ Spork.prefork { SporkDebugger.run } if Spork.using_spork?
147
+
148
+ rescue LoadError
149
+ raise LoadError, "Your project has loaded spork/ext/ruby-debug, which relies on the ruby-debug gem. It appears that ruby-debug is not installed. Please install it."
150
+ end