Skip to content

Commit

Permalink
Verify the minimum supported Kotlin version
Browse files Browse the repository at this point in the history
- store the minimal supported Gradle version in libs.versions.toml
- check that the README is up-to-date in the CheckReadmeTask
- add integration tests for checking the supported Kotlin version, and logged warning
  • Loading branch information
qurbonzoda committed Sep 10, 2024
1 parent 9729a3d commit 36fc347
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
`kotlinx-benchmark` is a toolkit for running benchmarks for multiplatform code written in Kotlin.
It is designed to work with Kotlin/JVM, Kotlin/JS, Kotlin/Native, and Kotlin/Wasm (experimental) targets.

To get started, ensure you're using Kotlin 1.9.20 or newer and Gradle 7.4 or newer.
To get started, ensure you're using Kotlin 2.0.0 or newer and Gradle 7.4 or newer.

## Features

Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ apiValidation {

val checkReadme by tasks.registering(CheckReadmeTask::class) {
minSupportedGradleVersion = libs.versions.minSupportedGradle
minSupportedKotlinVersion = libs.versions.minSupportedKotlin
readme = file("README.md")
}

Expand Down
17 changes: 11 additions & 6 deletions buildSrc/src/main/kotlin/tasks/CheckReadmeTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import org.gradle.api.tasks.PathSensitivity.*
abstract class CheckReadmeTask : DefaultTask() {
@get:Input
abstract val minSupportedGradleVersion: Property<String>
@get:Input
abstract val minSupportedKotlinVersion: Property<String>

@get:InputFile
@get:PathSensitive(RELATIVE)
Expand All @@ -20,23 +22,26 @@ abstract class CheckReadmeTask : DefaultTask() {
val readmeContents = readme.readText()

val minSupportedGradleVersion = minSupportedGradleVersion.get()
val minSupportedKotlinVersion = minSupportedKotlinVersion.get()

val matches = Regex("Gradle (?<version>[^ ]+) or newer").findAll(readmeContents).toList()
val matches = Regex("Kotlin (?<kotlinVersion>[^ ]+) or newer and Gradle (?<gradleVersion>[^ ]+) or newer")
.findAll(readmeContents).toList()

require(matches.size >= 1) {
"""
$readme does not contain correct min supported Gradle version.
$readme does not contain correct min supported Kotlin and Gradle versions.
${matches.size} matches found.
""".trimIndent()
}

matches.forEach { match ->
val version = match.groups["version"]?.value ?: error("Regex failed - could not find version")
require(minSupportedGradleVersion == version) {
val kotlinVersion = match.groups["kotlinVersion"]?.value ?: error("Regex failed - could not find kotlinVersion")
val gradleVersion = match.groups["gradleVersion"]?.value ?: error("Regex failed - could not find gradleVersion")
require(minSupportedKotlinVersion == kotlinVersion && minSupportedGradleVersion == gradleVersion) {
"""
$readme does not contain correct min supported Gradle version
$readme does not contain correct min supported Kotlin and Gradle versions.
Actual: ${match.value}
Expected: Gradle $minSupportedGradleVersion or newer
Expected: Kotlin $minSupportedKotlinVersion or newer and Gradle $minSupportedGradleVersion or newer
""".trimIndent()
}
}
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ jmh = "1.21"
gradle-pluginPublish = "0.21.0"

minSupportedGradle = "7.4"
minSupportedKotlin = "2.0.0"

[libraries]

Expand Down
1 change: 1 addition & 0 deletions integration/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ tasks.test {
systemProperty("kotlin_repo_url", rootProject.properties["kotlin_repo_url"])
systemProperty("kotlin_version", libs.versions.kotlin.get())
systemProperty("minSupportedGradleVersion", libs.versions.minSupportedGradle.get())
systemProperty("minSupportedKotlinVersion", libs.versions.minSupportedKotlin.get())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ enum class GradleTestVersion(val versionString: String) {
v8_0("8.0.2"),
MinSupportedGradleVersion("7.4"),
UnsupportedGradleVersion("7.3"),
MinSupportedKotlinVersion("2.0.0"),
UnsupportedKotlinVersion("1.9.20"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class Runner(
if (print) forwardStdOutput(System.out.bufferedWriter()) else this
}

fun run(vararg tasks: String, fn: BuildResult.() -> Unit) {
val gradle = gradle(*tasks)
@Suppress("UnstableApiUsage")
val buildResult = gradle.run()
buildResult.fn()
}

fun runAndSucceed(vararg tasks: String, fn: BuildResult.() -> Unit = {}) {
val gradle = gradle(*tasks)
val buildResult = gradle.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kotlinx.benchmark.integration

import kotlin.test.Test
import kotlin.test.assertEquals

class SupportedKotlinVersionTest : GradleTest() {

/** The minimum Kotlin version that kotlinx-benchmark plugin supports. */
private val minSupportedKotlinVersion = System.getProperty("minSupportedKotlinVersion")
private val warningMessage =
"JetBrains Gradle Benchmarks plugin requires Kotlin version ${GradleTestVersion.MinSupportedKotlinVersion.versionString}"

@Test
fun `test MinSupportedKotlinVersion matches the version used in build scripts`() {
assertEquals(minSupportedKotlinVersion, GradleTestVersion.MinSupportedKotlinVersion.versionString)
}

@Test
fun `when using min supported Kotlin version, expect no warning`() {
val runner = project("kotlin-multiplatform", kotlinVersion = GradleTestVersion.MinSupportedKotlinVersion.versionString)

runner.runAndSucceed(":help", "-q") {
assertOutputDoesNotContain(warningMessage)
}
}

@Test
fun `when using unsupported Gradle version, expect warning`() {
val runner = project("kotlin-multiplatform", kotlinVersion = GradleTestVersion.UnsupportedKotlinVersion.versionString)

runner.run(":help", "-q") {
assertOutputContains(warningMessage)
}
}
}
4 changes: 4 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ val generatePluginConstants by tasks.registering {
val minSupportedGradleVersion = libs.versions.minSupportedGradle
inputs.property("minSupportedGradleVersion", minSupportedGradleVersion)

val minSupportedKotlinVersion = libs.versions.minSupportedKotlin
inputs.property("minSupportedKotlinVersion", minSupportedKotlinVersion)

val kotlinCompilerVersion = libs.versions.kotlin
inputs.property("kotlinCompilerVersion", kotlinCompilerVersion)

Expand All @@ -133,6 +136,7 @@ val generatePluginConstants by tasks.registering {
|internal object BenchmarksPluginConstants {
| const val BENCHMARK_PLUGIN_VERSION = "${benchmarkPluginVersion.get()}"
| const val MIN_SUPPORTED_GRADLE_VERSION = "${minSupportedGradleVersion.get()}"
| const val MIN_SUPPORTED_KOTLIN_VERSION = "${minSupportedKotlinVersion.get()}"
| const val DEFAULT_KOTLIN_COMPILER_VERSION = "${kotlinCompilerVersion.get()}"
|}
|""".trimMargin()
Expand Down
5 changes: 3 additions & 2 deletions plugin/main/src/kotlinx/benchmark/gradle/BenchmarksPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kotlinx.benchmark.gradle.internal.BenchmarkDependencies
import kotlinx.benchmark.gradle.internal.BenchmarksPluginConstants
import kotlinx.benchmark.gradle.internal.BenchmarksPluginConstants.DEFAULT_KOTLIN_COMPILER_VERSION
import kotlinx.benchmark.gradle.internal.BenchmarksPluginConstants.MIN_SUPPORTED_GRADLE_VERSION
import kotlinx.benchmark.gradle.internal.BenchmarksPluginConstants.MIN_SUPPORTED_KOTLIN_VERSION
import kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi
import org.gradle.api.*
import org.gradle.api.provider.*
Expand Down Expand Up @@ -64,8 +65,8 @@ constructor(

plugins.withType(org.jetbrains.kotlin.gradle.plugin.KotlinBasePlugin::class.java) { kotlinPlugin ->
logger.info("Detected Kotlin plugin version '${kotlinPlugin.pluginVersion}'")
if (!getKotlinVersion(kotlinPlugin.pluginVersion).isAtLeast(1, 9, 20)) {
logger.error("JetBrains Gradle Benchmarks plugin requires Kotlin version 1.9.20 or higher")
if (getKotlinVersion(kotlinPlugin.pluginVersion) < getKotlinVersion(MIN_SUPPORTED_KOTLIN_VERSION)) {
logger.error("JetBrains Gradle Benchmarks plugin requires Kotlin version $MIN_SUPPORTED_KOTLIN_VERSION or higher")
}
extension.kotlinCompilerVersion.set(kotlinPlugin.pluginVersion)
}
Expand Down

0 comments on commit 36fc347

Please sign in to comment.