Class: Minitest::HTMLReporter

Inherits:
AbstractReporter
  • Object
show all
Defined in:
lib/minitest/html_plugin.rb

Overview

Custom reporter class that creates an HTML file in the docs folder

Constant Summary collapse

DOCS_DIR =
"#{__dir__}/../../docs".freeze
TESTS_DIR =
"#{__dir__}/../../test/asciidoctor/html".freeze

Instance Method Summary collapse

Constructor Details

#initializeHTMLReporter

Returns a new instance of HTMLReporter.



13
14
15
16
# File 'lib/minitest/html_plugin.rb', line 13

def initialize
  @results = {}
  super
end

Instance Method Details

#display_failure(failure, color) ⇒ Object



22
23
24
# File 'lib/minitest/html_plugin.rb', line 22

def display_failure(failure, color)
  %(<pre class="border border-#{color}"><code class="language-shell">#{failure}</code></pre>\n)
end

#display_result(name, adoc, html) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/minitest/html_plugin.rb', line 34

def display_result(name, adoc, html)
  key = "test_#{name}"
  failed = @results[key]&.size&.positive?
  color = failed ? "danger" : "success"
  id = "test-#{name.tr "_", "-"}"
  title = display_result_title name, id, failed, color
  pre = %(<pre><code class="language-asciidoc">#{CGI.escapeHTML adoc}</code></pre>\n)
  fail = failed ? display_failure(CGI.escapeHTML(@results[key].join("\n")), color) : ""
  %(<div>#{title}<div class="collapse full-width-bg" id="#{id}">#{pre}#{fail}#{html}</div></div>)
end

#display_result_title(name, id, failed, color) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/minitest/html_plugin.rb', line 26

def display_result_title(name, id, failed, color)
  style = %(style="vertical-align: -0.125em;")
  status_icon = %(<i class="bi bi-#{failed ? "x" : "check"}-square text-#{color}" #{style}></i>)
  chevron = %(<i class="bi bi-chevron-expand"></i>)
  attrs = %(type="button" data-bs-toggle="collapse" data-bs-target="##{id}")
  %(#{status_icon}<button #{attrs} class="btn btn-link">#{chevron} #{name.tr("_", " ").capitalize}</button>\n)
end

#record(result) ⇒ Object



18
19
20
# File 'lib/minitest/html_plugin.rb', line 18

def record(result)
  @results[result.name] = result.failures
end

#reportObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/minitest/html_plugin.rb', line 54

def report
  frontmatter = %(---\nlayout: default\ntitle: Test Results\n---\n)
  time = %(<p class="lead">#{Time.now.strftime("%d/%m/%Y %H:%M")}</p>\n)
  results = []
  Pathname(TESTS_DIR).children.sort.each do |pn|
    next unless pn.directory?

    report_files results, pn
  end
  html = %(#{frontmatter}#{time}#{results.join "\n"})
  File.write("#{DOCS_DIR}/index.html", html)
end

#report_files(results, dirname) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/minitest/html_plugin.rb', line 45

def report_files(results, dirname)
  dirname.children.sort.each do |filepath|
    next unless filepath.extname == ".adoc"

    results << display_result(filepath.basename.sub_ext("").to_s,
                              File.read(filepath), File.read(filepath.sub_ext(".html")))
  end
end