Class: SimpleCov::Formatter::JSONFormatter::ResultHashFormatter
| Relationships & Source Files | |
| Inherits: | Object |
| Defined in: | lib/simplecov/formatter/json_formatter/result_hash_formatter.rb |
Overview
Builds the hash that ::SimpleCov::Formatter::JSONFormatter serializes to coverage.json:
meta, per-file coverage data, group totals, and aggregate stats.
Constant Summary
-
SCHEMA_URL =
private
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 22"https://raw.githubusercontent.com/simplecov-ruby/simplecov/main/schemas/coverage-v#{SCHEMA_VERSION}.schema.json".freeze
-
SCHEMA_VERSION =
private
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 21
Bump SCHEMA_VERSION (and SCHEMA_URL) when the JSON shape changes. Additive changes bump the minor segment, removals or shape changes bump the major segment. The versioned file at schemas/coverage-vX.Y.schema.json is the canonical artifact consumers should pin to, schemas/coverage.schema.json is a convenience alias that always tracks the latest. See the
coverage.jsonschema section of the README for the rationale."1.0"
Class Method Summary
Instance Attribute Summary
-
#line_coverage_enabled? ⇒ Boolean
readonly
private
Mirrors SourceFileFormatter's predicate so meta.line_coverage tracks exactly which configurations cause the formatter to emit line stats.
Instance Method Summary
- #format
- #coverage_flags private
- #format_coverage_statistics(statistics) private
- #format_files private
- #format_groups private
- #format_line_statistic(stat) private
- #format_meta private
- #format_single_statistic(stat) private
-
#git_commit
private
Full git commit SHA of
SimpleCov.root's HEAD, or nil when the project isn't a git checkout or git isn't on PATH.
Constructor Details
.new(result, include_source: true) ⇒ ResultHashFormatter
Instance Attribute Details
#line_coverage_enabled? ⇒ Boolean (readonly, private)
Mirrors SourceFileFormatter's predicate so meta.line_coverage tracks exactly which configurations cause the formatter to emit line stats.
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 92
def line_coverage_enabled? SimpleCov.coverage_criterion_enabled?(:line) || SimpleCov.coverage_criterion_enabled?(:oneshot_line) end
Instance Method Details
#coverage_flags (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 81
def coverage_flags { line_coverage: line_coverage_enabled?, branch_coverage: SimpleCov.branch_coverage?, method_coverage: SimpleCov.method_coverage? } end
#format
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 30
def format { :$schema => SCHEMA_URL, : => , :total => format_coverage_statistics(@result.coverage_statistics), :coverage => format_files, :groups => format_groups, :errors => ErrorsFormatter.new(@result).call } end
#format_coverage_statistics(statistics) (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 96
def format_coverage_statistics(statistics) result = {} result[:lines] = format_line_statistic(statistics[:line]) if statistics[:line] result[:branches] = format_single_statistic(statistics[:branch]) if statistics[:branch] result[:methods] = format_single_statistic(statistics[:method]) if statistics[:method] result end
#format_files (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 43
def format_files @result.files.to_h do |source_file| [source_file.project_filename, SourceFileFormatter.new(source_file, include_source: @include_source).call] end end
#format_groups (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 49
def format_groups @result.groups.to_h do |name, file_list| stats = format_coverage_statistics(file_list.coverage_statistics) [name, stats.merge(files: file_list.map(&:project_filename))] end end
#format_line_statistic(stat) (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 104
def format_line_statistic(stat) { covered: stat.covered, missed: stat.missed, omitted: stat.omitted, total: stat.total, percent: stat.percent, strength: stat.strength } end
#format_meta (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 56
def { schema_version: SCHEMA_VERSION, simplecov_version: SimpleCov::VERSION, command_name: @result.command_name, project_name: SimpleCov.project_name, timestamp: @result.created_at.iso8601(3), root: SimpleCov.root, commit: git_commit }.merge!(coverage_flags) end
#format_single_statistic(stat) (private)
[ GitHub ]# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 115
def format_single_statistic(stat) { covered: stat.covered, missed: stat.missed, total: stat.total, percent: stat.percent, strength: stat.strength } end
#git_commit (private)
Full git commit SHA of SimpleCov.root's HEAD, or nil when the
project isn't a git checkout or git isn't on PATH. Recorded so tools
can recover the exact source a report was generated against, which
matters most when source_in_json false drops the source text from
coverage.json. stderr is captured (not forwarded) so a non-git project
doesn't print git's diagnostics to the build.