Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache erb templates when generating report #114

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/simplecov-html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Formatter
class HTMLFormatter
def initialize
@branchable_result = SimpleCov.branch_coverage?
@templates = {}
end

def format(result)
Expand Down Expand Up @@ -54,7 +55,7 @@ def line_status?(source_file, line)

# Returns the an erb instance for the template of given name
def template(name)
ERB.new(File.read(File.join(File.dirname(__FILE__), "../views/", "#{name}.erb")))
@templates[name] ||= ERB.new(File.read(File.join(File.dirname(__FILE__), "../views/", "#{name}.erb")))
end

def output_path
Expand Down
7 changes: 5 additions & 2 deletions views/source_file.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@
<% source_file.lines.each do |line| %>
<div>
<li class="<%= line_status?(source_file, line) %>" data-hits="<%= line.coverage ? line.coverage : '' %>" data-linenumber="<%= line.number %>">
<% if line.covered? %><span class="hits"><%= line.coverage %></span><% end %>
<% if line.skipped? %><span class="hits">skipped</span><% end %>
<% if line.covered? %>
<span class="hits"><%= line.coverage %></span>
<% elsif line.skipped? %>
<span class="hits">skipped</span>
<% end %>

<% if branchable_result? %>
<% source_file.branches_for_line(line.number).each do |branch_type, hit_count| %>
Expand Down