Introduced LogClient class (preparing for parallel cmdtest).
This commit is contained in:
parent
daa7f12fbd
commit
bb70f757ec
132
bin/cmdtest.rb
132
bin/cmdtest.rb
@ -47,6 +47,56 @@ module Cmdtest
|
|||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
|
|
||||||
|
class LogClient
|
||||||
|
def initialize
|
||||||
|
@listeners = []
|
||||||
|
|
||||||
|
@n_assert_failures = 0
|
||||||
|
@n_assert_errors = 0
|
||||||
|
@n_assert_successes = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_listener(listener)
|
||||||
|
@listeners << listener
|
||||||
|
end
|
||||||
|
|
||||||
|
def _notify_once(method, *args)
|
||||||
|
for listener in @listeners
|
||||||
|
listener.send(method, *args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def notify(method, *args)
|
||||||
|
if block_given?
|
||||||
|
_notify_once(method + "_begin", *args)
|
||||||
|
yield
|
||||||
|
_notify_once(method + "_end", *args)
|
||||||
|
else
|
||||||
|
_notify_once(method, *args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def everything_ok?
|
||||||
|
@n_assert_errors == 0 && @n_assert_failures == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_success
|
||||||
|
@n_assert_successes += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_failure(str)
|
||||||
|
@n_assert_failures += 1
|
||||||
|
notify("assert_failure", str)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assert_error(str)
|
||||||
|
@n_assert_errors += 1
|
||||||
|
notify("assert_error", str)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
#----------------------------------------------------------------------
|
||||||
|
|
||||||
class TestMethod
|
class TestMethod
|
||||||
|
|
||||||
def initialize(test_method, test_class)
|
def initialize(test_method, test_class)
|
||||||
@ -75,16 +125,16 @@ module Cmdtest
|
|||||||
!selected || runner.method_filter.skip?(*method_id)
|
!selected || runner.method_filter.skip?(*method_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(runner)
|
def run(clog, runner)
|
||||||
runner.notify("testmethod", @test_method) do
|
clog.notify("testmethod", @test_method) do
|
||||||
obj = @test_class.testcase_class.new(self, runner)
|
obj = @test_class.testcase_class.new(self, clog, runner)
|
||||||
obj._work_dir.chdir do
|
obj._work_dir.chdir do
|
||||||
obj.setup
|
obj.setup
|
||||||
begin
|
begin
|
||||||
obj.send(@test_method)
|
obj.send(@test_method)
|
||||||
runner.assert_success
|
clog.assert_success
|
||||||
rescue Cmdtest::AssertFailed => e
|
rescue Cmdtest::AssertFailed => e
|
||||||
runner.assert_failure(e.message)
|
clog.assert_failure(e.message)
|
||||||
runner.method_filter.error(*method_id)
|
runner.method_filter.error(*method_id)
|
||||||
rescue => e
|
rescue => e
|
||||||
io = StringIO.new
|
io = StringIO.new
|
||||||
@ -92,7 +142,7 @@ module Cmdtest
|
|||||||
io.puts " " + e.message + " (#{e.class})"
|
io.puts " " + e.message + " (#{e.class})"
|
||||||
io.puts "BACKTRACE:"
|
io.puts "BACKTRACE:"
|
||||||
io.puts e.backtrace.map {|line| " " + line }
|
io.puts e.backtrace.map {|line| " " + line }
|
||||||
runner.assert_error(io.string)
|
clog.assert_error(io.string)
|
||||||
runner.method_filter.error(*method_id)
|
runner.method_filter.error(*method_id)
|
||||||
end
|
end
|
||||||
obj.teardown
|
obj.teardown
|
||||||
@ -116,11 +166,11 @@ module Cmdtest
|
|||||||
@testcase_class.name.sub(/^.*::/, "")
|
@testcase_class.name.sub(/^.*::/, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(runner)
|
def run(clog, runner)
|
||||||
runner.notify("testclass", @testcase_class) do
|
clog.notify("testclass", @testcase_class) do
|
||||||
get_test_methods(runner).each do |method|
|
get_test_methods(runner).each do |method|
|
||||||
test_method = TestMethod.new(method, self)
|
test_method = TestMethod.new(method, self)
|
||||||
test_method.run(runner) unless test_method.skip?(runner)
|
test_method.run(clog, runner) unless test_method.skip?(runner)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -151,11 +201,11 @@ module Cmdtest
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(runner)
|
def run(clog, runner)
|
||||||
runner.notify("testfile", @path) do
|
clog.notify("testfile", @path) do
|
||||||
for test_class in @test_classes
|
for test_class in @test_classes
|
||||||
if ! test_class.get_test_methods(runner).empty?
|
if ! test_class.get_test_methods(runner).empty?
|
||||||
test_class.run(runner)
|
test_class.run(clog, runner)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -173,31 +223,10 @@ module Cmdtest
|
|||||||
|
|
||||||
def initialize(project_dir, opts)
|
def initialize(project_dir, opts)
|
||||||
@project_dir = project_dir
|
@project_dir = project_dir
|
||||||
@listeners = []
|
|
||||||
@opts = opts
|
@opts = opts
|
||||||
@method_filter = MethodFilter.new(FILTER_FILENAME, self)
|
@method_filter = MethodFilter.new(FILTER_FILENAME, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_listener(listener)
|
|
||||||
@listeners << listener
|
|
||||||
end
|
|
||||||
|
|
||||||
def notify_once(method, *args)
|
|
||||||
for listener in @listeners
|
|
||||||
listener.send(method, *args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def notify(method, *args)
|
|
||||||
if block_given?
|
|
||||||
notify_once(method + "_begin", *args)
|
|
||||||
yield
|
|
||||||
notify_once(method + "_end", *args)
|
|
||||||
else
|
|
||||||
notify_once(method, *args)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def _path_separator
|
def _path_separator
|
||||||
RbConfig::CONFIG["PATH_SEPARATOR"] || ":"
|
RbConfig::CONFIG["PATH_SEPARATOR"] || ":"
|
||||||
end
|
end
|
||||||
@ -206,7 +235,7 @@ module Cmdtest
|
|||||||
@orig_env_path.dup
|
@orig_env_path.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run(clog)
|
||||||
@orig_cwd = Dir.pwd
|
@orig_cwd = Dir.pwd
|
||||||
ENV["PATH"] = Dir.pwd + _path_separator + ENV["PATH"]
|
ENV["PATH"] = Dir.pwd + _path_separator + ENV["PATH"]
|
||||||
@orig_env_path = ENV["PATH"].split(_path_separator)
|
@orig_env_path = ENV["PATH"].split(_path_separator)
|
||||||
@ -233,34 +262,13 @@ module Cmdtest
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@n_assert_failures = 0
|
clog.notify("testsuite") do
|
||||||
@n_assert_errors = 0
|
|
||||||
@n_assert_successes = 0
|
|
||||||
notify("testsuite") do
|
|
||||||
for test_file in test_files
|
for test_file in test_files
|
||||||
test_file.run(self)
|
test_file.run(clog, self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@method_filter.write
|
@method_filter.write
|
||||||
end
|
end
|
||||||
|
|
||||||
def everything_ok?
|
|
||||||
@n_assert_errors == 0 && @n_assert_failures == 0
|
|
||||||
end
|
|
||||||
|
|
||||||
def assert_success
|
|
||||||
@n_assert_successes += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def assert_failure(str)
|
|
||||||
@n_assert_failures += 1
|
|
||||||
notify("assert_failure", str)
|
|
||||||
end
|
|
||||||
|
|
||||||
def assert_error(str)
|
|
||||||
@n_assert_errors += 1
|
|
||||||
notify("assert_error", str)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
@ -402,17 +410,19 @@ module Cmdtest
|
|||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
clog = LogClient.new
|
||||||
|
|
||||||
Util.opts = self
|
Util.opts = self
|
||||||
@project_dir = ProjectDir.new(files)
|
@project_dir = ProjectDir.new(files)
|
||||||
@runner = Runner.new(@project_dir, self)
|
@runner = Runner.new(@project_dir, self)
|
||||||
logger = ConsoleLogger.new(self)
|
logger = ConsoleLogger.new(self)
|
||||||
@runner.add_listener(logger)
|
clog.add_listener(logger)
|
||||||
if @xml
|
if @xml
|
||||||
@runner.add_listener(JunitLogger.new(self, @xml))
|
clog.add_listener(JunitLogger.new(self, @xml))
|
||||||
end
|
end
|
||||||
|
|
||||||
@runner.run
|
@runner.run(clog)
|
||||||
error_exit = @set_exit_code && ! @runner.everything_ok?
|
error_exit = @set_exit_code && ! clog.everything_ok?
|
||||||
exit( error_exit ? 1 : 0 )
|
exit( error_exit ? 1 : 0 )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,8 +64,9 @@ module Cmdtest
|
|||||||
|
|
||||||
attr_reader :_work_dir
|
attr_reader :_work_dir
|
||||||
|
|
||||||
def initialize(test_method, runner)
|
def initialize(test_method, clog, runner)
|
||||||
@_test_method = test_method
|
@_test_method = test_method
|
||||||
|
@_clog = clog
|
||||||
@_runner = runner
|
@_runner = runner
|
||||||
@_work_dir = Workdir.new(self, runner)
|
@_work_dir = Workdir.new(self, runner)
|
||||||
@_in_cmd = false
|
@_in_cmd = false
|
||||||
@ -541,7 +542,7 @@ module Cmdtest
|
|||||||
return if @_cmd_done
|
return if @_cmd_done
|
||||||
@_cmd_done = true
|
@_cmd_done = true
|
||||||
|
|
||||||
@_runner.notify("cmdline", @_cmdline, @_comment_str)
|
@_clog.notify("cmdline", @_cmdline, @_comment_str)
|
||||||
@_comment_str = nil
|
@_comment_str = nil
|
||||||
@_t1 = Time.now
|
@_t1 = Time.now
|
||||||
@_effects = @_work_dir.run_cmd(@_cmdline, @_env_path)
|
@_effects = @_work_dir.run_cmd(@_cmdline, @_env_path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user