/
main.go
385 lines (310 loc) · 11.2 KB
/
main.go
1
2
3
4
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
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
79
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-tools/go-steputils/input"
"github.com/bitrise-tools/go-steputils/tools"
"github.com/bitrise-tools/go-xamarin/builder"
"github.com/bitrise-tools/go-xamarin/constants"
"github.com/bitrise-tools/go-xamarin/tools/buildtools"
"github.com/bitrise-tools/go-xamarin/tools/nunit"
"github.com/bitrise-tools/go-xcode/simulator"
"github.com/hashicorp/go-version"
)
// ConfigsModel ...
type ConfigsModel struct {
SimulatorDevice string
SimulatorOsVersion string
TestToRun string
XamarinSolution string
XamarinConfiguration string
XamarinPlatform string
BuildTool string
DeployDir string
}
func createConfigsModelFromEnvs() ConfigsModel {
return ConfigsModel{
SimulatorDevice: os.Getenv("simulator_device"),
SimulatorOsVersion: os.Getenv("simulator_os_version"),
TestToRun: os.Getenv("test_to_run"),
XamarinSolution: os.Getenv("xamarin_project"),
XamarinConfiguration: os.Getenv("xamarin_configuration"),
XamarinPlatform: os.Getenv("xamarin_platform"),
BuildTool: os.Getenv("build_tool"),
DeployDir: os.Getenv("BITRISE_DEPLOY_DIR"),
}
}
func (configs ConfigsModel) print() {
log.Infof("Testing:")
log.Printf("- SimulatorDevice: %s", configs.SimulatorDevice)
log.Printf("- SimulatorOsVersion: %s", configs.SimulatorOsVersion)
log.Printf("- TestToRun: %s", configs.TestToRun)
log.Infof("Configs:")
log.Printf("- XamarinSolution: %s", configs.XamarinSolution)
log.Printf("- XamarinConfiguration: %s", configs.XamarinConfiguration)
log.Printf("- XamarinPlatform: %s", configs.XamarinPlatform)
log.Infof("Debug:")
log.Printf("- BuildTool: %s", configs.BuildTool)
log.Printf("- DeployDir: %s", configs.DeployDir)
}
func (configs ConfigsModel) validate() error {
if err := input.ValidateIfNotEmpty(configs.SimulatorDevice); err != nil {
return fmt.Errorf("SimulatorDevice - %s", err)
}
if err := input.ValidateIfNotEmpty(configs.SimulatorOsVersion); err != nil {
return fmt.Errorf("SimulatorOsVersion - %s", err)
}
if err := input.ValidateIfPathExists(configs.XamarinSolution); err != nil {
return fmt.Errorf("XamarinSolution - %s", err)
}
if err := input.ValidateIfNotEmpty(configs.XamarinConfiguration); err != nil {
return fmt.Errorf("XamarinConfiguration - %s", err)
}
if err := input.ValidateIfNotEmpty(configs.XamarinPlatform); err != nil {
return fmt.Errorf("XamarinPlatform - %s", err)
}
if err := input.ValidateWithOptions(configs.BuildTool, "msbuild", "xbuild"); err != nil {
return fmt.Errorf("BuildTool - %s", err)
}
return nil
}
func getLatestIOSVersion(osVersionSimulatorInfosMap simulator.OsVersionSimulatorInfosMap) (string, error) {
var latestVersionPtr *version.Version
for osVersion := range osVersionSimulatorInfosMap {
if !strings.HasPrefix(osVersion, "iOS") {
continue
}
versionStr := strings.TrimPrefix(osVersion, "iOS")
versionStr = strings.TrimSpace(versionStr)
versionPtr, err := version.NewVersion(versionStr)
if err != nil {
return "", fmt.Errorf("Failed to parse version (%s), error: %s", versionStr, err)
}
if latestVersionPtr == nil || versionPtr.GreaterThan(latestVersionPtr) {
latestVersionPtr = versionPtr
}
}
if latestVersionPtr == nil {
return "", fmt.Errorf("Failed to determin latest iOS simulator version")
}
versionSegments := latestVersionPtr.Segments()
if len(versionSegments) < 2 {
return "", fmt.Errorf("Invalid version created: %s, segments count < 2", latestVersionPtr.String())
}
return fmt.Sprintf("iOS %d.%d", versionSegments[0], versionSegments[1]), nil
}
func getSimulatorInfo(osVersion, deviceName string) (simulator.InfoModel, error) {
osVersionSimulatorInfosMap, err := simulator.GetOsVersionSimulatorInfosMap()
if err != nil {
return simulator.InfoModel{}, err
}
if osVersion == "latest" {
latestOSVersion, err := getLatestIOSVersion(osVersionSimulatorInfosMap)
if err != nil {
return simulator.InfoModel{}, err
}
osVersion = latestOSVersion
}
infos, ok := osVersionSimulatorInfosMap[osVersion]
if !ok {
return simulator.InfoModel{}, fmt.Errorf("No simulators found for os version: %s", osVersion)
}
for _, info := range infos {
if info.Name == deviceName {
return info, nil
}
}
return simulator.InfoModel{}, fmt.Errorf("No simulators found for os version: (%s), device name: (%s)", osVersion, deviceName)
}
func testResultLogContent(pth string) (string, error) {
if exist, err := pathutil.IsPathExists(pth); err != nil {
return "", fmt.Errorf("Failed to check if path (%s) exist, error: %s", pth, err)
} else if !exist {
return "", fmt.Errorf("test result not exist at: %s", pth)
}
content, err := fileutil.ReadStringFromFile(pth)
if err != nil {
return "", fmt.Errorf("Failed to read file (%s), error: %s", pth, err)
}
return content, nil
}
func parseErrorFromResultLog(content string) (string, error) {
failureLineFound := false
lastFailureMessage := ""
scanner := bufio.NewScanner(strings.NewReader(content))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "<failure>" {
failureLineFound = true
continue
}
if failureLineFound && strings.HasPrefix(line, "<message>") {
lastFailureMessage = line
}
failureLineFound = false
}
return lastFailureMessage, nil
}
func failf(format string, v ...interface{}) {
log.Errorf(format, v...)
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_RESULT", "failed"); err != nil {
log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_RESULT", err)
}
os.Exit(1)
}
func main() {
configs := createConfigsModelFromEnvs()
fmt.Println()
configs.print()
if err := configs.validate(); err != nil {
failf("Issue with input: %s", err)
}
// Get Simulator Infos
fmt.Println()
log.Infof("Collecting simulator info...")
simulatorInfo, err := getSimulatorInfo(configs.SimulatorOsVersion, configs.SimulatorDevice)
if err != nil {
failf("Failed to get simulator infos, error: %s", err)
}
log.Donef("Simulator (%s), id: (%s), status: %s", simulatorInfo.Name, simulatorInfo.ID, simulatorInfo.Status)
if err := os.Setenv("IOS_SIMULATOR_UDID", simulatorInfo.ID); err != nil {
failf("Failed to export simulator UDID, error: %s", err)
}
// ---
// Nunit Console path
nunitConsolePth, err := nunit.SystemNunit3ConsolePath()
if err != nil {
failf("Failed to get system insatlled nunit3-console.exe path, error: %s", err)
}
// ---
//
// build
fmt.Println()
log.Infof("Building all iOS Xamarin UITest and Referred Projects in solution: %s", configs.XamarinSolution)
buildTool := buildtools.Msbuild
if configs.BuildTool == "xbuild" {
buildTool = buildtools.Xbuild
}
builder, err := builder.New(configs.XamarinSolution, []constants.SDK{constants.SDKIOS}, buildTool)
if err != nil {
failf("Failed to create xamarin builder, error: %s", err)
}
callback := func(solutionName string, projectName string, sdk constants.SDK, testFramework constants.TestFramework, commandStr string, alreadyPerformed bool) {
fmt.Println()
if testFramework == constants.TestFrameworkXamarinUITest {
log.Infof("Building test project: %s", projectName)
} else {
log.Infof("Building project: %s", projectName)
}
log.Donef("$ %s", commandStr)
if alreadyPerformed {
log.Warnf("build command already performed, skipping...")
}
fmt.Println()
}
startTime := time.Now()
warnings, err := builder.BuildAndRunAllXamarinUITestAndReferredProjects(configs.XamarinConfiguration, configs.XamarinPlatform, nil, callback)
endTime := time.Now()
for _, warning := range warnings {
log.Warnf(warning)
}
if err != nil {
failf("Build failed, error: %s", err)
}
projectOutputMap, err := builder.CollectProjectOutputs(configs.XamarinConfiguration, configs.XamarinPlatform, startTime, endTime)
if err != nil {
failf("Failed to collect project outputs, error: %s", err)
}
testProjectOutputMap, warnings, err := builder.CollectXamarinUITestProjectOutputs(configs.XamarinConfiguration, configs.XamarinPlatform, startTime, endTime)
for _, warning := range warnings {
log.Warnf(warning)
}
if err != nil {
failf("Failed to collect test project output, error: %s", err)
}
if len(testProjectOutputMap) == 0 {
failf("No testable output generated")
}
// ---
//
// Run nunit tests
nunitConsole, err := nunit.New(nunitConsolePth)
if err != nil {
failf("Failed to create nunit console model, error: %s", err)
}
resultLogPth := filepath.Join(configs.DeployDir, "TestResult.xml")
nunitConsole.SetResultLogPth(resultLogPth)
// Artifacts
resultLog := ""
for testProjectName, testProjectOutput := range testProjectOutputMap {
if len(testProjectOutput.ReferredProjectNames) == 0 {
log.Warnf("Test project (%s) does not refers to any project, skipping...", testProjectName)
continue
}
for _, projectName := range testProjectOutput.ReferredProjectNames {
projectOutput, ok := projectOutputMap[projectName]
if !ok {
continue
}
appPth := ""
for _, output := range projectOutput.Outputs {
if output.OutputType == constants.OutputTypeAPP {
appPth = output.Pth
}
}
if appPth == "" {
failf("No app generated for project: %s", projectName)
}
// Set APP_BUNDLE_PATH env to let the test know which .app file should be tested
// This env is used in the Xamarin.UITest project to refer to the .app path
if err := os.Setenv("APP_BUNDLE_PATH", appPth); err != nil {
failf("Failed to set APP_BUNDLE_PATH environment, without this env test will fail, error: %s", err)
}
// Run test
fmt.Println()
log.Infof("Testing (%s) against (%s)", testProjectName, projectName)
log.Printf("test dll: %s", testProjectOutput.Output.Pth)
log.Printf("app: %s", appPth)
nunitConsole.SetDLLPth(testProjectOutput.Output.Pth)
nunitConsole.SetTestToRun(configs.TestToRun)
fmt.Println()
log.Infof("Running Xamarin UITest")
log.Donef("$ %s", nunitConsole.PrintableCommand())
fmt.Println()
err := nunitConsole.Run()
testLog, readErr := testResultLogContent(resultLogPth)
if readErr != nil {
log.Warnf("Failed to read test result, error: %s", readErr)
}
resultLog = testLog
if err != nil {
if errorMsg, err := parseErrorFromResultLog(resultLog); err != nil {
log.Warnf("Failed to parse error message from result log, error: %s", err)
} else if errorMsg != "" {
log.Errorf("%s", errorMsg)
}
if resultLog != "" {
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", resultLog); err != nil {
log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", err)
}
}
failf("Test failed, error: %s", err)
}
}
}
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_RESULT", "succeeded"); err != nil {
log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_RESULT", err)
}
if resultLog != "" {
if err := tools.ExportEnvironmentWithEnvman("BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", resultLog); err != nil {
log.Warnf("Failed to export environment: %s, error: %s", "BITRISE_XAMARIN_TEST_FULL_RESULTS_TEXT", err)
}
}
}