Class: GemHadar::SimpleCov::ContextFormatter

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Warn
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 Warn

#fail, #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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gem_hadar/simplecov.rb', line 50

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 rescue nil),
      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