Skip to content

Commit 4f79b5a

Browse files
authored
Update to Kotest 6.1 to align with updated API changes. (#559)
2 parents 79a0ad9 + 0260d52 commit 4f79b5a

File tree

19 files changed

+85
-44
lines changed

19 files changed

+85
-44
lines changed

jvm/example-kotest/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies {
1717
}
1818
tasks.test {
1919
useJUnitPlatform()
20+
systemProperty("kotest.framework.config.fqn", "com.example.kotest.KotestConfig")
2021
environment(properties.filter { it.key == "selfie" || it.key == "OPENAI_API_KEY" })
2122
inputs.files(fileTree("src/test") {
2223
include("**/*.ss")

jvm/example-kotest/src/test/kotlin/com/example/kotest/KotestConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package com.example.kotest
33
import com.diffplug.selfie.kotest.SelfieExtension
44

55
class KotestConfig : io.kotest.core.config.AbstractProjectConfig() {
6-
override fun extensions() = listOf(SelfieExtension(this))
6+
override val extensions = listOf(SelfieExtension(this))
77
}

jvm/gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ver_JUNIT_PIONEER=2.3.0
99
ver_OKIO=3.16.0
1010
ver_KOTLIN_TEST=2.0.0
1111
ver_KOTLIN_SERIALIZATION=1.9.0
12-
# Kotest 5.6.0 is the oldest that we support
13-
ver_KOTEST=5.6.0
12+
# Kotest 6.0.0 is the oldest that we support
13+
ver_KOTEST=6.1.4
1414

1515
ver_JVM_TARGET=11

jvm/selfie-runner-junit5/src/main/kotlin/com/diffplug/selfie/junit5/SelfieExtension.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 DiffPlug
2+
* Copyright (C) 2024-2026 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@ import io.kotest.core.listeners.BeforeSpecListener
2424
import io.kotest.core.listeners.FinalizeSpecListener
2525
import io.kotest.core.spec.Spec
2626
import io.kotest.core.test.TestCase
27-
import io.kotest.core.test.TestResult
27+
import io.kotest.engine.test.TestResult
2828
import kotlin.reflect.KClass
2929
import kotlinx.coroutines.currentCoroutineContext
3030
import kotlinx.coroutines.withContext
@@ -43,11 +43,12 @@ class SelfieExtension(projectConfig: AbstractProjectConfig) :
4343
execute: suspend (TestCase) -> TestResult
4444
): TestResult {
4545
val file = SnapshotSystemJUnit5.forClass(testCase.spec::class.java.name)
46-
val coroutineLocal = CoroutineDiskStorage(DiskStorageJUnit5(file, testCase.name.testName))
46+
val name = testCase.name.name
47+
val coroutineLocal = CoroutineDiskStorage(DiskStorageJUnit5(file, name))
4748
return withContext(currentCoroutineContext() + coroutineLocal) {
48-
file.startTest(testCase.name.testName, false)
49+
file.startTest(name, false)
4950
val result = execute(testCase)
50-
file.finishedTestWithSuccess(testCase.name.testName, false, result.isSuccess)
51+
file.finishedTestWithSuccess(name, false, result.isSuccess)
5152
result
5253
}
5354
}
@@ -58,8 +59,8 @@ class SelfieExtension(projectConfig: AbstractProjectConfig) :
5859
val file = SnapshotSystemJUnit5.forClass(kclass.java.name)
5960
results.entries.forEach {
6061
if (it.value.isIgnored) {
61-
file.startTest(it.key.name.testName, false)
62-
file.finishedTestWithSuccess(it.key.name.testName, false, false)
62+
file.startTest(it.key.name.name, false)
63+
file.finishedTestWithSuccess(it.key.name.name, false, false)
6364
}
6465
}
6566
SnapshotSystemJUnit5.forClass(kclass.java.name)

jvm/selfie-runner-kotest/src/commonMain/kotlin/com/diffplug/selfie/kotest/FSOkio.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 DiffPlug
2+
* Copyright (C) 2024-2026 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,18 +18,19 @@ package com.diffplug.selfie.kotest
1818
import com.diffplug.selfie.guts.FS
1919
import com.diffplug.selfie.guts.TypedPath
2020
import io.kotest.assertions.Actual
21-
import io.kotest.assertions.Exceptions
21+
import io.kotest.assertions.AssertionErrorBuilder
2222
import io.kotest.assertions.Expected
2323
import io.kotest.assertions.print.Printed
2424
import okio.FileSystem
2525
import okio.Path.Companion.toPath
2626

27-
expect internal val FS_SYSTEM: FileSystem
27+
internal expect val FS_SYSTEM: FileSystem
2828
internal fun TypedPath.toPath(): okio.Path = absolutePath.toPath()
2929

3030
internal object FSOkio : FS {
3131
override fun fileExists(typedPath: TypedPath): Boolean =
3232
FS_SYSTEM.metadataOrNull(typedPath.toPath())?.isRegularFile ?: false
33+
3334
/** Walks the files (not directories) which are children and grandchildren of the given path. */
3435
override fun <T> fileWalk(typedPath: TypedPath, walk: (Sequence<TypedPath>) -> T): T =
3536
walk(
@@ -40,9 +41,11 @@ internal object FSOkio : FS {
4041
FS_SYSTEM.read(typedPath.toPath()) { readByteArray() }
4142
override fun fileWriteBinary(typedPath: TypedPath, content: ByteArray): Unit =
4243
FS_SYSTEM.write(typedPath.toPath()) { write(content) }
44+
4345
/** Creates an assertion failed exception to throw. */
4446
override fun assertFailed(message: String, expected: Any?, actual: Any?): Throwable =
45-
if (expected == null && actual == null) Exceptions.createAssertionError(message, null)
47+
if (expected == null && actual == null)
48+
AssertionErrorBuilder.create().withMessage(message).build()
4649
else {
4750
val expectedStr = nullableToString(expected, "")
4851
val actualStr = nullableToString(actual, "")
@@ -55,10 +58,11 @@ internal object FSOkio : FS {
5558
comparisonAssertion(message, expectedStr, actualStr)
5659
}
5760
}
58-
private fun nullableToString(any: Any?, onNull: String): String =
59-
any?.let { it.toString() } ?: onNull
61+
private fun nullableToString(any: Any?, onNull: String): String = any?.toString() ?: onNull
6062
private fun comparisonAssertion(message: String, expected: String, actual: String): Throwable {
61-
return Exceptions.createAssertionError(
62-
message, null, Expected(Printed((expected))), Actual(Printed((actual))))
63+
return AssertionErrorBuilder.create()
64+
.withMessage(message)
65+
.withValues(Expected(Printed((expected))), Actual(Printed((actual))))
66+
.build()
6367
}
6468
}

jvm/selfie-runner-kotest/src/commonMain/kotlin/com/diffplug/selfie/kotest/SelfieExtension.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 DiffPlug
2+
* Copyright (C) 2024-2026 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,7 @@ import io.kotest.core.listeners.FinalizeSpecListener
2626
import io.kotest.core.source.SourceRef
2727
import io.kotest.core.spec.Spec
2828
import io.kotest.core.test.TestCase
29-
import io.kotest.core.test.TestResult
29+
import io.kotest.engine.test.TestResult
3030
import kotlin.jvm.JvmStatic
3131
import kotlin.reflect.KClass
3232
import kotlinx.coroutines.currentCoroutineContext
@@ -62,18 +62,19 @@ class SelfieExtension(
6262
val classOrFilename: String =
6363
when (val source = testCase.source) {
6464
is SourceRef.ClassSource -> source.fqn
65-
is SourceRef.FileSource -> source.fileName
65+
is SourceRef.ClassLineSource -> source.fqn
6666
is SourceRef.None -> TODO("Handle SourceRef.None")
6767
}
6868
return system.forClassOrFilename(classOrFilename)
6969
}
70+
7071
/** Called for every test method. */
7172
override suspend fun intercept(
7273
testCase: TestCase,
7374
execute: suspend (TestCase) -> TestResult
7475
): TestResult {
7576
val file = snapshotFileFor(testCase)
76-
val testName = testCase.name.testName
77+
val testName = testCase.name.name
7778
val coroutineLocal = CoroutineDiskStorage(DiskStorageKotest(file, testName))
7879
return withContext(currentCoroutineContext() + coroutineLocal) {
7980
file.startTest(testName)
@@ -89,8 +90,9 @@ class SelfieExtension(
8990
val file = results.keys.map { snapshotFileFor(it) }.firstOrNull() ?: return
9091
results.entries.forEach {
9192
if (it.value.isIgnored) {
92-
file.startTest(it.key.name.testName)
93-
file.finishedTestWithSuccess(it.key.name.testName, false)
93+
val name = it.key.name.name
94+
file.startTest(name)
95+
file.finishedTestWithSuccess(name, false)
9496
}
9597
}
9698
file.finishedClassWithSuccess(results.entries.all { it.value.isSuccess })

jvm/selfie-runner-kotest/src/commonTest/kotlin/com/diffplug/selfie/kotest/HarnessKotest.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023-2025 DiffPlug
2+
* Copyright (C) 2023-2026 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -177,13 +177,13 @@ open class HarnessKotest() : FunSpec() {
177177
fun content() = lines.subList(startInclusive, endInclusive + 1).joinToString("\n")
178178
fun setContent(mustBe: String) {
179179
FS_SYSTEM.write(subprojectFolder.resolve(subpath)) {
180-
for (i in 0 ..< startInclusive) {
180+
for (i in 0..<startInclusive) {
181181
writeUtf8(lines[i])
182182
writeUtf8("\n")
183183
}
184184
writeUtf8(mustBe)
185185
writeUtf8("\n")
186-
for (i in endInclusive + 1 ..< lines.size) {
186+
for (i in endInclusive + 1..<lines.size) {
187187
writeUtf8(lines[i])
188188
writeUtf8("\n")
189189
}
@@ -223,7 +223,25 @@ open class HarnessKotest() : FunSpec() {
223223
return FS_SYSTEM.read(pathStr.toPath()) { AssertionError(readUtf8()) }
224224
}
225225
}
226-
private fun testName() = "UT_${thisClassName()}"
226+
private fun testName(): String {
227+
val simpleClassName = "UT_${thisClassName()}"
228+
val matches =
229+
FS_SYSTEM.listRecursively(subprojectFolder)
230+
.filter { it.name == "$simpleClassName.kt" && !it.toString().contains("build") }
231+
.toList()
232+
if (matches.size == 1) {
233+
FS_SYSTEM.read(matches[0]) {
234+
while (!exhausted()) {
235+
val line = readUtf8Line() ?: break
236+
if (line.startsWith("package ")) {
237+
val pkg = line.removePrefix("package ").trim()
238+
return "$pkg.$simpleClassName"
239+
}
240+
}
241+
}
242+
}
243+
return simpleClassName
244+
}
227245
fun gradleWriteSS() {
228246
gradlew("test", "-PunderTest=true", "-Pselfie=overwrite", "--tests", testName())?.let {
229247
throw AssertionError("Expected write snapshots to succeed, but it failed", it)

jvm/settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ plugins {
2929
id 'org.jetbrains.kotlin.multiplatform' version '2.2.20' apply false
3030
// https://github.com/adamko-dev/dokkatoo/releases
3131
id 'dev.adamko.dokkatoo-html' version '2.4.0' apply false
32+
// https://plugins.gradle.org/plugin/io.kotest
33+
id 'io.kotest' version '6.1.4' apply false
3234
}
3335

3436
blowdryerSetup {

jvm/undertest-junit5-kotest/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ test {
3939
// defaults to 'write'
4040
systemProperty 'selfie', findProperty('selfie')
4141
systemProperty 'selfie.settings', findProperty('selfie.settings')
42+
systemProperty 'kotest.framework.config.fqn', 'undertest.junit5.JunitKotestProjectConfig'
4243
}

jvm/undertest-junit5-kotest/harness/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ ver_JUNIT_PIONEER=2.2.0
99
ver_OKIO=3.7.0
1010
ver_KOTLIN_TEST=1.9.22
1111
ver_KOTLIN_SERIALIZATION=1.6.3
12-
ver_KOTEST=5.8.0
12+
ver_KOTEST=6.1.4

0 commit comments

Comments
 (0)