|
| 1 | +# encoding: UTF-8 |
| 2 | + |
| 3 | +module Test::Reporters |
| 4 | + |
| 5 | + # HTML Test Reporter |
| 6 | + # |
| 7 | + # Writes the run as one self-contained HTML document, once the run has |
| 8 | + # finished: a tally, then every result under the heading of its case. |
| 9 | + # Test output is captured and shown with the test that wrote it. |
| 10 | + # |
| 11 | + class Html < Abstract |
| 12 | + |
| 13 | + LABELS = { |
| 14 | + :pass => 'PASS', |
| 15 | + :fail => 'FAIL', |
| 16 | + :error => 'ERROR', |
| 17 | + :todo => 'TODO', |
| 18 | + :skip => 'SKIP' |
| 19 | + } |
| 20 | + |
| 21 | + ESCAPES = { |
| 22 | + '&' => '&', '<' => '<', '>' => '>', '"' => '"', "'" => ''' |
| 23 | + } |
| 24 | + |
| 25 | + STYLE = <<-CSS |
| 26 | + :root { color-scheme: light dark; --pass: #1a7f37; --fail: #cf222e; --todo: #9a6700; --skip: #57606a; --line: #d0d7de; } |
| 27 | + @media (prefers-color-scheme: dark) { :root { --pass: #3fb950; --fail: #f85149; --todo: #d29922; --skip: #8b949e; --line: #30363d; } } |
| 28 | + body { font-family: system-ui, sans-serif; line-height: 1.5; max-width: 60rem; margin: 2rem auto; padding: 0 1rem; } |
| 29 | + h1 { margin-bottom: 0.25rem; } |
| 30 | + .tally { font-weight: bold; } |
| 31 | + .tally.passed { color: var(--pass); } |
| 32 | + .tally.failed { color: var(--fail); } |
| 33 | + section { border-left: 2px solid var(--line); padding-left: 1rem; margin: 1rem 0; } |
| 34 | + .result { margin: 0.25rem 0; } |
| 35 | + .status { display: inline-block; min-width: 3.5rem; font: bold 0.85em ui-monospace, monospace; } |
| 36 | + .pass .status { color: var(--pass); } |
| 37 | + .fail .status, .error .status { color: var(--fail); } |
| 38 | + .todo .status { color: var(--todo); } |
| 39 | + .skip .status { color: var(--skip); } |
| 40 | + .time, .reason, footer { color: var(--skip); font-size: 0.85em; } |
| 41 | + pre { margin: 0.25rem 0 0.5rem 3.5rem; padding: 0.5rem; border: 1px solid var(--line); overflow-x: auto; font-size: 0.85em; } |
| 42 | + CSS |
| 43 | + |
| 44 | + # |
| 45 | + def initialize(runner, output: $stdout) |
| 46 | + super(runner) |
| 47 | + @output = output |
| 48 | + @body = [] |
| 49 | + @depth = 0 |
| 50 | + end |
| 51 | + |
| 52 | + # Test output would otherwise land in the middle of the document. |
| 53 | + def capture_output? |
| 54 | + true |
| 55 | + end |
| 56 | + |
| 57 | + # |
| 58 | + def begin_suite(suite) |
| 59 | + @body = [] |
| 60 | + @depth = 0 |
| 61 | + end |
| 62 | + |
| 63 | + # |
| 64 | + def begin_case(test_case) |
| 65 | + title, *rest = test_case.to_s.strip.split("\n") |
| 66 | + level = [@depth + 2, 6].min |
| 67 | + @body << %[<section class="case">] |
| 68 | + @body << %[<h#{level}>#{h title}</h#{level}>] |
| 69 | + @body << %[<p>#{rest.map{ |line| h(line.strip) }.join('<br>')}</p>] unless rest.empty? |
| 70 | + @depth += 1 |
| 71 | + end |
| 72 | + |
| 73 | + # |
| 74 | + def end_case(test_case) |
| 75 | + @depth -= 1 |
| 76 | + @body << %[</section>] |
| 77 | + end |
| 78 | + |
| 79 | + # Add a completed result to the document. |
| 80 | + def record(result) |
| 81 | + label = result.test.to_s.strip.split("\n").first.to_s |
| 82 | + label = "#{label} (case)" if result.kind == :case |
| 83 | + |
| 84 | + line = %[<span class="status">#{LABELS.fetch(result.status)}</span> #{h label}] |
| 85 | + line << %[ <span class="reason">#{h result.reason}</span>] if result.status == :skip && result.reason |
| 86 | + line << %[ <span class="reason">#{h result.exception.message}</span>] if result.status == :todo && result.exception |
| 87 | + line << %[ <span class="time">#{'%.5fs' % result.elapsed}</span>] |
| 88 | + |
| 89 | + @body << %[<div class="result #{result.status}">#{line}] |
| 90 | + if [:fail, :error].include?(result.status) |
| 91 | + result.exceptions.each{ |exception| @body << %[<pre>#{h detail(exception, result.status == :error)}</pre>] } |
| 92 | + end |
| 93 | + @body << %[<pre class="stdout">#{h result.stdout}</pre>] unless result.stdout.empty? |
| 94 | + @body << %[<pre class="stderr">#{h result.stderr}</pre>] unless result.stderr.empty? |
| 95 | + @body << %[</div>] |
| 96 | + end |
| 97 | + |
| 98 | + # Write the document. |
| 99 | + def finish(summary) |
| 100 | + outcome = summary.success? ? 'passed' : 'failed' |
| 101 | + tally = if summary.total.zero? |
| 102 | + 'No tests were run.' |
| 103 | + else |
| 104 | + counts = ::Test::Result::STATUSES.map{ |status| "#{summary.counts[status]} #{TITLES[status]}" } |
| 105 | + "#{summary.total} tests: #{counts.join(', ')}." |
| 106 | + end |
| 107 | + |
| 108 | + @output.puts '<!DOCTYPE html>' |
| 109 | + @output.puts '<html lang="en">' |
| 110 | + @output.puts '<head>' |
| 111 | + @output.puts '<meta charset="utf-8">' |
| 112 | + @output.puts '<meta name="viewport" content="width=device-width, initial-scale=1">' |
| 113 | + @output.puts '<title>Test Report</title>' |
| 114 | + @output.puts "<style>\n#{STYLE.gsub(/^ /, '')}</style>" |
| 115 | + @output.puts '</head>' |
| 116 | + @output.puts '<body>' |
| 117 | + @output.puts '<h1>Test Report</h1>' |
| 118 | + @output.puts %[<p class="tally #{outcome}">#{h tally}</p>] |
| 119 | + @output.puts @body.join("\n") unless @body.empty? |
| 120 | + @output.puts %[<footer>Finished in #{'%.5fs' % summary.elapsed}. Generated by Rubytest #{h ::Test::VERSION} on #{h Time.now.strftime('%Y-%m-%d %H:%M:%S')}.</footer>] |
| 121 | + @output.puts '</body>' |
| 122 | + @output.puts '</html>' |
| 123 | + end |
| 124 | + |
| 125 | + private |
| 126 | + |
| 127 | + # Escape text for use in HTML. |
| 128 | + def h(text) |
| 129 | + text.to_s.gsub(/[&<>"']/, ESCAPES) |
| 130 | + end |
| 131 | + |
| 132 | + # An exception's class, message and where it was raised, with the rest |
| 133 | + # of its backtrace if asked. |
| 134 | + def detail(exception, backtrace=false) |
| 135 | + lines = ["#{exception.class}: #{exception.message}"] |
| 136 | + trace = exception.backtrace ? clean_backtrace(exception) : [] |
| 137 | + trace = trace.first(1) unless backtrace |
| 138 | + lines.concat(trace.map{ |line| " #{line}" }) |
| 139 | + lines.join("\n") |
| 140 | + end |
| 141 | + |
| 142 | + end |
| 143 | + |
| 144 | +end |
0 commit comments