Skip to content

Commit

Permalink
#835 Added printing iOS devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed Jul 16, 2020
1 parent ca1270a commit c3d2423
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 34 deletions.
3 changes: 3 additions & 0 deletions test_runner/docs/ascii/flank.jar_-firebase-test-ios.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ flank.jar
*doctor*::
Verifies flank firebase is setup correctly

*models*::
Information about available models

// end::picocli-generated-man-section-commands[]

// end::picocli-generated-full-manpage[]
3 changes: 3 additions & 0 deletions test_runner/docs/ascii/flank.jar_-ios.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ flank.jar
*doctor*::
Verifies flank firebase is setup correctly

*models*::
Information about available models

// end::picocli-generated-man-section-commands[]

// end::picocli-generated-full-manpage[]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.cli.firebase.test

import ftl.cli.firebase.test.ios.IosDoctorCommand
import ftl.cli.firebase.test.ios.IosRunCommand
import ftl.cli.firebase.test.ios.models.IosModelsCommand
import picocli.CommandLine
import picocli.CommandLine.Command

Expand All @@ -10,7 +11,8 @@ import picocli.CommandLine.Command
synopsisHeading = "",
subcommands = [
IosRunCommand::class,
IosDoctorCommand::class
IosDoctorCommand::class,
IosModelsCommand::class
],
usageHelpAutoWidth = true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ftl.cli.firebase.test.ios

import ftl.args.IosArgs
import ftl.cli.firebase.test.processValidation
import ftl.config.FtlConstants
import ftl.doctor.Doctor.validateYaml
import java.nio.file.Paths
import picocli.CommandLine.Command
Expand All @@ -28,7 +29,7 @@ class IosDoctorCommand : Runnable {
}

@Option(names = ["-c", "--config"], description = ["YAML config file path"])
var configPath: String = "./flank.ios.yml"
var configPath: String = FtlConstants.defaultIosConfig

@Option(names = ["-h", "--help"], usageHelp = true, description = ["Prints this help message"])
var usageHelpRequested: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ftl.cli.firebase.test.ios.models

import picocli.CommandLine

@CommandLine.Command(
name = "models",
headerHeading = "",
synopsisHeading = "%n",
descriptionHeading = "%n@|bold,underline Description:|@%n%n",
parameterListHeading = "%n@|bold,underline Parameters:|@%n",
optionListHeading = "%n@|bold,underline Options:|@%n",
header = ["Information about available models"],
description = ["Information about available models. For example prints list of available models to test against"],
subcommands = [IosModelsListCommand::class],
usageHelpAutoWidth = true
)
class IosModelsCommand : Runnable {
override fun run() {
CommandLine.usage(IosModelsCommand(), System.out)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ftl.cli.firebase.test.ios.models

import ftl.args.IosArgs
import ftl.config.FtlConstants
import ftl.ios.IosCatalog.devicesCatalogAsTable
import picocli.CommandLine
import java.nio.file.Paths

@CommandLine.Command(
name = "list",
headerHeading = "",
synopsisHeading = "%n",
descriptionHeading = "%n@|bold,underline Description:|@%n%n",
parameterListHeading = "%n@|bold,underline Parameters:|@%n",
optionListHeading = "%n@|bold,underline Options:|@%n",
header = ["Print current list of devices available to test against"],
description = ["Print current list of iOS devices available to test against"],
usageHelpAutoWidth = true
)
class IosModelsListCommand : Runnable {
override fun run() {
println(devicesCatalogAsTable(IosArgs.load(Paths.get(configPath)).project))
}

@CommandLine.Option(names = ["-c", "--config"], description = ["YAML config file path"])
var configPath: String = FtlConstants.defaultIosConfig

@CommandLine.Option(names = ["-h", "--help"], usageHelp = true, description = ["Prints this help message"])
var usageHelpRequested: Boolean = false
}
29 changes: 29 additions & 0 deletions test_runner/src/main/kotlin/ftl/environment/ListIOsDevices.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ftl.environment

import com.google.api.services.testing.model.IosModel
import ftl.util.applyColorsUsing
import ftl.util.buildTable

fun List<IosModel>.asPrintableTable() = createTestEnvironmentInfo().createIoDevicesTable()

fun List<IosModel>.createTestEnvironmentInfo() =
fold(mutableMapOf<String, MutableList<String>>()) { deviceInfo, iOsDevice ->
deviceInfo.apply {
getOrCreateList(MODEL_ID).add(iOsDevice.id.orUnknown())
getOrCreateList(MODEL_NAME).add(iOsDevice.name.orUnknown())
getOrCreateList(RESOLUTION).add(iOsDevice.resolution)
getOrCreateList(OS_VERSION_IDS).add(iOsDevice.supportedVersionIds?.joinToString().orEmpty())
getOrCreateList(TAGS).add(iOsDevice.tags?.joinToString().orEmpty())
}
}

private val IosModel.resolution
get() = if (screenX == null || screenY == null) "UNKNOWN" else "$screenY x $screenX"

private fun TestEnvironmentInfo.createIoDevicesTable() = buildTable(
createTableColumnFor(MODEL_ID),
createTableColumnFor(MODEL_NAME),
createTableColumnFor(RESOLUTION),
createTableColumnFor(OS_VERSION_IDS),
createTableColumnFor(TAGS).applyColorsUsing(tagToSystemOutColorMapper)
)
54 changes: 24 additions & 30 deletions test_runner/src/main/kotlin/ftl/ios/IosCatalog.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ftl.ios

import com.google.api.services.testing.model.IosDeviceCatalog
import ftl.environment.asPrintableTable
import ftl.gc.GcTesting
import ftl.http.executeWithRetry

Expand All @@ -13,30 +14,33 @@ object IosCatalog {
private val catalogMap: MutableMap<String, IosDeviceCatalog> = mutableMapOf()
private val xcodeMap: MutableMap<String, List<String>> = mutableMapOf()

fun xcodeVersions(projectId: String): List<String> {
val cached = xcodeMap[projectId]
if (cached != null) return cached
fun devicesCatalogAsTable(projectId: String) = iosDeviceCatalog(projectId).models.asPrintableTable()

val newVersions = iosDeviceCatalog(projectId).xcodeVersions.map { it.version }
xcodeMap[projectId] = newVersions
return newVersions
}
fun supportedXcode(version: String, projectId: String) = xcodeVersions(projectId).contains(version)

// Device catalogMap is different depending on the project id
fun iosDeviceCatalog(projectId: String): IosDeviceCatalog {
val cached = catalogMap[projectId]
if (cached != null) return cached
private fun xcodeVersions(projectId: String) =
xcodeMap.getOrPut(projectId) { iosDeviceCatalog(projectId).xcodeVersions.map { it.version } }

fun supportedDevice(
modelId: String,
versionId: String,
projectId: String
) = iosDeviceCatalog(projectId).models.find { it.id == modelId }?.supportedVersionIds?.contains(versionId) ?: false

try {
val newCatalog = GcTesting.get.testEnvironmentCatalog()
// Device catalogMap is different depending on the project id
private fun iosDeviceCatalog(
projectId: String
) = try {
catalogMap.getOrPut(projectId) {
GcTesting.get.testEnvironmentCatalog()
.get("ios")
.setProjectId(projectId)
.executeWithRetry().iosDeviceCatalog
catalogMap[projectId] = newCatalog
return newCatalog
} catch (e: java.lang.Exception) {
throw java.lang.RuntimeException(
"""
.executeWithRetry()
.iosDeviceCatalog
}
} catch (e: java.lang.Exception) {
throw java.lang.RuntimeException(
"""
Unable to access the test environment catalogMap. Firebase Test Lab for iOS is currently in beta.
Request access for your project via the following form:
https://goo.gl/forms/wAxbiNEP2pxeIRG82
Expand All @@ -46,16 +50,6 @@ on the Blaze or Flame billing plans, and that you have run
gcloud config set billing/quota_project project
If you are still having issues, please email [email protected] for support.""", e
)
}
}

fun supportedXcode(version: String, projectId: String): Boolean {
return xcodeVersions(projectId).contains(version)
}

fun supportedDevice(modelId: String, versionId: String, projectId: String): Boolean {
val model = iosDeviceCatalog(projectId).models.find { it.id == modelId }
return model?.supportedVersionIds?.contains(versionId) ?: false
)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ftl.cli.firebase.test.ios

import com.google.common.truth.Truth.assertThat
import ftl.cli.firebase.test.android.AndroidRunCommand
import ftl.config.Device
import ftl.config.FtlConstants
import ftl.config.FtlConstants.isWindows
Expand Down Expand Up @@ -322,7 +321,7 @@ class IosRunCommandTest {

@Test
fun `obfuscate parse`() {
val cmd = AndroidRunCommand()
val cmd = IosRunCommand()
CommandLine(cmd).parseArgs("--obfuscate")

assertThat(cmd.obfuscate).isTrue()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ftl.cli.firebase.test.ios.models

import com.google.common.truth.Truth.assertThat
import ftl.config.FtlConstants
import org.junit.Test
import picocli.CommandLine

class IosModelsListCommandTest {

@Test
fun iosModelsListCommandOptions() {
val cmd = IosModelsListCommand()
assertThat(cmd.configPath).isEqualTo(FtlConstants.defaultIosConfig)
cmd.configPath = "tmp"
assertThat(cmd.configPath).isEqualTo("tmp")

assertThat(cmd.usageHelpRequested).isFalse()
cmd.usageHelpRequested = true
assertThat(cmd.usageHelpRequested).isTrue()
}

@Test
fun iosModelsListCommandShouldParseConfig() {
val cmd = IosModelsListCommand()
CommandLine(cmd).parseArgs("--config=a")

assertThat(cmd.configPath).isEqualTo("a")
}
}
19 changes: 19 additions & 0 deletions test_runner/src/test/kotlin/ftl/ios/IosCatalogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,23 @@ class IosCatalogTest {
assertThat(IosCatalog.supportedXcode("9.2", projectId)).isTrue()
assertThat(IosCatalog.supportedXcode("0.1", projectId)).isFalse()
}

@Test
fun `should print available devices as table`() {
// given
val expectedHeaders = arrayOf("MODEL_ID", "MODEL_NAME", "RESOLUTION", "OS_VERSION_IDS", "TAGS")
val expectedSeparatorCount = expectedHeaders.size + 1

// when
val devicesTable = IosCatalog.devicesCatalogAsTable(projectId)
val headers = devicesTable.lines()[1]

// then
// has all necessary headers
expectedHeaders.forEach {
assertThat(headers.contains(it)).isTrue()
}
// number of separators match
assertThat(headers.count { it == '' }).isEqualTo(expectedSeparatorCount)
}
}

0 comments on commit c3d2423

Please sign in to comment.