Module: Slather::CoverageService::HtmlOutput

Defined in:
lib/slather/coverage_service/html_output.rb

Instance Method Summary collapse

Instance Method Details

#class_for_coverage_data(coverage_data) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/slather/coverage_service/html_output.rb', line 218

def class_for_coverage_data(coverage_data)
  case coverage_data
  when /\d/ then "covered"
  when /#/ then "missed"
  else "never"
  end
end

#class_for_coverage_percentage(percentage) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/slather/coverage_service/html_output.rb', line 234

def class_for_coverage_percentage(percentage)
  case
  when percentage > 85 then "cov_high"
  when percentage > 70 then "cov_medium"
  else "cov_low"
  end
end

#create_html_from_file(coverage_file) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/slather/coverage_service/html_output.rb', line 120

def create_html_from_file(coverage_file)
  filepath = coverage_file.source_file_pathname_relative_to_repo_root
  filename = File.basename(filepath)
  percentage = coverage_file.percentage_lines_tested

  cleaned_gcov_lines = coverage_file.cleaned_gcov_data.split("\n")
  is_file_empty = (cleaned_gcov_lines.count <= 0)

  template = generate_html_template(filename, false, is_file_empty)

  builder = Nokogiri::HTML::Builder.with(template.at('#reports')) { |cov|
    cov.h2(:class => "cov_title") {
      cov.span("Coverage for \"#{filename}\"" + (!is_file_empty ? " : " : ""))
      cov.span("#{'%.2f' % percentage}%", :class => class_for_coverage_percentage(percentage)) unless is_file_empty
    }

    cov.h4("(#{coverage_file.num_lines_tested} of #{coverage_file.num_lines_testable} relevant lines covered)", :class => "cov_subtitle")
    cov.h4(filepath, :class => "cov_filepath")

    if is_file_empty
      cov.p "¯\\_(ツ)_/¯"
      next
    end

    cov.table(:class => "source_code") {
      cleaned_gcov_lines.each do |line|
        data = line.split(':', 3)

        line_number = data[1].to_i
        next unless line_number > 0

        coverage_data = data[0].strip
        line_data = [line_number, data[2], hits_for_coverage_data(coverage_data)]
        classes = ["num", "src", "coverage"]

        cov.tr(:class => class_for_coverage_data(coverage_data)) {
          line_data.each_with_index { |line, idx|
            if idx != 1
              cov.td(line, :class => classes[idx])
            else
              cov.td(:class => classes[idx]) {
                cov.pre { cov.code(line, :class => "objc") }
              }
            end
          }
        }
      end
    }
  }

  @docs[filename] = builder.doc
end

#create_html_reports(coverage_files) ⇒ Object



40
41
42
43
# File 'lib/slather/coverage_service/html_output.rb', line 40

def create_html_reports(coverage_files)
  create_index_html(coverage_files)
  create_htmls_from_files(coverage_files)
end

#create_htmls_from_files(coverage_files) ⇒ Object



116
117
118
# File 'lib/slather/coverage_service/html_output.rb', line 116

def create_htmls_from_files(coverage_files)
  coverage_files.map { |file| create_html_from_file file }
end

#create_index_html(coverage_files) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/slather/coverage_service/html_output.rb', line 55

def create_index_html(coverage_files)
  project_name = File.basename(self.xcodeproj)
  template = generate_html_template(project_name, true, false)

  total_relevant_lines = 0
  total_tested_lines = 0
  coverage_files.each { |coverage_file|
    total_tested_lines += coverage_file.num_lines_tested
    total_relevant_lines += coverage_file.num_lines_testable
  }

  builder = Nokogiri::HTML::Builder.with(template.at('#reports')) { |cov|
    cov.h2 "Files for \"#{project_name}\""

    cov.h4 {
      percentage = (total_tested_lines / total_relevant_lines.to_f) * 100.0
      cov.span "Total Coverage : "
      cov.span '%.2f%%' % percentage, :class => class_for_coverage_percentage(percentage), :id => "total_coverage"
    }

    cov.input(:class => "search", :placeholder => "Search")

    cov.table(:class => "coverage_list", :cellspacing => 0,  :cellpadding => 0) {

      cov.thead {
        cov.tr {
          cov.th "%", :class => "col_num sort", "data-sort" => "data_percentage"
          cov.th "File", :class => "sort", "data-sort" => "data_filename"
          cov.th "Lines", :class => "col_percent sort", "data-sort" => "data_lines"
          cov.th "Relevant", :class => "col_percent sort", "data-sort" => "data_relevant"
          cov.th "Covered", :class => "col_percent sort", "data-sort" => "data_covered"
          cov.th "Missed", :class => "col_percent sort", "data-sort" => "data_missed"
        }
      }

      cov.tbody(:class => "list") {
        coverage_files.each { |coverage_file|
          filename = File.basename(coverage_file.source_file_pathname_relative_to_repo_root)
          filename_link = "#{filename}.html"

          cov.tr {
            percentage = coverage_file.percentage_lines_tested

            cov.td { cov.span '%.2f' % percentage, :class => "percentage #{class_for_coverage_percentage(percentage)} data_percentage" }
            cov.td(:class => "data_filename") {
              cov.a filename, :href => filename_link
            }
            cov.td "#{coverage_file.line_coverage_data.count}", :class => "data_lines"
            cov.td "#{coverage_file.num_lines_testable}", :class => "data_relevant"
            cov.td "#{coverage_file.num_lines_tested}", :class => "data_covered"
            cov.td "#{(coverage_file.num_lines_testable - coverage_file.num_lines_tested)}", :class => "data_missed"
          }
        }
      }
    }
  }

  @docs = Hash.new
  @docs[:index] = builder.doc
end

#gem_root_pathObject



214
215
216
# File 'lib/slather/coverage_service/html_output.rb', line 214

def gem_root_path
  File.expand_path File.join(File.dirname(__dir__), "../..")
end

#generate_html_template(title, is_index, is_file_empty) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/slather/coverage_service/html_output.rb', line 173

def generate_html_template(title, is_index, is_file_empty)
  logo_path = File.join(gem_root_path, "docs/logo.jpg")
  css_path = File.join(gem_root_path, "assets/slather.css")
  highlight_js_path = File.join(gem_root_path, "assets/highlight.pack.js")
  list_js_path = File.join(gem_root_path, "assets/list.min.js")

  builder = Nokogiri::HTML::Builder.new do |doc|
    doc.html {
      doc.head {
        doc.title "#{title} - Slather"
        doc.link :href => css_path, :media => "all", :rel => "stylesheet"
      }
      doc.body {
        doc.header {
          doc.div(:class => "row") {
            doc.a(:href => "index.html") { doc.img(:src => logo_path, :alt => "Slather logo") }
          }
        }
        doc.div(:class => "row") { doc.div(:id => "reports") }
        doc.footer {
          doc.div(:class => "row") {
            doc.p { doc.a("Fork me on Github", :href => "https://github.com/venmo/slather") }
            doc.p("© #{Date.today.year} Slather")
          }
        }

        if is_index
          doc.script :src => list_js_path
          doc.script "var reports = new List('reports', { valueNames: [ 'data_percentage', 'data_filename', 'data_lines', 'data_relevant', 'data_covered', 'data_missed' ]});"
        else
          unless is_file_empty
            doc.script :src => highlight_js_path
            doc.script "hljs.initHighlightingOnLoad();"
          end
        end
      }
    }
  end
  builder.doc
end

#generate_reports(reports) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/slather/coverage_service/html_output.rb', line 45

def generate_reports(reports)
  FileUtils.rm_rf(directory_path) if Dir.exist?(directory_path)
  FileUtils.mkdir_p(directory_path)

  reports.each do |name, doc|
    html_file = File.join(directory_path, "#{name}.html")
    File.write(html_file, doc.to_html)
  end
end

#hits_for_coverage_data(coverage_data) ⇒ Object



226
227
228
229
230
231
232
# File 'lib/slather/coverage_service/html_output.rb', line 226

def hits_for_coverage_data(coverage_data)
  case coverage_data
  when /\d/ then (coverage_data.to_i > 0) ? "#{coverage_data}x" : ""
  when /#/ then "!"
  else ""
  end
end

#open_coverage(index_html) ⇒ Object



35
36
37
38
# File 'lib/slather/coverage_service/html_output.rb', line 35

def open_coverage(index_html)
  path = File.expand_path index_html
  `open '#{path}'` if File.exist?(path)
end

#postObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/slather/coverage_service/html_output.rb', line 18

def post
  create_html_reports(coverage_files)
  generate_reports(@docs)

  index_html_path = File.join(directory_path, "index.html")
  if show_html
    open_coverage index_html_path
  else
    print_path_coverage index_html_path
  end
end


30
31
32
33
# File 'lib/slather/coverage_service/html_output.rb', line 30

def print_path_coverage(index_html)
  path = File.expand_path index_html
  puts "\nTo open the html reports, use \n\nopen '#{path}'\n\nor use '--show' flag to open it automatically.\n\n"
end