Skip to content

Commit

Permalink
Another round of refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelpasterz committed Oct 1, 2020
1 parent cd22c9b commit 65213bd
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 38 deletions.
1 change: 0 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ fun org.gradle.api.publish.maven.MavenPom.configureForFladle(pluginName: String)
tasks.withType(Test::class.java).configureEach {
// Test fixtures are stored in here so we should re-run tests if the test projects change.
inputs.dir("src/test/resources")
maxParallelForks = Runtime.getRuntime().availableProcessors()
testLogging {
events = setOf(TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.PASSED)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ data class FladleConfigImpl(
override val legacyJunitResult: Property<Boolean>,
override val fullJunitResult: Property<Boolean>
) : FladleConfig {
fun sanityRoboRun() {
/**
* Prepare config to run sanity robo.
*
* Sets [sanityRobo] property as `true`.
*
* Cleans [instrumentationApk], [additionalTestApks], [roboDirectives], [roboScript] properties.
*/
fun clearPropertiesForSanityRobo() {
sanityRobo.set(true)
additionalTestApks.empty()
instrumentationApk.set("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,25 @@ import org.gradle.api.provider.Property

@Throws(GradleException::class)
fun checkIfSanityAndValidateConfigs(config: FladleConfig) = when (config) {
is FlankGradleExtension -> config.checkAndValidateExtension()
is FladleConfigImpl -> config.checkAndValidateConfig()
else -> throw GradleException("Unable to check for sanity, check config type")
}

private fun FlankGradleExtension.checkAndValidateExtension() {
if (sanityRobo.getOrElse(false)) when {
instrumentationApk.isNotPresentOrBlank -> throwBaseConfigError("instrumentationApk")
additionalTestApks.isNotPresentOrEmpty -> throwBaseConfigError("additionalTestApks")
roboDirectives.isNotPresentOrEmpty -> throwBaseConfigError("roboDirectives")
roboScript.isNotPresentOrBlank -> throwBaseConfigError("roboScript")
is FlankGradleExtension -> config.checkAndValidateConfig() { option, _ ->
"Incorrect [base] configuration. [$option] can't be used together with sanityRobo."
}
is FladleConfigImpl -> config.checkAndValidateConfig(config.name) { option, name ->
"Incorrect [$name] configuration. [$option] can't be used together with sanityRobo. " +
"If you want to launch robo test run without robo script place only clearPropertiesForSanityRobo() into [$name] configuration"
}
else -> throw GradleException("Unable to check for sanity, check config type")
}

private fun FladleConfigImpl.checkAndValidateConfig() {
private fun FladleConfig.checkAndValidateConfig(name: String = "base", message: (String, String) -> String) {
if (sanityRobo.getOrElse(false)) when {
roboDirectives.isNotPresentOrEmpty -> throwAdditionalConfigError("roboDirectives", name)
roboScript.isNotPresentOrBlank -> throwAdditionalConfigError("roboScript", name)
instrumentationApk.isNotPresentOrBlank -> throwAdditionalConfigError("instrumentationApk", name)
additionalTestApks.isNotPresentOrEmpty -> throwAdditionalConfigError("additionalTestApks", name)
roboDirectives.isNotPresentOrEmpty -> throw GradleException(message("roboDirectives", name))
roboScript.isNotPresentOrBlank -> throw GradleException(message("roboScript", name))
instrumentationApk.isNotPresentOrBlank -> throw GradleException(message("instrumentationApk", name))
additionalTestApks.isNotPresentOrEmpty -> throw GradleException(message("additionalTestApks", name))
}
}

private fun throwBaseConfigError(option: String): Nothing =
throw GradleException("Incorrect [base] configuration. [$option] can't be used together with sanityRobo.")

private fun throwAdditionalConfigError(option: String, name: String): Nothing =
throw GradleException(
"Incorrect [$name] configuration. [$option] can't be used together with sanityRobo. " +
"If you want to launch robo test run without robo script place only sanityRoboRun() into [$name] configuration"
)

private val Property<String>.isNotPresentOrBlank
get() = orNull.isNullOrBlank().not()

Expand Down
12 changes: 6 additions & 6 deletions buildSrc/src/main/java/com/osacky/flank/gradle/YamlWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ internal class YamlWriter {
}

val shouldPrintTestAndRobo = config.sanityRobo.get().not()
val additionalProperties = writeAdditionalProperties(config, shouldPrintTestAndRobo)
val flankProperties = writeFlankProperties(config, shouldPrintTestAndRobo)
val additionalProperties = writeAdditionalProperties(config)
val flankProperties = writeFlankProperties(config)

return buildString {
appendln("gcloud:")
Expand All @@ -44,7 +44,7 @@ internal class YamlWriter {
}
}

internal fun writeFlankProperties(config: FladleConfig, printApk: Boolean = true): String = buildString {
internal fun writeFlankProperties(config: FladleConfig): String = buildString {
appendln("flank:")

appendProperty(config.testShards, name = "max-test-shards")
Expand All @@ -54,7 +54,7 @@ internal class YamlWriter {
appendProperty(config.projectId, name = "project")
appendProperty(config.keepFilePath, name = "keep-file-path")
appendListProperty(config.filesToDownload, name = "files-to-download") { appendln(" - $it") }
if (printApk)
if (!config.sanityRobo.get())
appendListProperty(config.additionalTestApks, name = "additional-app-test-apks") { appendln(" $it") }
appendProperty(config.runTimeout, name = "run-timeout")
appendProperty(config.ignoreFailedTests, name = "ignore-failed-tests")
Expand All @@ -67,7 +67,7 @@ internal class YamlWriter {
appendProperty(config.outputStyle, name = "output-style")
}

internal fun writeAdditionalProperties(config: FladleConfig, printRobo: Boolean = true): String = buildString {
internal fun writeAdditionalProperties(config: FladleConfig): String = buildString {
appendProperty(config.useOrchestrator, name = "use-orchestrator")
appendProperty(config.autoGoogleLogin, name = "auto-google-login")
appendProperty(config.recordVideo, name = "record-video")
Expand All @@ -87,7 +87,7 @@ internal class YamlWriter {
appendMapProperty(config.clientDetails, name = "client-details") { appendln(" ${it.key}: ${it.value}") }
appendMapProperty(config.otherFiles, name = "other-files") { appendln(" ${it.key}: ${it.value}") }
appendProperty(config.networkProfile, name = "network-profile")
if (printRobo) {
if (!config.sanityRobo.get()) {
appendProperty(config.roboScript, name = "robo-script")
appendListProperty(config.roboDirectives, name = "robo-directives") {
val value = it.getOrElse(2) { "" }.let { stringValue -> if (stringValue.isBlank()) "\"\"" else stringValue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder

private fun baseConfigMessage(option: String) = "Incorrect [base] configuration. [$option] can't be used together with sanityRobo."
private fun additionalConfigMessage(option: String, name: String) = "Incorrect [$name] configuration. [$option] can't be used together with sanityRobo. If you want to launch robo test run without robo script place only sanityRoboRun() into [$name] configuration"
private fun additionalConfigMessage(option: String, name: String) = "Incorrect [$name] configuration. [$option] can't be used together with sanityRobo. If you want to launch robo test run without robo script place only clearPropertiesForSanityRobo() into [$name] configuration"

class SanityRoboTest {
@get:Rule
Expand All @@ -33,7 +33,6 @@ class SanityRoboTest {
| instrumentationApk = "test.apk"
| configs {
| sanity {
| sanityRobo.set(true)
| makeSanityRun()
| }
| }
Expand Down Expand Up @@ -171,7 +170,7 @@ class SanityRoboTest {
| ]
| configs {
| sanity {
| sanityRobo.set(true)
| clearPropertiesForSanityRobo()
| roboScript.set("path/to/script.json")
| }
| }
Expand Down Expand Up @@ -219,7 +218,7 @@ class SanityRoboTest {
| ]
| configs {
| orange {
| sanityRoboRun()
| clearPropertiesForSanityRobo()
| }
| }
|}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SanityWithAutoConfigureTest {
smartFlankGcsPath = "gs://test-lab-yr9w6qsdvy45q-iurp80dm95h8a/flank/test_app_android.xml"
configs {
sanity {
sanityRoboRun()
clearPropertiesForSanityRobo()
useOrchestrator.set(false)
testTargets.set(project.provider { [
"class com.osacky.flank.gradle.sample.ExampleInstrumentedTest#runAndFail"
Expand Down

0 comments on commit 65213bd

Please sign in to comment.