123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Formatter::JSONFormatter

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base
Instance Chain:
self, Base
Inherits: SimpleCov::Formatter::Base
Defined in: lib/simplecov/formatter/json_formatter.rb,
lib/simplecov/formatter/json_formatter/errors_formatter.rb,
lib/simplecov/formatter/json_formatter/result_hash_formatter.rb,
lib/simplecov/formatter/json_formatter/source_file_formatter.rb

Overview

Writes coverage results as JSON to coverage/coverage.json. Used standalone, alongside the HTML formatter, or by external tools that consume ::SimpleCov output.

Constant Summary

Class Method Summary

Base - Inherited

.new

output_dir defaults to SimpleCov.coverage_path so the at_exit pipeline keeps working unchanged.

Instance Method Summary

Base - Inherited

#displayable_output_path

The path shown in the "Coverage report generated for X to Y" status line.

#entry_point_filename

Subclasses override to name the report's entry-point file (e.g. index.html for HTML, coverage.json for JSON), which gets appended to the directory in the status line.

#message_prefix

Subclasses override to prepend a marker (e.g. "JSON ") to the summary line.

#output_message

Emit one summary line per criterion that the run actually measured.

#output_path, #relative_or_absolute_output_path,
#stats_line

Returns nil for branch/method criteria that have nothing to measure (e.g. a file with no branches under branch coverage).

Constructor Details

This class inherits a constructor from SimpleCov::Formatter::Base

Class Method Details

.build_hash(result, include_source: SimpleCov.source_in_json)

include_source: defaults to SimpleCov.source_in_json (true by default) so the historical payload shape is unchanged. Callers that need the source array regardless of the global setting (the HTML formatter, which feeds the client-side viewer) pass include_source: true explicitly.

[ GitHub ]

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

def self.build_hash(result, include_source: SimpleCov.source_in_json)
  ResultHashFormatter.new(result, include_source: include_source).format
end

Instance Method Details

#entry_point_filename (private)

[ GitHub ]

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

def entry_point_filename
  FILENAME
end

#existing_timestamp(path) (private)

[ GitHub ]

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

def existing_timestamp(path)
  return nil unless File.exist?(path)

  timestamp = JSON.parse(File.read(path), symbolize_names: true).dig(:meta, :timestamp)
  timestamp && Time.iso8601(timestamp)
rescue JSON::ParserError, ArgumentError
  nil
end

#format(result)

[ GitHub ]

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

def format(result)
  FileUtils.mkdir_p(output_path)
  path = File.join(output_path, FILENAME)
  warn_if_concurrent_overwrite(path)
  File.write(path, JSON.pretty_generate(self.class.build_hash(result)))
  # stderr, not stdout: this is a status message, not the program's
  # output. Keeps the line out of pipelines like `rspec -f json`.
  warn output_message(result) unless @silent
end

#message_prefix (private)

[ GitHub ]

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

def message_prefix
  "JSON "
end

#warn_if_concurrent_overwrite(path) (private)

Warns when the existing coverage.json has a timestamp newer than this process's start time — a strong signal that a sibling test process (e.g., parallel_tests) wrote it while we were running, and that our write is about to clobber their data.

[ GitHub ]

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

def warn_if_concurrent_overwrite(path)
  start_time = SimpleCov.process_start_time or return
  existing_ts = existing_timestamp(path) or return
  return unless existing_ts > start_time

  warn "simplecov: #{path} was written at #{existing_ts.iso8601} — after " \
       "this process started at #{start_time.iso8601}. Overwriting " \
       "likely loses coverage data from a concurrent test run. For " \
       "parallel test setups, use SimpleCov::ResultMerger or run a single " \
       "collation step after all workers finish."
end