Skip to content

Commit 310ed9d

Browse files
authored
Ignore unsupported TestSources in SelfieTestExecutionListener (#550)
2 parents 999069d + 15c8ac1 commit 310ed9d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

jvm/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1212

1313
## [Unreleased]
14+
### Fixed
15+
- Unsupported test sources (such as `FieldSource`) no longer cause the JUnit5 runner to crash. ([#550](https://github.com/diffplug/selfie/pull/550) fixes [#549](https://github.com/diffplug/selfie/issues/549))
1416

1517
## [2.5.3] - 2025-05-21
1618
### Fixed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023-2024 DiffPlug
2+
* Copyright (C) 2023-2025 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.selfie.junit5
1717

18+
import java.util.logging.Level
19+
import java.util.logging.Logger
1820
import org.junit.platform.engine.TestExecutionResult
1921
import org.junit.platform.engine.support.descriptor.ClassSource
2022
import org.junit.platform.engine.support.descriptor.MethodSource
@@ -24,12 +26,14 @@ import org.junit.platform.launcher.TestPlan
2426

2527
/** This is automatically registered at runtime thanks to `META-INF/services`. */
2628
class SelfieTestExecutionListener : TestExecutionListener {
29+
private val logger: Logger = Logger.getLogger(SelfieTestExecutionListener::class.java.name)
2730
private val system = SnapshotSystemJUnit5
2831
override fun executionStarted(testIdentifier: TestIdentifier) {
2932
try {
3033
system.testListenerRunning.set(true)
3134
if (isRootOrKotest(testIdentifier)) return
32-
val (clazz, test) = parseClassTest(testIdentifier)
35+
val parsed = parseClassTest(testIdentifier) ?: return
36+
val (clazz, test) = parsed
3337
val snapshotFile = system.forClass(clazz)
3438
if (test == null) {
3539
snapshotFile.incrementContainers()
@@ -42,7 +46,8 @@ class SelfieTestExecutionListener : TestExecutionListener {
4246
}
4347
override fun executionSkipped(testIdentifier: TestIdentifier, reason: String) {
4448
try {
45-
val (clazz, test) = parseClassTest(testIdentifier)
49+
val parsed = parseClassTest(testIdentifier) ?: return
50+
val (clazz, test) = parsed
4651
if (test == null) {
4752
system.forClass(clazz).incrementContainers()
4853
system.forClass(clazz).decrementContainersWithSuccess(false)
@@ -59,7 +64,8 @@ class SelfieTestExecutionListener : TestExecutionListener {
5964
) {
6065
try {
6166
if (isRootOrKotest(testIdentifier)) return
62-
val (clazz, test) = parseClassTest(testIdentifier)
67+
val parsed = parseClassTest(testIdentifier) ?: return
68+
val (clazz, test) = parsed
6369
val isSuccess = testExecutionResult.status == TestExecutionResult.Status.SUCCESSFUL
6470
val snapshotFile = system.forClass(clazz)
6571
if (test == null) {
@@ -76,13 +82,16 @@ class SelfieTestExecutionListener : TestExecutionListener {
7682
}
7783
private fun isRootOrKotest(testIdentifier: TestIdentifier) =
7884
testIdentifier.parentId.isEmpty || testIdentifier.uniqueId.startsWith("[engine:kotest]")
79-
private fun parseClassTest(testIdentifier: TestIdentifier): Pair<String, String?> {
85+
private fun parseClassTest(testIdentifier: TestIdentifier): Pair<String, String?>? {
8086
return when (val source = testIdentifier.source.get()) {
8187
is ClassSource ->
8288
Pair(source.className, if (testIdentifier.isTest) testIdentifier.displayName else null)
8389
is MethodSource ->
8490
Pair(source.className, if (testIdentifier.isTest) source.methodName else null)
85-
else -> throw AssertionError("Unexpected source $source")
91+
else -> {
92+
logger.log(Level.FINE, "Skipping unsupported source $source")
93+
return null
94+
}
8695
}
8796
}
8897
}

0 commit comments

Comments
 (0)