Class: Fastlane::Actions::CollateJunitReportsAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



173
174
175
176
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 173

def self.authors
  #Adapted from the "fastlane-plugin-test_center" plugin by:
  ["lyndsey-ferguson/@lyndseydf"]
end

.available_optionsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 113

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :scheme,
      env_name: 'SCHEME',
      description: 'The test scheme being run',
      optional: false,
      default_value: '',
      type: String
    ),
    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
    ),
    FastlaneCore::ConfigItem.new(
      key: :assets,
      env_name: 'COLLATE_JUNIT_REPORTS_ASSETS',
      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 |assets|
        UI.user_error!('No junit report files found') if assets.empty?
        assets.each do |asset|
          UI.user_error!("Error: junit report not found: '#{asset}'") unless File.exist?(asset)
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :logs,
      env_name: 'COLLATE_JUNIT_REPORTS_LOGS',
      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 |logs|
        UI.user_error!('No junit report files found') if logs.empty?
        logs.each do |log|
          UI.user_error!("Error: junit report not found: '#{log}'") unless File.exist?(log)
        end
      end
    )
  ]
end

.clean_report(report) ⇒ Object



50
51
52
53
54
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 50

def self.clean_report(report)
  report = report.gsub(/<(\w+): 0x(\w+)>/, '(\1: 0x\2)')
  report = report.gsub(/<XCAccessibilityElement:\/>0x(\w+)/, '(XCAccessibilityElement): 0x\1')
  report
end

.copy_log(log_files, logs_folder) ⇒ Object



64
65
66
67
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 64

def self.copy_log(log_files, logs_folder)
  target_log = log_files.shift
  FileUtils.cp_r(target_log, logs_folder)
end

.descriptionObject



97
98
99
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 97

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

.detailsObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 101

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

.get_test_name(test_data) ⇒ Object



87
88
89
90
91
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 87

def self.get_test_name(test_data)
  test_name = test_data.xpath("(//key[contains(.,'TestSummaryGUID')])/../key[contains(.,'TestName')]/following-sibling::string").to_a[0].to_s
  test_name = test_name[8..-10]
  test_name
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 178

def self.is_supported?(platform)
  platform == :ios
end

.merge_assets(asset_files, assets_folder) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 56

def self.merge_assets(asset_files, assets_folder)
  #copy screenshots from all retries into the results folder
  Dir.mkdir(assets_folder) unless File.exists?(assets_folder)
  asset_files.each do |folder|
    FileUtils.cp_r(Dir[folder + '/*'], assets_folder)
  end
end

.merge_logs(log_files, logs_folder) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 69

def self.merge_logs(log_files, logs_folder)
  UI.verbose("Merging console logs...")
  all = ""
  target_log = log_files.shift
  log_files.each do |log|
    all = all + log + " "
  end
  sh("cat #{all}> #{target_log}")
  FileUtils.cp_r(target_log, logs_folder)
end

.mergeLists(target_report, retry_report, params) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 29

def self.mergeLists(target_report, retry_report, params)
  #retry_report = clean_report(retry_report)
  Dir.mkdir(params[:collated_report]) unless File.exists?(params[:collated_report])
  file_name = params[:collated_report] + "/action_TestSummaries.plist"
  retried_tests = retry_report.xpath("//key[contains(.,'TestSummaryGUID')]/..")

  current_node = retried_tests.shift

  #Copy retried test results into the base report
  while (current_node != nil)
    testName = get_test_name(current_node)
    matching_node = target_report.at_xpath("//string[contains(.,'#{testName}')]/..")

    if (!matching_node.nil?)
      matching_node.previous.next.replace(current_node)
      write_report_to_file(target_report, file_name)
    end
    current_node = retried_tests.shift
  end
end

.run(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 10

def self.run(params)
  report_filepaths = params[:reports].reverse
  if report_filepaths.size == 1
    FileUtils.cp(report_filepaths[0], params[:collated_report])
  else
    #Convert .plist reports to Nokogiri XML objects
    target_report = File.open(report_filepaths.shift) {|f| Nokogiri::XML(f)}
    file_name = params[:collated_report] + "/#{params[:scheme]}.test_result/1_Test/action_TestSummaries.plist"
    reports = report_filepaths.map { |report_filepath| Nokogiri::XML(Nokogiri::PList(open(report_filepath)).to_plist_xml) }

    #Merge .plist reports
    reports.each do |retry_report|
      mergeLists(target_report, retry_report, params)
    end
  end
  merge_assets(params[:assets], params[:collated_report] + "/Attachments")
  copy_log(params[:logs], params[:collated_report] + "/")
end

.write_report_to_file(report, file_name) ⇒ Object



80
81
82
83
84
85
# File 'lib/fastlane/plugin/retry_tests/actions/collate_junit_reports.rb', line 80

def self.write_report_to_file(report, file_name)
  File.new(file_name, 'w')
  File.open(file_name, 'w') do |f|
    f.write(report.to_xml)
  end
end