Class: SimpleCov::Result
- Inherits:
-
Object
- Object
- SimpleCov::Result
- 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
-
#command_name ⇒ Object
The command name that launched this result.
-
#created_at ⇒ Object
Defines when this result has been created.
-
#files ⇒ Object
(also: #source_files)
readonly
Returns all files that are applicable to this result (sans filters!) as instances of SimpleCov::SourceFile.
-
#original_result ⇒ Object
readonly
Returns the original Coverage.result used for this instance of SimpleCov::Result.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Object
Loads a SimpleCov::Result#to_hash dump.
Instance Method Summary collapse
-
#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.
-
#filenames ⇒ Object
Returns all filenames for source files contained in this result.
-
#format! ⇒ Object
Applies the configured SimpleCov.formatter on this result.
-
#groups ⇒ Object
Returns a Hash of groups for this result.
-
#initialize(original_result, command_name: nil, created_at: nil, not_loaded_files: Set.new, filter_config: FilterConfig.new) ⇒ Result
constructor
Initialize a new SimpleCov::Result from given Coverage.result (a Hash of filenames each containing an array of coverage data).
-
#source_file_for(path) ⇒ Object
Returns the SimpleCov::SourceFile for the given path, or nil if no matching file is in this result.
-
#to_hash ⇒ Object
Returns a hash representation of this Result that can be used for marshalling it into JSON.
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_name ⇒ Object
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_at ⇒ Object
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 |
#files ⇒ Object (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_result ⇒ Object (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 |
#filenames ⇒ Object
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 |
#groups ⇒ Object
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.(path, SimpleCov.root) files.find { |file| file.filename == target } end |
#to_hash ⇒ Object
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 |