123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Formatter::HTMLFormatter

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base
Instance Chain:
self, Base
Inherits: SimpleCov::Formatter::Base
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

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

Instance Method Details

#atomic_write(dest, content) (private)

Write content at dest via a uniquely-named temp file in the same directory, then File.rename onto the final path. rename is atomic and overwrite-safe, so:

  • parallel writers can't race on an unlink-then-write window, and
  • read-only existing files (e.g. assets shipped at 0444 from /nix/store) are replaced cleanly instead of triggering EACCES from opening the existing path for writing.
[ GitHub ]

  
# File 'lib/simplecov/formatter/html_formatter.rb', line 66

def atomic_write(dest, content)
  temp = "#{dest}.#{Process.pid}.#{rand(2**32).to_s(36)}"
  File.binwrite(temp, content)
  File.rename(temp, dest)
ensure
  FileUtils.rm_f(temp)
end

#copy_static_assets(dest_dir = output_path) (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/html_formatter.rb', line 53

def copy_static_assets(dest_dir = output_path)
  Dir[File.join(public_dir, "*")].each do |src|
    atomic_write(File.join(dest_dir, File.basename(src)), File.binread(src))
  end
end

#entry_point_filename (private)

[ GitHub ]

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

def entry_point_filename
  "index.html"
end

#format(result)

[ GitHub ]

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

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

[ GitHub ]

  
# 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

#public_dir (private)

[ GitHub ]

  
# File 'lib/simplecov/formatter/html_formatter.rb', line 74

def public_dir
  File.join(__dir__, "html_formatter/public/")
end