-
-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathcmd_device_testing.go
More file actions
175 lines (150 loc) · 5.26 KB
/
Copy pathcmd_device_testing.go
File metadata and controls
175 lines (150 loc) · 5.26 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
package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/danielpaulus/go-ios/ios/testmanagerd"
)
func runTestCommand(ctx commandContext) {
bundleID, _ := ctx.Args.String("--bundle-id")
testRunnerBundleId, _ := ctx.Args.String("--test-runner-bundle-id")
xctestConfig, _ := ctx.Args.String("--xctest-config")
testsToRunArg := ctx.Args["--test-to-run"]
var testsToRun []string
if testsToRunArg != nil && len(testsToRunArg.([]string)) > 0 {
testsToRun = testsToRunArg.([]string)
}
testsToSkipArg := ctx.Args["--test-to-skip"]
var testsToSkip []string
if testsToSkipArg != nil && len(testsToSkipArg.([]string)) > 0 {
testsToSkip = testsToSkipArg.([]string)
}
rawTestlog, rawTestlogErr := ctx.Args.String("--log-output")
env := splitKeyValuePairs(ctx.Args["--env"].([]string), "=")
isXCTest, _ := ctx.Args.Bool("--xctest")
config := testmanagerd.TestConfig{
BundleId: bundleID,
TestRunnerBundleId: testRunnerBundleId,
XctestConfigName: xctestConfig,
Env: env,
TestsToRun: testsToRun,
TestsToSkip: testsToSkip,
XcTest: isXCTest,
Device: ctx.Device,
}
if rawTestlogErr == nil {
var writer *os.File = os.Stdout
if rawTestlog != "-" {
file, err := os.Create(rawTestlog)
exitIfError("Cannot open file "+rawTestlog, err)
writer = file
}
defer writer.Close()
config.Listener = testmanagerd.NewTestListener(writer, writer, os.TempDir())
testResults, err := testmanagerd.RunTestWithConfig(context.TODO(), config)
if err != nil {
slog.Info("Failed running Xcuitest", "error", err)
}
fmt.Println(convertToJSONString(testResults))
} else {
config.Listener = testmanagerd.NewTestListener(io.Discard, io.Discard, os.TempDir())
testResults, err := testmanagerd.RunTestWithConfig(context.TODO(), config)
if err != nil {
slog.Info("Failed running Xcuitest", "error", err)
}
fmt.Println(convertToJSONString(testResults))
}
}
func runXCTestCommand(ctx commandContext) {
xctestrunFilePath, _ := ctx.Args.String("--xctestrun-file-path")
rawTestlog, rawTestlogErr := ctx.Args.String("--log-output")
if rawTestlogErr == nil {
var writer *os.File = os.Stdout
if rawTestlog != "-" {
file, err := os.Create(rawTestlog)
exitIfError("Cannot open file "+rawTestlog, err)
writer = file
}
defer writer.Close()
listener := testmanagerd.NewTestListener(writer, writer, os.TempDir())
testResults, err := testmanagerd.StartXCTestWithConfig(context.TODO(), xctestrunFilePath, ctx.Device, listener)
if err != nil {
slog.Info("Failed running Xctest", "error", err)
}
fmt.Println(convertToJSONString(testResults))
} else {
listener := testmanagerd.NewTestListener(io.Discard, io.Discard, os.TempDir())
testResults, err := testmanagerd.StartXCTestWithConfig(context.TODO(), xctestrunFilePath, ctx.Device, listener)
if err != nil {
slog.Info("Failed running Xctest", "error", err)
}
fmt.Println(convertToJSONString(testResults))
}
}
func runWDACommand(ctx commandContext) {
bundleID, _ := ctx.Args.String("--bundleid")
testbundleID, _ := ctx.Args.String("--testrunnerbundleid")
xctestconfig, _ := ctx.Args.String("--xctestconfig")
wdaargs := ctx.Args["--arg"].([]string)
wdaenv := splitKeyValuePairs(ctx.Args["--env"].([]string), "=")
if bundleID == "" && testbundleID == "" && xctestconfig == "" {
slog.Info("no bundle ids specified, falling back to defaults")
bundleID, testbundleID, xctestconfig = "com.facebook.WebDriverAgentRunner.xctrunner", "com.facebook.WebDriverAgentRunner.xctrunner", "WebDriverAgentRunner.xctest"
}
if bundleID == "" || testbundleID == "" || xctestconfig == "" {
slog.Error("please specify either NONE of bundleid, testbundleid and xctestconfig or ALL of them. At least one was empty.", "bundleid", bundleID, "testbundleid", testbundleID, "xctestconfig", xctestconfig)
return
}
slog.Info("Running wda", "bundleid", bundleID, "testbundleid", testbundleID, "xctestconfig", xctestconfig)
rawTestlog, rawTestlogErr := ctx.Args.String("--log-output")
var writer io.Writer
if rawTestlogErr == nil {
writerCloser := os.Stdout
writer = writerCloser
if rawTestlog != "-" {
file, err := os.Create(rawTestlog)
exitIfError("Cannot open file "+rawTestlog, err)
writer = file
}
defer writerCloser.Close()
} else {
writer = io.Discard
}
errorChannel := make(chan error)
defer close(errorChannel)
ctxWDA, stopWda := context.WithCancel(context.Background())
go func() {
_, err := testmanagerd.RunTestWithConfig(ctxWDA, testmanagerd.TestConfig{
BundleId: bundleID,
TestRunnerBundleId: testbundleID,
XctestConfigName: xctestconfig,
Env: wdaenv,
Args: wdaargs,
Device: ctx.Device,
Listener: testmanagerd.NewTestListener(writer, writer, os.TempDir()),
})
if err != nil {
errorChannel <- err
}
stopWda()
}()
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-errorChannel:
slog.Error("Failed running WDA", "error", err)
stopWda()
os.Exit(1)
case <-ctxWDA.Done():
slog.Error("WDA process ended unexpectedly")
os.Exit(1)
case signal := <-c:
slog.Info(fmt.Sprintf("os signal %d received, closing...", signal))
stopWda()
}
slog.Info("Done Closing")
}