writing a (very ugly) file coverage report

This commit is contained in:
Josh Holtrop 2017-01-15 14:19:45 -05:00
parent 0422f6f573
commit 1fe35ea23d
3 changed files with 82 additions and 4 deletions

View File

@ -0,0 +1,60 @@
<html>
<head>
<title>Coverage for <%= @source_file_name %></title>
<style>
.code {
font-family: monospace;
}
</style>
</head>
<body>
<h2>Coverage for <%= @source_file_name %></title>
<table>
<tr>
<th>Line Coverage:</th>
<% if @total_lines > 0 %>
<td><%= @covered_lines %> / <%= @total_lines %> = <%= (100.0 * @covered_lines / @total_lines).to_i %>%</td>
<% else %>
<td>-</td>
<% end %>
</tr>
<tr>
<th>Branch Coverage:</th>
<% if @total_branches > 0 %>
<td><%= @covered_branches %> / <%= @total_branches %> = <%= (100.0 * @covered_branches / @total_branches).to_i %>%</td>
<% else %>
<td>-</td>
<% end %>
</tr>
</table>
<table>
<tr>
<th>Execution Count</th>
<th>Branch Count</th>
<th>Line</th>
<th>Source</th>
</tr>
<% source_file_lines.each_with_index do |line, i| %>
<% line_number = i + 1 %>
<tr>
<td><%= file_coverage.get_line_count(line_number) %></td>
<td>
<% if branches = file_coverage.get_branches(line_number) %>
<% branches.each_with_index do |(branch_id, branch_coverage), i| %>
<% if i > 0 %>
<br/>
<% end %>
<%= branch_coverage[:taken_count] %>
<% if branch_coverage[:branch_info] %>
(<%= branch_coverage[:branch_info] %>)
<% end %>
<% end %>
<% end %>
</td>
<td><%= line_number %></td>
<td class="code"><%= CGI.escape_html(line.chomp).gsub("\t", "&nbsp;" * 4) %></td>
</tr>
<% end %>
</table>
</body>
</html>

View File

@ -1,6 +1,9 @@
module Gcovinator
class FileCoverage
attr_reader :line_counts
attr_reader :branches
def initialize
@line_counts = {}
@branches = {}
@ -25,7 +28,7 @@ module Gcovinator
@branches[line_number][branch_id][:branch_info] = branch_info
end
def get_branch(line_number)
def get_branches(line_number)
@branches[line_number]
end

View File

@ -8,7 +8,7 @@ module Gcovinator
@covered_lines = 0
@total_branches = 0
@covered_branches = 0
run(source_file_name, File.join(output_dir, report_file_name))
run(source_file_name, File.join(output_dir, report_file_name), file_coverage)
end
private
@ -23,18 +23,33 @@ module Gcovinator
source_file_name
end
def run(source_file_name, output_file_name)
def run(source_file_name, output_file_name, file_coverage)
analyze(file_coverage)
require "cgi"
require "erb"
source_file = read_source_file(source_file_name)
source_file_lines = source_file.lines.to_a
file_report_template = File.read(File.join(File.dirname(__FILE__), "../../assets/file_report.html.erb"))
erb = ERB.new(file_report_template, nil, "<>")
report = erb.run(binding.clone)
report = erb.result(binding.clone)
File.open(output_file_name, "w") do |fh|
fh.write(report)
end
end
def analyze(file_coverage)
file_coverage.line_counts.each do |line_number, count|
@total_lines += 1
@covered_lines += 1 if count > 0
end
file_coverage.branches.each do |line_number, branches|
branches.each do |branch_id, branch_coverage|
@total_branches += 1
@covered_branches += 1 if branch_coverage[:taken_count] > 0
end
end
end
def read_source_file(source_file_name)
begin
File.read(source_file_name)