[Groonga-commit] groonga/groonga at e459a15 [master] test: share common code

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Wed May 11 16:40:28 JST 2016


Kouhei Sutou	2016-05-11 16:40:28 +0900 (Wed, 11 May 2016)

  New Revision: e459a15f65b8db64289af578f592433c470a7456
  https://github.com/groonga/groonga/commit/e459a15f65b8db64289af578f592433c470a7456

  Message:
    test: share common code

  Modified files:
    test/command_line/helper/command_runner.rb
    test/command_line/suite/grndb/test_check.rb

  Modified: test/command_line/helper/command_runner.rb (+83 -25)
===================================================================
--- test/command_line/helper/command_runner.rb    2016-05-11 16:21:00 +0900 (ceda244)
+++ test/command_line/helper/command_runner.rb    2016-05-11 16:40:28 +0900 (d7dcfe2)
@@ -18,31 +18,37 @@ module CommandRunner
     end
   end
 
-  def run_command(*command_line)
-    env = {}
-    options = {
-      :out => @output_log_path.to_s,
-      :err => @error_output_log_path.to_s,
-    }
-    succeeded = system(env, *command_line, options)
-    output = @output_log_path.read
-    error_output = @error_output_log_path.read
-    unless succeeded
-      message = <<-MESSAGE.chomp
-failed to run: #{command_line.join(" ")}
--- output start --
-#{output.chomp}
--- output end --
--- error output start --
-#{error_output.chomp}
--- error output end --
-      MESSAGE
-      raise Error.new(output, error_output, message)
+  class ExternalProcess
+    attr_reader :pid
+    attr_reader :input
+    attr_reader :output
+    def initialize(pid, input, output)
+      @pid = pid
+      @input = input
+      @output = output
+    end
+
+    def run_command(command)
+      @input.puts(command)
+      @input.flush
+      @output.gets
+    end
+
+    def close
+      @input.close unles****@input*****?
+      @output.close unles****@outpu*****?
+    end
+  end
+
+  def run_command(*command_line, &block)
+    if block_given?
+      run_command_interactive(*command_line, &block)
+    else
+      run_command_sync(*command_line)
     end
-    Result.new(output, error_output)
   end
 
-  def groonga(command, *arguments)
+  def groonga(*groonga_command_line, &block)
     command_line = [
       "groonga",
       "--log-path", @log_path.to_s,
@@ -50,9 +56,8 @@ failed to run: #{command_line.join(" ")}
     ]
     command_line << "-n" unless @database_path.exist?
     command_line << @database_path.to_s
-    command_line << command
-    command_line.concat(arguments)
-    run_command(*command_line)
+    command_line.concat(groonga_command_line)
+    run_command(*command_line, &block)
   end
 
   def grndb(command, *arguments)
@@ -80,4 +85,57 @@ failed to run: #{command_line.join(" ")}
   def grndb_path
     find_program("grndb")
   end
+
+  private
+  def run_command_interactive(*command_line)
+    env = {}
+    IO.pipe do |input_read, input_write|
+      IO.pipe do |output_read, output_write|
+        options = {
+          :in  => input_read,
+          :out => output_write,
+          :err => @error_output_log_path.to_s,
+        }
+        pid = spawn(env, *command_line, options)
+        input_read.close
+        output_write.close
+        external_process = ExternalProcess.new(pid, input_write, output_read)
+        begin
+          yield(external_process)
+        ensure
+          begin
+            external_process.close
+            Process.waitpid(pid)
+          rescue SystemCallError
+          end
+        end
+        error_output = @error_output_log_path.read
+        Result.new("", error_output)
+      end
+    end
+  end
+
+  def run_command_sync(*command_line)
+    env = {}
+    options = {
+      :out => @output_log_path.to_s,
+      :err => @error_output_log_path.to_s,
+    }
+    succeeded = system(env, *command_line, options)
+    output = @output_log_path.read
+    error_output = @error_output_log_path.read
+    unless succeeded
+      message = <<-MESSAGE.chomp
+failed to run: #{command_line.join(" ")}
+-- output start --
+#{output.chomp}
+-- output end --
+-- error output start --
+#{error_output.chomp}
+-- error output end --
+      MESSAGE
+      raise Error.new(output, error_output, message)
+    end
+    Result.new(output, error_output)
+  end
 end

  Modified: test/command_line/suite/grndb/test_check.rb (+9 -29)
===================================================================
--- test/command_line/suite/grndb/test_check.rb    2016-05-11 16:21:00 +0900 (b2718fd)
+++ test/command_line/suite/grndb/test_check.rb    2016-05-11 16:40:28 +0900 (c887849)
@@ -14,23 +14,14 @@ Database is locked. It may be broken. Re-create the database.
 
   def test_dirty_database
     groonga("table_create", "Users", "TABLE_HASH_KEY", "ShortText")
-    IO.pipe do |to_groonga_read, to_groonga_write|
-      IO.pipe do |from_groonga_read, from_groonga_write|
-        pid = spawn("groonga", @database_path.to_s,
-                    :in => to_groonga_read,
-                    :out => from_groonga_write)
-        to_groonga_read.close
-        from_groonga_write.close
-        to_groonga_write.puts(<<-COMMAND)
+    groonga do |process|
+      process.run_command(<<-COMMAND)
 load --table Users
 [
 {"_key": "Alice"}
 ]
-        COMMAND
-        to_groonga_write.flush
-        from_groonga_read.gets
-        Process.kill(:KILL, pid)
-      end
+      COMMAND
+      Process.kill(:KILL, process.pid)
     end
     error = assert_raise(CommandRunner::Error) do
       grndb("check")
@@ -42,26 +33,15 @@ Database wasn't closed successfully. It may be broken. Re-create the database.
 
   def test_cleaned_database
     groonga("table_create", "Users", "TABLE_HASH_KEY", "ShortText")
-    IO.pipe do |to_groonga_read, to_groonga_write|
-      IO.pipe do |from_groonga_read, from_groonga_write|
-        pid = spawn("groonga", @database_path.to_s,
-                    :in => to_groonga_read,
-                    :out => from_groonga_write)
-        to_groonga_read.close
-        from_groonga_write.close
-        to_groonga_write.puts(<<-COMMAND)
+    groonga do |process|
+      process.run_command(<<-COMMAND)
 load --table Users
 [
 {"_key": "Alice"}
 ]
-        COMMAND
-        to_groonga_write.flush
-        from_groonga_read.gets
-        to_groonga_write.puts("io_flush Users")
-        to_groonga_write.flush
-        from_groonga_read.gets
-        Process.kill(:KILL, pid)
-      end
+      COMMAND
+      process.run_command("io_flush Users")
+      Process.kill(:KILL, process.pid)
     end
     result = grndb("check")
     assert_equal(["", ""],
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index