Skip to content

Commit 519e2ac

Browse files
author
Yuri Smirnov
committed
Merge remote-tracking branch 'upstream/main' into add-method-coverage-support
2 parents dea7be1 + 79ddf61 commit 519e2ac

8 files changed

Lines changed: 71 additions & 18 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ GEM
2323
parallel (1.22.1)
2424
parser (3.1.1.0)
2525
ast (~> 2.4.1)
26-
rack (2.2.3)
26+
rack (2.2.6.4)
2727
rainbow (3.1.1)
2828
rake (13.0.1)
2929
regexp_parser (2.2.1)

assets/javascripts/application.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,29 @@ $(document).ready(function () {
5656
}
5757
});
5858

59+
// Set-up of anchor of linenumber
60+
$('.source_table li[data-linenumber]').click(function () {
61+
$('#cboxLoadedContent').scrollTop(this.offsetTop);
62+
var new_anchor = curr_anchor.replace(/-.*/, '') + '-L' + $(this).data('linenumber');
63+
window.location.replace(window.location.href.replace(/#.*/, '#' + new_anchor));
64+
curr_anchor = new_anchor;
65+
return false;
66+
});
67+
5968
window.onpopstate = function (event) {
6069
if (window.location.hash.substring(0, 2) == "#_") {
6170
$.colorbox.close();
6271
curr_anchor = window.location.hash.substring(1);
6372
} else {
6473
if ($('#colorbox').is(':hidden')) {
65-
$('a.src_link[href="' + window.location.hash + '"]').colorbox({ open: true });
74+
var anchor = window.location.hash.substring(1);
75+
var ary = anchor.split('-L');
76+
var source_file_id = ary[0];
77+
var linenumber = ary[1];
78+
$('a.src_link[href="#' + source_file_id + '"]').colorbox({ open: true });
79+
if (linenumber !== undefined) {
80+
$('#cboxLoadedContent').scrollTop($('#cboxLoadedContent .source_table li[data-linenumber="' + linenumber + '"]')[0].offsetTop);
81+
}
6682
}
6783
}
6884
};
@@ -106,8 +122,15 @@ $(document).ready(function () {
106122

107123
if (window.location.hash) {
108124
var anchor = window.location.hash.substring(1);
109-
if (anchor.length == 40) {
125+
if (anchor.length === 40) {
110126
$('a.src_link[href="#' + anchor + '"]').click();
127+
} else if (anchor.length > 40) {
128+
var ary = anchor.split('-L');
129+
var source_file_id = ary[0];
130+
var linenumber = ary[1];
131+
$('a.src_link[href="#' + source_file_id + '"]').colorbox({ open: true });
132+
// Scroll to anchor of linenumber
133+
$('#' + source_file_id + ' li[data-linenumber="' + linenumber + '"]').click();
111134
} else {
112135
$('.group_tabs a.' + anchor.replace('_', '')).click();
113136
}

assets/stylesheets/screen.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ td.strong {
196196
border-left: 5px solid white;
197197
}
198198

199+
.source_table pre li:hover {
200+
cursor: pointer;
201+
text-decoration: underline black;
202+
}
203+
199204
.source_table pre li code {
200205
white-space: pre;
201206
white-space: pre-wrap;

lib/simplecov-html.rb

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "fileutils"
66
require "digest/sha1"
77
require "time"
8+
require "base64"
89

910
# Ensure we are using a compatible version of SimpleCov
1011
major, minor, patch = SimpleCov::VERSION.scan(/\d+/).first(3).map(&:to_i)
@@ -19,11 +20,16 @@ class HTMLFormatter # rubocop:disable Metrics/ClassLength
1920
def initialize
2021
@branch_coverage = SimpleCov.branch_coverage?
2122
@method_coverage = SimpleCov.method_coverage?
23+
@templates = {}
24+
@inline_assets = !ENV["SIMPLECOV_INLINE_ASSETS"].nil?
25+
@public_assets_dir = File.join(File.dirname(__FILE__), "../public/")
2226
end
2327

2428
def format(result)
25-
Dir[File.join(File.dirname(__FILE__), "../public/*")].each do |path|
26-
FileUtils.cp_r(path, asset_output_path)
29+
unless @inline_assets
30+
Dir[File.join(@public_assets_dir, "*")].each do |path|
31+
FileUtils.cp_r(path, asset_output_path)
32+
end
2733
end
2834

2935
File.open(File.join(output_path, "index.html"), "wb") do |file|
@@ -68,7 +74,7 @@ def line_status?(source_file, line)
6874

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

7480
def output_path
@@ -84,9 +90,26 @@ def asset_output_path
8490
end
8591

8692
def assets_path(name)
93+
return asset_inline(name) if @inline_assets
94+
8795
File.join("./assets", SimpleCov::Formatter::HTMLFormatter::VERSION, name)
8896
end
8997

98+
def asset_inline(name)
99+
path = File.join(@public_assets_dir, name)
100+
101+
# Only have a few content types, just hardcode them
102+
content_type = {
103+
".js" => "text/javascript",
104+
".png" => "image/png",
105+
".gif" => "image/gif",
106+
".css" => "text/css",
107+
}[File.extname(name)]
108+
109+
base64_content = Base64.strict_encode64 File.open(path).read
110+
"data:#{content_type};base64,#{base64_content}"
111+
end
112+
90113
# Returns the html for the given source_file
91114
def formatted_source_file(source_file)
92115
template("source_file").result(binding)

public/application.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/application.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

views/layout.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
66
<script src='<%= assets_path('application.js') %>' type='text/javascript'></script>
77
<link href='<%= assets_path('application.css') %>' media='screen, projection, print' rel='stylesheet' type='text/css' />
8-
<link rel="shortcut icon" type="image/png" href="<%= assets_path("favicon_#{coverage_css_class(result.source_files.covered_percent)}.png") %>" />
9-
<link rel="icon" type="image/png" href="<%= assets_path('favicon.png') %>" />
8+
<link rel="icon" type="image/png" href="<%= assets_path("favicon_#{coverage_css_class(result.source_files.covered_percent)}.png") %>" />
109
</head>
1110

1211
<body<%= ' data-branch-coverage=true' if branch_coverage? %>>

views/source_file.erb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@
6161
<% source_file.lines.each do |line| %>
6262
<div>
6363
<li class="<%= line_status?(source_file, line) %>" data-hits="<%= line.coverage ? line.coverage : '' %>" data-linenumber="<%= line.number %>">
64-
<% if line.covered? %><span class="hits"><%= line.coverage %></span><% end %>
65-
<% if line.skipped? %><span class="hits">skipped</span><% end %>
64+
<% if line.covered? %>
65+
<span class="hits"><%= line.coverage %></span>
66+
<% elsif line.skipped? %>
67+
<span class="hits">skipped</span>
68+
<% end %>
6669

6770
<% if branch_coverage? %>
6871
<% source_file.branches_for_line(line.number).each do |branch_type, hit_count| %>

0 commit comments

Comments
 (0)