Module: RSpec::Qbec::Helpers

Defined in:
lib/rspec/qbec/helper.rb

Overview

Helpers for run and parse qbec output

Instance Method Summary collapse

Instance Method Details

#run(cmd) ⇒ Object



11
12
13
# File 'lib/rspec/qbec/helper.rb', line 11

def run(cmd)
  Open3.capture3(cmd)
end

#run_qbec(env = '', args = '') ⇒ Object



15
16
17
# 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) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



54
55
56
57
58
59
60
61
62
# 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')}.yml"
    break unless File.exist?(file_path)
  end
  File.open(file_path, 'w') { |file| file.write(str) }
  file_path
end

#yaml(str, kind = '') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# 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