Skip to content

Commit bc3cdee

Browse files
transclaude
andcommitted
Bring the HTML reporter into Rubytest
rubytest-html's reporter predates the recorder and no longer loads. Rewrite it on record and finish: one self-contained document, written when the run finishes, with results nested under their cases. Escape names, messages and output, report skipped tests and cases, and capture test output so it appears with its test. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 5a90fb5 commit bc3cdee

5 files changed

Lines changed: 176 additions & 3 deletions

File tree

‎HISTORY.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* Bring the HTML reporter in from rubytest-html, rewritten for the recorder:
6+
`rubytest -f html`. It escapes what it writes, reports skipped tests and
7+
cases, and shows captured output with each test.
58
* Bring the Rake task in from rubytest-rake: `require 'rubytest/rake'` for
69
`Test::Rake::TestTask`. The task now fails when the run does (it always
710
exited 0), runs only the default globs that match a file, and lets `TEST`

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ test), `outline` (nested cases), `progress` (running count and timing), `tap`
5858
(TAP version 13), and `tapy` and `tapj` (TAP-Y and TAP-J, the structured
5959
streams that [tapout](https://github.com/rubyworks/tapout) reads). Select one
6060
with `rubytest -f FORMAT`; `-y` and `-j` are short for `-f tapy` and `-f tapj`.
61-
Formats load only when selected. The TAP formats capture test output: TAP
62-
emits it as diagnostics, and TAP-Y and TAP-J record it with each test.
61+
`html` writes the whole run as one self-contained HTML document
62+
(`rubytest -f html > report.html`). Formats load only when selected. The TAP
63+
and HTML formats capture test output: TAP emits it as diagnostics, and TAP-Y,
64+
TAP-J and HTML record it with each test.
6365

6466

6567
## Installation

‎demo/05_builtin_reporters.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,27 @@ The command line has a shortcut for each.
116116
cli.options.parse!([flag])
117117
cli.format.assert == format
118118
end
119+
120+
The HTML format writes one self-contained document once the run has
121+
finished. Names, messages and captured output are escaped.
122+
123+
markup = probe_type.new('markup <b>&</b>') { print '<script>'; true }
124+
html_suite = [Class.new(Array) { def to_s; 'a <case>'; end }.new([markup, failing]),
125+
pending, skipped, skipped_case, broken_case]
126+
success, output, report = render.call('html', html_suite)
127+
success.assert == false
128+
129+
output.lines.first.chomp.assert == '<!DOCTYPE html>'
130+
output.lines.last.chomp.assert == '</html>'
131+
output.scan('<div class="result ').size.assert == report.total
132+
output.scan('<section class="case">').size.assert == output.scan('</section>').size
133+
%w[pass fail error todo skip].each do |status|
134+
output.include?(%[<div class="result #{status}">]).assert == true
135+
end
136+
137+
output.include?('markup &lt;b&gt;&amp;&lt;/b&gt;').assert == true
138+
output.include?('<h2>a &lt;case&gt;</h2>').assert == true
139+
output.include?('<pre class="stdout">&lt;script&gt;</pre>').assert == true
140+
output.include?('<script>').assert == false
141+
output.include?('Assertion: ').assert == true
142+
output.include?('6 tests: 1 passing, 1 failures, 1 errors, 1 pending, 2 skipped.').assert == true

‎lib/rubytest/format/html.rb‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
'&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;', "'" => '&#39;'
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

‎lib/rubytest/runner.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def reporter_load(format)
433433
# TODO: Could use finder gem to look these up, but that's yet another dependency.
434434
#
435435
KNOWN_FORMATS = %w{
436-
dotprogress progress outline summary tap tapy tapj test
436+
dotprogress progress outline summary tap tapy tapj html test
437437
}
438438

439439
# Returns a list of available report types.

0 commit comments

Comments
 (0)