class Koboldy::Run

Public Class Methods

new(kobold_cmd = "kobold") click to toggle source
# File lib/koboldy/run.rb, line 7
def initialize(kobold_cmd = "kobold")
  @cmd = kobold_cmd
end

Public Instance Methods

additional_option(command) click to toggle source

@param [String] command Add aditional options. @return [Self]

# File lib/koboldy/run.rb, line 59
def additional_option(command)
  @cmd.concat(%( #{command}))
  self
end
approved(expected_folder) click to toggle source

@param [String] expected_folder A path to expected files @return [Self]

# File lib/koboldy/run.rb, line 24
def approved(expected_folder)
  @cmd.concat(%( --approved-folder "#{expected_folder}"))
  self
end
base_uri(path) click to toggle source

@param [String] path Path of current path.

# File lib/koboldy/run.rb, line 65
def base_uri(path)
  @cmd.concat(%( "#{path}"))
end
build(test_target_folder) click to toggle source

@param [String] test_target_folder A path to test target files @return [Self]

# File lib/koboldy/run.rb, line 38
def build(test_target_folder)
  @cmd.concat(%( --build-folder "#{test_target_folder}"))
  self
end
check_and_save(in_file_path = %(./tmp/kobold.txt)) click to toggle source

@param [String] in_file_path A path to file which you would like to save standard output and error. @return [String] File path which is saved results.

# File lib/koboldy/run.rb, line 71
def check_and_save(in_file_path = %(./tmp/kobold.txt))
  fail "no command: #{@cmd}" if @cmd.nil?
  FileUtils.mkdir_p(File::dirname(in_file_path)) unless Dir.exist?(File::dirname(in_file_path))
  Koboldy::Io.capture(@cmd, in_file_path)
  in_file_path
end
clear_cmd(kobold_cmd = "kobold") click to toggle source
# File lib/koboldy/run.rb, line 11
def clear_cmd(kobold_cmd = "kobold")
  @cmd = kobold_cmd
end
config(file) click to toggle source

@param [String] file A path to configuration file @return [Self]

# File lib/koboldy/run.rb, line 17
def config(file)
  @cmd.concat(%( --config "#{file}"))
  self
end
fail_additions() click to toggle source

Add “–fail-additions” option @return [Self]

# File lib/koboldy/run.rb, line 52
def fail_additions
  @cmd.concat(%( --fail-additions))
  self
end
fail_orphans() click to toggle source

Add “–fail-orphans” option @return [Self]

# File lib/koboldy/run.rb, line 45
def fail_orphans
  @cmd.concat(%( --fail-orphans))
  self
end
highlight(compared_folder) click to toggle source

@param [String] compared_folder A path to compared files @return [Self]

# File lib/koboldy/run.rb, line 31
def highlight(compared_folder)
  @cmd.concat(%( --highlight-folder "#{compared_folder}"))
  self
end
results(from_path, into_file = "") click to toggle source

@param [String] from_path A path you would like to analysis outputs by kobold command. @param [String] into_file(Option) A path you would like to save the result. If into_file is empty, then the method return JSON. @return [String] into_file A path to saved result file.

# File lib/koboldy/run.rb, line 81
def results(from_path, into_file = "")
  fail ArgumentError, "no #{from_path}" unless File.exist? from_path
  result = File.read(from_path)
  add, orp, dif = additions(result), orphans(result), different(result)

  hash = {
    :passing => passing(result),
    :failing =>  failing(result),
    :pending => pending(result),
    :additions => { count: add.length, messages: add }, # 期待結果に無い
    :orphans => { count: orp.length, messages: orp }, # 比較対象に無い
    :different => { count: dif.length, messages: dif }, # 比較結果が異なる
  }

  return JSON.pretty_generate(hash) if into_file.empty?

  File.write(into_file, JSON.pretty_generate(hash))
  into_file
end

Private Instance Methods

additions(result) click to toggle source
# File lib/koboldy/run.rb, line 123
def additions(result)
  result.scan(/Screen is new:.*/).map do |line|
    line.to_s.split(/\u001b\[0m\u001b\[90m/).first.gsub(/\AScreen is new: /, "")
  end
end
different(result) click to toggle source
# File lib/koboldy/run.rb, line 135
def different(result)
  result.scan(/Error: Screens are different for.*/).map do |line|
    line.to_s.split(/\u001b\[0m\u001b\[90m/).first.gsub(/\AError: Screens are different for /, "")
  end
end
failing(result) click to toggle source
# File lib/koboldy/run.rb, line 108
def failing(result)
  failing = result.scan(/.*failing.*/).first.to_s
  find_score(failing)
end
find_score(scored_line) click to toggle source
# File lib/koboldy/run.rb, line 118
def find_score(scored_line)
  return 0 if scored_line.nil? || scored_line.empty?
  scored_line.split(/\s/).find { |item| item =~ /\A\d+\z/ }.to_i
end
orphans(result) click to toggle source
# File lib/koboldy/run.rb, line 129
def orphans(result)
  result.scan(/Error: Approved screen is orphaned.*/).map do |line|
    line.to_s.split(/\u001b\[0m\u001b\[90m/).first.gsub(/\AError: Approved screen is orphaned: /, "")
  end
end
passing(result) click to toggle source
# File lib/koboldy/run.rb, line 103
def passing(result)
  passing = result.scan(/.*passing.*/).first.to_s
  find_score(passing)
end
pending(result) click to toggle source
# File lib/koboldy/run.rb, line 113
def pending(result)
  pending = result.scan(/.*pending.*/).first.to_s
  find_score(pending)
end