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

.clear_resultsetObject

Clear out the previously cached .resultset


125
126
127
# File 'lib/simplecov/result_merger.rb', line 125

def clear_resultset
  @resultset = nil
end

.merge_and_store(*results) ⇒ Object


65
66
67
68
69
# File 'lib/simplecov/result_merger.rb', line 65

def merge_and_store(*results)
  result = merge_results(*results)
  store_result(result) if result
  result
end

.merge_results(*results) ⇒ Object

Merge two or more SimpleCov::Results into a new one with merged coverage data and the command_name for the result consisting of a join on all source result's names


74
75
76
77
78
79
80
81
# File 'lib/simplecov/result_merger.rb', line 74

def merge_results(*results)
  parsed_results = JSON.parse(JSON.dump(results.map(&:original_result)))
  combined_result = SimpleCov::Combine::ResultsCombiner.combine(*parsed_results)
  result = SimpleCov::Result.new(combined_result)  # Specify the command name

  result.command_name = results.map(&:command_name).sort.join(", ")
  result
end

.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


88
89
90
# File 'lib/simplecov/result_merger.rb', line 88

def merged_result
  merge_results(*results)
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.


55
56
57
58
59
60
61
62
63
# File 'lib/simplecov/result_merger.rb', line 55

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

    results << result if (Time.now - result.created_at) < SimpleCov.merge_timeout
  end
  results
end

.resultsetObject

Loads the cached resultset from JSON and returns it as a Hash, caching it for subsequent accesses.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simplecov/result_merger.rb', line 24

def resultset
  @resultset ||= begin
    data = stored_data
    if data
      begin
        JSON.parse(data) || {}
      rescue StandardError
        {}
      end
    else
      {}
    end
  end
end

.resultset_pathObject

The path to the .resultset.json cache file


14
15
16
# File 'lib/simplecov/result_merger.rb', line 14

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

.resultset_writelockObject


18
19
20
# File 'lib/simplecov/result_merger.rb', line 18

def resultset_writelock
  File.join(SimpleCov.coverage_path, ".resultset.json.lock")
end

.store_result(result) ⇒ Object

Saves the given SimpleCov::Result in the resultset cache


93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/simplecov/result_merger.rb', line 93

def store_result(result)
  synchronize_resultset do
    # Ensure we have the latest, in case it was already cached
    clear_resultset
    new_set = resultset
    command_name, data = result.to_hash.first
    new_set[command_name] = data
    File.open(resultset_path, "w+") do |f_|
      f_.puts JSON.pretty_generate(new_set)
    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


40
41
42
43
44
45
46
47
48
49
# File 'lib/simplecov/result_merger.rb', line 40

def stored_data
  synchronize_resultset do
    return unless File.exist?(resultset_path)

    data = File.read(resultset_path)
    return if data.nil? || data.length < 2

    data
  end
end

.synchronize_resultsetObject

Ensure only one process is reading or writing the resultset at any given time


109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/simplecov/result_merger.rb', line 109

def synchronize_resultset  # make it reentrant

  return yield if defined?(@resultset_locked) && @resultset_locked

  begin
    @resultset_locked = true
    File.open(resultset_writelock, "w+") do |f|
      f.flock(File::LOCK_EX)
      yield
    end
  ensure
    @resultset_locked = false
  end
end