123456789_123456789_123456789_123456789_123456789_

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

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

Constructor Details

.new(result, include_source: true) ⇒ ResultHashFormatter

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 25

def initialize(result, include_source: true)
  @result = result
  @include_source = include_source
end

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.

[ GitHub ]

  
# 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,
    :meta => format_meta,
    :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 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
  }.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.

[ GitHub ]

  
# File 'lib/simplecov/formatter/json_formatter/result_hash_formatter.rb', line 74

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