Class: SimpleCov::Formatter::JSONFormatter

Inherits:
Base
  • Object
show all
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.

Defined Under Namespace

Classes: ErrorsFormatter, ResultHashFormatter, SourceFileFormatter

Constant Summary collapse

FILENAME =
"coverage.json"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Class Method Details

.build_hash(result, include_source: SimpleCov.source_in_json) ⇒ Object

‘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.



21
22
23
# 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

#format(result) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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