Module: SimpleCov::ResultMerger

Defined in:
lib/simplecov/result_merger.rb

Overview

Singleton that is responsible for caching, loading and merging SimpleCov::Results into a single result for coverage analysis based upon multiple test suites.

Class Method Summary collapse

Class Method Details

.merged_resultObject

Gets all SimpleCov::Results from cache, merges them and produces a new SimpleCov::Result with merged coverage data and the command_name for the result consisting of a join on all source result's names


59
60
61
62
63
64
65
66
67
68
# File 'lib/simplecov/result_merger.rb', line 59

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

.resultsObject

Gets the resultset hash and re-creates all included instances of SimpleCov::Result from that. All results that are above the SimpleCov.merge_timeout will be dropped. Returns an array of SimpleCov::Result items.


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simplecov/result_merger.rb', line 42

def results
  results = []
  resultset.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

.resultsetObject

Loads the cached resultset from YAML and returns it as a Hash


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simplecov/result_merger.rb', line 16

def resultset
  if stored_data
    # Detect and use available MultiJson API - it changed in v1.3
    if MultiJson.respond_to?(:adapter)
      MultiJson.load(stored_data)
    else
      MultiJson.decode(stored_data)
    end
  else
    {}
  end
end

.resultset_pathObject

The path to the resultset.yml cache file


11
12
13
# File 'lib/simplecov/result_merger.rb', line 11

def resultset_path
  File.join(SimpleCov.coverage_path, '.resultset.json')
end

.store_result(result) ⇒ Object

Saves the given SimpleCov::Result in the resultset cache


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/simplecov/result_merger.rb', line 71

def store_result(result)
  new_set = resultset
  command_name, data = result.to_hash.first
  new_set[command_name] = data
  File.open(resultset_path, "w+") do |f|
    if defined? ::JSON
      f.puts JSON.pretty_generate(new_set)
    else
      # Detect and use available MultiJson API - it changed in v1.3
      if MultiJson.respond_to?(:adapter)
        f.puts MultiJson.dump(new_set)
      else
        f.puts MultiJson.encode(new_set)
      end
    end
  end
  true
end

.stored_dataObject

Returns the contents of the resultset cache as a string or if the file is missing or empty nil


30
31
32
33
34
35
36
# File 'lib/simplecov/result_merger.rb', line 30

def stored_data
  if File.exist?(resultset_path) and stored_data = File.read(resultset_path) and stored_data.length >= 2
    stored_data
  else
    nil
  end
end