Class: SimpleCov::Result

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/simplecov/result.rb,
lib/simplecov/result/source_file_builder.rb,
lib/simplecov/result/missing_source_files_reporter.rb

Overview

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

Defined Under Namespace

Classes: FilterConfig, MissingSourceFilesReporter, SourceFileBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new SimpleCov::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 FilterConfig to opt out — useful for tests that build synthetic Results and don’t want the project’s filters or groups applied.



57
58
59
60
61
62
63
64
65
66
67
68
# 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

Instance Attribute Details

#command_nameObject

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



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

def command_name
  @command_name ||= SimpleCov.command_name
end

#created_atObject

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



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

def created_at
  @created_at ||= Time.now
end

#filesObject (readonly) Also known as: source_files

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



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

def files
  @files
end

#original_resultObject (readonly)

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



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

def original_result
  @original_result
end

Class Method Details

.from_hash(hash) ⇒ Object

Loads a SimpleCov::Result#to_hash dump



130
131
132
133
134
# 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 Method Details

#coverage_for(path) ⇒ Object

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 SimpleCov::Result#source_file_for for path resolution.



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

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

#filenamesObject

Returns all filenames for source files contained in this result



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

def filenames
  files.map(&:filename)
end

#format!Object

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.



101
102
103
104
105
106
# File 'lib/simplecov/result.rb', line 101

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

  formatter.new.format(self)
end

#groupsObject

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



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

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

#source_file_for(path) ⇒ Object

Returns the SimpleCov::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.



79
80
81
82
# 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_hashObject

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



120
121
122
123
124
125
126
127
# File 'lib/simplecov/result.rb', line 120

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