123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Result

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/simplecov/result.rb,
lib/simplecov/result/missing_source_files_reporter.rb,
lib/simplecov/result/source_file_builder.rb

Overview

A simplecov code coverage result, initialized from the Hash Ruby's built-in coverage library generates (Coverage.result).

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(original_result, command_name: nil, created_at: nil, not_loaded_files: Set.new, filter_config: FilterConfig.new) ⇒ Result

Initialize a new Result from given Coverage.result (a Hash of filenames each containing an array of coverage data).

filter_config defaults to the ::SimpleCov singleton's filter / group configuration so existing call sites are unchanged. Pass a custom Result::FilterConfig to opt out — useful for tests that build synthetic Results and don't want the project's filters or groups applied.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 57

def initialize(original_result, command_name: nil, created_at: nil, not_loaded_files: Set.new,
               filter_config: FilterConfig.new)
  @original_result = original_result.freeze
  @command_name = command_name
  @created_at = created_at
  @groups_config = filter_config.groups
  builder = SourceFileBuilder.new(original_result, not_loaded_files: not_loaded_files)
  @files = builder.call
  warn_about_missing_source_files(builder.missing_source_files, original_result.size)
  apply_cover_filters!(filter_config.cover_filters)
  apply_filters!(filter_config.filters)
end

Class Method Details

.from_hash(hash)

Loads a #to_hash dump

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 130

def self.from_hash(hash)
  hash.map do |command_name, data|
    new(data.fetch("coverage"), command_name: command_name, created_at: Time.at(data["timestamp"]))
  end
end

Instance Attribute Details

#command_name (rw)

The command name that launched this result. Delegated to SimpleCov.command_name if not set manually

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 115

def command_name
  @command_name ||= SimpleCov.command_name
end

#command_name=(value) (rw)

Explicitly set the command name that was used for this coverage result. Defaults to SimpleCov.command_name

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 25

attr_writer :command_name

#created_at (rw)

Defines when this result has been created. Defaults to Time.now

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 109

def created_at
  @created_at ||= Time.now
end

#created_at=(value) (rw)

Explicitly set the Time this result has been created

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 23

attr_writer :created_at

#files (readonly) Also known as: #source_files

Returns all files that are applicable to this result (sans filters!) as instances of SourceFile. Aliased as :source_files

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 20

attr_reader :files

#original_result (readonly)

Returns the original Coverage.result used for this instance of Result

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 17

attr_reader :original_result

#source_files (readonly)

Alias for #files.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 21

alias source_files files

Instance Method Details

#apply_cover_filters!(cover_filters) (private)

When any cover matcher is configured, restrict @files to source files matching at least one of them. With no cover matchers configured this is a no-op, preserving the historical "everything required, then filtered" universe.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 164

def apply_cover_filters!(cover_filters)
  return if cover_filters.empty?

  @files = SimpleCov::FileList.new(
    @files.select { |source_file| cover_filters.any? { |filter| filter.matches?(source_file) } }
  )
end

#apply_filters!(filters) (private)

Applies the given filter chain to @files, dropping each source file that any filter matches.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 154

def apply_filters!(filters)
  filters.each do |filter|
    @files = SimpleCov::FileList.new(@files.reject { |source_file| filter.matches?(source_file) })
  end
end

#coverage (private)

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 148

def coverage
  original_result.slice(*filenames)
end

#coverage_for(path)

Returns the line:/branch:/method: coverage_statistics hash for the given file path, or nil if no matching source file is in this result. See #source_file_for for path resolution.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 87

def coverage_for(path)
  source_file_for(path)&.coverage_statistics
end

#filenames

Returns all filenames for source files contained in this result

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 71

def filenames
  files.map(&:filename)
end

#format!

Applies the configured SimpleCov.formatter on this result. Returns nil if formatting has been opted out of (SimpleCov.formatter false / SimpleCov.formatters []) — the cheap path for non-final processes in a parallel CI run, which only need their .resultset.json on disk. See #964.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 101

def format!
  formatter = SimpleCov.formatter
  return nil if formatter.nil?

  formatter.new.format(self)
end

#groups

Returns a Hash of groups for this result. Define groups using SimpleCov.group 'Models', 'app/models'

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 92

def groups
  @groups ||= SimpleCov.grouped(files, groups: @groups_config)
end

#source_file_for(path)

Returns the SourceFile for the given path, or nil if no matching file is in this result. The path is resolved against SimpleCov.root, so callers can pass either an absolute path or a project-relative one.

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 79

def source_file_for(path)
  target = File.expand_path(path, SimpleCov.root)
  files.find { |file| file.filename == target }
end

#to_hash

Returns a hash representation of this Result that can be used for marshalling it into JSON

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 120

def to_hash
  {
    command_name => {
      "coverage" => coverage,
      "timestamp" => created_at.to_i
    }
  }
end

#warn_about_missing_source_files(missing, input_size) (private)

[ GitHub ]

  
# File 'lib/simplecov/result.rb', line 138

def warn_about_missing_source_files(missing, input_size)
  return if missing.empty?

  MissingSourceFilesReporter.new(
    missing,
    input_size: input_size,
    every_entry_dropped: @files.empty? && missing.size == input_size
  ).warn!
end