-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutil_fns.go
More file actions
342 lines (269 loc) · 9.43 KB
/
Copy pathutil_fns.go
File metadata and controls
342 lines (269 loc) · 9.43 KB
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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/olekukonko/tablewriter"
)
func getDevices() ([]string, error) {
var devices []string
devices_input := os.Getenv("devices_list")
if devices_input == "" {
return devices, errors.New(fmt.Sprintf(BUILD_FAILED_ERROR, "invalid device format"))
}
scanner := bufio.NewScanner(strings.NewReader(devices_input))
for scanner.Scan() {
device := scanner.Text()
device = strings.TrimSpace(device)
if device == "" {
continue
}
devices = append(devices, device)
}
return devices, nil
}
// any other capability which we're not taking from pre-defined inputs can be passed in api_params
func appendExtraCapabilities(payload string) []byte {
out := map[string]interface{}{}
json.Unmarshal([]byte(payload), &out)
scanner := bufio.NewScanner(strings.NewReader(os.Getenv("api_params")))
for scanner.Scan() {
test_sharding := scanner.Text()
test_sharding = strings.TrimSpace(test_sharding)
if test_sharding == "" {
continue
}
test_values := strings.Split(test_sharding, "=")
key := test_values[0]
out[key] = test_values[1]
}
outputJSON, _ := json.Marshal(out)
return outputJSON
}
func getTestFilters(payload *BrowserStackPayload) {
scanner := bufio.NewScanner(strings.NewReader(os.Getenv("filter_test")))
for scanner.Scan() {
test_filters := scanner.Text()
test_filters = strings.TrimSpace(test_filters)
if test_filters == "" {
continue
}
test_values := strings.Split(test_filters, ",")
for i := 0; i < len(test_values); i++ {
test_value := strings.Split(test_values[i], " ")
switch test_value[0] {
case "skip-testing":
*&payload.SkipTesting = append(*&payload.SkipTesting, test_value[1])
case "only-testing":
*&payload.OnlyTesting = append(*&payload.OnlyTesting, test_value[1])
}
}
}
}
// this util only picks data from env and map it to the struct
func createBuildPayload() BrowserStackPayload {
instrumentation_logs, _ := strconv.ParseBool(os.Getenv("instrumentation_logs"))
network_logs, _ := strconv.ParseBool(os.Getenv("network_logs"))
device_logs, _ := strconv.ParseBool(os.Getenv("device_logs"))
debug_screenshots, _ := strconv.ParseBool(os.Getenv("debug_screenshots"))
video_recording, _ := strconv.ParseBool(os.Getenv("video_recording"))
use_local, _ := strconv.ParseBool(os.Getenv("use_local"))
use_dynamic_tests, _ := strconv.ParseBool(os.Getenv("use_dynamic_tests"))
sharding_data := TestSharding{}
if os.Getenv("use_test_sharding") != "" {
err := json.Unmarshal([]byte(os.Getenv("use_test_sharding")), &sharding_data)
if err != nil {
fmt.Println(err.Error())
}
}
payload := BrowserStackPayload{
InstrumentationLogs: instrumentation_logs,
NetworkLogs: network_logs,
DeviceLogs: device_logs,
DebugScreenshots: debug_screenshots,
VideoRecording: video_recording,
DynamicTests: use_dynamic_tests,
Project: os.Getenv("project"),
ProjectNotifyURL: os.Getenv("project_notify_url"),
UseLocal: use_local,
}
getTestFilters(&payload)
if len(sharding_data.Mapping) != 0 && sharding_data.NumberOfShards != 0 {
payload.UseTestSharding = sharding_data
}
payload.Devices, _ = getDevices()
return payload
}
func failf(format string, args ...interface{}) {
log.Fatalf(format, args...)
os.Exit(1)
}
// this works as a goroutine which will run in background
// on a different thread without effecting any other code
func setInterval(someFunc func(), milliseconds int, async bool) chan bool {
// How often to fire the passed in function
// in milliseconds
interval := time.Duration(milliseconds) * time.Millisecond
// Setup the ticket and the channel to signal
// the ending of the interval
ticker := time.NewTicker(interval)
clear := make(chan bool)
// Put the selection in a go routine
// so that the for loop is none blocking
go func() {
for {
select {
case <-ticker.C:
if async {
// This won't block
go someFunc()
} else {
// This will block
someFunc()
}
case <-clear:
ticker.Stop()
}
}
}()
// We return the channel so we can pass in
// a value to it to clear the interval
return clear
}
func jsonParse(base64String string) map[string]interface{} {
parsed_json := make(map[string]interface{})
err := json.Unmarshal([]byte(base64String), &parsed_json)
if err != nil {
failf("Unable to parse app_upload API response: %s", err)
}
return parsed_json
}
// this function only print data to the console.
func printBuildStatus(build_details map[string]interface{}) {
log.Println("Build finished")
log.Println("Test results summary:")
devices := build_details["devices"].([]interface{})
build_id := build_details["id"]
data := [][]string{}
if len(devices) == 1 {
sessions := devices[0].(map[string]interface{})["sessions"].([]interface{})[0].(map[string]interface{})
session_status := sessions["status"].(string)
session_test_cases := sessions["testcases"].(map[string]interface{})
session_test_status := session_test_cases["status"].(map[string]interface{})
total_test := session_test_cases["count"]
passed_test := session_test_status["passed"]
device_name := devices[0].(map[string]interface{})["device"].(string)
if session_status == "passed" {
result := fmt.Sprintf("PASSED (%v/%v passed)", passed_test, total_test)
data = append(data, []string{build_id.(string), device_name, result})
}
if session_status == "failed" || session_status == "error" {
result := fmt.Sprintf("FAILED (%v/%v passed)", passed_test, total_test)
data = append(data, []string{build_id.(string), device_name, result})
}
} else {
for i := 0; i < len(devices); i++ {
sessions := devices[i].(map[string]interface{})["sessions"].([]interface{})[0].(map[string]interface{})
session_status := sessions["status"].(string)
session_test_cases := sessions["testcases"].(map[string]interface{})
session_test_status := session_test_cases["status"].(map[string]interface{})
total_test := session_test_cases["count"]
passed_test := session_test_status["passed"]
device_name := devices[i].(map[string]interface{})["device"].(string)
if session_status == "passed" {
result := fmt.Sprintf("PASSED (%v/%v passed)", passed_test, total_test)
data = append(data, []string{build_id.(string), device_name, result})
}
if session_status == "failed" || session_status == "error" {
result := fmt.Sprintf("FAILED (%v/%v passed)", passed_test, total_test)
data = append(data, []string{build_id.(string), device_name, result})
}
}
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Build Id", "Devices", "Status"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
// find all the files from the directory which matches the pattern eg: *.md
func WalkMatch(root, ext string) []string {
var files_found []string
filepath.WalkDir(root, func(path string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if matched, err := filepath.Match(ext, filepath.Base(path)); err != nil {
return err
} else if matched {
files_found = append(files_found, path)
}
return nil
})
return files_found
}
func locateAppFile(location string, file_name string) string {
app_extension := "app"
file_name_and_extension := file_name + "." + app_extension
split_path := strings.Split(location, "/")
get_file_name := split_path[len(split_path)-1]
file_path := ""
// If location is already .app file, return that. Else if location is directory,
// check if it contains any .app files with the specified name.
check_file_extension := strings.Split(get_file_name, ".")
if len(check_file_extension) > 0 && check_file_extension[len(check_file_extension)-1] == app_extension {
file_path = location
} else if strings.Contains(get_file_name, "test_bundle") {
files := WalkMatch(location+"/Debug-iphoneos/", file_name_and_extension)
if len(files) < 1 {
failf(FILE_NOT_FOUND, file_name_and_extension)
}
file_path = files[len(files)-1]
} else {
failf(FILE_NOT_FOUND, file_name_and_extension)
}
return file_path
}
// Locates .app, moves it into a Payload folder and compresses that folder into .ipa.
func locateAppBundleFileAndIpa(app_bundle_location string, app_bundle_name string) string {
app_bundle_path := locateAppFile(app_bundle_location, app_bundle_name)
app_zip_name := app_bundle_name + ".ipa"
_, mkdir_err := exec.Command("mkdir", "Payload").Output()
if mkdir_err != nil {
failf(FILE_DIR_ERROR, mkdir_err)
}
_, err := exec.Command("cp", "-r", app_bundle_path, "Payload/Application.app").Output()
if err != nil {
failf(FILE_COPY_ERROR, err)
}
_, zipping_err := exec.Command("zip", "-r", "-D", app_zip_name, "Payload").Output()
if zipping_err != nil {
failf(FILE_ZIP_ERROR, zipping_err)
}
return app_zip_name
}
// Locates runner .app and compresses it into .zip.
func locateTestRunnerFileAndZip(test_suite_location string) error {
test_runner_app_path := locateAppFile(test_suite_location, "*-Runner")
file_path := strings.Split(test_runner_app_path, "/")
test_runner_file_name := file_path[len(file_path)-1]
_, err := exec.Command("cp", "-r", test_runner_app_path, ".").Output()
if err != nil {
return errors.New(fmt.Sprintf(FILE_ZIP_ERROR, err))
}
_, zipping_err := exec.Command("zip", "-r", "-D", TEST_RUNNER_ZIP_FILE_NAME, test_runner_file_name).Output()
if zipping_err != nil {
return errors.New(fmt.Sprintf(FILE_ZIP_ERROR, zipping_err))
}
return nil
}