123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::ResultAdapter

Relationships & Source Files
Inherits: Object
Defined in: lib/simplecov/result_adapter.rb

Overview

Responsible for adapting the format of the coverage result whether it's default or with statistics

Constant Summary

  • ADDRESS_PATTERN = private

    Normalize memory addresses in method coverage keys so that results from different processes can be merged. Anonymous class names like "#Class:0x00007ff19ab24790" get inconsistent addresses across runs. Address widths vary by runtime (32-bit hosts: 8 hex chars; 64-bit CRuby: 16; some JVM/TruffleRuby formats may differ), so match any length of hex digits and collapse to a single placeholder.

    # File 'lib/simplecov/result_adapter.rb', line 46
    /0x\h+/
  • ADDRESS_PLACEHOLDER = private
    # File 'lib/simplecov/result_adapter.rb', line 49
    "0x0"
  • SINGLETON_WRAPPER_PATTERN = private

    Strip the # wrapper Ruby's Coverage adds to singleton-class method keys. module_function and class methods get recorded both as singleton ([#<Class:Foo>, <code>:m</code>, …]) and instance/module ([Foo, <code>:m</code>, …]) entries pointing at the same source location; only one of the two is ever reachable at runtime, so we merge them. Only applies to named constants — anonymous-class addresses like # are left alone (handled by ADDRESS_PATTERN above).

    # File 'lib/simplecov/result_adapter.rb', line 59
    /\A#<Class:([A-Z_][\w:]*)>\z/

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(result) ⇒ ResultAdapter

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 10

def initialize(result)
  @result = result
end

Class Method Details

.call(*args)

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 14

def self.call(*args)
  new(*args).adapt
end

Instance Attribute Details

#result (readonly)

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 8

attr_reader :result

Instance Method Details

#adapt

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 18

def adapt
  return unless result

  result.to_h do |file_name, cover_statistic|
    [file_name, adapt_one(file_name, cover_statistic)]
  end
end

#adapt_one(file_name, cover_statistic) (private)

Pre-0.18 resultsets pointed each filename straight at a line-coverage array; everything since uses the {lines:, branches:, methods:} shape. Newer entries also need their methods table massaged before downstream code merges across processes.

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 32

def adapt_one(file_name, cover_statistic)
  return {"lines" => cover_statistic} if cover_statistic.is_a?(Array)

  adapt_oneshot_lines_if_needed(file_name, cover_statistic)
  normalize_method_keys(cover_statistic)
  cover_statistic
end

#adapt_oneshot_lines_if_needed(file_name, cover_statistic) (private)

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 77

def adapt_oneshot_lines_if_needed(file_name, cover_statistic)
  return unless cover_statistic.key?(:oneshot_lines)

  oneshot_lines = cover_statistic.delete(:oneshot_lines)
  line_stub     = build_line_stub(file_name, oneshot_lines)
  oneshot_lines.each { |covered_line| line_stub[covered_line - 1] = 1 }
  cover_statistic[:lines] = line_stub
end

#build_line_stub(file_name, oneshot_lines) (private)

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 86

def build_line_stub(file_name, oneshot_lines)
  Coverage.line_stub(file_name)
rescue Errno::ENOENT, SyntaxError
  Array.new(oneshot_lines.max || 0, nil)
end

#normalize_method_keys(cover_statistic) (private)

[ GitHub ]

  
# File 'lib/simplecov/result_adapter.rb', line 62

def normalize_method_keys(cover_statistic)
  methods = cover_statistic[:methods]
  return unless methods

  cover_statistic[:methods] = methods.each_with_object({}) do |(key, count), normalized|
    normalized_key = key.dup
    normalized_key[0] = key[0].to_s
                              .gsub(ADDRESS_PATTERN, ADDRESS_PLACEHOLDER)
                              .sub(SINGLETON_WRAPPER_PATTERN, '\1')
    # Keys may collide after normalization (anonymous classes sharing a
    # method name, or singleton + instance forms of a module_function method).
    normalized[normalized_key] = normalized.fetch(normalized_key, 0) + count
  end
end