Skip to content

Commit

Permalink
refactor: Structural output Android models describe (#1887)
Browse files Browse the repository at this point in the history
Fixes #1846

## Test Plan
> How do we know the code works?

- Code is refactored according to #1846
- Android describe models command works like previously

## Checklist

- [x] Unit tested
  • Loading branch information
piotradamczyk5 authored May 4, 2021
1 parent b3c4a76 commit 26d516e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 31 deletions.
12 changes: 7 additions & 5 deletions test_runner/src/main/kotlin/ftl/domain/DescribeAndroidModels.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package ftl.domain

import flank.common.logLn
import ftl.api.fetchDeviceModelAndroid
import ftl.args.AndroidArgs
import ftl.environment.android.getDescription
import ftl.presentation.Output
import ftl.run.exception.FlankConfigurationError
import ftl.run.exception.FlankGeneralError
import java.nio.file.Paths

interface DescribeAndroidModels {
interface DescribeAndroidModels : Output {
val configPath: String
val modelId: String
}

operator fun DescribeAndroidModels.invoke() {
if (modelId.isBlank()) throw FlankConfigurationError("Argument MODEL_ID must be specified.")
// TODO move getDescription() and printing presentation layer during refactor of presentation after #1728
logLn(fetchDeviceModelAndroid(AndroidArgs.loadOrDefault(Paths.get(configPath)).project).getDescription(modelId))
fetchDeviceModelAndroid(AndroidArgs.loadOrDefault(Paths.get(configPath)).project)
.find { it.id == modelId }
?.out()
?: throw FlankGeneralError("ERROR: '$modelId' is not a valid model")
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package ftl.presentation.cli.firebase.test.android.models

import ftl.api.DeviceModel
import ftl.config.FtlConstants
import ftl.domain.DescribeAndroidModels
import ftl.domain.invoke
import ftl.presentation.cli.firebase.test.android.models.describe.prepareDescription
import ftl.presentation.outputLogger
import ftl.presentation.throwUnknownType
import picocli.CommandLine

@CommandLine.Command(
Expand Down Expand Up @@ -45,4 +49,11 @@ class AndroidModelDescribeCommand :
override var modelId: String = ""

override fun run() = invoke()

override val out = outputLogger {
when (this) {
is DeviceModel.Android -> prepareDescription()
else -> throwUnknownType()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package ftl.environment.android
package ftl.presentation.cli.firebase.test.android.models.describe

import ftl.api.DeviceModel
import ftl.run.exception.FlankGeneralError

fun List<DeviceModel.Android>.getDescription(modelId: String) = findModel(modelId)?.prepareDescription().orErrorMessage(modelId)

private fun List<DeviceModel.Android>.findModel(modelId: String) = firstOrNull { it.id == modelId }

private fun DeviceModel.Android.prepareDescription() = """
fun DeviceModel.Android.prepareDescription() = """
brand: $brand
codename: $codename
form: $form
Expand Down Expand Up @@ -36,8 +31,6 @@ private fun String.appendThumbnail(thumbnailUrl: String?) =
if (!thumbnailUrl.isNullOrBlank()) StringBuilder(this).appendLine("\n$THUBNAIL_URL_HEADER $thumbnailUrl").toString()
else this

private fun String?.orErrorMessage(modelId: String) = this ?: throw FlankGeneralError("ERROR: '$modelId' is not a valid model")

private const val SUPPORTED_ABIS_HEADER = "supportedAbis:"
private const val SUPPORTED_VERSIONS_HEADER = "supportedVersionIds:"
private const val TAGS_HEADER = "tags:"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ftl.environment.android

import ftl.api.DeviceModel
import ftl.run.exception.FlankGeneralError
import ftl.test.util.TestHelper.getThrowable
import org.junit.Assert
import ftl.presentation.cli.firebase.test.android.models.describe.prepareDescription
import org.junit.Assert.assertEquals
import org.junit.Test

class AndroidModelDescriptionTest {
Expand All @@ -30,7 +29,7 @@ class AndroidModelDescriptionTest {
)
)

val modelDescription = models.getDescription("walleye")
val modelDescription = models.find { it.id == "walleye" }?.prepareDescription()
val expected = """
brand: Google
codename: walleye
Expand All @@ -54,7 +53,7 @@ class AndroidModelDescriptionTest {
- default
thumbnailUrl: https://lh3.googleusercontent.com/j4urvb3lXTaFGZI6IzHmAjum2HQVID1OHPhDB7dOzRvXb2WscSX2RFwEEFFSYhajqRO5Yu0e6FYQ
""".trimIndent()
Assert.assertEquals(expected, modelDescription)
assertEquals(expected, modelDescription)
}

@Test
Expand All @@ -80,7 +79,7 @@ class AndroidModelDescriptionTest {
)
)

val modelDescription = models.getDescription("walleye")
val modelDescription = models.find { it.id == "walleye" }?.prepareDescription()
val expected = """
brand: Google
codename: walleye
Expand All @@ -102,16 +101,6 @@ class AndroidModelDescriptionTest {
- 28
thumbnailUrl: https://lh3.googleusercontent.com/j4urvb3lXTaFGZI6IzHmAjum2HQVID1OHPhDB7dOzRvXb2WscSX2RFwEEFFSYhajqRO5Yu0e6FYQ
""".trimIndent()
Assert.assertEquals(expected, modelDescription)
}

@Test(expected = FlankGeneralError::class)
fun `should return error message if model not found and throw FlankGeneralError`() {
val versions = listOf<DeviceModel.Android>()
val versionName = "test"
val localesDescription = getThrowable { versions.getDescription(versionName) }
val expected = "ERROR: '$versionName' is not a valid model"
Assert.assertEquals(expected, localesDescription.message)
throw localesDescription
assertEquals(expected, modelDescription)
}
}

0 comments on commit 26d516e

Please sign in to comment.