Module: Spork
- Defined in:
- lib/spork.rb,
lib/spork/runner.rb
Defined Under Namespace
Modules: CustomIOStreams, GemHelpers Classes: AppFramework, Diagnoser, Forker, RunStrategy, Runner, Server, TestFramework
Constant Summary collapse
- BINARY =
File.(File.dirname(__FILE__) + '/../bin/spork')
- LIBDIR =
Pathname.new(File.(File.dirname(__FILE__)))
Class Method Summary collapse
-
.after_each_run(prevent_double_run = true, &block) ⇒ Object
Run a block after specs are run.
- .detect_and_require(subfolder) ⇒ Object
-
.each_run(prevent_double_run = true, &block) ⇒ Object
Run a block AFTER the fork occurs.
-
.exec_after_each_run ⇒ Object
Used by the server.
-
.exec_each_run(&block) ⇒ Object
Used by the server.
-
.exec_prefork(&block) ⇒ Object
Used by the server.
-
.other_spork_gem_load_paths ⇒ Object
This method is used to auto-discover peer plugins such as spork-testunit.
-
.prefork(prevent_double_run = true, &block) ⇒ Object
Run a block, during prefork mode.
- .state ⇒ Object
-
.trap_class_method(klass, method_name) ⇒ Object
Same as trap_method, but for class methods instead.
-
.trap_method(klass, method_name) ⇒ Object
Traps an instance method of a class (or module) so any calls to it don’t actually run until Spork.exec_each_run.
- .using_spork? ⇒ Boolean
Class Method Details
.after_each_run(prevent_double_run = true, &block) ⇒ Object
Run a block after specs are run.
Parameters
-
prevent_double_run
- Pass false to disable double run prevention
46 47 48 49 |
# File 'lib/spork.rb', line 46 def after_each_run(prevent_double_run = true, &block) return if prevent_double_run && already_ran?(caller.first) after_each_run_procs << block end |
.detect_and_require(subfolder) ⇒ Object
100 101 102 103 104 |
# File 'lib/spork.rb', line 100 def detect_and_require(subfolder) ([LIBDIR.to_s] + other_spork_gem_load_paths).uniq.each do |gem_path| Dir.glob(File.join(gem_path, subfolder)).each { |file| require file } end end |
.each_run(prevent_double_run = true, &block) ⇒ Object
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.
Parameters
-
prevent_double_run
- Pass false to disable double run prevention
32 33 34 35 36 37 38 39 |
# File 'lib/spork.rb', line 32 def each_run(prevent_double_run = true, &block) return if prevent_double_run && already_ran?(caller.first) if state == :prefork each_run_procs << block else yield end end |
.exec_after_each_run ⇒ Object
Used by the server. Called to run all of the after_each_run blocks.
75 76 77 78 79 |
# File 'lib/spork.rb', line 75 def exec_after_each_run # processes in reverse order similar to at_exit while p = after_each_run_procs.pop; p.call; end true end |
.exec_each_run(&block) ⇒ Object
Used by the server. Called to run all of the prefork blocks.
66 67 68 69 70 71 72 |
# File 'lib/spork.rb', line 66 def exec_each_run(&block) @state = :run activate_after_each_run_at_exit_hook each_run_procs.each { |p| p.call } each_run_procs.clear yield if block_given? end |
.exec_prefork(&block) ⇒ Object
Used by the server. Called when loading the prefork blocks of the code.
60 61 62 63 |
# File 'lib/spork.rb', line 60 def exec_prefork(&block) @state = :prefork yield end |
.other_spork_gem_load_paths ⇒ Object
This method is used to auto-discover peer plugins such as spork-testunit.
107 108 109 110 111 |
# File 'lib/spork.rb', line 107 def other_spork_gem_load_paths @other_spork_gem_load_paths ||= Spork::GemHelpers.latest_load_paths.grep(/spork/).select do |g| not g.match(%r{/spork-[0-9\-.]+/lib}) # don't include other versions of spork end end |
.prefork(prevent_double_run = true, &block) ⇒ Object
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.
Parameters
-
prevent_double_run
- Pass false to disable double run prevention
22 23 24 25 |
# File 'lib/spork.rb', line 22 def prefork(prevent_double_run = true, &block) return if prevent_double_run && already_ran?(caller.first) yield end |
.state ⇒ Object
55 56 57 |
# File 'lib/spork.rb', line 55 def state @state ||= :not_using_spork end |
.trap_class_method(klass, method_name) ⇒ Object
Same as trap_method, but for class methods instead
96 97 98 |
# File 'lib/spork.rb', line 96 def trap_class_method(klass, method_name) trap_method((class << klass; self; end), method_name) end |
.trap_method(klass, method_name) ⇒ Object
Traps an instance method of a class (or module) so any calls to it don’t actually run until Spork.exec_each_run
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/spork.rb', line 82 def trap_method(klass, method_name) method_name_without_spork, method_name_with_spork = alias_method_names(method_name, :spork) klass.class_eval <<-EOF, __FILE__, __LINE__ + 1 alias :#{method_name_without_spork} :#{method_name} unless method_defined?(:#{method_name_without_spork}) def #{method_name}(*args, &block) Spork.each_run(false) do #{method_name_without_spork}(*args, &block) end end EOF end |
.using_spork? ⇒ Boolean
51 52 53 |
# File 'lib/spork.rb', line 51 def using_spork? state != :not_using_spork end |