Skip to content

Commit df9089f

Browse files
authored
Merge pull request #81 from foxish/fix-inner-html
Use innerhtml instead of getting text inside failure and error tags
2 parents 43a335e + 1d02d9c commit df9089f

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

ruby/bin/annotate

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env ruby
22

33
require 'rexml/document'
4+
require 'rexml/element'
45

56
# Reads a list of junit files and returns a nice Buildkite build annotation on
67
# STDOUT that summarizes any failures.
@@ -21,6 +22,16 @@ end
2122
junit_report_files = Dir.glob(File.join(junits_dir, "**", "*"))
2223
failures = []
2324

25+
def text_content(element)
26+
# Handle mulptiple CDATA/text children elements
27+
text = element.texts().map(&:value).join.strip
28+
if text.empty?
29+
nil
30+
else
31+
text
32+
end
33+
end
34+
2435
junit_report_files.sort.each do |file|
2536
next if File.directory?(file)
2637

@@ -33,10 +44,10 @@ junit_report_files.sort.each do |file|
3344
name = testcase.attributes['name'].to_s
3445
failed_test = testcase.attributes[failure_format].to_s
3546
testcase.elements.each("failure") do |failure|
36-
failures << Failure.new(name, failed_test, failure.text, job, :failure)
47+
failures << Failure.new(name, failed_test, text_content(failure), job, :failure)
3748
end
3849
testcase.elements.each("error") do |error|
39-
failures << Failure.new(name, failed_test, error.text, job, :error)
50+
failures << Failure.new(name, failed_test, text_content(error), job, :error)
4051
end
4152
end
4253
end

ruby/tests/annotate_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,26 @@
401401

402402
assert_equal 0, status.exitstatus
403403
end
404+
405+
it "handles cdata formatted XML files" do
406+
output, status = Open3.capture2e("#{__dir__}/../bin/annotate", "#{__dir__}/failure-with-cdata/")
407+
408+
assert_equal <<~OUTPUT, output
409+
Parsing junit.xml
410+
--- ❓ Checking failures
411+
There is 1 failure/error 😭
412+
--- ✍️ Preparing annotation
413+
1 error:
414+
415+
<details>
416+
<summary><code>Account#maximum_jobs_added_by_pipeline_changer returns 250 by default in spec.models.account_spec</code></summary>
417+
418+
<code><pre>First line of failure output
419+
Second line of failure output</pre></code>
420+
421+
</details>
422+
OUTPUT
423+
424+
assert_equal 0, status.exitstatus
425+
end
404426
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<testsuite name="rspec" tests="2" skipped="0" failures="0" errors="1" time="49.290713" timestamp="2018-04-18T23:29:42+00:00" hostname="626d214475e4">
3+
<testcase classname="spec.models.account_spec" name="Account#maximum_jobs_added_by_pipeline_changer returns 500 if the account is ABC" file="./spec/models/account_spec.rb" time="0.020013"/>
4+
<testcase classname="spec.models.account_spec" name="Account#maximum_jobs_added_by_pipeline_changer returns 250 by default" file="./spec/models/account_spec.rb" time="0.967127">
5+
<error message=" expected: 250 got: 500 (compared using eql?) " type="RSpec::Expectations::ExpectationNotMetError">
6+
<![CDATA[First line of failure output]]>
7+
<![CDATA[Second line of failure output]]>
8+
</error>
9+
</testcase>
10+
</testsuite>

0 commit comments

Comments
 (0)