-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathIosArgs.kt
More file actions
213 lines (187 loc) · 8.1 KB
/
IosArgs.kt
File metadata and controls
213 lines (187 loc) · 8.1 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
package ftl.args
import com.google.common.annotations.VisibleForTesting
import ftl.args.ArgsHelper.assertCommonProps
import ftl.args.ArgsHelper.assertFileExists
import ftl.args.ArgsHelper.assertGcsFileExists
import ftl.args.ArgsHelper.createGcsBucket
import ftl.args.ArgsHelper.createJunitBucket
import ftl.args.ArgsHelper.evaluateFilePath
import ftl.args.ArgsHelper.mergeYmlMaps
import ftl.args.ArgsHelper.yamlMapper
import ftl.args.ArgsToString.listToString
import ftl.args.ArgsToString.mapToString
import ftl.args.ArgsToString.objectsToString
import ftl.args.yml.FlankYml
import ftl.args.yml.GcloudYml
import ftl.args.yml.IosFlankYml
import ftl.args.yml.IosGcloudYml
import ftl.args.yml.IosGcloudYmlParams
import ftl.args.yml.YamlDeprecated
import ftl.cli.firebase.test.ios.IosRunCommand
import ftl.config.Device
import ftl.config.FtlConstants
import ftl.ios.IosCatalog
import ftl.ios.Xctestrun
import ftl.util.FlankTestMethod
import ftl.util.FlankFatalError
import java.io.Reader
import java.nio.file.Files
import java.nio.file.Path
class IosArgs(
gcloudYml: GcloudYml,
iosGcloudYml: IosGcloudYml,
flankYml: FlankYml,
iosFlankYml: IosFlankYml,
override val data: String,
val cli: IosRunCommand? = null
) : IArgs {
private val gcloud = gcloudYml.gcloud
override val resultsBucket: String
override val resultsDir = cli?.resultsDir ?: gcloud.resultsDir
override val recordVideo = cli?.recordVideo ?: cli?.noRecordVideo?.not() ?: gcloud.recordVideo
override val testTimeout = cli?.timeout ?: gcloud.timeout
override val async = cli?.async ?: gcloud.async
override val resultsHistoryName = cli?.resultsHistoryName ?: gcloud.resultsHistoryName
override val flakyTestAttempts = cli?.flakyTestAttempts ?: gcloud.flakyTestAttempts
private val iosGcloud = iosGcloudYml.gcloud
var xctestrunZip = cli?.test ?: iosGcloud.test ?: throw FlankFatalError("test is not set")
var xctestrunFile = cli?.xctestrunFile ?: iosGcloud.xctestrunFile ?: throw FlankFatalError("xctestrun-file is not set")
val xcodeVersion = cli?.xcodeVersion ?: iosGcloud.xcodeVersion
val devices = cli?.device ?: iosGcloud.device
private val flank = flankYml.flank
override val maxTestShards = cli?.maxTestShards ?: flank.maxTestShards
override val shardTime = cli?.shardTime ?: flank.shardTime
override val repeatTests = cli?.repeatTests ?: flank.repeatTests
override val smartFlankGcsPath = cli?.smartFlankGcsPath ?: flank.smartFlankGcsPath
override val smartFlankDisableUpload = cli?.smartFlankDisableUpload ?: flank.smartFlankDisableUpload
override val testTargetsAlwaysRun = cli?.testTargetsAlwaysRun ?: flank.testTargetsAlwaysRun
override val filesToDownload = cli?.filesToDownload ?: flank.filesToDownload
override val disableSharding = cli?.disableSharding ?: flank.disableSharding
override val project = cli?.project ?: flank.project
override val localResultDir = cli?.localResultsDir ?: flank.localResultsDir
override val runTimeout = cli?.runTimeout ?: flank.runTimeout
override val clientDetails = cli?.clientDetails ?: gcloud.clientDetails
override val networkProfile = cli?.networkProfile ?: gcloud.networkProfile
override val ignoreFailedTests = cli?.ignoreFailedTests ?: flank.ignoreFailedTests
override val keepFilePath = cli?.keepFilePath ?: flank.keepFilePath
private val iosFlank = iosFlankYml.flank
val testTargets = cli?.testTargets ?: iosFlank.testTargets.filterNotNull()
// computed properties not specified in yaml
val testShardChunks: ShardChunks by lazy {
if (disableSharding) return@lazy listOf(emptyList<String>())
val validTestMethods = Xctestrun.findTestNames(xctestrunFile)
val testsToShard = filterTests(validTestMethods, testTargets).distinct().map { FlankTestMethod(it, ignored = false) }
ArgsHelper.calculateShards(testsToShard, this)
}
init {
resultsBucket = createGcsBucket(project, cli?.resultsBucket ?: gcloud.resultsBucket)
createJunitBucket(project, flank.smartFlankGcsPath)
if (xctestrunZip.startsWith(FtlConstants.GCS_PREFIX)) {
assertGcsFileExists(xctestrunZip)
} else {
xctestrunZip = evaluateFilePath(xctestrunZip)
assertFileExists(xctestrunZip, "xctestrunZip")
}
xctestrunFile = evaluateFilePath(xctestrunFile)
assertFileExists(xctestrunFile, "xctestrunFile")
devices.forEach { device -> assertDeviceSupported(device) }
assertXcodeSupported(xcodeVersion)
assertCommonProps(this)
}
private fun assertXcodeSupported(xcodeVersion: String?) {
if (xcodeVersion == null) return
if (!IosCatalog.supportedXcode(xcodeVersion, this.project)) {
throw FlankFatalError(("Xcode $xcodeVersion is not a supported Xcode version"))
}
}
private fun assertDeviceSupported(device: Device) {
if (!IosCatalog.supportedDevice(device.model, device.version, this.project)) {
throw FlankFatalError("iOS ${device.version} on ${device.model} is not a supported device")
}
}
override fun toString(): String {
return """
IosArgs
gcloud:
results-bucket: $resultsBucket
results-dir: $resultsDir
record-video: $recordVideo
timeout: $testTimeout
async: $async
client-details: ${mapToString(clientDetails)}
network-profile: $networkProfile
results-history-name: $resultsHistoryName
# iOS gcloud
test: $xctestrunZip
xctestrun-file: $xctestrunFile
xcode-version: $xcodeVersion
device:${objectsToString(devices)}
num-flaky-test-attempts: $flakyTestAttempts
flank:
max-test-shards: $maxTestShards
shard-time: $shardTime
num-test-runs: $repeatTests
smart-flank-gcs-path: $smartFlankGcsPath
smart-flank-disable-upload: $smartFlankDisableUpload
test-targets-always-run:${listToString(testTargetsAlwaysRun)}
files-to-download:${listToString(filesToDownload)}
keep-file-path: $keepFilePath
# iOS flank
test-targets:${listToString(testTargets)}
disable-sharding: $disableSharding
project: $project
local-result-dir: $localResultDir
run-timeout: $runTimeout
ignore-failed-tests: $ignoreFailedTests
""".trimIndent()
}
companion object : IArgsCompanion {
override val validArgs by lazy {
mergeYmlMaps(GcloudYml, IosGcloudYml, FlankYml, IosFlankYml)
}
fun load(yamlPath: Path, cli: IosRunCommand? = null): IosArgs = load(Files.newBufferedReader(yamlPath), cli)
@VisibleForTesting
internal fun load(yamlReader: Reader, cli: IosRunCommand? = null): IosArgs {
val data = YamlDeprecated.modifyAndThrow(yamlReader, android = false)
val flankYml = yamlMapper.readValue(data, FlankYml::class.java)
val iosFlankYml = yamlMapper.readValue(data, IosFlankYml::class.java)
val gcloudYml = yamlMapper.readValue(data, GcloudYml::class.java)
val iosGcloudYml = yamlMapper.readValue(data, IosGcloudYml::class.java)
return IosArgs(
gcloudYml,
iosGcloudYml,
flankYml,
iosFlankYml,
data,
cli
)
}
fun default(): IosArgs {
return IosArgs(
GcloudYml(),
IosGcloudYml(IosGcloudYmlParams(test = ".", xctestrunFile = ".")),
FlankYml(),
IosFlankYml(),
"",
IosRunCommand()
)
}
}
}
fun filterTests(validTestMethods: List<String>, testTargetsRgx: List<String?>): List<String> {
if (testTargetsRgx.isEmpty()) {
return validTestMethods
}
return validTestMethods.filter { test ->
testTargetsRgx.filterNotNull().forEach { target ->
try {
if (test.matches(target.toRegex())) {
return@filter true
}
} catch (e: Exception) {
throw IllegalArgumentException("Invalid regex: $target", e)
}
}
return@filter false
}
}