class SimpleCov::Formatter::JSONFormatter::ResultHashFormatter
meta, per-file coverage data, group totals, and aggregate stats.
Builds the hash that JSONFormatter serializes to coverage.json:
def coverage_flags
def coverage_flags { line_coverage: line_coverage_enabled?, branch_coverage: SimpleCov.branch_coverage?, method_coverage: SimpleCov.method_coverage? } end
def format
def format { :$schema => SCHEMA_URL, :meta => format_meta, :total => format_coverage_statistics(@result.coverage_statistics), :coverage => format_files, :groups => format_groups, :errors => ErrorsFormatter.new(@result).call } end
def format_coverage_statistics(statistics)
def format_coverage_statistics(statistics) result = {} #: Hash[Symbol, untyped] 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
def format_files
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
def format_groups
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
def format_line_statistic(stat)
def format_line_statistic(stat) { covered: stat.covered, missed: stat.missed, omitted: stat.omitted, total: stat.total, percent: stat.percent, strength: stat.strength } end
def format_meta
def format_meta { 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, primary_coverage: SimpleCov.primary_coverage.to_s }.merge!(coverage_flags) end
def format_single_statistic(stat)
def format_single_statistic(stat) { covered: stat.covered, missed: stat.missed, total: stat.total, percent: stat.percent, strength: stat.strength } end
def git_commit
coverage.json. stderr is captured (not forwarded) so a non-git project
matters most when `source_in_json false` drops the source text from
can recover the exact source a report was generated against, which
project isn't a git checkout or git isn't on PATH. Recorded so tools
Full git commit SHA of `SimpleCov.root`'s HEAD, or nil when the
def git_commit output, status = Open3.capture2e("git", "-C", SimpleCov.root.to_s, "rev-parse", "HEAD") status.success? ? output.strip : nil rescue StandardError nil end
def initialize(result, include_source: true)
def initialize(result, include_source: true) @result = result @include_source = include_source end
def line_coverage_enabled?
tracks exactly which configurations cause the formatter to
Mirrors SourceFileFormatter's predicate so meta.line_coverage
def line_coverage_enabled? SimpleCov.coverage_criterion_enabled?(:line) || SimpleCov.coverage_criterion_enabled?(:oneshot_line) end