Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spec::JUnitFormatter enhancements #8599

Merged
merged 18 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions spec/std/spec/junit_formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe "JUnit Formatter" do

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="2" errors="0" failures="0">
<testsuite tests="2" disabled="0" errors="0" failures="0" time="0.0">
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something"/>
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something else"/>
</testsuite>
Expand All @@ -26,7 +26,7 @@ describe "JUnit Formatter" do

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="1" errors="0" failures="1">
<testsuite tests="1" disabled="0" errors="0" failures="1" time="0.0">
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something">
<failure/>
</testcase>
Expand All @@ -43,7 +43,7 @@ describe "JUnit Formatter" do

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="1" errors="1" failures="0">
<testsuite tests="1" disabled="0" errors="1" failures="0" time="0.0">
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something">
<error/>
</testcase>
Expand All @@ -63,9 +63,9 @@ describe "JUnit Formatter" do

expected = <<-XML
<?xml version="1.0"?>
<testsuite tests="4" errors="2" failures="1">
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something1"/>
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something2">
<testsuite tests="4" disabled="0" errors="2" failures="1" time="0.0">
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something1" time="2.0"/>
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something2" time="0.5">
<failure/>
</testcase>
<testcase file="spec/some_spec.cr" classname="spec.some_spec" name="should do something3">
Expand Down Expand Up @@ -124,7 +124,7 @@ private def build_report
output = String::Builder.new
formatter = Spec::JUnitFormatter.new(output)
yield formatter
formatter.finish
formatter.finish(Time::Span.zero, false)
output.to_s
end

Expand Down
2 changes: 1 addition & 1 deletion spec/std/spec/tap_formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ private def build_report
String.build do |io|
formatter = Spec::TAPFormatter.new(io)
yield formatter
formatter.finish
formatter.finish(Time::Span.zero, false)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/spec/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module Spec
end

def finish(elapsed_time, aborted = false)
Spec.formatters.each(&.finish)
Spec.formatters.each(&.finish(elapsed_time, aborted))
Spec.formatters.each(&.print_results(elapsed_time, aborted))
end

Expand Down
4 changes: 2 additions & 2 deletions src/spec/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Spec
def report(result)
end

def finish
def finish(elapsed_time, aborted)
end

def print_results(elapsed_time : Time::Span, aborted : Bool)
Expand All @@ -30,7 +30,7 @@ module Spec
@io.flush
end

def finish
def finish(elapsed_time, aborted)
@io.puts
end

Expand Down
17 changes: 15 additions & 2 deletions src/spec/junit_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ module Spec
@results << result
end

def finish
def finish(elapsed_time, aborted)
io = @io
io.puts %(<?xml version="1.0"?>)
io << %(<testsuite tests=") << @results.size
io << %(" disabled=") << (@summary[:pending]? || 0)
io << %(" errors=") << (@summary[:error]? || 0)
io << %(" failures=") << (@summary[:fail]? || 0) << %(">)
io << %(" failures=") << (@summary[:fail]? || 0)
io << %(" time=") << elapsed_time.to_f
io << %(">)

io.puts

Expand All @@ -43,6 +46,11 @@ module Spec
io << %(" name=")
HTML.escape(result.description, io)

if elapsed = result.elapsed
io << %(" time=")
io << elapsed.to_f
end

if tag = inner_content_tag(result.kind)
io.puts %(">)

Expand Down Expand Up @@ -72,6 +80,11 @@ module Spec
HTML.escape(message, io)
io << '"'
end
if tag == :error
io << %( type=")
io << exception.class.name
io << '"'
end
io << '>'

if backtrace = exception.backtrace?
Expand Down
2 changes: 1 addition & 1 deletion src/spec/tap_formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Spec::TAPFormatter < Spec::Formatter
@io.puts
end

def finish
def finish(elapsed_time, aborted)
@io << "1.." << @counter
@io.puts
end
Expand Down