Class: GemHadar::SimpleCov::ContextFormatter

Inherits:
Object
  • Object
show all
Includes:
FileUtils, WarnModule
Defined in:
lib/gem_hadar/simplecov.rb

Overview

A formatter class that generates detailed JSON coverage reports from SimpleCov results.

This class is responsible for processing code coverage data produced by SimpleCov and transforming it into a structured JSON format that includes both line and branch coverage statistics for individual files, as well as overall project coverage metrics. It calculates various percentages and counts, then writes the complete coverage data to a dedicated JSON file in the coverage directory.

Examples:

Using the ContextFormatter to generate coverage reports

formatter = GemHadar::SimpleCov::ContextFormatter.new
formatter.format(simplecov_result)

Instance Method Summary collapse

Methods included from WarnModule

#warn

Instance Method Details

#format(result) ⇒ String

The format method processes code coverage results and generates a detailed JSON report.

This method takes a SimpleCov result object and transforms its coverage data into a structured format that includes both line and branch coverage statistics for individual files, as well as overall project coverage metrics. It calculates various percentages and counts, then writes the complete coverage data to a JSON file in the coverage directory.

Parameters:

  • result (SimpleCov::Result)

    the coverage result object containing files and statistics

Returns:

  • (String)

    an empty string, as the method’s primary purpose is to write data to a file



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gem_hadar/simplecov.rb', line 79

def format(result)
  files = result.files.map do |file|
    line_coverage_statistics = extract_coverage_info(file.coverage_statistics[:line])
    branch_coverage_statistics = extract_coverage_info(file.coverage_statistics[:branch])
    {
      filename: file.filename,
      line_coverage_statistics:,
      branch_coverage_statistics:,
    }
  end
  covered_files = result.files.count { |file| file.coverage_statistics[:line].covered > 0 }
  uncovered_files = result.files.count { |file| file.coverage_statistics[:line].covered == 0 }
  files_count = result.files.length
  files_covered_percent = files_count > 0 ? (100 * covered_files.to_f / files_count).round(2) : 0
  branch_covered_percent =
    result.total_branches > 0 ? (result.covered_branches.to_f / result.total_branches * 100).round(2) : 0
  coverage_data = {
    project_name:,
    version:,
    timestamp: Time.now.iso8601,
    files:,
    overall_coverage: {
      covered_percent: result.covered_percent.round(2),
      total_lines: result.total_lines,
      covered_lines: result.covered_lines,
      missed_lines: result.missed_lines,
      branch_covered_percent:,
      total_branches: result.total_branches,
      covered_branches: result.covered_branches,
      missed_branches: result.missed_branches,
      coverage_strength: result.covered_strength.round(2),
      least_covered_file: result.least_covered_file,
      covered_files:,
      uncovered_files:,
      files_count:,
      files_covered_percent:,
    },
  }

  # Write to a dedicated coverage data file,
  coverage_dir = Pathname.new('coverage')
  mkdir_p coverage_dir
  filename = File.expand_path(coverage_dir + 'coverage_context.json')
  File.secure_write(filename, JSON.pretty_generate(coverage_data))
  STDERR.puts "Wrote detailed coverage context to #{filename.to_s.inspect}."
  ""
end