Class: Highway::Steps::Library::XcodeTestStep

Inherits:
Step
  • Object
show all
Defined in:
lib/highway/steps/library/xcode_test.rb

Class Method Summary collapse

Methods inherited from Step

root_parameter

Class Method Details

.nameObject



14
15
16
# File 'lib/highway/steps/library/xcode_test.rb', line 14

def self.name
  "xcode_test"
end

.parametersObject



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/highway/steps/library/xcode_test.rb', line 18

def self.parameters
  [
    Parameters::Single.new(
      name: "clean",
      type: Types::Bool.new(),
      required: false,
      default: true,
    ),
    Parameters::Single.new(
      name: "configuration",
      type: Types::String.new(),
      required: false,
    ),
    Parameters::Single.new(
      name: "device",
      type: Types::String.new(),
      required: false,
    ),
    Parameters::Single.new(
      name: "flags",
      type: Types::Array.new(Types::String.new()),
      required: false,
      default: [],
    ),
    Parameters::Single.new(
      name: "project",
      type: Types::AnyOf.new(
        project: Types::String.regex(/.+\.xcodeproj/),
        workspace: Types::String.regex(/.+\.xcworkspace/),
      ),
      required: true,
    ),
    Parameters::Single.new(
      name: "scheme",
      type: Types::String.new(),
      required: true,
    ),
    Parameters::Single.new(
      name: "skip_build",
      type: Types::Bool.new(),
      required: false,
      default: false,
    ),
    Parameters::Single.new(
      name: "settings",
      type: Types::Hash.new(Types::String.new(), validate: lambda { |dict| dict.keys.all? { |key| /[A-Z_][A-Z0-9_]*/ =~ key } }),
      required: false,
      default: {},
    ),
    Parameters::Single.new(
      name: "code_coverage",
      type: Types::Bool.new(),
      required: false,
    ),
    Parameters::Single.new(
      name: "fastlane_params",
      type: Types::Hash.new(Types::Any.new()),
      required: false
    ),
  ]
end

.run(parameters:, context:, report:) ⇒ Object



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:)

  # Interpret the parameters. At this point they are parsed and
  # transformed to be recognizable by Fastlane.

  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]

  # Prepare artifacts. Create temporary directories, get file names that
  # will be later passed to the build command.

  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)

  # Configure xcpretty. Set custom locations of report artifacts so that
  # we can track them accurately.

  output_dir = context.artifacts_dir
  output_types = ["html", "junit"].join(",")
  output_files = [output_html_file, output_junit_file].join(",")
  xcodebuild_formatter = "xcpretty"

  # Prepare temporary variables.

  report_test = {}
  report_artifacts = {}
  rescued_error = nil

  # Run the build and test.
  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

    # A compile error occured. Save it to be re-raised later.

    report_test[:result] = :error
    rescued_error = error

  rescue FastlaneCore::Interface::FastlaneTestFailure => error

    # A test failure occured. Save it to be re-raised later.

    report_test[:result] = :failure
    rescued_error = error

  else

    # Build succeeded!

    report_test[:result] = :success

  end

  # Now the real fun begins. Move the raw xcodebuild log from temporary
  # directory to artifacts directory.

  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

  # Save the artifact paths in the subreport.

  report_artifacts[:raw] = output_raw_path
  report_artifacts[:html] = output_html_path
  report_artifacts[:junit] = output_junit_path

  # Load the raw log and pipe it through xcpretty with a JSON formatter.
  # That will output a machine-readable information about everything
  # that happened in the build.

  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

  # Export JSON file report to environment variable

  context.env["XCODE_TEST_JSON_REPORT_PATH"] = temp_json_report_path

  # Load the build report and a JUnit report into memory.

  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

  # Extract test numbers from JUnit report.

  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

  # Extract compile errors from the build report.

  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

  # Extract test failures from the build report.

  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

  # Save the test and artifacts subreports in the report.

  report[:test] = report_test
  report[:artifacts] = report_artifacts

  # Re-raise the error after the report is finally prepared.

  raise rescued_error if rescued_error != nil

end