Yoji Shidara
null+****@clear*****
Thu Sep 19 12:12:52 JST 2013
Yoji Shidara 2013-09-19 12:12:52 +0900 (Thu, 19 Sep 2013) New Revision: fce8dfb4ab5024a9560cd5264862fe464db46673 https://github.com/droonga/drntest/commit/fce8dfb4ab5024a9560cd5264862fe464db46673 Message: Extract TestRunner Copied files: lib/drntest/test-runner.rb (from lib/drntest/tester.rb) Modified files: lib/drntest/tester.rb Copied: lib/drntest/test-runner.rb (+7 -73) 51% =================================================================== --- lib/drntest/tester.rb 2013-09-18 18:22:22 +0900 (e1b1481) +++ lib/drntest/test-runner.rb 2013-09-19 12:12:52 +0900 (ec6d894) @@ -1,80 +1,14 @@ -require "optparse" -require "json" -require "droonga/client" -require "tempfile" -require "pp" - module Drntest - class Tester - class << self - def run(argv=nil) - argv ||= ARGV.dup - tester = new - option_parser = create_option_parser(tester) - targets = option_parser.parse!(argv) - tester.run(*targets) - end - - private - def create_option_parser(tester) - parser = OptionParser.new - - parser.banner += " TEST_FILE..." - - parser.on("--port=PORT", - "Connect to fluent-plugin-droonga on PORT", - "(#{tester.port})") do |port| - tester.port = port - end - - parser.on("--host=HOST", - "Connect to fluent-plugin-droonga on HOST", - "(#{tester.host})") do |host| - tester.host = host - end - - parser.on("--tag=TAG", - "Send messages to fluent-plugin-droonga with TAG", - "(#{tester.tag})") do |tag| - tester.tag = tag - end - - parser - end - end - - attr_accessor :port, :host, :tag + class TestRunner + attr_reader :tester, :target - def initialize - @port = 24224 - @host = "localhost" - @tag = "droonga" - end - - def run(*targets) - tests = load_tests(*targets) - tests.each do |test| - run_test(test) - end - 0 # FIXME - end - - def load_tests(*targets) - tests = [] - targets.each do |target| - target_path = Pathname(target) - next unless target_path.exist? - if target_path.directory? - tests += Pathname.glob(target_path + "**" + "*.test") - else - tests << target_path - end - end - tests + def initialize(tester, target) + @tester = tester + @target = target end - def run_test(target) - client = Droonga::Client.new(tag: tag, port: port) + def run + client = Droonga::Client.new(tag: tester.tag, port: tester.port) envelope = JSON.parse(File.read(target)) target_path = Pathname(target) expected_path = target_path.sub_ext(".expected") Modified: lib/drntest/tester.rb (+3 -64) =================================================================== --- lib/drntest/tester.rb 2013-09-18 18:22:22 +0900 (e1b1481) +++ lib/drntest/tester.rb 2013-09-19 12:12:52 +0900 (4ba3cc5) @@ -3,6 +3,7 @@ require "json" require "droonga/client" require "tempfile" require "pp" +require "drntest/test-runner" module Drntest class Tester @@ -54,7 +55,8 @@ module Drntest def run(*targets) tests = load_tests(*targets) tests.each do |test| - run_test(test) + test_runner = TestRunner.new(self, test) + test_runner.run end 0 # FIXME end @@ -72,68 +74,5 @@ module Drntest end tests end - - def run_test(target) - client = Droonga::Client.new(tag: tag, port: port) - envelope = JSON.parse(File.read(target)) - target_path = Pathname(target) - expected_path = target_path.sub_ext(".expected") - - print "#{target}: " - actual = client.connection.send_receive(envelope) - unless actual - puts "No response received" - return - end - actual = normalize_result(actual) - actual_json = actual.to_json - - if File.exist?(expected_path) - expected = JSON.parse(File.read(expected_path)) - expected = normalize_result(expected) - if expected == actual - puts "PASS" - else - puts "FAIL" - show_diff(expected, actual) - reject_path = target_path.sub_ext(".reject") - puts "Saving received result as #{reject_path}" - File.write(reject_path, actual_json) - end - else - actual_path = target_path.sub_ext(".actual") - puts "No expectation specified. Saving result as #{actual_path}." - File.write(actual_path, actual_json) - end - end - - def show_diff(expected, actual) - expected_pretty = expected.pretty_inspect - actual_pretty = actual.pretty_inspect - - create_temporary_file("expected", expected_pretty) do |expected_file| - create_temporary_file("actual", actual_pretty) do |actual_file| - diff_options = [ - "-u", - "--label", "(expected)", expected_file.path, - "--label", "(actual)", actual_file.path - ] - system("diff", *diff_options) - end - end - end - - def create_temporary_file(key, content) - file = Tempfile.new("drntest-#{key}") - file.write(content) - file.close - yield(file) - end - - def normalize_result(result) - result = result.dup - result[1] = 0 # Mask start time - result - end end end -------------- next part -------------- HTML����������������������������...Download