# frozen_string_literal: true
# rubocop:disable Metrics/LineLength
module RspecJunitFormatterBitbucket
# Write XML file
class XML
def initialize(output, example)
@test = example
@output = output
@output << first_node
end
def dump
@output << %(\n)
@output << %(\n)
@output << %(\n)
@output << %(\n)
dump_examples
@output << %(\n)
end
private
def first_node
%(\n)
end
def testsuite_attr
<<~TESTSUITE
name="rspec#{Format.escape(ENV['TEST_ENV_NUMBER'].to_s)}"
tests="#{@test.example_count}"
skipped="#{@test.pending_count}"
failures="#{@test.failure_count}"
errors="0"
time="#{Format.escape(format('%.6f', @test.duration))}"
timestamp="#{Format.escape(@test.started.iso8601)}"
hostname="#{Format.escape(Socket.gethostname)}"
TESTSUITE
end
def property_attr
<<~PROPERTY
name="seed"
value="#{Format.escape(RSpec.configuration.seed.to_s)}"
PROPERTY
end
def dump_examples
@test.examples.each do |example|
case @test.result_of(example)
when :pending
dump_pending(example)
when :failed
dump_failed(example)
else
dump_example(example)
end
end
end
def dump_pending(example)
dump_example(example) do
@output << %()
end
end
def dump_failed(example)
dump_example(example) do
@output << %()
@output << Format.escape(@test.failure_for(example))
@output << %()
end
end
def dump_example(example)
@output << %()
yield if block_given?
dump_output(example)
@output << %(\n)
end
def testcase_attr(example)
<<~TESTCASE
classname="#{Format.escape(Attribute.classname_for(example))}"
name="#{Format.escape(Attribute.description_for(example))}"
file="#{Format.escape(Attribute.example_group_file_path_for(example))}"
time="#{Format.escape(format('%.6f', Attribute.duration_for(example)))}"
TESTCASE
end
def failure_attr(example)
<<~FAILURE
message="#{Format.escape(@test.failure_message_for(example))}"
type="#{Format.escape(@test.failure_type_for(example))}"
FAILURE
end
def rm_char_nw_line(txt)
txt.gsub("\n", ' ').gsub("\r", '').chomp(' ')
end
def dump_output(example)
stdout = @test.stdout_for(example)
write_stdout(stdout) unless stdout.nil? # && stdout.empty?
stderr = @test.stderr_for(example)
write_stderr(stderr) unless stderr.nil? # && stderr.empty?
end
def write_stdout(stdout)
@output << %()
@output << Format.escape(stdout)
@output << %()
end
def write_stderr(stderr)
@output << %()
@output << Format.escape(stderr)
@output << %()
end
end
end
# rubocop:enable Metrics/LineLength