Skip to content

Commit 8ea2cf8

Browse files
committed
Add crazy monkey patch^2 to address Rails'
Rails interferes with the Test::Unit::TestCase#run method, but doesn't properly emit all events. Replace their monkey patch with our own. Use a nerfed form of this monkey patch when trying for a dry run. Thanks to Comron for help identifying the problem!
1 parent da22c6e commit 8ea2cf8

File tree

1 file changed

+87
-3
lines changed

1 file changed

+87
-3
lines changed

src/qa/runner-assets/ruby/test-unit.rb

+87-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ def tapout_fault(fault)
146146

147147
@stdcom.drain!(doc)
148148

149-
150149
@trace.emit_stats
151150
emit doc
152151
@already_outputted = true
@@ -204,9 +203,94 @@ def emit(doc)
204203
Test::Unit::TestCase.class_eval do
205204
remove_method :run_test
206205
def run_test; end
207-
def run_setup; end
208-
def run_teardown; end
206+
207+
def run_setup
208+
yield if block_given?
209+
end
210+
209211
def run_cleanup; end
212+
def run_teardown; end
213+
end
214+
215+
if defined?(::ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit)
216+
# Modified from https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/testing/setup_and_teardown.rb#L61
217+
::ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit.module_eval do
218+
remove_method :run
219+
220+
# This redefinition is unfortunate but test/unit shows us no alternative.
221+
# Doubly unfortunate: hax to support Mocha's hax.
222+
def run(result)
223+
return if @method_name.to_s == "default_test"
224+
225+
@_result = result
226+
@internal_data.test_started
227+
228+
yield(Test::Unit::TestCase::STARTED, name)
229+
yield(Test::Unit::TestCase::STARTED_OBJECT, self)
230+
231+
@internal_data.test_finished
232+
result.add_run
233+
yield(Test::Unit::TestCase::FINISHED, name)
234+
yield(Test::Unit::TestCase::FINISHED_OBJECT, self)
235+
end
236+
end
237+
end
238+
else
239+
if defined?(::ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit)
240+
# Modified from https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/testing/setup_and_teardown.rb#L61
241+
::ActiveSupport::Testing::SetupAndTeardown::ForClassicTestUnit.module_eval do
242+
remove_method :run
243+
244+
# This redefinition is unfortunate but test/unit shows us no alternative.
245+
# Doubly unfortunate: hax to support Mocha's hax.
246+
def run(result)
247+
return if @method_name.to_s == "default_test"
248+
249+
@_result = result
250+
@internal_data.test_started
251+
252+
mocha_counter = retrieve_mocha_counter(self, result)
253+
yield(Test::Unit::TestCase::STARTED, name)
254+
yield(Test::Unit::TestCase::STARTED_OBJECT, self)
255+
256+
begin
257+
begin
258+
run_callbacks :setup do
259+
setup
260+
__send__(@method_name)
261+
mocha_verify(mocha_counter) if mocha_counter
262+
end
263+
rescue Mocha::ExpectationError => e
264+
add_failure(e.message, e.backtrace)
265+
rescue Test::Unit::AssertionFailedError => e
266+
add_failure(e.message, e.backtrace)
267+
rescue Exception => e
268+
raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
269+
add_error(e)
270+
ensure
271+
begin
272+
teardown
273+
run_callbacks :teardown
274+
rescue Mocha::ExpectationError => e
275+
add_failure(e.message, e.backtrace)
276+
rescue Test::Unit::AssertionFailedError => e
277+
add_failure(e.message, e.backtrace)
278+
rescue Exception => e
279+
raise if PASSTHROUGH_EXCEPTIONS.include?(e.class)
280+
add_error(e)
281+
end
282+
end
283+
ensure
284+
mocha_teardown if mocha_counter
285+
end
286+
287+
result.add_run
288+
@internal_data.test_finished
289+
290+
yield(Test::Unit::TestCase::FINISHED, name)
291+
yield(Test::Unit::TestCase::FINISHED_OBJECT, self)
292+
end
293+
end
210294
end
211295
end
212296

0 commit comments

Comments
 (0)