1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow ActiveSupport's isolation tests to run with MiniTest on 1.9

This commit is contained in:
Carl Lerche 2009-12-30 20:48:15 -08:00
parent 1fbd02e446
commit d39d7f5f44
2 changed files with 208 additions and 160 deletions

View file

@ -36,6 +36,15 @@ module ActiveSupport
!ENV["NO_FORK"] && RUBY_PLATFORM !~ /mswin|mingw|java/ !ENV["NO_FORK"] && RUBY_PLATFORM !~ /mswin|mingw|java/
end end
def self.included(base)
if defined?(::MiniTest) && base < ::MiniTest::Unit::TestCase
base.send :include, MiniTest
elsif defined?(Test::Unit)
base.send :include, TestUnit
end
end
module TestUnit
def run(result) def run(result)
unless defined?(@@ran_class_setup) unless defined?(@@ran_class_setup)
self.class.setup if self.class.respond_to?(:setup) self.class.setup if self.class.respond_to?(:setup)
@ -54,10 +63,29 @@ module ActiveSupport
end end
end end
proxy = Marshal.load(serialized) retval, proxy = Marshal.load(serialized)
proxy.__replay__(@_result) proxy.__replay__(@_result)
yield(Test::Unit::TestCase::FINISHED, name) yield(Test::Unit::TestCase::FINISHED, name)
retval
end
end
module MiniTest
def run(runner)
unless defined?(@@ran_class_setup)
self.class.setup if self.class.respond_to?(:setup)
@@ran_class_setup = true
end
serialized = run_in_isolation do |runner|
super(runner)
end
retval, proxy = Marshal.load(serialized)
proxy.__replay__(runner)
retval
end
end end
module Forking module Forking
@ -67,8 +95,8 @@ module ActiveSupport
pid = fork do pid = fork do
read.close read.close
proxy = ProxyTestResult.new proxy = ProxyTestResult.new
yield proxy retval = yield proxy
write.puts [Marshal.dump(proxy)].pack("m") write.puts [Marshal.dump([retval, proxy])].pack("m")
exit! exit!
end end
@ -87,9 +115,9 @@ module ActiveSupport
if ENV["ISOLATION_TEST"] if ENV["ISOLATION_TEST"]
proxy = ProxyTestResult.new proxy = ProxyTestResult.new
yield proxy retval = yield proxy
File.open(ENV["ISOLATION_OUTPUT"], "w") do |file| File.open(ENV["ISOLATION_OUTPUT"], "w") do |file|
file.puts [Marshal.dump(proxy)].pack("m") file.puts [Marshal.dump([retval, proxy])].pack("m")
end end
exit! exit!
else else

View file

@ -1,12 +1,10 @@
require 'abstract_unit' require 'abstract_unit'
require 'rbconfig' require 'rbconfig'
if defined?(MiniTest) || defined?(Test::Unit::TestResultFailureSupport) # if defined?(MiniTest) || defined?(Test::Unit::TestResultFailureSupport)
$stderr.puts "Isolation tests can test test-unit 1 only" # $stderr.puts "Isolation tests can test test-unit 1 only"
else if ENV['CHILD']
# Does awesome
if ENV['CHILD']
class ChildIsolationTest < ActiveSupport::TestCase class ChildIsolationTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
@ -72,7 +70,7 @@ else
require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg")) require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
end end
end end
else else
class ParentIsolationTest < ActiveSupport::TestCase class ParentIsolationTest < ActiveSupport::TestCase
File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "w") {} File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "w") {}
@ -82,7 +80,10 @@ else
ENV.delete("CHILD") ENV.delete("CHILD")
def setup def setup
# Extract the results defined?(::MiniTest) ? parse_minitest : parse_testunit
end
def parse_testunit
@results = {} @results = {}
OUTPUT[/Started\n\s*(.*)\s*\nFinished/mi, 1].to_s.split(/\s*\n\s*/).each do |result| OUTPUT[/Started\n\s*(.*)\s*\nFinished/mi, 1].to_s.split(/\s*\n\s*/).each do |result|
result =~ %r'^(\w+)\(\w+\):\s*(\.|E|F)$' result =~ %r'^(\w+)\(\w+\):\s*(\.|E|F)$'
@ -98,16 +99,36 @@ else
end end
end end
def parse_minitest
@results = {}
OUTPUT[/Started\n\s*(.*)\s*\nFinished/mi, 1].to_s.split(/\s*\n\s*/).each do |result|
result =~ %r'^\w+#(\w+):.*:\s*(.*Assertion.*|.*RuntimeError.*|\.\s*)$'
val = :success
val = :error if $2.include?('RuntimeError')
val = :failure if $2.include?('Assertion')
@results[$1] = val
end
# Extract the backtraces
@backtraces = {}
OUTPUT.scan(/^\s*\d+\).*?\n\n/m).each do |backtrace|
# \n 1) Error:\ntest_captures_errors(ChildIsolationTest):
backtrace =~ %r'\s*\d+\)\s*(Error|Failure):\n(\w+)'i
@backtraces[$2] = { :type => $1, :output => backtrace }
end
end
def assert_failing(name) def assert_failing(name)
assert_equal :failure, @results[name.to_s], "Test #{name} did not fail" assert_equal :failure, @results[name.to_s], "Test #{name} failed"
end end
def assert_passing(name) def assert_passing(name)
assert_equal :success, @results[name.to_s], "Test #{name} did not pass" assert_equal :success, @results[name.to_s], "Test #{name} passed"
end end
def assert_erroring(name) def assert_erroring(name)
assert_equal :error, @results[name.to_s], "Test #{name} did not error" assert_equal :error, @results[name.to_s], "Test #{name} errored"
end end
test "has all tests" do test "has all tests" do
@ -144,12 +165,12 @@ else
test "backtrace is printed for errors" do test "backtrace is printed for errors" do
assert_equal 'Error', @backtraces["test_captures_errors"][:type] assert_equal 'Error', @backtraces["test_captures_errors"][:type]
assert_match %r{isolation_test.rb:\d+:in `test_captures_errors'}, @backtraces["test_captures_errors"][:output] assert_match %r{isolation_test.rb:\d+}, @backtraces["test_captures_errors"][:output]
end end
test "backtrace is printed for failures" do test "backtrace is printed for failures" do
assert_equal 'Failure', @backtraces["test_captures_failures"][:type] assert_equal 'Failure', @backtraces["test_captures_failures"][:type]
assert_match %r{isolation_test.rb:\d+:in `test_captures_failures'}, @backtraces["test_captures_failures"][:output] assert_match %r{isolation_test.rb:\d+}, @backtraces["test_captures_failures"][:output]
end end
test "self.setup is run only once" do test "self.setup is run only once" do
@ -158,5 +179,4 @@ else
end end
end end
end
end end