Skip to content

Commit

Permalink
Fix test error caused by mangled jsBrowserTest callstacks
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspittle committed Nov 25, 2024
1 parent 2064568 commit 750ffb2
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,45 @@ class DeferredStrategy(private val summary: String? = null) : FailureStrategy {
private val reports = mutableListOf<Report>()

// e.g. JVM: at com.varabyte.truthish.AssertAllTest.assertAllCallstacksAreCorrect(AssertAllTest.kt:133)
// e.g. Node #1: at /Users/d9n/Code/1p/truthish/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt:18:25
// e.g. Node #2: at AssertAllTest.protoOf.assertAllCollectsMultipleErrors_tljnxt_k$ (/Users/d9n/Code/1p/truthish/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt:14:13)
private val jvmNodeCallstackRegex = Regex("\\s+at ?[^ ]* ?([^ ]+\\.kt:\\d+[^ ]+)")
// exclude slashes so as not to clash with js node
private val jvmCallstackRegex = Regex("\\s+at ([^ /]+\\.kt:\\d+[^ ]+)")

// e.g. Node: at /Users/d9n/Code/1p/truthish/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt:18:25
// NOTE: There are also callstack lines like "at AssertAllTest.protoOf.assertAllCollectsMultipleErrors_tljnxt_k$ (/Users/d9n/Code/1p/truthish/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt:14:13)"
// but I think we can skip over them and still get the user callstack line that we want.
private val jsNodeCallstackRegex = Regex("\\s+at (/[^)]+)\\)?")

// e.g. at 1 test.kexe 0x104adf41f kfun:com.varabyte.truthish.AssertAllTest#assertAllCallstacksAreCorrect(){} + 1847 (/Users/d9n/Code/1p/truthish/src/commonTest/kotlin/com/varabyte/truthish/AssertAllTest.kt:133:21)
private val knCallstackRegex = Regex("\\s+at.+kfun:(.+)")

// e.g. at protoOf.assertAllCallstacksAreCorrect_nyk2hi(/var/folders/5x/f_r3s2p53rx2l_lffc7m9nmw0000gn/T/_karma_webpack_413015/commons.js:29616)
private val jsBrowserCallstackRegex = Regex("\\.js\\?")

override fun handle(report: Report) {
reports.add(report)

val callstackLine =
Throwable()
.stackTraceToString()
.takeIf { it.contains("common.js") }
// Reject JS browser callstacks because they're mangled
.takeUnless { jsBrowserCallstackRegex.containsMatchIn(it) }
?.split("\n")
?.drop(1) // Drop "java.lang.Throwable" line
?.asSequence()
?.mapNotNull { stackTraceLine ->
jvmNodeCallstackRegex.matchEntire(stackTraceLine) ?: knCallstackRegex.matchEntire(stackTraceLine)
jvmCallstackRegex.matchEntire(stackTraceLine)
?: knCallstackRegex.matchEntire(stackTraceLine)
?: jsNodeCallstackRegex.matchEntire(stackTraceLine)
}
?.map { match -> match.groupValues[1] }
?.filterNot {
it.startsWith("com.varabyte.truthish.failure.")
|| it.startsWith("com.varabyte.truthish.subjects.")
|| it.startsWith("kotlin.") // Kotlin/Native
|| it.contains("/com/varabyte/truthish/failure/")
|| it.contains("/com/varabyte/truthish/subjects/")
|| it.contains("/kotlin/js/runtime/")
|| it.contains("/com/varabyte/truthish/failure/") // NodeJS
|| it.contains("/com/varabyte/truthish/subjects/") // NodeJS
|| it.contains("/kotlin/js/runtime/") // NodeJS
|| it.contains("/src/kotlin/util/") // NodeJS
}
?.firstOrNull()

Expand Down

0 comments on commit 750ffb2

Please sign in to comment.