80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/highway/steps/library/xcode_test.rb', line 80
def self.run(parameters:, context:, report:)
clean = parameters["clean"]
configuration = parameters["configuration"]
device = parameters["device"]
scheme = parameters["scheme"]
skip_build = parameters["skip_build"]
code_coverage = parameters["code_coverage"]
fastlane_params = parameters["fastlane_params"]
flags = parameters["flags"].join(" ")
settings = parameters["settings"].map { |setting, value| "#{setting}=\"#{value.shellescape}\"" }.join(" ")
xcargs = flags + settings
xcargs = nil if xcargs.empty?
project_key = parameters["project"][:tag]
project_value = parameters["project"][:value]
output_raw_temp_dir = Dir.mktmpdir()
output_raw_path = report.prepare_artifact("raw.log")
output_html_path = report.prepare_artifact("report.html")
output_junit_path = report.prepare_artifact("report.junit")
output_html_file = File.basename(output_html_path)
output_junit_file = File.basename(output_junit_path)
output_dir = context.artifacts_dir
output_types = ["html", "junit"].join(",")
output_files = [output_html_file, output_junit_file].join(",")
xcodebuild_formatter = "xcpretty"
report_test = {}
report_artifacts = {}
rescued_error = nil
options = {
project_key => project_value,
clean: clean,
configuration: configuration,
device: device,
scheme: scheme,
code_coverage: code_coverage,
skip_build: skip_build,
xcargs: xcargs,
buildlog_path: output_raw_temp_dir,
output_directory: output_dir,
output_types: output_types,
output_files: output_files,
xcodebuild_formatter: xcodebuild_formatter,
}
begin
fastlane_params = fastlane_params.nil? ? {} : fastlane_params
combined_options = fastlane_params.merge(options)
context.run_action("run_tests", options: combined_options)
rescue FastlaneCore::Interface::FastlaneBuildFailure => error
report_test[:result] = :error
rescued_error = error
rescue FastlaneCore::Interface::FastlaneTestFailure => error
report_test[:result] = :failure
rescued_error = error
else
report_test[:result] = :success
end
output_raw_temp_path = Dir.glob(File.join(output_raw_temp_dir, "*.log")).first
unless output_raw_temp_path.nil?
FileUtils.mv(output_raw_temp_path, output_raw_path)
end
report_artifacts[:raw] = output_raw_path
report_artifacts[:html] = output_html_path
report_artifacts[:junit] = output_junit_path
error_callback = ->(error) {
rescued_error = error
}
xcpretty_json_formatter_path = context.run_sh("xcpretty-json-formatter", bundle_exec: true, silent: true, on_error: error_callback)
temp_json_report_path = File.join(Dir.mktmpdir(), "report.json")
context.with_modified_env({"XCPRETTY_JSON_FILE_OUTPUT" => temp_json_report_path}) do
context.run_sh(["cat", output_raw_path, "| xcpretty --formatter", xcpretty_json_formatter_path], bundle_exec: true, silent: true, on_error: error_callback)
end
context.env["XCODE_TEST_JSON_REPORT_PATH"] = temp_json_report_path
junit_report = Scan::TestResultParser.new.parse_result(File.read(output_junit_path))
if File.exist?(temp_json_report_path)
xcode_report = JSON.parse(File.read(temp_json_report_path))
else
xcode_report = Hash.new()
end
report_test_count = {}
report_test_count[:all] = junit_report[:tests]
report_test_count[:failed] = junit_report[:failures]
report_test_count[:succeeded] = report_test_count[:all] - report_test_count[:failed]
report_test[:count] = report_test_count
report_test_errors = []
report_test_errors += xcode_report.fetch("file_missing_errors", []).map { |entry|
{location: File.basename(entry["file_path"]), reason: entry["reason"]}
}
report_test_errors += xcode_report.fetch("compile_errors", []).map { |entry|
{location: File.basename(entry["file_path"]), reason: entry["reason"]}
}
report_test_errors += xcode_report.fetch("undefined_symbols_errors", []).map { |entry|
{location: entry["symbol"], reason: entry["message"]}
}
report_test_errors += xcode_report.fetch("format_duplicate_symbols", []).map {
{location: nil, reason: entry["message"]}
}
report_test_errors += xcode_report.fetch("errors", []).map { |entry|
{location: nil, reason: entry}
}
report_test[:errors] = report_test_errors
report_test_failures = xcode_report.fetch("tests_failures", {}).values.flatten.map { |entry|
{location: entry["test_case"], reason: entry["reason"]}
}
report_test[:failures] = report_test_failures
report[:test] = report_test
report[:artifacts] = report_artifacts
raise rescued_error if rescued_error != nil
end
|