Skip to content

Commit

Permalink
Use default JDK for running tests on CI
Browse files Browse the repository at this point in the history
  • Loading branch information
osipxd committed Sep 24, 2024
1 parent f431d2c commit 689782f
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 73 deletions.
31 changes: 16 additions & 15 deletions buildSrc/src/main/kotlin/JvmConfig.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import org.gradle.api.*
Expand All @@ -10,7 +10,7 @@ import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.*

fun Project.configureJvm() {
val jdk = when (name) {
val compileJdk = when (name) {
in jdk11Modules -> 11
else -> 8
}
Expand All @@ -23,10 +23,10 @@ fun Project.configureJvm() {
sourceSets.apply {
val jvmMain by getting {
dependencies {
if (jdk > 6) {
if (compileJdk > 6) {
api("org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}")
}
if (jdk > 7) {
if (compileJdk > 7) {
api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Versions.kotlin}")
api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:${Versions.coroutines}") {
exclude(module = "kotlin-stdlib")
Expand Down Expand Up @@ -66,12 +66,12 @@ fun Project.configureJvm() {
}
}

val jvmTest: KotlinJvmTest = tasks.getByName<KotlinJvmTest>("jvmTest") {
val jvmTest = tasks.getByName<KotlinJvmTest>("jvmTest") {
ignoreFailures = true
maxHeapSize = "2g"
exclude("**/*StressTest*")
useJUnitPlatform()
configureJavaLauncher(jdk)
configureJavaToolchain(compileJdk)
}

tasks.create<Test>("stressTest") {
Expand All @@ -85,7 +85,7 @@ fun Project.configureJvm() {
systemProperty("enable.stress.tests", "true")
include("**/*StressTest*")
useJUnitPlatform()
configureJavaLauncher(jdk)
configureJavaToolchain(compileJdk)
}

tasks.getByName<Jar>("jvmJar").apply {
Expand All @@ -101,15 +101,16 @@ fun Project.configureJvm() {
}

/**
* JUnit 5 requires Java 11+
* On local machine use for tests the JDK used for compilation.
* On CI use the default JDK.
*/
fun Test.configureJavaLauncher(jdk: Int) {
if (jdk < 11) {
val javaToolchains = project.extensions.getByType<JavaToolchainService>()
val customLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of("11")
}
javaLauncher = customLauncher
private fun Test.configureJavaToolchain(compileJdk: Int) {
// JUnit 5 requires JDK 11+
val testJdk = (if (CI) currentJdk else compileJdk).coerceAtLeast(11)
val javaToolchains = project.the<JavaToolchainService>()

javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(testJdk)
}
}

Expand Down
34 changes: 4 additions & 30 deletions buildSrc/src/main/kotlin/KtorBuildProperties.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import org.gradle.api.*
import org.gradle.api.tasks.testing.*
import org.gradle.jvm.toolchain.*
import org.gradle.kotlin.dsl.*

/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

private val java_version: String = System.getProperty("java.version", "8.0.0")

private val versionComponents = java_version
.split(".")
.take(2)
.filter { it.isNotBlank() }
.map { Integer.parseInt(it) }

val IDEA_ACTIVE: Boolean = System.getProperty("idea.active") == "true"

val OS_NAME = System.getProperty("os.name").lowercase()

val CI = System.getenv("TEAMCITY_VERSION") != null

val HOST_NAME = when {
OS_NAME.startsWith("linux") -> "linux"
OS_NAME.startsWith("windows") -> "windows"
OS_NAME.startsWith("mac") -> "macos"
else -> error("Unknown os name `$OS_NAME`")
}

val currentJdk = if (versionComponents[0] == 1) versionComponents[1] else versionComponents[0]
val currentJdk = JavaVersion.current().majorVersion.toInt()

val jdk11Modules = listOf(
"ktor-client-java",
Expand All @@ -40,16 +27,3 @@ val jdk11Modules = listOf(
"ktor-server-jetty-test-http2-jakarta",
"ktor-server-tomcat-jakarta",
)

fun Project.useJdkVersionForJvmTests(version: Int) {
tasks.getByName("jvmTest").apply {
check(this is Test)

val javaToolchains = project.extensions.getByType<JavaToolchainService>()
javaLauncher.set(
javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(version))
}
)
}
}
4 changes: 0 additions & 4 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import org.gradle.api.*
import org.tomlj.*

/*
* Copyright 2014-2023 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

/**
* A specific set of versions, read from gradle/libs.versions.toml.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import test.server.*

/*
Expand All @@ -7,5 +11,3 @@ import test.server.*
description = "Ktor client Auth support"

apply<TestServerPlugin>()

useJdkVersionForJvmTests(11)
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/*
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

description = "Ktor client Byte Order Mark support"

useJdkVersionForJvmTests(11)
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

useJdkVersionForJvmTests(11)

apply<test.server.TestServerPlugin>()

kotlin.sourceSets {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

description = "Ktor client JSON support"

Expand Down Expand Up @@ -29,5 +29,3 @@ kotlin {
}
}
}

useJdkVersionForJvmTests(11)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

kotlin.sourceSets {
commonTest {
Expand All @@ -9,5 +9,3 @@ kotlin.sourceSets {
}
}
}

useJdkVersionForJvmTests(11)
12 changes: 4 additions & 8 deletions ktor-client/ktor-client-tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test.server.*

/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import test.server.*

description = "Common tests for client"

Expand All @@ -12,8 +12,6 @@ plugins {

apply<TestServerPlugin>()

val osName = System.getProperty("os.name")

kotlin.sourceSets {
commonMain {
dependencies {
Expand Down Expand Up @@ -101,5 +99,3 @@ kotlin.sourceSets {
}
}
}

useJdkVersionForJvmTests(11)

0 comments on commit 689782f

Please sign in to comment.