module RSpec::Qbec::Helpers

Helpers for run and parse qbec output

Public Instance Methods

run(cmd) click to toggle source
# File lib/rspec/qbec/helper.rb, line 11
def run(cmd)
  Open3.capture3(cmd)
end
run_qbec(env = '', args = '') click to toggle source
# File lib/rspec/qbec/helper.rb, line 15
def run_qbec(env = '', args = '')
  run("qbec --root #{RSpec.configuration.qbec_root} show #{args} #{env}")
end
to_ostruct(object) click to toggle source
# File lib/rspec/qbec/helper.rb, line 19
def to_ostruct(object)
  case object
  when Hash
    OpenStruct.new(object.transform_values { |v| to_ostruct(v) })
  when Array
    object.map { |x| to_ostruct(x) }
  else
    object
  end
end
write_file(str) click to toggle source
# File lib/rspec/qbec/helper.rb, line 54
def write_file(str)
  file_path = ''
  loop do
    file_path = Dir.tmpdir + "/qbec_#{DateTime.now.strftime('%Q')}"
    break unless File.exist?(file_path)
  end
  File.open(file_path, 'w') { |file| file.write(str) }
  file_path
end
yaml(str, kind = '') click to toggle source
# File lib/rspec/qbec/helper.rb, line 30
def yaml(str, kind = '')
  matches = str.split(/(?:\n+|\A)---(?:\n+|\Z)/m)
  res = []
  matches.each do |v|
    res << to_ostruct(YAML.safe_load(v)) if v != ''
  end
  if kind != ''
    kind = kind.downcase
    res = res.select { |v| v.kind.downcase == kind }
  end
  res
end
yaml_several(str, template) click to toggle source
# File lib/rspec/qbec/helper.rb, line 43
def yaml_several(str, template)
  match = str.match(/#{regexp_source(template)}\n(.+?)\n(# Source|\z)/m)
  return if match.nil?

  str_ar = match[1].split('---')
  yamls = str_ar.each_with_object([]) do |el, res|
    res << to_ostruct(YAML.safe_load(el))
  end
  yamls.reject { |el| el == false }.compact
end