Project

General

Profile

Feature #44345 » 0001-Use-SimpleCov-s-built-in-HTML-formatter-instead-of-R.patch

Go MAEDA, 2026-08-16 04:13

View differences:

test/coverage/html_formatter.rb
1
# frozen_string_literal: true
2

  
3
# Redmine - project management software
4
# Copyright (C) 2006-  Jean-Philippe Lang
5
#
6
# This program is free software; you can redistribute it and/or
7
# modify it under the terms of the GNU General Public License
8
# as published by the Free Software Foundation; either version 2
9
# of the License, or (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19

  
20
require 'erb'
21
require 'cgi'
22

  
23
# A simple formatter for SimpleCov
24
module Redmine
25
  module Coverage
26
    class HtmlFormatter
27
      def format(result)
28
        File.open(File.join(output_path, "index.html"), "w") do |file|
29
          file.puts template('index').result(binding)
30
        end
31
        result.source_files.each do |source_file|
32
          File.open(File.join(output_path, source_file_result(source_file)), "w") do |file|
33
            file.puts template('source').result(binding).force_encoding('utf-8')
34
          end
35
        end
36
        puts output_message(result)
37
      end
38

  
39
      def output_message(result)
40
        "Coverage report generated for #{result.command_name} to #{output_path}. #{result.covered_lines} / #{result.total_lines} LOC (#{result.covered_percent.round(2)}%) covered."
41
      end
42

  
43
      private
44

  
45
      def now
46
        @now = Time.now.utc
47
      end
48

  
49
      def output_path
50
        SimpleCov.coverage_path
51
      end
52

  
53
      def shortened_filename(source_file)
54
        source_file.filename.gsub(SimpleCov.root, '.').delete_prefix('./')
55
      end
56

  
57
      def link_to_source_file(source_file)
58
        %(<a href="#{source_file_result source_file}">#{shortened_filename source_file}</a>)
59
      end
60

  
61
      def source_file_result(source_file)
62
        shortened_filename(source_file).gsub('/', '__')+'.html'
63
      end
64

  
65
      def revision_link
66
        if revision = Redmine::VERSION.revision
67
          %(<a href="http://www.redmine.org/projects/redmine/repository/revisions/#{revision}">r#{revision}</a>)
68
        end
69
      end
70

  
71
      # Returns the an erb instance for the template of given name
72
      def template(name)
73
        ERB.new(File.read(File.join(File.dirname(__FILE__), 'views', "#{name}.erb")))
74
      end
75
    end
76
  end
77
end
test/coverage/views/index.erb
1
<!DOCTYPE html>
2
<head>
3
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
4
<title>Redmine code coverage</title>
5
<style>
6
html {overflow-y:scroll;}
7
body {font-family:"Lucida Grande","Lucida Sans",Verdana,Helvetica,Arial,sans-serif; font-size:80%;}
8
h1 {color:#777; margin-bottom:0.2em;}
9
h2 {color:#aaa;margin-top:1em;font-size:18px;}
10
table {width:100%; border-collapse:collapse;}
11
th, td {border:1px solid #e2e2e2;}
12
td {text-align:right; white-space: nowrap; font-family:"Bitstream Vera Sans Mono","Monaco","Courier New",monospace;}
13
td.filename {text-align:left; white-space: normal; font-family:"Lucida Grande","Lucida Sans",Verdana,Helvetica,Arial,sans-serif;}
14
th {background:#e2e2e2;}
15
table.file_list tr:hover { background-color:#ffffdd; }
16
#generation {color:#777; font-size:90%;}
17
a, a:link, a:visited {color:#169; text-decoration:none;}
18
a:hover, a:active {color:#c61a1a; text-decoration:underline;}
19
div.percent {height:1em; empty-cells:show; padding:0px; border-collapse:collapse; width:100px !important; float:left; margin:0 0.5em 0 0.5em;}
20
div.percent div {float:left; height:1em; padding:0px !important;}
21
div.percent div.covered {background:#8c7;}
22
div.percent div.uncovered {background:#d76;}
23
</style>
24
</head>
25
<body>
26
<h1>Redmine code coverage</h1>
27
<p id='generation'>
28
  Generated on <%= now %> (<%= revision_link %>).
29
  More information about this environment at <a href='http://www.redmine.org/projects/redmine/wiki/Continuous_integration'>redmine.org</a>.
30
</p>
31

  
32
<table class="file_list">
33
  <thead>
34
    <tr>
35
      <th>File</th>
36
      <th colspan="2">% covered</th>
37
      <th>Lines</th>
38
      <th>Relevant</th>
39
      <th>Covered</th>
40
    </tr>
41
  </thead>
42
  <tbody>
43
    <% result.source_files.each do |source_file| %>
44
    <tr>
45
      <td class="filename"><%= link_to_source_file(source_file) %></td>
46
      <td><%= "%.1f" % source_file.covered_percent %> %</td>
47
      <td>
48
        <div class="percent">
49
          <div class="covered" style="width:<%= source_file.covered_percent.to_i %>px"></div>
50
          <div class="uncovered" style="width:<%= 100 - source_file.covered_percent.to_i %>px"></div>
51
        </div>
52
      </td>
53
      <td><%= source_file.lines.count %></td>
54
      <td><%= source_file.covered_lines.count + source_file.missed_lines.count %></td>
55
      <td><%= source_file.covered_lines.count %></td>
56
    </tr>
57
    <% end %>
58
  </tbody>
59
</table>  
60
</body>
61
</html>
test/coverage/views/source.erb
1
<!DOCTYPE html>
2
<head>
3
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
4
<title>Redmine code coverage</title>
5
<style>
6
html {overflow-y:scroll;}
7
body {font-family:"Lucida Grande","Lucida Sans",Verdana,Helvetica,Arial,sans-serif; font-size:80%;}
8
h1 {color:#777; margin-bottom:0.2em;}
9
h2 {color:#aaa; margin-top:1em; font-size:18px;}
10
#generation {color:#777; font-size:90%;}
11
a, a:link, a:visited {color:#169; text-decoration:none;}
12
a:hover, a:active {color:#c61a1a; text-decoration:underline;}
13
pre, code {
14
  color: #000000;
15
  font-family: "Bitstream Vera Sans Mono","Monaco","Courier New",monospace;
16
  font-size: 95%;
17
  line-height: 1.3em;
18
  margin-top: 0;
19
  margin-bottom: 0;
20
  padding: 0;
21
}
22
div.source {border:1px solid #e2e2e2;}
23
.covered {background:#bed2be;}
24
.missed {background:#fba;}
25
.never {background:#eee;}
26
</style>
27
</head>
28
<body>
29
<h1>Redmine code coverage</h1>
30
<p id='generation'>
31
  Generated on <%= now %> (<%= revision_link %>).
32
  More information about this environment at <a href='http://www.redmine.org/projects/redmine/wiki/Continuous_integration'>redmine.org</a>.
33
</p>
34
<h2><%= shortened_filename source_file %> (<%= "%.1f" % source_file.covered_percent %> %)</h2>
35

  
36
<div class="source">
37
<% source_file.lines.each_with_index do |line, i| %>
38
  <pre class="<%= line.status %>" data-hits="<%= line.coverage ? line.coverage : '' %>" data-linenumber="<%= line.number %>"
39
  ><code class="ruby"><%= i.to_s.rjust 4 %> <%= CGI.escapeHTML(line.src.chomp) %></code></pre>
40
<% end %>
41
</div>
test/test_helper.rb
19 19

  
20 20
if ENV["COVERAGE"]
21 21
  require 'simplecov'
22
  require_relative 'coverage/html_formatter'
23
  SimpleCov.formatter = Redmine::Coverage::HtmlFormatter
24 22
  SimpleCov.start 'rails'
25 23
end
26 24

  
(4-4/5)