Class: Fastlane::Actions::CollateJsonReportsAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



160
161
162
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 160

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

.available_optionsObject



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
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 118

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :reports,
      env_name: 'COLLATE_JSON_REPORTS_REPORTS',
      description: 'An array of JSON 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 JSON report files found') if reports.empty?
        reports.each do |report|
          UI.user_error!("Error: JSON report not found: '#{report}'") unless File.exist?(report)
        end
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :collated_report,
      env_name: 'COLLATE_JSON_REPORTS_COLLATED_REPORT',
      description: 'The final JSON report file where all testcases will be merged into',
      optional: true,
      default_value: 'result.json',
      type: String
    )
  ]
end

.categoryObject



168
169
170
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 168

def self.category
  :testing
end

.descriptionObject

:nocov:



102
103
104
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 102

def self.description
  "🔹 Combines multiple json report files into one json report file"
end

.detailsObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 106

def self.details
  "The first JSON report is used as the base report. Due to the nature of " \
  "xcpretty JSON reports, only the failing test cases are recorded. " \
  "Testcases that failed in previous reports that no longer appear in " \
  "later reports are assumed to have passed in a re-run, thus not appearing " \
  "in the collated report. " \
  "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 JSON reports will have fewer failing tests." \
  ""
end

.example_codeObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 144

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

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 164

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

.merge_reports(base_report, other_report) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 23

def self.merge_reports(base_report, other_report)
  base_report.each_key do |key|
    unless %w(previous_tests_failures tests_failures tests_summary_messages).include?(key)
      base_report[key].concat(other_report[key])
    end
  end
  base_report["tests_failures"] = other_report["tests_failures"]
  update_failed_tests_count(base_report, other_report)
  update_time(base_report, other_report)
  update_unexpected_failures(base_report, other_report)
end

.run(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 6

def self.run(params)
  report_filepaths = params[:reports]
  if report_filepaths.size == 1
    FileUtils.cp(report_filepaths[0], params[:collated_report])
  else
    base_report_json = JSON.parse(File.read(report_filepaths.shift))
    report_filepaths.each do |report_file|
      report_json = JSON.parse(File.read(report_file))
      update_previous_test_failures(base_report_json)
      merge_reports(base_report_json, report_json)
    end
    File.open(params[:collated_report], 'w') do |f|
      f.write(JSON.pretty_generate(base_report_json))
    end
  end
end

.times_from_summary(summary) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 62

def self.times_from_summary(summary)
  time_regex = '(?:\d|\.)+'
  match = /in (?<test_time>#{time_regex}) \((?<total_time>#{time_regex})\) seconds/.match(summary)
  return [
    match['test_time'].to_f,
    match['total_time'].to_f
  ]
end

.unexpected_failures_from_summary(summary) ⇒ Object



57
58
59
60
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 57

def self.unexpected_failures_from_summary(summary)
  /\((?<unexpected_failures>\d+) unexpected\)/ =~ summary
  unexpected_failures.to_i
end

.update_failed_tests_count(base_report, other_report) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 71

def self.update_failed_tests_count(base_report, other_report)
  /\s+Executed \d+ tests?, with (?<failed_test_count>\d+) failures?/ =~ other_report['tests_summary_messages'][0]

  base_report['tests_summary_messages'][0].sub!(
    /(\d+) failure/,
    "#{failed_test_count} failure"
  )
end

.update_previous_test_failures(base_report) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 80

def self.update_previous_test_failures(base_report)
  previous_tests_failures = base_report['previous_tests_failures']
  if previous_tests_failures
    tests_failures = base_report['tests_failures']
    tests_failures.each do |failure_suite, failures|
      if previous_tests_failures.key?(failure_suite)
        previous_tests_failures[failure_suite].concat(failures)
      else
        previous_tests_failures[failure_suite] = failures
      end
    end
  else
    base_report['previous_tests_failures'] = base_report['tests_failures'] || {}
  end
  base_report.delete('tests_failures')
end

.update_time(base_report, other_report) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 35

def self.update_time(base_report, other_report)
  base_test_time, base_total_time = times_from_summary(base_report['tests_summary_messages'][0])
  other_test_time, other_total_time = times_from_summary(other_report['tests_summary_messages'][0])
  time_regex = '(?:\d|\.)+'

  test_time_sum = (base_test_time + other_test_time).round(3)
  total_time_sum = (base_total_time + other_total_time).round(3)
  base_report['tests_summary_messages'][0].sub!(
    /in #{time_regex} \(#{time_regex}\) seconds/,
    "in #{test_time_sum} (#{total_time_sum}) seconds"
  )
end

.update_unexpected_failures(base_report, other_report) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/fastlane/plugin/test_center/actions/collate_json_reports.rb', line 48

def self.update_unexpected_failures(base_report, other_report)
  base_unexpected_failures = unexpected_failures_from_summary(base_report['tests_summary_messages'][0])
  other_unexpected_failures = unexpected_failures_from_summary(other_report['tests_summary_messages'][0])
  base_report['tests_summary_messages'][0].sub!(
    /\(\d+ unexpected\)/,
    "(#{base_unexpected_failures + other_unexpected_failures} unexpected)"
  )
end