Class: SimpleCov::Formatter::HTMLFormatter

Inherits:
Base
  • Object
show all
Defined in:
lib/simplecov/formatter/html_formatter.rb

Overview

Generates an HTML coverage report by writing a coverage_data.js file alongside pre-compiled static assets (index.html, application.js/css). Uses JSONFormatter.build_hash to serialize the result, then writes both coverage.json and coverage_data.js from the same in-memory hash.

Constant Summary collapse

DATA_FILENAME =
"coverage_data.js"

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#format(result) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/simplecov/formatter/html_formatter.rb', line 17

def format(result)
  # `coverage_data.js` feeds the client-side viewer, which renders
  # source from the embedded array — it always needs `source`,
  # regardless of `SimpleCov.source_in_json`. The side-file
  # `coverage.json` honors the setting so downstream tools that
  # read source from disk can opt into a smaller payload. When
  # the setting is at its default (true), the two files share a
  # single serialization.
  FileUtils.mkdir_p(output_path)
  viewer_json = JSON.pretty_generate(JSONFormatter.build_hash(result, include_source: true))
  coverage_json = SimpleCov.source_in_json ? viewer_json : JSON.pretty_generate(JSONFormatter.build_hash(result))

  atomic_write(File.join(output_path, JSONFormatter::FILENAME), coverage_json)
  atomic_write(File.join(output_path, DATA_FILENAME), "window.SIMPLECOV_DATA = #{viewer_json};\n")

  copy_static_assets
  # 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

#format_from_json(json_path, output_dir) ⇒ Object

Generate HTML from a pre-existing coverage.json file without needing a live SimpleCov::Result or even a running test suite.



40
41
42
43
44
45
# File 'lib/simplecov/formatter/html_formatter.rb', line 40

def format_from_json(json_path, output_dir)
  FileUtils.mkdir_p(output_dir)
  json = File.read(json_path)
  atomic_write(File.join(output_dir, DATA_FILENAME), "window.SIMPLECOV_DATA = #{json};\n")
  copy_static_assets(output_dir)
end