@@ -11,19 +11,6 @@ junits_dir = ARGV[0]
11
11
abort ( "Usage: annotate <junits-dir>" ) unless junits_dir
12
12
abort ( "#{ junits_dir } does not exist" ) unless Dir . exist? ( junits_dir )
13
13
14
- job_pattern = ENV [ 'BUILDKITE_PLUGIN_JUNIT_ANNOTATE_JOB_UUID_FILE_PATTERN' ]
15
- job_pattern = '-(.*).xml' if !job_pattern || job_pattern . empty?
16
-
17
- failure_format = ENV [ 'BUILDKITE_PLUGIN_JUNIT_ANNOTATE_FAILURE_FORMAT' ]
18
- failure_format = 'classname' if !failure_format || failure_format . empty?
19
-
20
- class Failure < Struct . new ( :name , :failed_test , :body , :job , :type , :message )
21
- end
22
-
23
- junit_report_files = Dir . glob ( File . join ( junits_dir , "**" , "*" ) )
24
- testcases = 0
25
- failures = [ ]
26
-
27
14
def text_content ( element )
28
15
# Handle mulptiple CDATA/text children elements
29
16
text = element . texts ( ) . map ( &:value ) . join . strip
@@ -44,58 +31,66 @@ def message_content(element)
44
31
end
45
32
end
46
33
34
+ class Failure < Struct . new ( :body , :message , :type , :file , :name , :suite_name )
35
+ def self . from ( element , testcase , testsuite )
36
+ new (
37
+ text_content ( element ) ,
38
+ message_content ( element ) ,
39
+ element . attributes [ 'type' ] ,
40
+ testcase . attributes [ 'file' ] ,
41
+ testcase . attributes [ 'name' ] ,
42
+ testsuite . attributes [ 'name' ]
43
+ )
44
+ end
45
+ end
46
+
47
+ junit_report_files = Dir . glob ( File . join ( junits_dir , "**" , "*" ) )
48
+ stats = { skipped : 0 , failures : 0 , tests : 0 , assertions : 0 , time : 0.0 }
49
+ failures = [ ]
50
+
47
51
junit_report_files . sort . each do |file |
48
52
next if File . directory? ( file )
49
53
50
54
STDERR . puts "Parsing #{ file . sub ( junits_dir , '' ) } "
51
- job = File . basename ( file ) [ /#{ job_pattern } / , 1 ]
52
55
xml = File . read ( file )
53
56
doc = REXML ::Document . new ( xml )
54
57
55
- REXML ::XPath . each ( doc , '//testsuite//testcase' ) do |testcase |
56
- testcases += 1
57
- name = testcase . attributes [ 'name' ] . to_s
58
- failed_test = testcase . attributes [ failure_format ] . to_s
59
- testcase . elements . each ( "failure" ) do |failure |
60
- failures << Failure . new ( name , failed_test , text_content ( failure ) , job , :failure , message_content ( failure ) )
61
- end
62
- testcase . elements . each ( "error" ) do |error |
63
- failures << Failure . new ( name , failed_test , text_content ( error ) , job , :error , message_content ( error ) )
58
+ REXML ::XPath . each ( doc , '//testsuite' ) do |testsuite |
59
+ stats [ :assertions ] += testsuite . attributes [ 'assertions' ] . to_i
60
+ stats [ :failures ] += testsuite . attributes [ 'failures' ] . to_i
61
+ stats [ :skipped ] += testsuite . attributes [ 'skipped' ] . to_i
62
+ stats [ :tests ] += testsuite . attributes [ 'tests' ] . to_i
63
+ stats [ :time ] += testsuite . attributes [ 'time' ] . to_f
64
+
65
+ testsuite . elements . each ( "testcase" ) do |testcase |
66
+ testcase . elements . each ( "failure" ) do |element |
67
+ failures << Failure . from ( element , testcase , testsuite )
68
+ end
69
+ testcase . elements . each ( "error" ) do |element |
70
+ failures << Failure . from ( element , testcase , testsuite )
71
+ end
64
72
end
65
73
end
66
74
end
67
75
68
76
STDERR . puts "--- ❓ Checking failures"
69
- STDERR . puts "#{ testcases } testcases found "
77
+ STDERR . puts "Ran ** #{ stats [ :tests ] } ** tests in ** #{ stats [ :time ] . round ( 2 ) } s**. "
70
78
71
79
if failures . empty?
72
80
STDERR . puts "There were no failures/errors 🙌"
73
81
exit 0
74
82
else
75
- STDERR . puts "There #{ failures . length == 1 ? "is 1 failure/error" : "are #{ failures . length } failures/errors" } 😭"
83
+ STDERR . puts "There #{ failures . length == 1 ? "is **1*8 failure/error" : "are ** #{ failures . length } ** failures/errors" } 😭"
76
84
end
77
85
78
86
STDERR . puts "--- ✍️ Preparing annotation"
79
87
80
- failures_count = failures . select { |f | f . type == :failure } . length
81
- errors_count = failures . select { |f | f . type == :error } . length
82
-
83
- puts [
84
- failures_count == 0 ? nil : ( failures_count == 1 ? "1 failure" : "#{ failures_count } failures" ) ,
85
- errors_count === 0 ? nil : ( errors_count == 1 ? "1 error" : "#{ errors_count } errors" ) ,
86
- ] . compact . join ( " and " ) + ":\n \n "
87
-
88
+ puts "Ran **#{ stats [ :tests ] } ** tests in **#{ stats [ :time ] . round ( 2 ) } s**."
88
89
failures . each do |failure |
89
90
puts "<details>"
90
- puts "<summary><code>#{ failure . name } in #{ failure . failed_test } </code></summary>\n \n "
91
- if failure . message
92
- puts "<p>#{ failure . message . chomp . strip } </p>\n \n "
93
- end
91
+ puts "<summary><code>#{ failure . suite_name } ##{ failure . name } </code></summary>\n \n "
94
92
if failure . body
95
- puts "<pre><code>#{ CGI . escapeHTML ( failure . body . chomp . strip ) } </code></pre>\n \n "
96
- end
97
- if failure . job
98
- puts "in <a href=\" ##{ failure . job } \" >Job ##{ failure . job } </a>"
93
+ puts "<pre class=\" term\" ><code>#{ CGI . escapeHTML ( failure . message . chomp . strip ) } \n \n #{ CGI . escapeHTML ( failure . body . chomp . strip ) } </code></pre>\n \n "
99
94
end
100
95
puts "</details>"
101
96
puts "" unless failure == failures . last
0 commit comments