123456789_123456789_123456789_123456789_123456789_

Class: SimpleCov::Formatter::HTMLFormatter

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/simplecov/formatter/html_formatter.rb,
lib/simplecov/formatter/html_formatter/coverage_helpers.rb,
lib/simplecov/formatter/html_formatter/view_helpers.rb

Overview

Generates an HTML coverage report from ::SimpleCov results.

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(silent: false, inline_assets: false) ⇒ HTMLFormatter

[ GitHub ]

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

def initialize(silent: false, inline_assets: false)
  @branch_coverage = SimpleCov.branch_coverage?
  @method_coverage = SimpleCov.method_coverage?
  @templates = {}
  @inline_assets = inline_assets || ENV.key?("SIMPLECOV_INLINE_ASSETS")
  @public_assets_dir = File.join(__dir__, "html_formatter/public/")
  @silent = silent
end

Instance Attribute Details

#branch_coverage?Boolean (readonly, private)

[ GitHub ]

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

def branch_coverage?
  @branch_coverage
end

#method_coverage?Boolean (readonly, private)

[ GitHub ]

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

def method_coverage?
  @method_coverage
end

Instance Method Details

#asset_inline(name) (private)

[ GitHub ]

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

def asset_inline(name)
  path = File.join(@public_assets_dir, name)
  base64_content = [File.read(path)].pack("m0")
  "data:#{CONTENT_TYPES.fetch(File.extname(name))};base64,#{base64_content}"
end

#asset_output_path (private)

[ GitHub ]

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

def asset_output_path
  @asset_output_path ||= File.join(output_path, "assets", VERSION).tap do |path|
    FileUtils.mkdir_p(path)
  end
end

#assets_path(name) (private)

[ GitHub ]

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

def assets_path(name)
  return asset_inline(name) if @inline_assets

  File.join("./assets", VERSION, name)
end

#format(result)

[ GitHub ]

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

def format(result)
  unless @inline_assets
    Dir[File.join(@public_assets_dir, "*")].each do |path|
      FileUtils.cp_r(path, asset_output_path, remove_destination: true)
    end
  end

  File.write(File.join(output_path, "index.html"), template("layout").result(binding), mode: "wb")
  puts output_message(result) unless @silent
end

#formatted_file_list(title, source_files) (private)

[ GitHub ]

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

def formatted_file_list(title, source_files)
  template("file_list").result(binding)
end

#formatted_source_file(source_file) (private)

[ GitHub ]

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

def formatted_source_file(source_file)
  template("source_file").result(binding)
rescue Encoding::CompatibilityError => e
  puts "Encoding problems with file #{source_file.filename}. Simplecov/ERB can't handle non ASCII characters in filenames. Error: #{e.message}."
  %(<div class="source_table" id="#{id(source_file)}"><div class="header"><h2>Encoding Error</h2><p>#{ERB::Util.html_escape(e.message)}</p></div></div>)
end

#output_message(result) (private)

[ GitHub ]

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

def output_message(result)
  lines = ["Coverage report generated for #{result.command_name} to #{output_path}"]
  lines << "Line coverage: #{render_stats(result, :line)}"
  lines << "Branch coverage: #{render_stats(result, :branch)}" if branch_coverage?
  lines << "Method coverage: #{render_stats(result, :method)}" if method_coverage?
  lines.join("\n")
end

#output_path (private)

[ GitHub ]

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

def output_path
  SimpleCov.coverage_path
end

#render_stats(result, criterion) (private)

[ GitHub ]

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

def render_stats(result, criterion)
  stats = result.coverage_statistics.fetch(criterion)
  Kernel.format("%<covered>d / %<total>d (%<percent>.2f%%)", covered: stats.covered, total: stats.total, percent: stats.percent)
end

#template(name) (private)

[ GitHub ]

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

def template(name)
  @templates[name] ||= ERB.new(File.read(File.join(__dir__, "html_formatter/views/", "#{name}.erb")), trim_mode: "-")
end