Module: ReShaper

Defined in:
lib/serverspec_launcher/helpers/reshaper.rb

Class Method Summary collapse

Class Method Details

.deep_set(hash, path, value) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/serverspec_launcher/helpers/reshaper.rb', line 5

def self.deep_set(hash, path, value)
  *path, final_key = path
  to_set = path.empty? ? hash : hash.dig(*path)

  return unless to_set
  to_set[final_key] = to_set[final_key] ? to_set[final_key].merge(value) : value
end

.get_report(report_file) ⇒ Object



26
27
28
29
30
# File 'lib/serverspec_launcher/helpers/reshaper.rb', line 26

def self.get_report(report_file)
  json = File.read(report_file).gsub(',]', ']').gsub(',}', '}')
  data = JSON.parse(json)
  data
end

.reshape_report(file) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/serverspec_launcher/helpers/reshaper.rb', line 32

def self.reshape_report(file)
  report_name = /\/([\w\-:]*)_extended.json/.match(file)[1]
  report = get_report(file)
  data = transform_data report
  outpath = File.dirname file
  File.open("#{outpath}/#{report_name}_reshaped.json", 'w') { |f| f.write data.to_json }
  File.open("#{outpath}/#{report_name}_reshaped.yaml", 'w') { |f| f.write data.to_yaml }
end

.transform_data(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/serverspec_launcher/helpers/reshaper.rb', line 13

def self.transform_data(data)
  new_data = {}
  (data['examples'] || {}).each do |ex|
    tree = []
    ex['context'].push(ex['description']).each do |k|
      tree.push k
      deep_set(new_data, tree, {})
      deep_set(new_data, tree, ex) if tree.length == ex['context'].length
    end
  end
  {"examples" => new_data }.merge(data.reject{ |k,v| k == 'examples' })
end