Class: GemHadar::SimpleCov::ContextFormatter
- Inherits:
-
Object
- Object
- GemHadar::SimpleCov::ContextFormatter
- 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.
Instance Method Summary collapse
-
#extract_coverage_info(coverage_statistics) ⇒ Hash
private
The extract_coverage_info method transforms coverage statistics into a structured hash format.
-
#format(result) ⇒ String
The format method processes code coverage results and generates a detailed JSON report.
-
#project_name ⇒ String
private
The project_name method retrieves the name of the current working directory.
-
#version ⇒ String
private
The version method reads and returns the current version string from the VERSION file.
Methods included from Warn
Instance Method Details
#extract_coverage_info(coverage_statistics) ⇒ Hash (private)
The extract_coverage_info method transforms coverage statistics into a structured hash format.
This method takes a coverage statistics object and extracts specific attributes into a new hash, making the data more accessible for reporting and analysis.
statistics object to process
keys :total, :covered, :missed, :strength, and :percent
142 143 144 145 146 |
# File 'lib/gem_hadar/simplecov.rb', line 142 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 |
#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.(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 |
#project_name ⇒ String (private)
The project_name method retrieves the name of the current working directory.
This method obtains the absolute path of the current working directory and extracts its basename, returning it as a string. It is typically used to identify the project name based on the directory structure.
110 111 112 |
# File 'lib/gem_hadar/simplecov.rb', line 110 def project_name Pathname.pwd.basename.to_s # I hope… end |
#version ⇒ String (private)
The version method reads and returns the current version string from the VERSION file.
This method attempts to read the contents of the VERSION file, removing any trailing whitespace. If the VERSION file does not exist, it returns the string ‘unknown’ instead.
‘unknown’ if the file is missing
123 124 125 126 127 128 |
# File 'lib/gem_hadar/simplecov.rb', line 123 def version File.read('VERSION').chomp rescue Errno::ENOENT => e warn "Using version 'unknown', caught #{e.class}: #{e}" 'unknown' end |