Class: Fastlane::Actions::CollateJunitReportsAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.attribute_sum_string(node1, node2, attribute) ⇒ Object



141
142
143
144
145
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 141

def self.attribute_sum_string(node1, node2, attribute)
  value1 = node1.attributes[attribute].to_i
  value2 = node2.attributes[attribute].to_i
  (value1 + value2).to_s
end

.authorsObject



209
210
211
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 209

def self.authors
  ["lyndsey-ferguson/@lyndseydf"]
end

.available_optionsObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 167

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :reports,
      env_name: 'COLLATE_JUNIT_REPORTS_REPORTS',
      description: 'An array of junit reports to collate. The first report is used as the base into which other reports are merged in',
      optional: false,
      type: Array,
      verify_block: proc do |reports|
        UI.user_error!('No junit report files found') if reports.empty?
        reports.each do |report|
          UI.user_error!("Error: junit report not found: '#{report}'") unless File.exist?(report)
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :collated_report,
      env_name: 'COLLATE_JUNIT_REPORTS_COLLATED_REPORT',
      description: 'The final junit report file where all testcases will be merged into',
      optional: true,
      default_value: 'result.xml',
      type: String
    )
  ]
end

.categoryObject



213
214
215
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 213

def self.category
  :testing
end

.collapse_testcase_multiple_failures_in_testsuite(testsuite) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 47

def self.collapse_testcase_multiple_failures_in_testsuite(testsuite)
  testcases_with_failures = REXML::XPath.match(testsuite, 'testcase[failure]')

  while testcases_with_failures.size > 1
    target_testcase = testcases_with_failures.shift

    name = target_testcase.attributes['name']
    classname = target_testcase.attributes['classname']

    failures = REXML::XPath.match(testsuite, "testcase[@name='#{name}'][@classname='#{classname}']/failure")
    next unless failures.size > 1

    failures[1..-1].each do |failure|
      failure_clone = failure.clone
      failure_clone.text = failure.text
      target_testcase << failure_clone

      testsuite.delete_element(failure.parent)
      testcases_with_failures.delete(failure.parent)
    end
  end
end

.collate_testsuite(target_testsuite, other_testsuite) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 91

def self.collate_testsuite(target_testsuite, other_testsuite)
  other_testsuite.elements.each('testcase') do |testcase|
    classname = testcase.attributes['classname']
    name = testcase.attributes['name']
    target_testcase = REXML::XPath.first(target_testsuite, "testcase[@name='#{name}' and @classname='#{classname}']")
    # Replace target_testcase with testcase
    if target_testcase
      UI.verbose("      collate_testsuite with testcase #{name}")
      UI.verbose("      replacing \"#{target_testcase}\" with \"#{testcase}\"")
      parent = target_testcase.parent
      increment_testcase_tries(target_testcase, testcase) unless testcase.root == target_testcase.root 
      parent.insert_after(target_testcase, testcase)
      parent.delete_element(target_testcase)
      UI.verbose("")
      UI.verbose("      target_testcase after replacement \"#{parent}\"")
    else
      target_testsuite << testcase
    end
  end
end

.descriptionObject



151
152
153
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 151

def self.description
  "Combines and combines tests from multiple junit report files"
end

.detailsObject



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 155

def self.details
  "The first junit report is used as the base report. Testcases " \
  "from other reports are added if they do not already exist, or " \
  "if the testcases already exist, they are replaced." \
  "" \
  "This is done because it is assumed that fragile tests, when " \
  "re-run will often succeed due to less interference from other " \
  "tests and the subsequent junit reports will have more passed tests." \
  "" \
  "Inspired by Derek Yang's fastlane-plugin-merge_junit_report"
end

.example_codeObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 193

def self.example_code
  [
    "
    UI.important(
      'example: ' \\
      'collate the xml reports to a temporary file \"result.xml\"'
    )
    reports = Dir['../spec/fixtures/*.xml'].map { |relpath| File.absolute_path(relpath) }
    collate_junit_reports(
      reports: reports,
      collated_report: File.join(Dir.mktmpdir, 'result.xml')
    )
    "
  ]
end

.flatten_duplicate_testsuites(report, testsuite) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 70

def self.flatten_duplicate_testsuites(report, testsuite)
  testsuite_name = testsuite.attributes['name']
  duplicate_testsuites = REXML::XPath.match(report, "//testsuite[@name='#{testsuite_name}']")
  if duplicate_testsuites.size > 1
    UI.verbose("    > flattening_duplicate_testsuites")
    duplicate_testsuites.drop(1).each do |duplicate_testsuite|
      collate_testsuite(testsuite, duplicate_testsuite)
      duplicate_testsuite.parent.delete_element(duplicate_testsuite)
    end
    UI.verbose("    < flattening_duplicate_testsuites")
  end
  update_testsuite_counts(testsuite)
end

.increment_testcase_tries(target_testcase, testcase) ⇒ Object



112
113
114
115
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 112

def self.increment_testcase_tries(target_testcase, testcase)
  try_count = target_testcase.attributes['retries']
  testcase.attributes['retries'] = (try_count.to_i + 1).to_s
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 217

def self.is_supported?(platform)
  %i[ios mac].include?(platform)
end

.preprocess_testsuites(report) ⇒ Object



84
85
86
87
88
89
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 84

def self.preprocess_testsuites(report)
  report.elements.each('//testsuite') do |testsuite|
    flatten_duplicate_testsuites(report, testsuite)
    collapse_testcase_multiple_failures_in_testsuite(testsuite)
  end
end

.run(params) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 5

def self.run(params)
  report_filepaths = params[:reports]
  if report_filepaths.size == 1
    FileUtils.cp(report_filepaths[0], params[:collated_report])
  else
    UI.verbose("collate_junit_reports with #{report_filepaths}")
    reports = report_filepaths.map { |report_filepath| REXML::Document.new(File.new(report_filepath)) }
    # copy any missing testsuites
    target_report = reports.shift
    target_report.root.attributes['retries'] = reports.size.to_s
    preprocess_testsuites(target_report)

    reports.each do |report|
      preprocess_testsuites(report)
      UI.verbose("> collating last report file #{report_filepaths.last}")
      report.elements.each('//testsuite') do |testsuite|
        testsuite_name = testsuite.attributes['name']
        target_testsuite = REXML::XPath.first(target_report, "//testsuite[@name='#{testsuite_name}']")
        if target_testsuite
          UI.verbose("  > collating testsuite #{testsuite_name}")
          collate_testsuite(target_testsuite, testsuite)
          UI.verbose("  < collating testsuite #{testsuite_name}")
        else
          testable = REXML::XPath.first(target_report, "//testsuites")
          testable << testsuite
        end
      end
      UI.verbose("< collating last report file #{report_filepaths.last}")
    end
    target_report.elements.each('//testsuite') do |testsuite|
      update_testsuite_counts(testsuite)
    end
    testable = REXML::XPath.first(target_report, 'testsuites')
    update_testable_counts(testable)

    FileUtils.mkdir_p(File.dirname(params[:collated_report]))
    File.open(params[:collated_report], 'w') do |f|
      target_report.write(f, 2)
    end
  end
end

.update_testable_counts(testable) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 117

def self.update_testable_counts(testable)
  testsuites = REXML::XPath.match(testable, 'testsuite')
  test_count = 0
  failure_count = 0
  testsuites.each do |testsuite|
    test_count += testsuite.attributes['tests'].to_i
    failure_count += testsuite.attributes['failures'].to_i
  end
  testable.attributes['tests'] = test_count.to_s
  testable.attributes['failures'] = failure_count.to_s
end

.update_testsuite_counts(testsuite) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fastlane/plugin/test_center/actions/collate_junit_reports.rb', line 129

def self.update_testsuite_counts(testsuite)
  testcases = REXML::XPath.match(testsuite, 'testcase')
  testsuite.attributes['tests'] = testcases.size.to_s
  failure_count = testcases.reduce(0) do |count, testcase|
    if REXML::XPath.first(testcase, 'failure')
      count += 1
    end
    count
  end
  testsuite.attributes['failures'] = failure_count.to_s
end