Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jvm/example-kotest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies {
}
tasks.test {
useJUnitPlatform()
systemProperty("kotest.framework.config.fqn", "com.example.kotest.KotestConfig")
environment(properties.filter { it.key == "selfie" || it.key == "OPENAI_API_KEY" })
inputs.files(fileTree("src/test") {
include("**/*.ss")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package com.example.kotest
import com.diffplug.selfie.kotest.SelfieExtension

class KotestConfig : io.kotest.core.config.AbstractProjectConfig() {
override fun extensions() = listOf(SelfieExtension(this))
override val extensions = listOf(SelfieExtension(this))
}
4 changes: 2 additions & 2 deletions jvm/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ver_JUNIT_PIONEER=2.3.0
ver_OKIO=3.16.0
ver_KOTLIN_TEST=2.0.0
ver_KOTLIN_SERIALIZATION=1.9.0
# Kotest 5.6.0 is the oldest that we support
ver_KOTEST=5.6.0
# Kotest 6.0.0 is the oldest that we support
ver_KOTEST=6.1.0

ver_JVM_TARGET=11
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 DiffPlug
* Copyright (C) 2024-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@ import io.kotest.core.listeners.BeforeSpecListener
import io.kotest.core.listeners.FinalizeSpecListener
import io.kotest.core.spec.Spec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.engine.test.TestResult
import kotlin.reflect.KClass
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.withContext
Expand All @@ -43,11 +43,11 @@ class SelfieExtension(projectConfig: AbstractProjectConfig) :
execute: suspend (TestCase) -> TestResult
): TestResult {
val file = SnapshotSystemJUnit5.forClass(testCase.spec::class.java.name)
val coroutineLocal = CoroutineDiskStorage(DiskStorageJUnit5(file, testCase.name.testName))
val coroutineLocal = CoroutineDiskStorage(DiskStorageJUnit5(file, testCase.name.name))
return withContext(currentCoroutineContext() + coroutineLocal) {
file.startTest(testCase.name.testName, false)
file.startTest(testCase.name.name, false)
val result = execute(testCase)
file.finishedTestWithSuccess(testCase.name.testName, false, result.isSuccess)
file.finishedTestWithSuccess(testCase.name.name, false, result.isSuccess)
result
}
}
Expand All @@ -58,8 +58,8 @@ class SelfieExtension(projectConfig: AbstractProjectConfig) :
val file = SnapshotSystemJUnit5.forClass(kclass.java.name)
results.entries.forEach {
if (it.value.isIgnored) {
file.startTest(it.key.name.testName, false)
file.finishedTestWithSuccess(it.key.name.testName, false, false)
file.startTest(it.key.name.name, false)
file.finishedTestWithSuccess(it.key.name.name, false, false)
}
}
SnapshotSystemJUnit5.forClass(kclass.java.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 DiffPlug
* Copyright (C) 2024-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,18 +18,19 @@ package com.diffplug.selfie.kotest
import com.diffplug.selfie.guts.FS
import com.diffplug.selfie.guts.TypedPath
import io.kotest.assertions.Actual
import io.kotest.assertions.Exceptions
import io.kotest.assertions.AssertionErrorBuilder
import io.kotest.assertions.Expected
import io.kotest.assertions.print.Printed
import okio.FileSystem
import okio.Path.Companion.toPath

expect internal val FS_SYSTEM: FileSystem
internal expect val FS_SYSTEM: FileSystem
internal fun TypedPath.toPath(): okio.Path = absolutePath.toPath()

internal object FSOkio : FS {
override fun fileExists(typedPath: TypedPath): Boolean =
FS_SYSTEM.metadataOrNull(typedPath.toPath())?.isRegularFile ?: false

/** Walks the files (not directories) which are children and grandchildren of the given path. */
override fun <T> fileWalk(typedPath: TypedPath, walk: (Sequence<TypedPath>) -> T): T =
walk(
Expand All @@ -40,9 +41,11 @@ internal object FSOkio : FS {
FS_SYSTEM.read(typedPath.toPath()) { readByteArray() }
override fun fileWriteBinary(typedPath: TypedPath, content: ByteArray): Unit =
FS_SYSTEM.write(typedPath.toPath()) { write(content) }

/** Creates an assertion failed exception to throw. */
override fun assertFailed(message: String, expected: Any?, actual: Any?): Throwable =
if (expected == null && actual == null) Exceptions.createAssertionError(message, null)
if (expected == null && actual == null)
AssertionErrorBuilder.create().withMessage(message).build()
else {
val expectedStr = nullableToString(expected, "")
val actualStr = nullableToString(actual, "")
Expand All @@ -55,10 +58,11 @@ internal object FSOkio : FS {
comparisonAssertion(message, expectedStr, actualStr)
}
}
private fun nullableToString(any: Any?, onNull: String): String =
any?.let { it.toString() } ?: onNull
private fun nullableToString(any: Any?, onNull: String): String = any?.toString() ?: onNull
private fun comparisonAssertion(message: String, expected: String, actual: String): Throwable {
return Exceptions.createAssertionError(
message, null, Expected(Printed((expected))), Actual(Printed((actual))))
return AssertionErrorBuilder.create()
.withMessage(message)
.withValues(Expected(Printed((expected))), Actual(Printed((actual))))
.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2024 DiffPlug
* Copyright (C) 2024-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,7 +26,7 @@ import io.kotest.core.listeners.FinalizeSpecListener
import io.kotest.core.source.SourceRef
import io.kotest.core.spec.Spec
import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.engine.test.TestResult
import kotlin.jvm.JvmStatic
import kotlin.reflect.KClass
import kotlinx.coroutines.currentCoroutineContext
Expand Down Expand Up @@ -62,18 +62,19 @@ class SelfieExtension(
val classOrFilename: String =
when (val source = testCase.source) {
is SourceRef.ClassSource -> source.fqn
is SourceRef.FileSource -> source.fileName
is SourceRef.ClassLineSource -> source.fqn
is SourceRef.None -> TODO("Handle SourceRef.None")
}
return system.forClassOrFilename(classOrFilename)
}

/** Called for every test method. */
override suspend fun intercept(
testCase: TestCase,
execute: suspend (TestCase) -> TestResult
): TestResult {
val file = snapshotFileFor(testCase)
val testName = testCase.name.testName
val testName = testCase.name.name
val coroutineLocal = CoroutineDiskStorage(DiskStorageKotest(file, testName))
return withContext(currentCoroutineContext() + coroutineLocal) {
file.startTest(testName)
Expand All @@ -89,8 +90,8 @@ class SelfieExtension(
val file = results.keys.map { snapshotFileFor(it) }.firstOrNull() ?: return
results.entries.forEach {
if (it.value.isIgnored) {
file.startTest(it.key.name.testName)
file.finishedTestWithSuccess(it.key.name.testName, false)
file.startTest(it.key.name.name)
file.finishedTestWithSuccess(it.key.name.name, false)
}
}
file.finishedClassWithSuccess(results.entries.all { it.value.isSuccess })
Expand Down
1 change: 1 addition & 0 deletions jvm/undertest-junit5-kotest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ test {
// defaults to 'write'
systemProperty 'selfie', findProperty('selfie')
systemProperty 'selfie.settings', findProperty('selfie.settings')
systemProperty 'kotest.framework.config.fqn', 'undertest.junit5.JunitKotestProjectConfig'
}
2 changes: 1 addition & 1 deletion jvm/undertest-junit5-kotest/harness/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ ver_JUNIT_PIONEER=2.2.0
ver_OKIO=3.7.0
ver_KOTLIN_TEST=1.9.22
ver_KOTLIN_SERIALIZATION=1.6.3
ver_KOTEST=5.8.0
ver_KOTEST=6.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.diffplug.selfie.junit5.SelfieExtension
import io.kotest.core.config.AbstractProjectConfig

class JunitKotestProjectConfig : AbstractProjectConfig() {
override fun extensions() = listOf(SelfieExtension(this))
override val extensions = listOf(SelfieExtension(this))
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package undertest.junit5

import com.diffplug.selfie.coroutines.expectSelfie
import io.kotest.common.ExperimentalKotest
import io.kotest.core.spec.style.FunSpec
import io.kotest.engine.concurrency.TestExecutionMode
import kotlinx.coroutines.delay

@OptIn(ExperimentalKotest::class)
class UT_KotestConcurrencyStressTest :
FunSpec({
concurrency = 100
testExecutionMode = TestExecutionMode.LimitedConcurrency(100)
for (d in 1..1000) {
val digit = d
test(String.format("test %04d", digit)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package undertest.kotest

import com.diffplug.selfie.coroutines.expectSelfie
import io.kotest.common.ExperimentalKotest
import io.kotest.core.spec.style.FunSpec
import io.kotest.engine.concurrency.TestExecutionMode
import kotlinx.coroutines.delay

@OptIn(ExperimentalKotest::class)
class UT_ConcurrencyStressTest :
FunSpec({
concurrency = 100
testExecutionMode = TestExecutionMode.LimitedConcurrency(100)
for (d in 1..1000) {
val digit = d
test(String.format("test %04d", digit)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.diffplug.selfie.kotest.SelfieExtension
import io.kotest.core.config.AbstractProjectConfig

object KotestProjectConfig : AbstractProjectConfig() {
override fun extensions() = listOf(SelfieExtension(this))
override val extensions = listOf(SelfieExtension(this))
}
3 changes: 3 additions & 0 deletions selfie.dev/src/pages/jvm/kotest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class ProjectConfig : AbstractProjectConfig() {
}
```

Note that your `AbstractProjectConfig` must either have the fully qualified name of `io.kotest.provided.ProjectConfig` or the system property `kotest.framework.config.fqn` must be set.
See [Kotest docs](https://kotest.io/docs/framework/project-config.html) for more info.

## Selfie and coroutines

In a regular JUnit 5 test, you call `Selfie.expectSelfie(...)`, like so
Expand Down
Loading