class SimplecovHtmlInline::Formatter

A SimpleCov formatter based on {SimpleCov::Formatter::HTMLFormatter} that uses inline/embedded assets

Constants

MEDIA_TYPE_MAPPING

Mapping from file extension to media type

PUBLIC_DIR

Directory where simplecov-html assets live

Public Instance Methods

format(result) click to toggle source

Creates an HTML coverage report with inline assets

@param result [SimpleCov::Result] the SimpleCov result to format

# File lib/simplecov_html_inline/formatter.rb, line 29
def format(result)
  # Same as SimpleCov::Formatter::HTMLFormatter#format except we omit the
  # copying of asset files
  File.open(File.join(output_path, 'index.html'), 'wb') do |file|
    file.puts template('layout').result(binding)
  end
  puts output_message(result)
end

Private Instance Methods

assets_path(name) click to toggle source

Constructs a data URI for an asset with the given name.

@note

This overrides {SimpleCov::Formatter::HTMLFormatter#assets_path} (which
returns a relative URL)

@param name [String]

name of file (relative to simplecov-html's public directory) to return
a data URI for

@return [String]

data URI for the asset, or an empty string if an asset with the given
name does not exist
# File lib/simplecov_html_inline/formatter.rb, line 52
def assets_path(name)
  file_path = File.join(PUBLIC_DIR, name)
  return '' unless File.exist?(file_path)

  media_type = MEDIA_TYPE_MAPPING.fetch(File.extname(name))
  contents = IO.read(file_path)

  if media_type == 'text/css'
    contents.gsub!(/url\(['"]?(.+?)['"]?\)/) { "url(#{assets_path($1)})" }
  end

  "data:#{media_type};base64,#{Base64.strict_encode64(contents)}"
end