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/asciidoc".freeze
TESTS_DIR =
"#{__dir__}/../../test/asciidoctor/html".freeze
CONFIG_FILE =
"#{DOCS_DIR}/config.yml".freeze

Instance Method Summary collapse

Constructor Details

#initializeHTMLReporter

Returns a new instance of HTMLReporter.



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

def initialize
  @results = {}
  super
end

Instance Method Details

#display_failure(failure) ⇒ Object



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

def display_failure(failure)
  %([source,shell,role="text-bg-danger"]\n----\n#{failure}\n----\n)
end

#display_result(name, adoc) ⇒ Object



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

def display_result(name, adoc)
  key = "test_#{name}"
  failed = @results[key]&.size&.positive?
  color = failed ? "danger" : "success"
  id = "test-#{name.tr "_", "-"}"
  title = display_result_title name, failed, color
  pre = %([source,asciidoc]\n------\n#{adoc}\n------\n)
  fail = failed ? display_failure(@results[key].join("\n")) : ""
  %([##{id}]\n== #{title}\n\n#{pre}#{fail}\n\n#{adoc}\n\n)
end

#display_result_title(name, failed, color) ⇒ Object



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

def display_result_title(name, failed, color)
  status_icon = %(pass:[<i class="bi bi-#{failed ? "x" : "check"}-lg text-#{color}"></i>])
  %(#{status_icon} #{name.tr("_", " ").capitalize})
end

#record(result) ⇒ Object



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

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

#reportObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/minitest/html_plugin.rb', line 52

def report
  time = %([.lead]\n#{Time.now.strftime("%d/%m/%Y %H:%M")}\n)
  results = []
  Pathname(TESTS_DIR).children.reject { |f| f.file? || f.basename.to_s.start_with?("_") }.sort.each do |pn|
    report_files results, pn
  end
  adoc = %(= Test Results\nRavi Rajani\n#{time}\n#{results.join "\n"})
  File.write("#{DOCS_DIR}/tests.adoc", adoc)
  Asciidoctor::Html::CLI.run({ "config-file": CONFIG_FILE, watch: false })
end

#report_files(results, dirname) ⇒ Object



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

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))
  end
end