Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: data scratch getLocales #1828

Merged
merged 10 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ftl.adapter.google.toAndroidApiModel
import ftl.adapter.google.toIosApiModel
import ftl.api.DeviceModel
import ftl.client.google.AndroidCatalog
import ftl.ios.IosCatalog
import ftl.client.google.IosCatalog

object GoogleAndroidDeviceModel :
DeviceModel.Android.Fetch,
Expand Down
16 changes: 16 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/GoogleLocalesFetch.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ftl.adapter

import ftl.adapter.google.toApiModel
import ftl.api.Locale
import ftl.api.Platform
import ftl.client.google.AndroidCatalog
import ftl.client.google.IosCatalog

object GoogleLocalesFetch :
Locale.Fetch,
(Locale.Identity) -> List<Locale> by { id ->
when (id.platform) {
Platform.ANDROID -> AndroidCatalog.getLocales(id.projectId).toApiModel()
Platform.IOS -> IosCatalog.getLocales(id.projectId).toApiModel()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ftl.adapter.google.toApiModel
import ftl.api.Orientation
import ftl.api.Platform
import ftl.client.google.AndroidCatalog
import ftl.ios.IosCatalog
import ftl.client.google.IosCatalog

object GoogleOrientationFetch :
Orientation.Fetch,
Expand Down
59 changes: 59 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/google/ListLocales.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ftl.adapter.google

import ftl.api.Locale
import ftl.environment.TAGS
import ftl.environment.TestEnvironmentInfo
import ftl.environment.createTableColumnFor
import ftl.environment.getOrCreateList
import ftl.environment.tagToSystemOutColorMapper
import ftl.run.exception.FlankGeneralError
import ftl.util.applyColorsUsing
import ftl.util.buildTable

fun List<Locale>.asPrintableTable() = createTestEnvironment().createLocalesTable()

fun List<Locale>.createTestEnvironment() =
fold(mutableMapOf<String, MutableList<String>>()) { allLocales, locale ->
allLocales.apply {
getOrCreateList(LOCALE).add(locale.id.orEmpty())
getOrCreateList(NAME).add(locale.name.orEmpty())
getOrCreateList(REGION).add(locale.region.orEmpty())
Sloox marked this conversation as resolved.
Show resolved Hide resolved
getOrCreateList(TAGS).add(locale.tags.joinToString().orEmpty())
}
}

fun TestEnvironmentInfo.createLocalesTable() = buildTable(
createTableColumnFor(LOCALE),
createTableColumnFor(NAME),
createTableColumnFor(REGION),
createTableColumnFor(ftl.environment.TAGS).applyColorsUsing(tagToSystemOutColorMapper)
)

private const val LOCALE = "LOCALE"
private const val NAME = "NAME"
private const val REGION = "REGION"

fun List<Locale>.getLocaleDescription(localeId: String) =
findLocales(localeId)?.prepareDescription().orErrorMessage(localeId).plus("\n")

private fun List<Locale>.findLocales(localeId: String) = find { it.id == localeId }

private fun Locale.prepareDescription() = """
id: $id
name: $name
""".trimIndent().addRegionIfExist(region).addTagsIfExists(this)

private fun String.addRegionIfExist(region: String?) =
if (!region.isNullOrEmpty()) StringBuilder(this).appendLine("\nregion: $region").trim().toString()
else this

private fun String.addTagsIfExists(locale: Locale) =
if (!locale.tags.isNullOrEmpty()) StringBuilder(this).appendLine("\ntags:").appendTagsToList(locale)
else this

private fun StringBuilder.appendTagsToList(locale: Locale) = apply {
locale.tags.filterNotNull().forEach { tag -> appendLine("- $tag") }
}.trim().toString()

private fun String?.orErrorMessage(locale: String) =
this ?: throw FlankGeneralError("ERROR: '$locale' is not a valid locale")
12 changes: 12 additions & 0 deletions test_runner/src/main/kotlin/ftl/adapter/google/LocalesAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ftl.adapter.google

import ftl.api.Locale

internal fun List<com.google.testing.model.Locale>.toApiModel(): List<Locale> = map { locale ->
Sloox marked this conversation as resolved.
Show resolved Hide resolved
Locale(
id = locale.id.orEmpty(),
name = locale.name.orEmpty(),
region = locale.region.orEmpty(),
tags = locale.tags.orEmpty()
)
}
20 changes: 20 additions & 0 deletions test_runner/src/main/kotlin/ftl/api/Locale.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ftl.api

import ftl.adapter.GoogleLocalesFetch

val fetchLocales: Locale.Fetch get() = GoogleLocalesFetch

data class Locale(
val id: String,
val name: String,
val region: String,
val tags: List<String>,
) {

data class Identity(
val projectId: String,
val platform: Platform
)

interface Fetch : (Identity) -> List<Locale>
}
5 changes: 0 additions & 5 deletions test_runner/src/main/kotlin/ftl/api/Orientation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,3 @@ data class Orientation(
) {
interface Fetch : (String, Platform) -> List<Orientation>
}

enum class Platform {
ANDROID,
IOS
}
6 changes: 6 additions & 0 deletions test_runner/src/main/kotlin/ftl/api/Platform.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ftl.api

enum class Platform {
ANDROID,
IOS
}
4 changes: 2 additions & 2 deletions test_runner/src/main/kotlin/ftl/args/ValidateIosArgs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.google.common.annotations.VisibleForTesting
import flank.common.logLn
import ftl.api.fetchDeviceModelIos
import ftl.args.yml.Type
import ftl.ios.IosCatalog
import ftl.ios.IosCatalog.getSupportedVersionId
import ftl.client.google.IosCatalog
import ftl.client.google.IosCatalog.getSupportedVersionId
import ftl.ios.xctest.XcTestRunData
import ftl.ios.xctest.common.XcTestRunVersion
import ftl.ios.xctest.common.mapToRegex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ object AndroidCatalog {

fun supportedOrientations(projectId: String): List<Orientation> = deviceCatalog(projectId).runtimeConfiguration.orientations

fun localesAsTable(projectId: String) = getLocales(projectId).asPrintableTable()
private fun localesAsTable(projectId: String) = getLocales(projectId).asPrintableTable()

fun getLocaleDescription(projectId: String, locale: String) = getLocales(projectId).getLocaleDescription(locale)
private fun getLocaleDescription(projectId: String, locale: String) = getLocales(projectId).getLocaleDescription(locale)

private fun getLocales(projectId: String) = deviceCatalog(projectId).runtimeConfiguration.locales
internal fun getLocales(projectId: String) = deviceCatalog(projectId).runtimeConfiguration.locales

fun androidModelIds(projectId: String) =
modelMap.getOrPut(projectId) { deviceCatalog(projectId).models.map { it.id } }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ftl.ios
package ftl.client.google

import com.google.testing.model.IosDeviceCatalog
import com.google.testing.model.IosModel
Expand Down Expand Up @@ -29,11 +29,11 @@ object IosCatalog {

private fun getVersionsList(projectId: String) = iosDeviceCatalog(projectId).versions

fun localesAsTable(projectId: String) = iosDeviceCatalog(projectId).runtimeConfiguration.locales.asPrintableTable()
private fun localesAsTable(projectId: String) = iosDeviceCatalog(projectId).runtimeConfiguration.locales.asPrintableTable()

fun getLocaleDescription(projectId: String, locale: String) = getLocales(projectId).getLocaleDescription(locale)
private fun getLocaleDescription(projectId: String, locale: String) = getLocales(projectId).getLocaleDescription(locale)

private fun getLocales(projectId: String) = iosDeviceCatalog(projectId).runtimeConfiguration.locales
internal fun getLocales(projectId: String) = iosDeviceCatalog(projectId).runtimeConfiguration.locales

fun supportedOrientations(projectId: String): List<Orientation> =
iosDeviceCatalog(projectId).runtimeConfiguration.orientations
Expand Down
14 changes: 12 additions & 2 deletions test_runner/src/main/kotlin/ftl/domain/DescribeAndroidLocales.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ftl.domain

import flank.common.log
import ftl.adapter.google.getLocaleDescription
import ftl.api.Locale
import ftl.api.Platform
import ftl.api.fetchLocales
import ftl.args.AndroidArgs
import ftl.client.google.AndroidCatalog
import ftl.run.exception.FlankConfigurationError
import java.nio.file.Paths

Expand All @@ -13,5 +16,12 @@ interface DescribeAndroidLocales {

fun DescribeAndroidLocales.invoke() {
if (locale.isBlank()) throw FlankConfigurationError("Argument LOCALE must be specified.")
log(AndroidCatalog.getLocaleDescription(AndroidArgs.loadOrDefault(Paths.get(configPath)).project, locale))
log(
fetchLocales(
Locale.Identity(
AndroidArgs.loadOrDefault(Paths.get(configPath)).project,
Platform.ANDROID
)
).getLocaleDescription(locale)
)
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package ftl.domain

import flank.common.logLn
import ftl.adapter.google.asPrintableTable
import ftl.api.Locale.Identity
import ftl.api.Platform
import ftl.api.fetchAndroidOsVersion
import ftl.api.fetchDeviceModelAndroid
import ftl.api.fetchIpBlocks
import ftl.api.fetchLocales
import ftl.api.fetchNetworkProfiles
import ftl.api.fetchOrientation
import ftl.api.fetchSoftwareCatalog
import ftl.args.AndroidArgs
import ftl.client.google.AndroidCatalog
import ftl.environment.android.toCliTable
import ftl.environment.common.toCliTable
import ftl.environment.toCliTable
Expand All @@ -24,7 +26,7 @@ fun DescribeAndroidTestEnvironment.invoke() {
// TODO move toCliTable() to presentation layer during refactor of presentation after #1728
logLn(fetchAndroidOsVersion(projectId).toCliTable())
logLn(fetchDeviceModelAndroid(projectId).toCliTable()) // TODO move toCliTable() to presentation layer during refactor of presentation after #1728
logLn(AndroidCatalog.localesAsTable(projectId))
logLn(fetchLocales(Identity(projectId, Platform.ANDROID)).asPrintableTable())
logLn(fetchSoftwareCatalog().toCliTable())
logLn(fetchNetworkProfiles().toCliTable())
logLn(fetchOrientation(projectId, Platform.ANDROID).toCliTable()) // TODO move toCliTable() to presentation layer during refactor of presentation after #1728
Expand Down
9 changes: 7 additions & 2 deletions test_runner/src/main/kotlin/ftl/domain/DescribeIosLocales.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package ftl.domain

import flank.common.log
import ftl.adapter.google.getLocaleDescription
import ftl.api.Locale.Identity
import ftl.api.Platform
import ftl.api.fetchLocales
import ftl.args.IosArgs
import ftl.ios.IosCatalog
import ftl.run.exception.FlankConfigurationError
import java.nio.file.Paths

Expand All @@ -13,5 +16,7 @@ interface DescribeIosLocales {

fun DescribeIosLocales.invoke() {
if (locale.isBlank()) throw FlankConfigurationError("Argument LOCALE must be specified.")
log(IosCatalog.getLocaleDescription(IosArgs.loadOrDefault(Paths.get(configPath)).project, locale))
log(
fetchLocales(Identity(IosArgs.loadOrDefault(Paths.get(configPath)).project, Platform.IOS)).getLocaleDescription(locale)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ftl.domain
import flank.common.logLn
import ftl.api.fetchDeviceModelIos
import ftl.args.IosArgs
import ftl.environment.android.getDescription
import ftl.environment.ios.getDescription
import ftl.run.exception.FlankConfigurationError
import java.nio.file.Paths
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package ftl.domain

import flank.common.logLn
import ftl.adapter.google.asPrintableTable
import ftl.api.Locale.Identity
import ftl.api.Platform
import ftl.api.fetchDeviceModelIos
import ftl.api.fetchIpBlocks
import ftl.api.fetchLocales
import ftl.api.fetchNetworkProfiles
import ftl.api.fetchOrientation
import ftl.api.fetchSoftwareCatalog
import ftl.args.IosArgs
import ftl.client.google.IosCatalog
import ftl.environment.common.toCliTable
import ftl.environment.ios.toCliTable
import ftl.environment.toCliTable
import ftl.ios.IosCatalog
import java.nio.file.Paths

interface DescribeIosTestEnvironment {
Expand All @@ -22,7 +25,7 @@ operator fun DescribeIosTestEnvironment.invoke() {
val projectId = IosArgs.loadOrDefault(Paths.get(configPath)).project
logLn(fetchDeviceModelIos(projectId).toCliTable()) // TODO move toCliTable() and printing presentation layer during refactor of presentation after #1728
logLn(IosCatalog.softwareVersionsAsTable(projectId))
logLn(IosCatalog.localesAsTable(projectId))
logLn(fetchLocales(Identity(projectId, Platform.IOS)).asPrintableTable())
logLn(fetchSoftwareCatalog().toCliTable())
logLn(fetchNetworkProfiles().toCliTable())
logLn(fetchOrientation(projectId, Platform.IOS).toCliTable()) // TODO move toCliTable() and printing presentation layer during refactor of presentation after #1728
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ftl.domain

import flank.common.logLn
import ftl.args.IosArgs
import ftl.ios.IosCatalog
import ftl.client.google.IosCatalog
import ftl.run.exception.FlankConfigurationError
import java.nio.file.Paths

Expand Down
9 changes: 7 additions & 2 deletions test_runner/src/main/kotlin/ftl/domain/ListAndroidLocales.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package ftl.domain

import flank.common.logLn
import ftl.adapter.google.asPrintableTable
import ftl.api.Locale.Identity
import ftl.api.Platform
import ftl.api.fetchLocales
import ftl.args.AndroidArgs
import ftl.client.google.AndroidCatalog
import java.nio.file.Paths

interface ListAndroidLocales {
val configPath: String
}

operator fun ListAndroidLocales.invoke() {
logLn(AndroidCatalog.localesAsTable(projectId = AndroidArgs.loadOrDefault(Paths.get(configPath)).project))
logLn(
fetchLocales(Identity(AndroidArgs.loadOrDefault(Paths.get(configPath)).project, Platform.ANDROID)).asPrintableTable()
)
}
7 changes: 5 additions & 2 deletions test_runner/src/main/kotlin/ftl/domain/ListIosLocales.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package ftl.domain

import flank.common.logLn
import ftl.adapter.google.asPrintableTable
import ftl.api.Locale.Identity
import ftl.api.Platform
import ftl.api.fetchLocales
import ftl.args.IosArgs
import ftl.ios.IosCatalog
import java.nio.file.Paths

interface ListIosLocales {
val configPath: String
}

operator fun ListIosLocales.invoke() {
logLn(IosCatalog.localesAsTable(projectId = IosArgs.loadOrDefault(Paths.get(configPath)).project))
logLn(fetchLocales(Identity(IosArgs.loadOrDefault(Paths.get(configPath)).project, Platform.IOS)).asPrintableTable())
}
2 changes: 1 addition & 1 deletion test_runner/src/main/kotlin/ftl/domain/ListIosVersions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ftl.domain

import flank.common.logLn
import ftl.args.IosArgs
import ftl.ios.IosCatalog
import ftl.client.google.IosCatalog
import java.nio.file.Paths

interface ListIosVersions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package ftl.android

import com.google.common.truth.Truth.assertThat
import com.google.testing.model.AndroidDevice
import ftl.adapter.google.asPrintableTable
import ftl.api.Locale
import ftl.api.Platform
import ftl.api.fetchAndroidOsVersion
import ftl.api.fetchDeviceModelAndroid
import ftl.api.fetchLocales
import ftl.client.google.AndroidCatalog
import ftl.environment.android.toCliTable
import ftl.test.util.FlankTestRunner
Expand Down Expand Up @@ -110,7 +114,7 @@ class AndroidCatalogTest {
val expectedSeparatorCount = expectedHeaders.size + 1

// when
val devicesTable = AndroidCatalog.localesAsTable(projectId)
val devicesTable = fetchLocales(Locale.Identity(projectId, Platform.ANDROID)).asPrintableTable()
val headers = devicesTable.lines()[1]

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class AndroidLocalesDescribeCommandTest {
fun `should execute AndroidCatalog getLocaleDescription when run AndroidLocalesDescribeCommand`() {
mockkObject(AndroidCatalog) {
CommandLine(AndroidLocalesDescribeCommand()).execute("pl")
verify { AndroidCatalog.getLocaleDescription(any(), any()) }
verify { AndroidCatalog.getLocales(any()) }
}
}

Expand Down
Loading