pietervisser-spork 0.7.5
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +11 -0
- data/assets/bootstrap.rb +29 -0
- data/bin/spork +20 -0
- data/features/cucumber_rails_integration.feature +118 -0
- data/features/diagnostic_mode.feature +41 -0
- data/features/rails_delayed_loading_workarounds.feature +115 -0
- data/features/rspec_rails_integration.feature +93 -0
- data/features/spork_debugger.feature +108 -0
- data/features/steps/general_steps.rb +3 -0
- data/features/steps/rails_steps.rb +53 -0
- data/features/steps/sandbox_steps.rb +115 -0
- data/features/support/background_job.rb +63 -0
- data/features/support/env.rb +111 -0
- data/features/unknown_app_framework.feature +42 -0
- data/geminstaller.yml +9 -0
- data/lib/spork/app_framework/rails.rb +158 -0
- data/lib/spork/app_framework/rails_stub_files/application.rb +1 -0
- data/lib/spork/app_framework/rails_stub_files/application_controller.rb +22 -0
- data/lib/spork/app_framework/rails_stub_files/application_helper.rb +3 -0
- data/lib/spork/app_framework/unknown.rb +6 -0
- data/lib/spork/app_framework.rb +74 -0
- data/lib/spork/custom_io_streams.rb +25 -0
- data/lib/spork/diagnoser.rb +103 -0
- data/lib/spork/ext/ruby-debug.rb +150 -0
- data/lib/spork/forker.rb +70 -0
- data/lib/spork/run_strategy/forking.rb +30 -0
- data/lib/spork/run_strategy.rb +40 -0
- data/lib/spork/runner.rb +90 -0
- data/lib/spork/server.rb +74 -0
- data/lib/spork/test_framework/cucumber.rb +24 -0
- data/lib/spork/test_framework/rspec.rb +14 -0
- data/lib/spork/test_framework.rb +167 -0
- data/lib/spork.rb +130 -0
- data/spec/spec_helper.rb +108 -0
- data/spec/spork/app_framework/rails_spec.rb +22 -0
- data/spec/spork/app_framework/unknown_spec.rb +12 -0
- data/spec/spork/app_framework_spec.rb +16 -0
- data/spec/spork/diagnoser_spec.rb +105 -0
- data/spec/spork/forker_spec.rb +44 -0
- data/spec/spork/run_strategy/forking_spec.rb +38 -0
- data/spec/spork/runner_spec.rb +50 -0
- data/spec/spork/server_spec.rb +15 -0
- data/spec/spork/test_framework/cucumber_spec.rb +11 -0
- data/spec/spork/test_framework/rspec_spec.rb +10 -0
- data/spec/spork/test_framework_spec.rb +114 -0
- data/spec/spork_spec.rb +157 -0
- data/spec/support/fake_framework.rb +15 -0
- data/spec/support/fake_run_strategy.rb +21 -0
- metadata +119 -0
@@ -0,0 +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')
|
data/lib/spork.rb
ADDED
@@ -0,0 +1,130 @@
|
|
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
|
+
path, file = File.split caller_line
|
112
|
+
if caller_line.end_with? file
|
113
|
+
file, line = file.split(":")
|
114
|
+
else
|
115
|
+
line = caller_line.match(/(\d+)$/)[1]
|
116
|
+
end
|
117
|
+
File.expand_path(File.join(path, file), Dir.pwd) + ":" + line
|
118
|
+
end
|
119
|
+
|
120
|
+
def already_ran?(caller_script_and_line)
|
121
|
+
return true if already_ran.include?(expanded_caller(caller_script_and_line))
|
122
|
+
already_ran << expanded_caller(caller_script_and_line)
|
123
|
+
false
|
124
|
+
end
|
125
|
+
|
126
|
+
def each_run_procs
|
127
|
+
@each_run_procs ||= []
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
unless $spec_helper_loaded
|
4
|
+
$spec_helper_loaded = true
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
|
7
|
+
SPEC_TMP_DIR = File.expand_path('tmp', File.dirname(__FILE__))
|
8
|
+
require 'spork'
|
9
|
+
require 'stringio'
|
10
|
+
require 'fileutils'
|
11
|
+
|
12
|
+
Spec::Runner.configure do |config|
|
13
|
+
config.before(:each) do
|
14
|
+
$test_stdout = StringIO.new
|
15
|
+
$test_stderr = StringIO.new
|
16
|
+
@current_dir = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
config.after(:each) do
|
20
|
+
FileUtils.rm_rf(SPEC_TMP_DIR) if File.directory?(SPEC_TMP_DIR)
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_file(filename, contents)
|
25
|
+
FileUtils.mkdir_p(SPEC_TMP_DIR) unless File.directory?(SPEC_TMP_DIR)
|
26
|
+
|
27
|
+
in_current_dir do
|
28
|
+
FileUtils.mkdir_p(File.dirname(filename))
|
29
|
+
File.open(filename, 'wb') { |f| f << contents }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def create_helper_file(test_framework = FakeFramework)
|
34
|
+
create_file(test_framework.helper_file, "# stub spec helper file")
|
35
|
+
end
|
36
|
+
|
37
|
+
def in_current_dir(&block)
|
38
|
+
Dir.chdir(current_dir, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def current_dir
|
42
|
+
@current_dir ||= SPEC_TMP_DIR
|
43
|
+
end
|
44
|
+
|
45
|
+
def change_current_dir(sub_path)
|
46
|
+
@current_dir = File.expand_path(sub_path, SPEC_TMP_DIR)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
module Spec
|
52
|
+
module Matchers
|
53
|
+
class IncludeAStringLike
|
54
|
+
def initialize(substring_or_regex)
|
55
|
+
case substring_or_regex
|
56
|
+
when String
|
57
|
+
@regex = Regexp.new(Regexp.escape(substring_or_regex))
|
58
|
+
when Regexp
|
59
|
+
@regex = substring_or_regex
|
60
|
+
else
|
61
|
+
raise ArgumentError, "don't know what to do with the #{substring_or_regex.class} you provided"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def matches?(list_of_strings)
|
66
|
+
@list_of_strings = list_of_strings
|
67
|
+
@list_of_strings.any? { |s| s =~ @regex }
|
68
|
+
end
|
69
|
+
def failure_message
|
70
|
+
"#{@list_of_strings.inspect} expected to include a string like #{@regex.inspect}"
|
71
|
+
end
|
72
|
+
def negative_failure_message
|
73
|
+
"#{@list_of_strings.inspect} expected to not include a string like #{@regex.inspect}, but did"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def include_a_string_like(substring_or_regex)
|
78
|
+
IncludeAStringLike.new(substring_or_regex)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module Spork::TestIOStreams
|
84
|
+
def self.included(klass)
|
85
|
+
klass.send(:extend, ::Spork::TestIOStreams::ClassMethods)
|
86
|
+
end
|
87
|
+
|
88
|
+
def stderr
|
89
|
+
self.class.stderr
|
90
|
+
end
|
91
|
+
|
92
|
+
def stdout
|
93
|
+
self.class.stdout
|
94
|
+
end
|
95
|
+
|
96
|
+
module ClassMethods
|
97
|
+
def stderr
|
98
|
+
$test_stderr
|
99
|
+
end
|
100
|
+
|
101
|
+
def stdout
|
102
|
+
$test_stdout
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
Dir.glob(File.dirname(__FILE__) + "/support/*.rb").each { |f| require(f) }
|
108
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
describe Spork::AppFramework::Rails do
|
4
|
+
describe ".version" do
|
5
|
+
it "detects the current version of rails" do
|
6
|
+
create_file("config/environment.rb", "RAILS_GEM_VERSION = '2.1.0'")
|
7
|
+
in_current_dir do
|
8
|
+
Spork::AppFramework::Rails.new.version.should == "2.1.0"
|
9
|
+
end
|
10
|
+
|
11
|
+
create_file("config/environment.rb", 'RAILS_GEM_VERSION = "2.1.0"')
|
12
|
+
in_current_dir do
|
13
|
+
Spork::AppFramework::Rails.new.version.should == "2.1.0"
|
14
|
+
end
|
15
|
+
|
16
|
+
create_file("config/environment.rb", 'RAILS_GEM_VERSION = "> 2.1.0"')
|
17
|
+
in_current_dir do
|
18
|
+
Spork::AppFramework::Rails.new.version.should == "> 2.1.0"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
describe Spork::AppFramework::Unknown do
|
4
|
+
it "requires bootstrapping" do
|
5
|
+
Spork::AppFramework::Unknown.new.bootstrap_required?.should == true
|
6
|
+
end
|
7
|
+
|
8
|
+
it "has no known entry point" do
|
9
|
+
Spork::AppFramework::Unknown.new.entry_point.should be_nil
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Spork::AppFramework do
|
4
|
+
describe ".detect_framework" do
|
5
|
+
it "detects when rails is installed and available" do
|
6
|
+
create_file("config/environment.rb", "RAILS_GEM_VERSION = '2.1.0'")
|
7
|
+
in_current_dir do
|
8
|
+
Spork::AppFramework.detect_framework.short_name.should == "Rails"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns Unknown when no framework known detected" do
|
13
|
+
Spork::AppFramework.detect_framework.short_name.should == "Unknown"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Spork::Diagnoser do
|
5
|
+
after(:each) do
|
6
|
+
Spork::Diagnoser.remove_hook!
|
7
|
+
Spork::Diagnoser.loaded_files.clear
|
8
|
+
end
|
9
|
+
|
10
|
+
def run_simulation(directory, filename = nil, contents = nil, &block)
|
11
|
+
FileUtils.mkdir_p(directory)
|
12
|
+
Dir.chdir(directory) do
|
13
|
+
if filename
|
14
|
+
File.open(filename, 'wb') { |f| f << contents }
|
15
|
+
Spork::Diagnoser.install_hook!
|
16
|
+
require filename
|
17
|
+
end
|
18
|
+
yield if block_given?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "installs it's hook and tells you when files have been loaded" do
|
23
|
+
run_simulation(SPEC_TMP_DIR, 'my_awesome_library_include.rb', '1 + 5')
|
24
|
+
Spork::Diagnoser.loaded_files.keys.should include_a_string_like('my_awesome_library_include')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'excludes files outside of Dir.pwd' do
|
28
|
+
run_simulation(SPEC_TMP_DIR + '/project_root', '../external_dependency.rb', '1 + 5')
|
29
|
+
Spork::Diagnoser.loaded_files.keys.should_not include_a_string_like('external_dependency')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "excludes files outside of Dir.pwd but in ruby's include path" do
|
33
|
+
directory = SPEC_TMP_DIR + '/project_root'
|
34
|
+
external_dependency_dir = SPEC_TMP_DIR + '/external_dependency'
|
35
|
+
$: << external_dependency_dir
|
36
|
+
FileUtils.mkdir_p(directory)
|
37
|
+
FileUtils.mkdir_p(external_dependency_dir)
|
38
|
+
Dir.chdir(directory) do
|
39
|
+
File.open(external_dependency_dir + '/the_most_awesome_external_dependency_ever.rb', 'wb') { |f| f << 'funtimes = true' }
|
40
|
+
Spork::Diagnoser.install_hook!
|
41
|
+
require 'the_most_awesome_external_dependency_ever'
|
42
|
+
end
|
43
|
+
|
44
|
+
Spork::Diagnoser.loaded_files.keys.should_not include_a_string_like('the_most_awesome_external_dependency_ever')
|
45
|
+
$:.pop
|
46
|
+
end
|
47
|
+
|
48
|
+
it "expands files to their fully their fully qualified path" do
|
49
|
+
directory = SPEC_TMP_DIR + '/project_root'
|
50
|
+
lib_directory = directory + '/lib'
|
51
|
+
$: << lib_directory
|
52
|
+
FileUtils.mkdir_p(lib_directory)
|
53
|
+
Dir.chdir(directory) do
|
54
|
+
File.open(lib_directory + "/the_most_awesome_lib_file_ever.rb", "wb") { |f| f << "funtimes = true" }
|
55
|
+
Spork::Diagnoser.install_hook!
|
56
|
+
require 'the_most_awesome_lib_file_ever'
|
57
|
+
end
|
58
|
+
|
59
|
+
Spork::Diagnoser.loaded_files.keys.should include_a_string_like('lib/the_most_awesome_lib_file_ever')
|
60
|
+
$:.pop
|
61
|
+
end
|
62
|
+
|
63
|
+
it "can tell the difference between a folder in the project path and a file in an external path" do
|
64
|
+
directory = SPEC_TMP_DIR + '/project_root'
|
65
|
+
external_dependency_dir = SPEC_TMP_DIR + '/external_dependency'
|
66
|
+
$: << external_dependency_dir
|
67
|
+
FileUtils.mkdir_p(directory)
|
68
|
+
FileUtils.mkdir_p(external_dependency_dir)
|
69
|
+
Dir.chdir(directory) do
|
70
|
+
FileUtils.mkdir_p(directory + '/a_popular_folder_name')
|
71
|
+
File.open(external_dependency_dir + '/a_popular_folder_name.rb', 'wb') { |f| f << 'funtimes = true' }
|
72
|
+
Spork::Diagnoser.install_hook!
|
73
|
+
require 'a_popular_folder_name'
|
74
|
+
end
|
75
|
+
|
76
|
+
Spork::Diagnoser.loaded_files.keys.should_not include_a_string_like('a_popular_folder_name')
|
77
|
+
$:.pop
|
78
|
+
end
|
79
|
+
|
80
|
+
it "filters backtrace beyond the last line matching the entry point" do
|
81
|
+
Spork::Diagnoser.install_hook!("test_filter/environment.rb")
|
82
|
+
create_file("test_filter/environment.rb", "require 'test_filter/app.rb'")
|
83
|
+
create_file("test_filter/app.rb", "require 'test_filter/my_model.rb'")
|
84
|
+
create_file("test_filter/my_model.rb", "'my model here'")
|
85
|
+
in_current_dir do
|
86
|
+
require 'test_filter/environment.rb'
|
87
|
+
end
|
88
|
+
f = Spork::Diagnoser.loaded_files
|
89
|
+
f[f.keys.grep(/app.rb/).first].last.should include('test_filter/environment.rb')
|
90
|
+
f[f.keys.grep(/my_model.rb/).first].last.should include('test_filter/environment.rb')
|
91
|
+
f[f.keys.grep(/environment.rb/).first].should == []
|
92
|
+
end
|
93
|
+
|
94
|
+
describe ".output_results" do
|
95
|
+
it "outputs the results relative to the current directory" do
|
96
|
+
Spork::Diagnoser.loaded_files["/project_path/lib/file.rb"] = "/project_path/lib/parent_file.rb:35"
|
97
|
+
Dir.stub!(:pwd).and_return("/project_path")
|
98
|
+
out = StringIO.new
|
99
|
+
Spork::Diagnoser.output_results(out)
|
100
|
+
out.string.should =~ %r([^/]lib/file.rb)
|
101
|
+
out.string.should =~ %r([^/]lib/parent_file.rb)
|
102
|
+
out.string.should_not include("/project_path/")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Spork::Forker do
|
4
|
+
describe ".new" do
|
5
|
+
it "runs a block in a fork" do
|
6
|
+
$var = "hello world"
|
7
|
+
Spork::Forker.new { $var = "booyah" }.result
|
8
|
+
$var.should == "hello world"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#result" do
|
13
|
+
it "returns the result" do
|
14
|
+
Spork::Forker.new { "results" }.result.should == "results"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#running?" do
|
19
|
+
it "reports when the fork is running" do
|
20
|
+
forker = Spork::Forker.new { sleep 0.1 }
|
21
|
+
forker.running?.should == true
|
22
|
+
forker.result
|
23
|
+
sleep 0.1
|
24
|
+
forker.running?.should == false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#abort" do
|
29
|
+
it "aborts a fork and returns nil for the result" do
|
30
|
+
started_at = Time.now
|
31
|
+
ended_at = nil
|
32
|
+
forker = Spork::Forker.new { sleep 5 }
|
33
|
+
Thread.new do
|
34
|
+
forker.result.should == nil
|
35
|
+
ended_at = Time.now
|
36
|
+
end
|
37
|
+
sleep 0.5
|
38
|
+
forker.abort
|
39
|
+
sleep 0.1
|
40
|
+
(ended_at - started_at).should be_close(0.5, 0.1)
|
41
|
+
forker.running?.should == false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
|
+
|
3
|
+
describe Spork::RunStrategy::Forking do
|
4
|
+
before(:each) do
|
5
|
+
@fake_framework = FakeFramework.new
|
6
|
+
@run_strategy = Spork::RunStrategy::Forking.new(@fake_framework)
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns the result of the run_tests method from the forked child" do
|
11
|
+
create_helper_file
|
12
|
+
@fake_framework.stub!(:run_tests).and_return("tests were ran")
|
13
|
+
@run_strategy.run("test", STDOUT, STDIN).should == "tests were ran"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "aborts the current running thread when another run is started" do
|
17
|
+
create_helper_file
|
18
|
+
@fake_framework.wait_time = 0.25
|
19
|
+
first_run = Thread.new { @run_strategy.run("test", STDOUT, STDIN).should == nil }
|
20
|
+
sleep(0.05)
|
21
|
+
@run_strategy.run("test", STDOUT, STDIN).should == true
|
22
|
+
|
23
|
+
# wait for the first to finish
|
24
|
+
first_run.join
|
25
|
+
end
|
26
|
+
|
27
|
+
it "can abort the current run" do
|
28
|
+
create_helper_file
|
29
|
+
@fake_framework.wait_time = 5
|
30
|
+
started_at = Time.now
|
31
|
+
first_run = Thread.new { @run_strategy.run("test", STDOUT, STDIN).should == true }
|
32
|
+
sleep(0.05)
|
33
|
+
@run_strategy.send(:abort)
|
34
|
+
sleep(0.01) while @run_strategy.running?
|
35
|
+
|
36
|
+
(Time.now - started_at).should < @fake_framework.wait_time
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Spork::Runner do
|
4
|
+
def use_test_server(klass = Spork::TestFramework::RSpec)
|
5
|
+
@test_framework = klass.new
|
6
|
+
Spork::TestFramework.stub!(:factory).and_return(@test_framework)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@out, @err = StringIO.new, StringIO.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "finds a matching server with a prefix" do
|
14
|
+
Spork::Runner.new(["rs"], @out, @err).find_test_framework.class.should == Spork::TestFramework::RSpec
|
15
|
+
end
|
16
|
+
|
17
|
+
it "shows an error message if no matching server was found" do
|
18
|
+
Spork::Runner.new(["argle_bargle"], @out, @err).run.should == false
|
19
|
+
@err.string.should include(%(Couldn't find a supported test framework that begins with 'argle_bargle'))
|
20
|
+
end
|
21
|
+
|
22
|
+
it "bootstraps a server when -b is passed in" do
|
23
|
+
use_test_server
|
24
|
+
@test_framework.should_receive(:bootstrap)
|
25
|
+
Spork::Runner.new(['rspec', '-b'], @out, @err).run
|
26
|
+
end
|
27
|
+
|
28
|
+
it "aborts if it can't preload" do
|
29
|
+
use_test_server
|
30
|
+
@test_framework.should_receive(:preload).and_return(false)
|
31
|
+
Spork::Server.should_not_receive(:run)
|
32
|
+
Spork::Runner.new(['rspec'], @out, @err).run
|
33
|
+
end
|
34
|
+
|
35
|
+
it "runs the server if all is well" do
|
36
|
+
use_test_server
|
37
|
+
@test_framework.should_receive(:preload).and_return(true)
|
38
|
+
Spork::Server.should_receive(:run)
|
39
|
+
Spork::Runner.new(['rspec'], @out, @err).run
|
40
|
+
@err.string.should include("Using RSpec")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "outputs a list of supported servers, along with supported asterisk" do
|
44
|
+
Spork::Server.stub!(:supported_test_frameworks).and_return([Spork::TestFramework::RSpec, Spork::TestFramework::Cucumber])
|
45
|
+
Spork::TestFramework::RSpec.stub!(:available?).and_return(true)
|
46
|
+
Spork::TestFramework::Cucumber.stub!(:available?).and_return(false)
|
47
|
+
|
48
|
+
Spork::Runner.new(['rspec'], @out, @err).supported_test_frameworks_text.should include("(*) RSpec")
|
49
|
+
end
|
50
|
+
end
|