Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit 33edbb6

Browse files
KazuCocoayunikkk
authored andcommitted
add filtering with device pattern (#94)
* add filtering with device pattern * fix the argument name and regax usage to filter devices * fix syntax error * ignore --device-pattern if --devices is passed * Revert "ignore --device-pattern if --devices is passed" This reverts commit 4333049. * raise error if --devices and --device-patten passed * add tests for error case * fix remarks and define validateArguments as member function * define a private method and use apply
1 parent beae497 commit 33edbb6

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

composer/src/main/kotlin/com/gojuno/composer/Args.kt

+19-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ data class Args(
1414
val outputDirectory: String,
1515
val instrumentationArguments: List<Pair<String, String>>,
1616
val verboseOutput: Boolean,
17-
val devices: List<String>
17+
val devices: List<String>,
18+
val devicePattern: String
1819
)
1920

2021
// No way to share array both for runtime and annotation without reflection.
@@ -92,9 +93,22 @@ private class JCommanderArgs {
9293
names = arrayOf("--devices"),
9394
required = false,
9495
variableArity = true,
95-
description = "Connected devices/emulators that will be used to run tests against. If not passed — tests will run on all connected devices/emulators. Usage example: `--devices emulator-5554 emulator-5556`."
96+
description = "Connected devices/emulators that will be used to run tests against. If not passed — tests will run on all connected devices/emulators. Specifying both `--devices` and `--device-pattern` will result in an error. Usage example: `--devices emulator-5554 emulator-5556`."
9697
)
9798
var devices: List<String>? = null
99+
100+
@Parameter(
101+
names = arrayOf("--device-pattern"),
102+
required = false,
103+
description = "Connected devices/emulators that will be used to run tests against. If not passed — tests will run on all connected devices/emulators. Specifying both `--device-pattern` and `--devices` will result in an error. Usage example: `--device-pattern \"somePatterns\"`."
104+
)
105+
var devicePattern: String? = null
106+
}
107+
108+
private fun validateArguments(args: Args) {
109+
if(!args.devicePattern.isEmpty() && !args.devices.isEmpty()) {
110+
throw IllegalArgumentException("Specifying both --devices and --device-pattern is prohibited.")
111+
}
98112
}
99113

100114
fun parseArgs(rawArgs: Array<String>): Args {
@@ -129,8 +143,8 @@ fun parseArgs(rawArgs: Array<String>): Args {
129143
}
130144
},
131145
verboseOutput = jCommanderArgs.verboseOutput ?: false,
132-
devices = jCommanderArgs.devices ?: emptyList()
146+
devices = jCommanderArgs.devices ?: emptyList(),
147+
devicePattern = jCommanderArgs.devicePattern ?: ""
133148
)
134-
}
149+
}.apply { validateArguments(this) }
135150
}
136-

composer/src/main/kotlin/com/gojuno/composer/Main.kt

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ fun main(rawArgs: Array<String>) {
3838
val gson = Gson()
3939

4040
val suites: List<Suite> = connectedAdbDevices()
41+
.map { devices ->
42+
when (args.devicePattern.isEmpty()) {
43+
true -> devices
44+
false -> Regex(args.devicePattern).let { regex -> devices.filter { regex.matches(it.id) }}
45+
}
46+
}
4147
.map {
4248
when (args.devices.isEmpty()) {
4349
true -> it

composer/src/test/kotlin/com/gojuno/composer/ArgsSpec.kt

+24-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.gojuno.composer
33
import com.gojuno.janulator.Args
44
import com.gojuno.janulator.parseArgs
55
import org.assertj.core.api.Assertions.assertThat
6+
import org.assertj.core.api.Assertions.assertThatThrownBy
67
import org.jetbrains.spek.api.Spek
78
import org.jetbrains.spek.api.dsl.context
89
import org.jetbrains.spek.api.dsl.it
@@ -30,7 +31,8 @@ class ArgsSpec : Spek({
3031
outputDirectory = "composer-output",
3132
instrumentationArguments = emptyList(),
3233
verboseOutput = false,
33-
devices = emptyList()
34+
devices = emptyList(),
35+
devicePattern = ""
3436
))
3537
}
3638
}
@@ -103,4 +105,25 @@ class ArgsSpec : Spek({
103105
}
104106

105107
}
108+
109+
context("parse args with passed --device-pattern") {
110+
111+
val args by memoized {
112+
parseArgs(rawArgsWithOnlyRequiredFields + arrayOf("--device-pattern", "[abc|def]"))
113+
}
114+
115+
it("parses correctly device-pattern") {
116+
assertThat(args.devicePattern).isEqualTo("[abc|def]")
117+
}
118+
}
119+
120+
context("parse args with passed --devices and --device-pattern") {
121+
122+
it("raises argument error") {
123+
assertThatThrownBy { parseArgs(rawArgsWithOnlyRequiredFields + arrayOf("--device-pattern", "[abc|def]") + arrayOf("--devices", "emulator-5554")) }
124+
.isInstanceOf(IllegalArgumentException::class.java)
125+
.hasMessageContaining("Specifying both --devices and --device-pattern is prohibited.")
126+
}
127+
}
128+
106129
})

0 commit comments

Comments
 (0)