Repository: apple/pkl
File: buildSrc/src/main/kotlin/pklGradlePluginTest.gradle.kts (Kotlin)

Code before:
    "Candidate" ->
      task("compatibilityTestCandidate") {
        val versionInfo = GradleVersionInfo.fetchRc()
        if (versionInfo?.activeRc == true) {
          dependsOn(createCompatibilityTestTask(versionInfo))
        } else {
          doLast { println("No active Gradle release candidate available.") }
        }
      }
    // latest nightly
    "Nightly" ->
      task("compatibilityTestNightly") {
        val versionInfo = GradleVersionInfo.fetchNightly()
        dependsOn(createCompatibilityTestTask(versionInfo))
      }
    // explicit version
    else ->
      createCompatibilityTestTask(
        taskNameSuffix,
        "https://services.gradle.org/distributions-snapshots/gradle-$taskNameSuffix-bin.zip",
      )
  }
}

fun createCompatibilityTestTask(versionInfo: GradleVersionInfo): Task =
  createCompatibilityTestTask(versionInfo.version, versionInfo.downloadUrl)

fun createCompatibilityTestTask(version: String, downloadUrl: String): Task {
  return tasks.create("compatibilityTest$version", Test::class.java) {
    mustRunAfter(tasks.test)

    maxHeapSize = tasks.test.get().maxHeapSize
    jvmArgs = tasks.test.get().jvmArgs
    classpath = tasks.test.get().classpath
    systemProperty("testGradleVersion", version)
    systemProperty("testGradleDistributionUrl", downloadUrl)

    doFirst {
      if (version == gradle.gradleVersion && gradle.taskGraph.hasTask(tasks.test.get())) {
        // don't test same version twice
        println("This version has already been tested by the `test` task.")
        throw StopExecutionException()
      }
    }
  }
}


Review comment (suggestion):
Not sure how this cast can succeed, but, it's also not necessary; `dependsOn` can accept a `Provider<Task>`

```suggestion
  createCompatibilityTestTask(versionInfo.version, versionInfo.downloadUrl)
```

Code after:
    "Candidate" ->
      task("compatibilityTestCandidate") {
        val versionInfo = GradleVersionInfo.fetchRc()
        if (versionInfo?.activeRc == true) {
          dependsOn(createCompatibilityTestTask(versionInfo))
        } else {
          doLast { println("No active Gradle release candidate available.") }
        }
      }
    // latest nightly
    "Nightly" ->
      task("compatibilityTestNightly") {
        val versionInfo = GradleVersionInfo.fetchNightly()
        dependsOn(createCompatibilityTestTask(versionInfo))
      }
    // explicit version
    else ->
      createCompatibilityTestTask(
        taskNameSuffix,
        "https://services.gradle.org/distributions-snapshots/gradle-$taskNameSuffix-bin.zip",
      )
  }
}

fun createCompatibilityTestTask(versionInfo: GradleVersionInfo) =
  createCompatibilityTestTask(versionInfo.version, versionInfo.downloadUrl)

fun createCompatibilityTestTask(version: String, downloadUrl: String): TaskProvider<Test> {
  return tasks.register("compatibilityTest$version", Test::class.java) {
    mustRunAfter(tasks.test)

    maxHeapSize = tasks.test.get().maxHeapSize
    jvmArgs = tasks.test.get().jvmArgs
    classpath = tasks.test.get().classpath
    systemProperty("testGradleVersion", version)
    systemProperty("testGradleDistributionUrl", downloadUrl)

    doFirst {
      if (version == gradle.gradleVersion && gradle.taskGraph.hasTask(tasks.test.get())) {
        // don't test same version twice
        println("This version has already been tested by the `test` task.")
        throw StopExecutionException()
      }
    }
  }
}