pietervisser-spork 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +11 -0
  3. data/assets/bootstrap.rb +29 -0
  4. data/bin/spork +20 -0
  5. data/features/cucumber_rails_integration.feature +118 -0
  6. data/features/diagnostic_mode.feature +41 -0
  7. data/features/rails_delayed_loading_workarounds.feature +115 -0
  8. data/features/rspec_rails_integration.feature +93 -0
  9. data/features/spork_debugger.feature +108 -0
  10. data/features/steps/general_steps.rb +3 -0
  11. data/features/steps/rails_steps.rb +53 -0
  12. data/features/steps/sandbox_steps.rb +115 -0
  13. data/features/support/background_job.rb +63 -0
  14. data/features/support/env.rb +111 -0
  15. data/features/unknown_app_framework.feature +42 -0
  16. data/geminstaller.yml +9 -0
  17. data/lib/spork/app_framework/rails.rb +158 -0
  18. data/lib/spork/app_framework/rails_stub_files/application.rb +1 -0
  19. data/lib/spork/app_framework/rails_stub_files/application_controller.rb +22 -0
  20. data/lib/spork/app_framework/rails_stub_files/application_helper.rb +3 -0
  21. data/lib/spork/app_framework/unknown.rb +6 -0
  22. data/lib/spork/app_framework.rb +74 -0
  23. data/lib/spork/custom_io_streams.rb +25 -0
  24. data/lib/spork/diagnoser.rb +103 -0
  25. data/lib/spork/ext/ruby-debug.rb +150 -0
  26. data/lib/spork/forker.rb +70 -0
  27. data/lib/spork/run_strategy/forking.rb +30 -0
  28. data/lib/spork/run_strategy.rb +40 -0
  29. data/lib/spork/runner.rb +90 -0
  30. data/lib/spork/server.rb +74 -0
  31. data/lib/spork/test_framework/cucumber.rb +24 -0
  32. data/lib/spork/test_framework/rspec.rb +14 -0
  33. data/lib/spork/test_framework.rb +167 -0
  34. data/lib/spork.rb +130 -0
  35. data/spec/spec_helper.rb +108 -0
  36. data/spec/spork/app_framework/rails_spec.rb +22 -0
  37. data/spec/spork/app_framework/unknown_spec.rb +12 -0
  38. data/spec/spork/app_framework_spec.rb +16 -0
  39. data/spec/spork/diagnoser_spec.rb +105 -0
  40. data/spec/spork/forker_spec.rb +44 -0
  41. data/spec/spork/run_strategy/forking_spec.rb +38 -0
  42. data/spec/spork/runner_spec.rb +50 -0
  43. data/spec/spork/server_spec.rb +15 -0
  44. data/spec/spork/test_framework/cucumber_spec.rb +11 -0
  45. data/spec/spork/test_framework/rspec_spec.rb +10 -0
  46. data/spec/spork/test_framework_spec.rb +114 -0
  47. data/spec/spork_spec.rb +157 -0
  48. data/spec/support/fake_framework.rb +15 -0
  49. data/spec/support/fake_run_strategy.rb +21 -0
  50. metadata +119 -0
@@ -0,0 +1,103 @@
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
+
34
+ def load(string)
35
+ ::Spork::Diagnoser.add_included_file(string, caller)
36
+ load_without_diagnoser(string)
37
+ end
38
+ end
39
+ end
40
+
41
+ def add_included_file(filename, callstack)
42
+ filename = expand_filename(filename)
43
+ return unless File.exist?(filename)
44
+ loaded_files[filename] = filter_callstack(caller) if subdirectory?(filename)
45
+ end
46
+
47
+ # Uninstall the hook. Generally useful only for testing the Diagnoser.
48
+ def remove_hook!
49
+ return unless Kernel.private_instance_methods.include?('require_without_diagnoser')
50
+ Kernel.class_eval do
51
+ alias :require :require_without_diagnoser
52
+ alias :load :load_without_diagnoser
53
+
54
+ undef_method(:require_without_diagnoser)
55
+ undef_method(:load_without_diagnoser)
56
+ end
57
+ true
58
+ end
59
+
60
+ # output the results of a diagnostic run.
61
+ #
62
+ # == Parameters
63
+ #
64
+ # * +stdout+ - An IO stream to output the results to.
65
+ def output_results(stdout)
66
+ project_prefix = Dir.pwd + "/"
67
+ minimify = lambda { |f| f.gsub(project_prefix, '')}
68
+ stdout.puts "- Spork Diagnosis -\n"
69
+ stdout.puts "-- Summary --"
70
+ stdout.puts loaded_files.keys.sort.map(&minimify)
71
+ stdout.puts "\n\n\n"
72
+ stdout.puts "-- Detail --"
73
+ loaded_files.keys.sort.each do |file|
74
+ stdout.puts "\n\n\n--- #{minimify.call(file)} ---\n"
75
+ stdout.puts loaded_files[file].map(&minimify)
76
+ end
77
+ end
78
+
79
+ private
80
+ def filter_callstack(callstack, entry_file = @entry_file)
81
+ callstack.pop until callstack.empty? || callstack.last.include?(@entry_file) if @entry_file
82
+ callstack.map do |line|
83
+ next if line.include?('lib/spork/diagnoser.rb')
84
+ line.gsub!('require_without_diagnoser', 'require')
85
+ line
86
+ end.compact
87
+ end
88
+
89
+ def expand_filename(filename)
90
+ ([Dir.pwd] + $:).each do |attempted_path|
91
+ attempted_filename = File.expand_path(filename, attempted_path)
92
+ return attempted_filename if File.file?(attempted_filename)
93
+ attempted_filename = attempted_filename + ".rb"
94
+ return attempted_filename if File.file?(attempted_filename)
95
+ end
96
+ filename
97
+ end
98
+
99
+ def subdirectory?(directory)
100
+ File.expand_path(directory, Dir.pwd).include?(@dir)
101
+ end
102
+ end
103
+ end
@@ -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
@@ -0,0 +1,70 @@
1
+ # A helper class that allows you to run a block inside of a fork, and then get the result from that block.
2
+ #
3
+ # == Example:
4
+ #
5
+ # forker = Spork::Forker.new do
6
+ # sleep 3
7
+ # "success"
8
+ # end
9
+ #
10
+ # forker.result # => "success"
11
+ class Spork::Forker
12
+
13
+ # Raised if the fork died (was killed) before it sent it's response back.
14
+ class ForkDiedException < Exception; end
15
+ def initialize(&block)
16
+ return unless block_given?
17
+ @child_io, @server_io = UNIXSocket.socketpair
18
+ @child_pid = Kernel.fork do
19
+ @server_io.close
20
+ Marshal.dump(yield, @child_io)
21
+ # wait for the parent to acknowledge receipt of the result.
22
+ master_response =
23
+ begin
24
+ Marshal.load(@child_io)
25
+ rescue EOFError
26
+ nil
27
+ end
28
+
29
+ # terminate, skipping any at_exit blocks.
30
+ exit!(0)
31
+ end
32
+ @child_io.close
33
+ end
34
+
35
+ # Wait for the fork to finish running, and then return its return value.
36
+ #
37
+ # If the fork was aborted, then result returns nil.
38
+ def result
39
+ return unless running?
40
+ result_thread = Thread.new do
41
+ begin
42
+ @result = Marshal.load(@server_io)
43
+ Marshal.dump('ACK', @server_io)
44
+ rescue ForkDiedException, EOFError
45
+ @result = nil
46
+ end
47
+ end
48
+ Process.wait(@child_pid)
49
+ result_thread.raise(ForkDiedException) if @result.nil?
50
+ @child_pid = nil
51
+ @result
52
+ end
53
+
54
+ # abort the current running fork
55
+ def abort
56
+ if running?
57
+ Process.kill(Signal.list['TERM'], @child_pid)
58
+ @child_pid = nil
59
+ true
60
+ end
61
+ end
62
+
63
+ def running?
64
+ return false unless @child_pid
65
+ Process.getpgid(@child_pid)
66
+ true
67
+ rescue Errno::ESRCH
68
+ false
69
+ end
70
+ end
@@ -0,0 +1,30 @@
1
+ class Spork::RunStrategy::Forking < Spork::RunStrategy
2
+ def self.available?
3
+ Kernel.respond_to?(:fork)
4
+ end
5
+
6
+ def run(argv, stderr, stdout)
7
+ abort if running?
8
+
9
+ @child = ::Spork::Forker.new do
10
+ $stdout, $stderr = stdout, stderr
11
+ load test_framework.helper_file
12
+ Spork.exec_each_run
13
+ test_framework.run_tests(argv, stderr, stdout)
14
+ end
15
+ @child.result
16
+ end
17
+
18
+ def abort
19
+ @child && @child.abort
20
+ end
21
+
22
+ def preload
23
+ test_framework.preload
24
+ end
25
+
26
+ def running?
27
+ @child && @child.running?
28
+ end
29
+
30
+ end
@@ -0,0 +1,40 @@
1
+ class Spork::RunStrategy
2
+ attr_reader :test_framework
3
+ @@run_strategies = []
4
+
5
+ def initialize(test_framework)
6
+ @test_framework = test_framework
7
+ end
8
+
9
+ def preload
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def run(argv, input, output)
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def cleanup
18
+ raise NotImplementedError
19
+ end
20
+
21
+ def running?
22
+ raise NotImplementedError
23
+ end
24
+
25
+ def abort
26
+ raise NotImplementedError
27
+ end
28
+
29
+ protected
30
+ def self.factory(test_framework)
31
+ Spork::RunStrategy::Forking.new(test_framework)
32
+ end
33
+
34
+ def self.inherited(subclass)
35
+ @@run_strategies << subclass
36
+ end
37
+
38
+ end
39
+
40
+ Dir[File.dirname(__FILE__) + "/run_strategy/*.rb"].each { |file| require file }
@@ -0,0 +1,90 @@
1
+ require 'optparse'
2
+
3
+ module Spork
4
+ # This is used by bin/spork. It's wrapped in a class because it's easier to test that way.
5
+ class Runner
6
+ attr_reader :test_framework
7
+
8
+ def self.run(args, output, error)
9
+ self.new(args, output, error).run
10
+ end
11
+
12
+ def initialize(args, output, error)
13
+ raise ArgumentError, "expected array of args" unless args.is_a?(Array)
14
+ @output = output
15
+ @error = error
16
+ @options = {}
17
+ opt = OptionParser.new
18
+ opt.banner = "Usage: spork [test framework name] [options]\n\n"
19
+
20
+ opt.separator "Options:"
21
+ opt.on("-b", "--bootstrap") {|ignore| @options[:bootstrap] = true }
22
+ opt.on("-d", "--diagnose") {|ignore| @options[:diagnose] = true }
23
+ opt.on("-h", "--help") {|ignore| @options[:help] = true }
24
+ opt.on("-p", "--port [PORT]") {|port| @options[:port] = port }
25
+ non_option_args = args.select { |arg| ! args[0].match(/^-/) }
26
+ @options[:server_matcher] = non_option_args[0]
27
+ opt.parse!(args)
28
+
29
+ if @options[:help]
30
+ @output.puts opt
31
+ @output.puts
32
+ @output.puts supported_test_frameworks_text
33
+ exit(0)
34
+ end
35
+ end
36
+
37
+ def supported_test_frameworks_text
38
+ text = StringIO.new
39
+
40
+ text.puts "Supported test frameworks:"
41
+ text.puts Spork::TestFramework.supported_test_frameworks.sort { |a,b| a.short_name <=> b.short_name }.map { |s| (s.available? ? '(*) ' : '( ) ') + s.short_name }
42
+ text.puts "\nLegend: ( ) - not detected in project (*) - detected\n"
43
+ text.string
44
+ end
45
+
46
+ # Returns a server for the specified (or the detected default) testing framework. Returns nil if none detected, or if the specified is not supported or available.
47
+ def find_test_framework
48
+ Spork::TestFramework.factory(@output, @error, options[:server_matcher])
49
+ rescue Spork::TestFramework::NoFrameworksAvailable => e
50
+ @error.puts e.message
51
+ rescue Spork::TestFramework::FactoryException => e
52
+ @error.puts "#{e.message}\n\n#{supported_test_frameworks_text}"
53
+ end
54
+
55
+ def run
56
+ return false unless test_framework = find_test_framework
57
+ ENV["DRB"] = 'true'
58
+ @error.puts "Using #{test_framework.short_name}"
59
+ @error.flush
60
+
61
+ case
62
+ when options[:bootstrap]
63
+ test_framework.bootstrap
64
+ when options[:diagnose]
65
+ require 'spork/diagnoser'
66
+
67
+ Spork::Diagnoser.install_hook!(test_framework.entry_point)
68
+ test_framework.preload
69
+ Spork::Diagnoser.output_results(@output)
70
+ return true
71
+ else
72
+ Spork.using_spork!
73
+ run_strategy = Spork::RunStrategy.factory(test_framework)
74
+ return(false) unless run_strategy.preload
75
+ Spork::Server.run(:port => @options[:port] || test_framework.default_port, :run_strategy => run_strategy)
76
+ return true
77
+ end
78
+ end
79
+
80
+ private
81
+ attr_reader :options
82
+
83
+ end
84
+ end
85
+
86
+
87
+
88
+
89
+
90
+
@@ -0,0 +1,74 @@
1
+ require 'drb/drb'
2
+ require 'rbconfig'
3
+ require 'spork/forker.rb'
4
+ require 'spork/custom_io_streams.rb'
5
+ require 'spork/app_framework.rb'
6
+
7
+ # An abstract class that is implemented to create a server
8
+ #
9
+ # (This was originally based off of spec_server.rb from rspec-rails (David Chelimsky), which was based on Florian Weber's TDDMate)
10
+ class Spork::Server
11
+ attr_reader :run_strategy
12
+ include Spork::CustomIOStreams
13
+
14
+ def initialize(options = {})
15
+ @run_strategy = options[:run_strategy]
16
+ @port = options[:port]
17
+ end
18
+
19
+ def self.run(options = {})
20
+ new(options).listen
21
+ end
22
+
23
+ # Sets up signals and starts the DRb service. If it's successful, it doesn't return. Not ever. You don't need to override this.
24
+ def listen
25
+ raise RuntimeError, "you must call Spork.using_spork! before starting the server" unless Spork.using_spork?
26
+ trap("SIGINT") { sig_int_received }
27
+ trap("SIGTERM") { abort; exit!(0) }
28
+ trap("USR2") { abort; restart } if Signal.list.has_key?("USR2")
29
+ @drb_service = DRb.start_service("druby://127.0.0.1:#{port}", self)
30
+ Spork.each_run { @drb_service.stop_service }
31
+ stderr.puts "Spork is ready and listening on #{port}!"
32
+ stderr.flush
33
+ DRb.thread.join
34
+ end
35
+
36
+ attr_accessor :port
37
+
38
+ # This is the public facing method that is served up by DRb. To use it from the client side (in a testing framework):
39
+ #
40
+ # DRb.start_service("druby://localhost:0") # this allows Ruby to do some magical stuff so you can pass an output stream over DRb.
41
+ # # see http://redmine.ruby-lang.org/issues/show/496 to see why localhost:0 is used.
42
+ # spec_server = DRbObject.new_with_uri("druby://127.0.0.1:8989")
43
+ # spec_server.run(options.argv, $stderr, $stdout)
44
+ #
45
+ # When implementing a test server, don't override this method: override run_tests instead.
46
+ def run(argv, stderr, stdout)
47
+ run_strategy.run(argv, stderr, stdout)
48
+ end
49
+
50
+ def abort
51
+ run_strategy.abort
52
+ end
53
+
54
+ private
55
+ def restart
56
+ stderr.puts "restarting"
57
+ stderr.flush
58
+ config = ::Config::CONFIG
59
+ ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
60
+ command_line = [ruby, $0, ARGV].flatten.join(' ')
61
+ exec(command_line)
62
+ end
63
+
64
+ def sig_int_received
65
+ stdout.puts "\n"
66
+ if run_strategy.running?
67
+ abort
68
+ stderr.puts "Running tests stopped. Press CTRL-C again to stop the server."
69
+ stderr.flush
70
+ else
71
+ exit!(0)
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ class Spork::TestFramework::Cucumber < Spork::TestFramework
2
+ DEFAULT_PORT = 8990
3
+ HELPER_FILE = File.join(Dir.pwd, "features/support/env.rb")
4
+
5
+ class << self
6
+ # REMOVE WHEN SUPPORT FOR 0.3.95 AND EARLIER IS DROPPED
7
+ attr_accessor :mother_object
8
+ end
9
+
10
+ def preload
11
+ require 'cucumber'
12
+ begin
13
+ @step_mother = ::Cucumber::StepMother.new
14
+ @step_mother.load_programming_language('rb')
15
+ rescue NoMethodError => pre_cucumber_0_4 # REMOVE WHEN SUPPORT FOR PRE-0.4 IS DROPPED
16
+ @step_mother = Spork::Server::Cucumber.mother_object
17
+ end
18
+ super
19
+ end
20
+
21
+ def run_tests(argv, stderr, stdout)
22
+ ::Cucumber::Cli::Main.new(argv, stdout, stderr).execute!(@step_mother)
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ class Spork::TestFramework::RSpec < Spork::TestFramework
2
+ DEFAULT_PORT = 8989
3
+ HELPER_FILE = File.join(Dir.pwd, "spec/spec_helper.rb")
4
+
5
+ def run_tests(argv, stderr, stdout)
6
+ ::Spec::Runner::CommandLine.run(
7
+ ::Spec::Runner::OptionParser.parse(
8
+ argv,
9
+ stderr,
10
+ stdout
11
+ )
12
+ )
13
+ end
14
+ end