Module: SimpleCov::ResultMerger

Defined in:
lib/simplecov/result_merger.rb

Class Method Summary collapse

Class Method Details

.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



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

def merge_results(*results)
  merged = SimpleCov::RawCoverage.merge_results(*results.map(&:original_result))
  result = SimpleCov::Result.new(merged)
  # 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



76
77
78
# File 'lib/simplecov/result_merger.rb', line 76

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.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simplecov/result_merger.rb', line 48

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

.resultset(force_reload = false) ⇒ Object

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



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

def resultset(force_reload = false)
  @resultset = nil if force_reload

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

.resultset_pathObject

The path to the .resultset.json cache file



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

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

.resultset_writelockObject



16
17
18
# File 'lib/simplecov/result_merger.rb', line 16

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



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/simplecov/result_merger.rb', line 81

def store_result(result)
  File.open(resultset_writelock, "w+") do |f|
    f.flock(File::LOCK_EX)
    new_set = resultset(true)
    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



37
38
39
40
41
42
# File 'lib/simplecov/result_merger.rb', line 37

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