Module: Slather::CoverageService::HtmlOutput

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#docsObject (readonly)

Returns the value of attribute docs.



8
9
10
# File 'lib/slather/coverage_service/html_output.rb', line 8

def docs
  @docs
end

Instance Method Details

#class_for_coverage_line(coverage_file, coverage_line) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/slather/coverage_service/html_output.rb', line 231

def class_for_coverage_line(coverage_file, coverage_line)
  hits = coverage_file.coverage_for_line(coverage_line)
  case
  when hits == nil then "never"
  when hits > 0 then "covered"
  else "missed"
  end
end

#class_for_coverage_percentage(percentage) ⇒ Object



249
250
251
252
253
254
255
# File 'lib/slather/coverage_service/html_output.rb', line 249

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



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
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/slather/coverage_service/html_output.rb', line 132

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("#{decimal_f(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

    line_number_separator = coverage_file.line_number_separator

    cov.table(:class => "source_code") {
      cleaned_gcov_lines.each do |line|

        line_number = coverage_file.line_number_in_line(line)
        next unless line_number > 0

        line_source = line.split(line_number_separator, 3)[2]
        line_data = [line_number, line_source, hits_for_coverage_line(coverage_file, line)]
        classes = ["num", "src", "coverage"]

        cov.tr(:class => class_for_coverage_line(coverage_file,line)) {
          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



47
48
49
50
# File 'lib/slather/coverage_service/html_output.rb', line 47

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



128
129
130
# File 'lib/slather/coverage_service/html_output.rb', line 128

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

#create_index_html(coverage_files) ⇒ Object



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
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/slather/coverage_service/html_output.rb', line 67

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 decimal_f(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 = CGI.escape(filename) + ".html"

          cov.tr {
            percentage = coverage_file.percentage_lines_tested

            cov.td { cov.span decimal_f(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



227
228
229
# File 'lib/slather/coverage_service/html_output.rb', line 227

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

#generate_html_template(title, is_index, is_file_empty) ⇒ Object



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
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/slather/coverage_service/html_output.rb', line 186

def generate_html_template(title, is_index, is_file_empty)
  logo_path = "logo.jpg"
  css_path = "slather.css"
  highlight_js_path = "highlight.pack.js"
  list_js_path = "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/SlatherOrg/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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/slather/coverage_service/html_output.rb', line 52

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

  FileUtils.cp(File.join(gem_root_path, "docs/logo.jpg"), directory_path)
  FileUtils.cp(File.join(gem_root_path, "assets/slather.css"), directory_path)
  FileUtils.cp(File.join(gem_root_path, "assets/highlight.pack.js"), directory_path)
  FileUtils.cp(File.join(gem_root_path, "assets/list.min.js"), 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_line(coverage_file, coverage_line) ⇒ Object



240
241
242
243
244
245
246
247
# File 'lib/slather/coverage_service/html_output.rb', line 240

def hits_for_coverage_line(coverage_file, coverage_line)
  hits = coverage_file.coverage_for_line(coverage_line)
  case
  when hits == nil then ""
  when hits > 0 then "#{hits}x"
  else "!"
  end
end

#open_coverage(index_html) ⇒ Object



42
43
44
45
# File 'lib/slather/coverage_service/html_output.rb', line 42

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

#postObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/slather/coverage_service/html_output.rb', line 25

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


37
38
39
40
# File 'lib/slather/coverage_service/html_output.rb', line 37

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