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

Commit

Permalink
add filtering with device pattern (#94)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KazuCocoa authored and yunikkk committed Aug 14, 2017
1 parent beae497 commit 33edbb6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
24 changes: 19 additions & 5 deletions composer/src/main/kotlin/com/gojuno/composer/Args.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ data class Args(
val outputDirectory: String,
val instrumentationArguments: List<Pair<String, String>>,
val verboseOutput: Boolean,
val devices: List<String>
val devices: List<String>,
val devicePattern: String
)

// No way to share array both for runtime and annotation without reflection.
Expand Down Expand Up @@ -92,9 +93,22 @@ private class JCommanderArgs {
names = arrayOf("--devices"),
required = false,
variableArity = true,
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`."
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`."
)
var devices: List<String>? = null

@Parameter(
names = arrayOf("--device-pattern"),
required = false,
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\"`."
)
var devicePattern: String? = null
}

private fun validateArguments(args: Args) {
if(!args.devicePattern.isEmpty() && !args.devices.isEmpty()) {
throw IllegalArgumentException("Specifying both --devices and --device-pattern is prohibited.")
}
}

fun parseArgs(rawArgs: Array<String>): Args {
Expand Down Expand Up @@ -129,8 +143,8 @@ fun parseArgs(rawArgs: Array<String>): Args {
}
},
verboseOutput = jCommanderArgs.verboseOutput ?: false,
devices = jCommanderArgs.devices ?: emptyList()
devices = jCommanderArgs.devices ?: emptyList(),
devicePattern = jCommanderArgs.devicePattern ?: ""
)
}
}.apply { validateArguments(this) }
}

6 changes: 6 additions & 0 deletions composer/src/main/kotlin/com/gojuno/composer/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ fun main(rawArgs: Array<String>) {
val gson = Gson()

val suites: List<Suite> = connectedAdbDevices()
.map { devices ->
when (args.devicePattern.isEmpty()) {
true -> devices
false -> Regex(args.devicePattern).let { regex -> devices.filter { regex.matches(it.id) }}
}
}
.map {
when (args.devices.isEmpty()) {
true -> it
Expand Down
25 changes: 24 additions & 1 deletion composer/src/test/kotlin/com/gojuno/composer/ArgsSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.gojuno.composer
import com.gojuno.janulator.Args
import com.gojuno.janulator.parseArgs
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.context
import org.jetbrains.spek.api.dsl.it
Expand Down Expand Up @@ -30,7 +31,8 @@ class ArgsSpec : Spek({
outputDirectory = "composer-output",
instrumentationArguments = emptyList(),
verboseOutput = false,
devices = emptyList()
devices = emptyList(),
devicePattern = ""
))
}
}
Expand Down Expand Up @@ -103,4 +105,25 @@ class ArgsSpec : Spek({
}

}

context("parse args with passed --device-pattern") {

val args by memoized {
parseArgs(rawArgsWithOnlyRequiredFields + arrayOf("--device-pattern", "[abc|def]"))
}

it("parses correctly device-pattern") {
assertThat(args.devicePattern).isEqualTo("[abc|def]")
}
}

context("parse args with passed --devices and --device-pattern") {

it("raises argument error") {
assertThatThrownBy { parseArgs(rawArgsWithOnlyRequiredFields + arrayOf("--device-pattern", "[abc|def]") + arrayOf("--devices", "emulator-5554")) }
.isInstanceOf(IllegalArgumentException::class.java)
.hasMessageContaining("Specifying both --devices and --device-pattern is prohibited.")
}
}

})

0 comments on commit 33edbb6

Please sign in to comment.