class GemHadar::SimpleCov::ContextFormatter
formatter.format(simplecov_result)
formatter = GemHadar::SimpleCov::ContextFormatter.new
@example Using the ContextFormatter to generate coverage reports
the coverage directory.
counts, then writes the complete coverage data to a dedicated JSON file in
overall project coverage metrics. It calculates various percentages and
both line and branch coverage statistics for individual files, as well as
SimpleCov and transforming it into a structured JSON format that includes
This class is responsible for processing code coverage data produced by
SimpleCov results.
A formatter class that generates detailed JSON coverage reports from
def extract_coverage_info(coverage_statistics)
-
(Hash)
- a hash containing the extracted coverage metrics with
Parameters:
-
coverage_statistics
(SimpleCov::CoverageStatistics
) -- the coverage
def extract_coverage_info(coverage_statistics) %i[ total covered missed strength percent ].each_with_object({}) do |attr, hash| hash[attr] = coverage_statistics.send(attr) end end
def format(result)
-
(String)
- an empty string, as the method's primary purpose is to write data to a file
Parameters:
-
result
(SimpleCov::Result
) -- the coverage result object containing files and statistics
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
def project_name
-
(String)
- the name of the current working directory
def project_name Pathname.pwd.basename.to_s # I hope⦠end
def version
-
(String)
- the version string read from the VERSION file, or
def version File.read('VERSION').chomp rescue Errno::ENOENT => e warn "Using version 'unknown', caught #{e.class}: #{e}" 'unknown' end