From 6669b93c6aec1ad676ad5f6ac6e1cb0119253edc Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Mon, 14 Apr 2025 11:36:21 +0200 Subject: [PATCH 1/2] Relax Kotlin contracts for `Executable` parameters Since `Executable` is allowed/expected to throw exceptions, the semantics of being called `EXACTLY_ONCE` are blurry because it might not be executed entirely when throwing an exception. Since the Kotlin 2.1 compiler emits a warning for these parameters, we're relaxing their contracts to be called `AT_MOST_ONCE` instead. --- .../kotlin/org/junit/jupiter/api/Assertions.kt | 14 +++++++------- .../api/KotlinAssertTimeoutAssertionsTests.kt | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt index 44c13138a03d..a3dcbce59eb9 100644 --- a/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt +++ b/junit-jupiter-api/src/main/kotlin/org/junit/jupiter/api/Assertions.kt @@ -351,7 +351,7 @@ inline fun assertThrows( @API(status = STABLE, since = "5.11") inline fun assertDoesNotThrow(executable: () -> R): R { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) } return Assertions.assertDoesNotThrow(evaluateAndWrap(executable)) @@ -374,7 +374,7 @@ inline fun assertDoesNotThrow( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) } return assertDoesNotThrow({ message }, executable) @@ -397,7 +397,7 @@ inline fun assertDoesNotThrow( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) callsInPlace(message, AT_MOST_ONCE) } @@ -411,7 +411,7 @@ inline fun assertDoesNotThrow( @PublishedApi internal inline fun evaluateAndWrap(executable: () -> R): ThrowingSupplier { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) } return try { @@ -439,7 +439,7 @@ fun assertTimeout( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) } return Assertions.assertTimeout(timeout, executable) @@ -463,7 +463,7 @@ fun assertTimeout( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) } return Assertions.assertTimeout(timeout, executable, message) @@ -487,7 +487,7 @@ fun assertTimeout( executable: () -> R ): R { contract { - callsInPlace(executable, EXACTLY_ONCE) + callsInPlace(executable, AT_MOST_ONCE) callsInPlace(message, AT_MOST_ONCE) } diff --git a/jupiter-tests/src/test/kotlin/org/junit/jupiter/api/KotlinAssertTimeoutAssertionsTests.kt b/jupiter-tests/src/test/kotlin/org/junit/jupiter/api/KotlinAssertTimeoutAssertionsTests.kt index a10d58420c8d..9fad8fd659bb 100644 --- a/jupiter-tests/src/test/kotlin/org/junit/jupiter/api/KotlinAssertTimeoutAssertionsTests.kt +++ b/jupiter-tests/src/test/kotlin/org/junit/jupiter/api/KotlinAssertTimeoutAssertionsTests.kt @@ -156,8 +156,8 @@ internal class KotlinAssertTimeoutAssertionsTests { } @Test - fun `assertTimeout with value initialization in lambda`() { - val value: Int + fun `assertTimeout with value assignment in lambda`() { + var value = 0 assertTimeout(ofMillis(500)) { value = 10 } @@ -165,8 +165,8 @@ internal class KotlinAssertTimeoutAssertionsTests { } @Test - fun `assertTimeout with message and value initialization in lambda`() { - val value: Int + fun `assertTimeout with message and value assignment in lambda`() { + var value = 0 assertTimeout(ofMillis(500), "message") { value = 10 } @@ -174,8 +174,8 @@ internal class KotlinAssertTimeoutAssertionsTests { } @Test - fun `assertTimeout with message supplier and value initialization in lambda`() { - val value: Int + fun `assertTimeout with message supplier and value assignment in lambda`() { + var value = 0 @Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE") val valueInMessageSupplier: Int From ab26f26dd0000cefbcf83b558b0f8bc8426ecc24 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Mon, 14 Apr 2025 11:37:03 +0200 Subject: [PATCH 2/2] Treat Kotlin compiler warnings as errors again Now that #4371 is resolved. --- junit-jupiter-api/junit-jupiter-api.gradle.kts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/junit-jupiter-api/junit-jupiter-api.gradle.kts b/junit-jupiter-api/junit-jupiter-api.gradle.kts index 7932c998c884..402b5323eb57 100644 --- a/junit-jupiter-api/junit-jupiter-api.gradle.kts +++ b/junit-jupiter-api/junit-jupiter-api.gradle.kts @@ -22,10 +22,6 @@ dependencies { } tasks { - compileKotlin { - // https://github.com/junit-team/junit5/issues/4371 - compilerOptions.allWarningsAsErrors = false - } jar { bundle { val version = project.version