Repository: runningcode/fladle
Pull request #121 (2020-05-17 06:46:06)

Use ObjectFactory instead of injecting project
This avoids referencing the project inside the extension.﻿


Patch:
diff --git a/build.gradle b/build.gradle
index f2b3bf0..12d694c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -35,5 +35,5 @@ buildScan {
 
 wrapper {
     distributionType = Wrapper.DistributionType.ALL
-    gradleVersion = '6.4'
+    gradleVersion = '6.4.1'
 }
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 d089e90..7374f24 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradleExtension.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradleExtension.kt
@@ -1,20 +1,21 @@
 package com.osacky.flank.gradle
 
 import groovy.lang.Closure
+import javax.inject.Inject
 import org.gradle.api.NamedDomainObjectContainer
-import org.gradle.api.Project
 import org.gradle.api.file.RegularFileProperty
+import org.gradle.api.model.ObjectFactory
 import org.gradle.api.provider.ListProperty
 import org.gradle.api.provider.Property
 import org.gradle.kotlin.dsl.listProperty
 import org.gradle.kotlin.dsl.property
 
-open class FlankGradleExtension(project: Project) : FladleConfig {
-  val flankCoordinates: Property<String> = project.objects.property(String::class.java).convention("com.github.flank:flank")
-  val flankVersion: Property<String> = project.objects.property(String::class.java).convention("20.05.1")
+open class FlankGradleExtension @Inject constructor(objects: ObjectFactory) : FladleConfig {
+  val flankCoordinates: Property<String> = objects.property(String::class.java).convention("com.github.flank:flank")
+  val flankVersion: Property<String> = objects.property(String::class.java).convention("20.05.1")
   // Project id is automatically discovered by default. Use this to override the project id.
   override var projectId: String? = null
-  override val serviceAccountCredentials: RegularFileProperty = project.objects.fileProperty()
+  override val serviceAccountCredentials: RegularFileProperty = objects.fileProperty()
   override var useOrchestrator: Boolean = false
   override var autoGoogleLogin: Boolean = false
   override var devices: List<Map<String, String>> = listOf(mapOf("model" to "NexusLowRes", "version" to "28"))
@@ -34,13 +35,13 @@ open class FlankGradleExtension(project: Project) : FladleConfig {
   override var flakyTestAttempts = 0
 
   // Variant to use for configuring output APK.
-  var variant: Property<String> = project.objects.property()
+  var variant: Property<String> = objects.property()
 
   /**
    * debugApk and instrumentationApk are [Property<String>] and not [RegularFileProperty] because we support wildcard characters.
    */
-  val debugApk: Property<String> = project.objects.property()
-  val instrumentationApk: Property<String> = project.objects.property()
+  val debugApk: Property<String> = objects.property()
+  val instrumentationApk: Property<String> = objects.property()
 
   override var directoriesToPull: List<String> = emptyList()
 
@@ -60,11 +61,11 @@ open class FlankGradleExtension(project: Project) : FladleConfig {
 
   override var resultsDir: String? = null
 
-  override var additionalTestApks: ListProperty<String> = project.objects.listProperty()
+  override var additionalTestApks: ListProperty<String> = objects.listProperty()
 
-  override var runTimeout: Property<String> = project.objects.property()
+  override var runTimeout: Property<String> = objects.property()
 
-  override var ignoreFailedTests: Property<Boolean> = project.objects.property()
+  override var ignoreFailedTests: Property<Boolean> = objects.property()
 
   override var disableSharding: Boolean = false
 
@@ -72,7 +73,7 @@ open class FlankGradleExtension(project: Project) : FladleConfig {
 
   override var testRunnerClass: String? = null
 
-  override var localResultsDir: Property<String> = project.objects.property()
+  override var localResultsDir: Property<String> = objects.property()
 
   override var numUniformShards: Int? = null
 
@@ -88,7 +89,7 @@ open class FlankGradleExtension(project: Project) : FladleConfig {
 
   override var roboDirectives: List<List<String>> = emptyList()
 
-  val configs: NamedDomainObjectContainer<FladleConfigImpl> = project.container(FladleConfigImpl::class.java) {
+  val configs: NamedDomainObjectContainer<FladleConfigImpl> = objects.domainObjectContainer(FladleConfigImpl::class.java) {
     FladleConfigImpl(
       name = it,
       projectId = projectId,
diff --git a/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradlePlugin.kt b/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradlePlugin.kt
index f76cce7..250d5cc 100644
--- a/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradlePlugin.kt
+++ b/buildSrc/src/main/java/com/osacky/flank/gradle/FlankGradlePlugin.kt
@@ -8,6 +8,7 @@ import org.gradle.api.Project
 import org.gradle.api.artifacts.Configuration
 import org.gradle.api.tasks.JavaExec
 import org.gradle.api.tasks.TaskContainer
+import org.gradle.kotlin.dsl.create
 import org.gradle.util.GradleVersion
 
 class FlankGradlePlugin : Plugin<Project> {
@@ -18,7 +19,7 @@ class FlankGradlePlugin : Plugin<Project> {
     // Create Configuration to store flank dependency
     target.configurations.create(FLADLE_CONFIG)
 
-    val extension = target.extensions.create("fladle", FlankGradleExtension::class.java, target)
+    val extension = target.extensions.create<FlankGradleExtension>("fladle", target.objects)
 
     configureTasks(target, extension)
   }
@@ -143,7 +144,7 @@ class FlankGradlePlugin : Plugin<Project> {
     get() = configurations.getByName(FLADLE_CONFIG)
 
   companion object {
-    val GRADLE_MIN_VERSION: GradleVersion = GradleVersion.version("5.1")
+    val GRADLE_MIN_VERSION: GradleVersion = GradleVersion.version("5.4")
     const val TASK_GROUP = "fladle"
     const val FLADLE_CONFIG = "fladle"
     fun Project.log(message: String) {
diff --git a/docs/changelog.md b/docs/changelog.md
index 9eb5905..b8c4dee 100644
--- a/docs/changelog.md
+++ b/docs/changelog.md
@@ -7,6 +7,8 @@
 
 !!! Warning "Breaking API Change"
     [additionalTestApks](/fladle/configuration/#additionaltestapks) now uses ListProperty instead of the previous Map. This is to allow for lazy configuration of the provided files.
+!!! Warning
+    Minimum supported Gradle version is now 5.4.
 
 ## 0.9.4
 * Update [Flank to 20.05.1](https://github.com/Flank/flank/releases/tag/v20.05.0). Huge new release!
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index fd0c5a3..21e622d 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,5 @@
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.4-all.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists


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 00267f9..74e1385 100644
--- a/buildSrc/src/test/java/com/osacky/flank/gradle/YamlWriterTest.kt
+++ b/buildSrc/src/test/java/com/osacky/flank/gradle/YamlWriterTest.kt
@@ -123,7 +123,7 @@ class YamlWriterTest {
 
   @Test
   fun verifyMissingServiceThrowsError() {
-    val extension = FlankGradleExtension(project)
+    val extension = FlankGradleExtension(project.objects)
     try {
       yamlWriter.createConfigProps(extension, extension)
       fail()
@@ -583,7 +583,7 @@ class YamlWriterTest {
 
   @Test
   fun writeNoKeepFilePath() {
-    val extension = FlankGradleExtension(project)
+    val extension = emptyExtension()
 
     assertEquals("flank:\n" +
             "  keep-file-path: false\n" +
@@ -646,8 +646,8 @@ class YamlWriterTest {
 
   @Test
   fun verifyDefaultValues() {
-    val defaultFlankProperties = FlankGradleExtension(project).toFlankProperties()
-    val defaultAdditionalProperties = FlankGradleExtension(project).toAdditionalProperties().trimIndent()
+    val defaultFlankProperties = emptyExtension().toFlankProperties()
+    val defaultAdditionalProperties = emptyExtension().toAdditionalProperties().trimIndent()
 
     val expectedFlank = """
       flank:
@@ -819,7 +819,8 @@ class YamlWriterTest {
     """.trimMargin()))
   }
 
-  private fun emptyExtension(block: FlankGradleExtension.() -> Unit) = FlankGradleExtension(project).apply(block)
+  private fun emptyExtension() = FlankGradleExtension(project.objects)
+  private fun emptyExtension(block: FlankGradleExtension.() -> Unit) = emptyExtension().apply(block)
   private fun FlankGradleExtension.toFlankProperties() = yamlWriter.writeFlankProperties(this).trimIndent()
   private fun FlankGradleExtension.toAdditionalProperties() = yamlWriter.writeAdditionalProperties(this)
 }
diff --git a/buildSrc/src/test/java/com/osacky/flank/gradle/integration/FlankGradlePluginIntegrationTest.kt b/buildSrc/src/test/java/com/osacky/flank/gradle/integration/FlankGradlePluginIntegrationTest.kt
index 47d3bb8..c7e8d2a 100644
--- a/buildSrc/src/test/java/com/osacky/flank/gradle/integration/FlankGradlePluginIntegrationTest.kt
+++ b/buildSrc/src/test/java/com/osacky/flank/gradle/integration/FlankGradlePluginIntegrationTest.kt
@@ -9,61 +9,61 @@ import org.junit.rules.TemporaryFolder
 
 class FlankGradlePluginIntegrationTest {
 
-    @get:Rule
-    var testProjectRoot = TemporaryFolder()
+  @get:Rule
+  var testProjectRoot = TemporaryFolder()
 
-    fun writeBuildGradle(build: String) {
-        val file = testProjectRoot.newFile("build.gradle")
-        file.writeText(build)
-    }
+  val minSupportGradleVersion = "5.5"
+  val oldVersion = "5.3.1"
+
+  fun writeBuildGradle(build: String) {
+    val file = testProjectRoot.newFile("build.gradle")
+    file.writeText(build)
+  }
 
-    @Test
-    fun testLowGradleVersionFailsBuild() {
-        writeBuildGradle(
-            """plugins {
+  @Test
+  fun testLowGradleVersionFailsBuild() {
+    writeBuildGradle(
+        """plugins {
              |  id "com.osacky.fladle"
              |}""".trimMargin()
-        )
-        try {
-            GradleRunner.create()
-                .withProjectDir(testProjectRoot.root)
-                .withPluginClasspath()
-                .withGradleVersion("5.0")
-                .build()
-        } catch (expected: UnexpectedBuildFailure) {
-            assertThat(expected).hasMessageThat().contains("Fladle requires at minimum version Gradle 5.1. Detected version Gradle 5.0")
-        }
-    }
+    )
+    val result = GradleRunner.create()
+        .withProjectDir(testProjectRoot.root)
+        .withPluginClasspath()
+        .withGradleVersion(oldVersion)
+        .buildAndFail()
+    assertThat(result.output).contains("Fladle requires at minimum version Gradle 5.4. Detected version Gradle 5.3.1")
+  }
 
-    @Test
-    fun testGradleSixZero() {
-        writeBuildGradle(
-                """plugins {
+  @Test
+  fun testGradleSixZero() {
+    writeBuildGradle(
+        """plugins {
              |  id "com.osacky.fladle"
              |}""".trimMargin()
-        )
-        val result = GradleRunner.create()
-                .withProjectDir(testProjectRoot.root)
-                .withPluginClasspath()
-                .withGradleVersion("6.0")
-                .build()
+    )
+    val result = GradleRunner.create()
+        .withProjectDir(testProjectRoot.root)
+        .withPluginClasspath()
+        .withGradleVersion("6.0")
+        .build()
 
-        assertThat(result.output).contains("SUCCESS")
-    }
+    assertThat(result.output).contains("SUCCESS")
+  }
 
-    @Test
-    fun testMinSupportedGradleVersionWorks() {
-        writeBuildGradle(
-            """plugins {
+  @Test
+  fun testMinSupportedGradleVersionWorks() {
+    writeBuildGradle(
+        """plugins {
              |  id "com.osacky.fladle"
              |}""".trimMargin()
-        )
-        GradleRunner.create()
-            .withProjectDir(testProjectRoot.root)
-            .withPluginClasspath()
-            .withGradleVersion("5.1")
-            .build()
-    }
+    )
+    GradleRunner.create()
+        .withProjectDir(testProjectRoot.root)
+        .withPluginClasspath()
+        .withGradleVersion(minSupportGradleVersion)
+        .build()
+  }
 
   @Test
   fun testMissingServiceAccountWithProjectId() {
@@ -81,56 +81,52 @@ class FlankGradlePluginIntegrationTest {
     GradleRunner.create()
         .withProjectDir(testProjectRoot.root)
         .withPluginClasspath()
-        .withGradleVersion("5.3.1")
+        .withGradleVersion(minSupportGradleVersion)
         .withArguments("printYml")
         .build()
   }
 
-    @Test
-    fun testMissingServiceAccountFailsBuild() {
-        writeBuildGradle(
-            """plugins {
+  @Test
+  fun testMissingServiceAccountFailsBuild() {
+    writeBuildGradle(
+        """plugins {
              |  id "com.osacky.fladle"
              |}
              |
              |fladle {
              |  debugApk = "foo"
              |}""".trimMargin()
-        )
-        try {
-
-            GradleRunner.create()
-                .withProjectDir(testProjectRoot.root)
-                .withPluginClasspath()
-                .withGradleVersion("5.3.1")
-                .withArguments("printYml")
-                .build()
-        } catch (expected: UnexpectedBuildFailure) {
-            assertThat(expected).hasMessageThat().contains("ServiceAccountCredentials in fladle extension not set. https://github.com/runningcode/fladle#serviceaccountcredentials")
-        }
-    }
+    )
+    val result = GradleRunner.create()
+        .withProjectDir(testProjectRoot.root)
+        .withPluginClasspath()
+        .withGradleVersion(minSupportGradleVersion)
+        .withArguments("printYml")
+        .buildAndFail()
+    assertThat(result.output).contains("ServiceAccountCredentials in fladle extension not set. https://github.com/runningcode/fladle#serviceaccountcredentials")
+  }
 
-    @Test
-    fun testMissingApkFailsBuild() {
-        writeBuildGradle(
-            """plugins {
+  @Test
+  fun testMissingApkFailsBuild() {
+    writeBuildGradle(
+        """plugins {
              |  id "com.osacky.fladle"
              |}
              |fladle {
              |  serviceAccountCredentials = project.layout.projectDirectory.file("foo")
              |}
              |""".trimMargin()
-        )
-        try {
+    )
+    try {
 
-            GradleRunner.create()
-                .withProjectDir(testProjectRoot.root)
-                .withPluginClasspath()
-                .withGradleVersion("5.3.1")
-                .withArguments("printYml")
-                .build()
-        } catch (expected: UnexpectedBuildFailure) {
-            assertThat(expected).hasMessageThat().contains("debugApk must be specified")
-        }
+      GradleRunner.create()
+          .withProjectDir(testProjectRoot.root)
+          .withPluginClasspath()
+          .withGradleVersion(minSupportGradleVersion)
+          .withArguments("printYml")
+          .build()
+    } catch (expected: UnexpectedBuildFailure) {
+      assertThat(expected).hasMessageThat().contains("debugApk must be specified")
     }
+  }
 }