summaryrefslogtreecommitdiff
path: root/scripts/merge-simplecov
blob: 2af5f8c091800147e0b58d4095f316d55a9e185b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env ruby
begin
  load File.expand_path('../spring', __FILE__)
rescue LoadError => e
  raise unless e.message.include?('spring')
end

require 'simplecov'

def read(file)
  return unless File.exist?(file)
  data = File.read(file)
  return if data.nil? || data.length < 2
  data
end

def load(file)
  begin
    JSON.parse(read(file))
  rescue
    {}
  end
end

def files
  Dir.glob(File.join(SimpleCov.coverage_path, '*', '.resultset.json'))
end

def resultsfiles
  files.map { |file| load(file) }
end

def resultsets
  resultsfiles.reduce({}, :merge)
end

def all_results
  results = []
  resultsets.each do |command_name, data|
    result = SimpleCov::Result.from_hash(command_name => data)
    # Only add result if the timeout is above the configured threshold
    if (Time.now - result.created_at) < SimpleCov.merge_timeout
      results << result
    end
  end
  results
end

def merge_resultset(a1, a2)
  return a1 || [] unless a2
  return a2 || [] unless a1

  new_array = a1.dup
  a2.each_with_index do |element, i|
    if element.nil? && new_array[i].nil?
      new_array[i] = nil
    elsif element.nil? && new_array[i] == 0 || element == 0 && new_array[i].nil?
      new_array[i] = nil
    else
      local_value = element || 0
      other_value = new_array[i] || 0
      new_array[i] = local_value + other_value
    end
  end
  new_array
end

def merge_hashes(h1, h2)
  new_resultset = {}
  (h1.keys + h2.keys).each do |filename|
    new_resultset[filename] = []
  end

  new_resultset.each_key do |filename|
    new_resultset[filename] = merge_resultset(h1[filename], h2[filename])
  end
  new_resultset
end

def merged_result
  merged = {}
  results = all_results
  results.each do |result|
    merged = merge_hashes(result.original_result, merged)
  end
  result = SimpleCov::Result.new(merged)
  # Specify the command name
  result.command_name = results.map(&:command_name).sort.join(", ")
  result
end

# Ignore CI environment
ENV['CI'] = nil
ENV['CI_BUILD_NAME'] = nil

require_relative '../spec/simplecov_env'

merged_result.format!