Repository: runningcode/fladle (kotlin)

Issue:
Support for Test Targets for shards
Please include the a property  in the fladle extension to configure Flank's `test-targets-for-shard` argument (added in https://github.com/Flank/flank/pull/1299).

Pull request:
Fixes #205

Patch:
diff --git a/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfig.kt b/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfig.kt
index df1d1d30..e9650e38 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfig.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfig.kt
@@ -318,4 +318,9 @@ interface FladleConfig {
   @get:Input
   @get:Optional
   val disableResultsUpload: Property<Boolean>
+
+  @get:SinceFlank("21.01.1")
+  @get:Input
+  @get:Optional
+  val testTargetsForShard: ListProperty<String>
 }
diff --git a/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfigImpl.kt b/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfigImpl.kt
index 014a0261..ade4176b 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfigImpl.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/FladleConfigImpl.kt
@@ -52,7 +52,8 @@ data class FladleConfigImpl(
   override val defaultTestTime: Property<Double>,
   override val useAverageTestTimeForNewTests: Property<Boolean>,
   override val defaultClassTestTime: Property<Double>,
-  override val disableResultsUpload: Property<Boolean>
+  override val disableResultsUpload: Property<Boolean>,
+  override val testTargetsForShard: ListProperty<String>
 ) : FladleConfig {
   /**
    * Prepare config to run sanity robo.
diff --git a/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradleExtension.kt b/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradleExtension.kt
index 7b62eb51..334e189e 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradleExtension.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradleExtension.kt
@@ -117,6 +117,9 @@ open class FlankGradleExtension @Inject constructor(objects: ObjectFactory) : Fl
   override val defaultClassTestTime: Property<Double> = objects.property()
 
   override val disableResultsUpload: Property<Boolean> = objects.property()
+
+  override val testTargetsForShard: ListProperty<String> = objects.listProperty()
+
   @Internal
   val configs: NamedDomainObjectContainer<FladleConfigImpl> = objects.domainObjectContainer(FladleConfigImpl::class.java) {
     FladleConfigImpl(
@@ -166,7 +169,8 @@ open class FlankGradleExtension @Inject constructor(objects: ObjectFactory) : Fl
       useAverageTestTimeForNewTests = objects.property<Boolean>().convention(useAverageTestTimeForNewTests),
       defaultTestTime = objects.property<Double>().convention(defaultTestTime),
       defaultClassTestTime = objects.property<Double>().convention(defaultClassTestTime),
-      disableResultsUpload = objects.property<Boolean>().convention(disableResultsUpload)
+      disableResultsUpload = objects.property<Boolean>().convention(disableResultsUpload),
+      testTargetsForShard = objects.listProperty<String>().convention(testTargetsForShard)
     )
   }
 
diff --git a/buildSrc/src/main/java/com/osacky/flank/gradle/YamlWriter.kt b/buildSrc/src/main/java/com/osacky/flank/gradle/YamlWriter.kt
index 96802441..92665ff9 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/YamlWriter.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/YamlWriter.kt
@@ -52,8 +52,9 @@ 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 (!config.sanityRobo.get())
+    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")
     appendProperty(config.disableSharding, name = "disable-sharding")
@@ -67,6 +68,9 @@ internal class YamlWriter {
     appendProperty(config.defaultClassTestTime, name = "default-class-test-time")
     appendProperty(config.useAverageTestTimeForNewTests, name = "use-average-test-time-for-new-tests")
     appendProperty(config.disableResultsUpload, name = "disable-results-upload")
+    appendListProperty(config.testTargetsForShard, name = "test-targets-for-shard") {
+      appendln("    - $it")
+    }
   }
 
   internal fun writeAdditionalProperties(config: FladleConfig): String = buildString {
diff --git a/buildSrc/src/main/java/com/osacky/flank/gradle/validation/ValidateOptions.kt b/buildSrc/src/main/java/com/osacky/flank/gradle/validation/ValidateOptions.kt
index a5dc3df6..f0d7a497 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/validation/ValidateOptions.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/validation/ValidateOptions.kt
@@ -13,7 +13,7 @@ fun validateOptionsUsed(config: FladleConfig, flank: String) = config::class.mem
     when (val prop = it.call(config)) {
       is Property<*> -> prop.isPresent
       is MapProperty<*, *> -> prop.isPresent
-      is ListProperty<*> -> prop.isPresent
+      is ListProperty<*> -> prop.isPresent && prop.get().isNotEmpty()
       else -> false
     }
   }
diff --git a/docs/changelog.md b/docs/changelog.md
index 6e7fe5ce..e1ca28eb 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -2,6 +2,7 @@
 
 ## Unreleased
 * Bump Flank version to 21.01.1
+* Add support for test-targets-for-shard [Fixes #205](https://github.com/runningcode/fladle/issues/205)
 
 ## 0.13.1
 * Fix flankAuth task throwing exception. [Fixes #195](https://github.com/runningcode/fladle/issues/195)
diff --git a/sample/build.gradle b/sample/build.gradle
index 281ca03d..20b38470 100644
--- a/sample/build.gradle
+++ b/sample/build.gradle
@@ -18,7 +18,7 @@ android {
 }
 
 fladle {
-    flankVersion = "20.08.4"
+    flankVersion = "21.1.1"
     serviceAccountCredentials = project.layout.projectDirectory.file("flank-gradle-5cf02dc90531.json")
     // Project Id is not needed if serviceAccountCredentials are set.
 //    projectId("flank-gradle")


Test patch:
diff --git a/buildSrc/src/test/java/com/osacky/flank/gradle/YamlWriterTest.kt b/buildSrc/src/test/java/com/osacky/flank/gradle/YamlWriterTest.kt
index 67b72bc5..0914e83a 100644
--- a/buildSrc/src/test/java/com/osacky/flank/gradle/YamlWriterTest.kt
+++ b/buildSrc/src/test/java/com/osacky/flank/gradle/YamlWriterTest.kt
@@ -1173,6 +1173,24 @@ class YamlWriterTest {
     assertThat(properties).contains("disable-results-upload: true")
   }
 
+  @Test
+  fun writeTestTargetsForShard() {
+    val properties = emptyExtension {
+      testTargetsForShard.addAll(
+        "class com.example.test_app.bar.BarInstrumentedTest",
+        "class com.example.test_app.foo.FooInstrumentedTest"
+      )
+    }.toFlankProperties()
+
+    assertThat(properties).contains(
+      """
+      |  test-targets-for-shard:
+      |    - class com.example.test_app.bar.BarInstrumentedTest
+      |    - class com.example.test_app.foo.FooInstrumentedTest
+      """.trimMargin()
+    )
+  }
+
   private fun emptyExtension() = FlankGradleExtension(project.objects)
   private fun emptyExtension(block: FlankGradleExtension.() -> Unit) = emptyExtension().apply(block)
   private fun FlankGradleExtension.toFlankProperties() = yamlWriter.writeFlankProperties(this).trimIndent()
diff --git a/buildSrc/src/test/java/com/osacky/flank/gradle/validation/ValidateOptionsTest.kt b/buildSrc/src/test/java/com/osacky/flank/gradle/validation/ValidateOptionsTest.kt
index 8c792587..8e671bd2 100644
--- a/buildSrc/src/test/java/com/osacky/flank/gradle/validation/ValidateOptionsTest.kt
+++ b/buildSrc/src/test/java/com/osacky/flank/gradle/validation/ValidateOptionsTest.kt
@@ -40,6 +40,23 @@ class ValidateOptionsTest {
 
     validateOptionsUsed(config, "20.09.10")
   }
+
+  @Test
+  fun `ListProperty check also checks for empty list`() {
+    config.testTargetsForShard.set(listOf())
+    validateOptionsUsed(config, "20.09.10")
+  }
+
+  @Test
+  fun `ListProperty isPresent when set`() {
+    config.testTargetsForShard.set(listOf("class com.example.Bar"))
+    try {
+      validateOptionsUsed(config, "20.09.10")
+    } catch (e: IllegalStateException) {
+      assertThat(e).hasMessageThat().contains("Option testTargetsForShard is available since flank 21.1.1, which is higher than used 20.9.10")
+    }
+  }
+
   @Test
   fun `should throw an error when unavailable option used -- multi config`() {
     testProjectRoot.writeBuildDotGradle(