Navigation Menu

Skip to content

Commit

Permalink
Add --output_type option to test msgpack output
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 14, 2012
1 parent d00b0fc commit 9c68246
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions grntest.gemspec
Expand Up @@ -42,6 +42,7 @@ Gem::Specification.new do |spec|
end

spec.add_dependency("json")
spec.add_dependency("msgpack")

spec.add_development_dependency("bundler")
spec.add_development_dependency("rake")
Expand Down
57 changes: 46 additions & 11 deletions lib/grntest/tester.rb
Expand Up @@ -25,6 +25,7 @@
require "cgi/util"

require "json"
require "msgpack"

require "grntest/version"

Expand Down Expand Up @@ -73,6 +74,15 @@ def create_option_parser(tester, tag)
tester.interface = interface
end

available_output_types = ["json", "msgpack"]
available_output_type_labels = available_output_types.join(", ")
parser.on("--output-type=TYPE", available_output_types,
"Use TYPE as the output type",
"[#{available_output_type_labels}]",
"(#{tester.output_type})") do |type|
tester.output_type = type
end

available_testees = ["groonga", "groonga-httpd"]
available_testee_labels = available_testees.join(", ")
parser.on("--testee=TESTEE", available_testees,
Expand Down Expand Up @@ -192,7 +202,7 @@ def parse_name_or_pattern(name)
end

attr_accessor :groonga, :groonga_httpd, :groonga_suggest_create_dataset
attr_accessor :interface, :testee
attr_accessor :interface, :output_type, :testee
attr_accessor :base_directory, :diff, :diff_options
attr_accessor :n_workers
attr_accessor :output
Expand All @@ -205,6 +215,7 @@ def initialize
@groonga_httpd = "groonga-httpd"
@groonga_suggest_create_dataset = "groonga-suggest-create-dataset"
@interface = :stdio
@output_type = "json"
@testee = "groonga"
@base_directory = Pathname(".")
@reporter = nil
Expand Down Expand Up @@ -672,6 +683,7 @@ def execute_groonga_script
context.base_directory = @tester.base_directory.expand_path
context.groonga_suggest_create_dataset =
@tester.groonga_suggest_create_dataset
context.output_type = @tester.output_type
run_groonga(context) do |executor|
executor.execute(test_script_path)
end
Expand Down Expand Up @@ -924,9 +936,10 @@ def normalize_result(result)
when :input
normalized_result << content
when :output
case options[:format]
when "json"
status, *values = JSON.parse(content)
type = options[:type]
case type
when "json", "msgpack"
status, *values = parse_result(content, type)
normalized_status = normalize_status(status)
normalized_output_content = [normalized_status, *values]
normalized_output = JSON.generate(normalized_output_content)
Expand All @@ -945,6 +958,17 @@ def normalize_result(result)
normalized_result
end

def parse_result(result, type)
case type
when "json"
JSON.parse(result)
when "msgpack"
MessagePack.unpack(result.chomp)
else
raise "Unknown type: #{type}"
end
end

def normalize_status(status)
return_code, started_time, elapsed_time, *rest = status
_ = started_time = elapsed_time # for suppress warnings
Expand Down Expand Up @@ -1011,6 +1035,7 @@ class Context
attr_accessor :base_directory, :temporary_directory_path, :db_path
attr_accessor :groonga_suggest_create_dataset
attr_accessor :result
attr_accessor :output_type
def initialize
@logging = true
@base_directory = Pathname(".")
Expand All @@ -1019,6 +1044,7 @@ def initialize
@groonga_suggest_create_dataset = "groonga-suggest-create-dataset"
@n_nested = 0
@result = []
@output_type = "json"
@log = nil
end

Expand Down Expand Up @@ -1067,7 +1093,7 @@ def initialize(context=nil)
@pending_command = ""
@pending_load_command = nil
@current_command_name = nil
@output_format = nil
@output_type = nil
@context = context || Context.new
end

Expand Down Expand Up @@ -1190,18 +1216,24 @@ def execute_command_line(command_line)
def extract_command_info(command_line)
@current_command, *@current_arguments = Shellwords.split(command_line)
if @current_command == "dump"
@output_format = "groonga-command"
@output_type = "groonga-command"
else
@output_format = "json"
@output_type = @context.output_type
@current_arguments.each_with_index do |word, i|
if /\A--output_format(?:=(.+))?\z/ =~ word
@output_format = $1 || words[i + 1]
if /\A--output_type(?:=(.+))?\z/ =~ word
@output_type = $1 || words[i + 1]
break
end
end
end
end

def have_output_type_argument?
@current_arguments.any? do |argument|
/\A--output_type(?:=.+)?\z/ =~ argument
end
end

def multiline_load_command?
@current_command == "load" and
not @current_arguments.include?("--values")
Expand Down Expand Up @@ -1262,9 +1294,9 @@ def log_input(content)
def log_output(content)
log(:output, content,
:command => @current_command,
:format => @output_format)
:type => @output_type)
@current_command = nil
@output_format = nil
@output_type = nil
end

def log_error(content)
Expand All @@ -1280,6 +1312,9 @@ def initialize(input, output, context=nil)
end

def send_command(command_line)
unless have_output_type_argument?
command_line = command_line.sub(/$/, " --output_type #{@output_type}")
end
begin
@input.print(command_line)
@input.flush
Expand Down

0 comments on commit 9c68246

Please sign in to comment.