Skip to content

Commit

Permalink
[Swift Export] update test data when a flag is set (KT-72052)
Browse files Browse the repository at this point in the history
Merge-request: KT-MR-19754
Merged-by: Anton Bannykh <Anton.Bannykh@jetbrains.com>
  • Loading branch information
anton-bannykh authored and qodana-bot committed Jan 22, 2025
1 parent 25d347d commit 1e00112
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ class UpdateTestDataHandler(

override fun suppressIfNeeded(failedAssertions: List<WrappedException>): List<WrappedException> {
if (enabled || System.getProperty("kotlin.test.update.test.data") == "true") {
val assertationFailures = failedAssertions
.flatMap { (it.cause as? MultipleFailuresError)?.failures ?: listOf(it.cause) }
.filterIsInstance<AssertionFailedError>()
for (failure in assertationFailures) {
val path = (failure.expected?.value as? FileInfo)?.path ?: continue
File(path).writeText(failure.actual.stringRepresentation)
failedAssertions.forEach {
it.cause.tryUpdateTestData()
}
}
return failedAssertions
}
}

fun Throwable.tryUpdateTestData() {
when {
this is AssertionFailedError -> tryUpdateTestData()
this is MultipleFailuresError -> this.failures.forEach { it.tryUpdateTestData() }
}
}

fun AssertionFailedError.tryUpdateTestData() {
val path = (expected?.value as? FileInfo)?.path ?: return
File(path).writeText(actual.stringRepresentation)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.konan.test.blackbox.support.compilation.TestCompilat
import org.jetbrains.kotlin.konan.test.blackbox.support.swiftExportConfigMap
import org.jetbrains.kotlin.konan.test.blackbox.support.util.flatMapToSet
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.junit.jupiter.api.assertAll
import java.io.File
import kotlin.io.path.*

Expand All @@ -39,9 +40,11 @@ abstract class AbstractKlibBasedSwiftRunnerTest : AbstractSwiftExportTest() {
val expectedCHeader = expectedFiles / it.name / "${it.name}.h"
val expectedKotlinBridge = expectedFiles / it.name / "${it.name}.kt"

KotlinTestUtils.assertEqualsToFile(expectedSwift, files.swiftApi.readText())
KotlinTestUtils.assertEqualsToFile(expectedCHeader, files.cHeaderBridges.readText())
KotlinTestUtils.assertEqualsToFile(expectedKotlinBridge, files.kotlinBridges.readText())
assertAll(
{ KotlinTestUtils.assertEqualsToFile(expectedSwift, files.swiftApi.readText()) },
{ KotlinTestUtils.assertEqualsToFile(expectedCHeader, files.cHeaderBridges.readText()) },
{ KotlinTestUtils.assertEqualsToFile(expectedKotlinBridge, files.kotlinBridges.readText()) }
)
}
is SwiftExportModule.SwiftOnly -> {
val expectedFiles = testPathFull.toPath() / "golden_result/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ package org.jetbrains.kotlin.swiftexport.standalone
import org.jetbrains.kotlin.konan.test.blackbox.support.NativeTestSupport.createTestRunSettings
import org.jetbrains.kotlin.konan.test.blackbox.support.NativeTestSupport.getOrCreateTestRunProvider
import org.jetbrains.kotlin.konan.test.blackbox.support.settings.NativeTestInstances
import org.jetbrains.kotlin.test.backend.handlers.tryUpdateTestData
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeAllCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler
import org.junit.jupiter.api.extension.TestInstancePostProcessor
import org.opentest4j.AssertionFailedError

class SwiftExportTestSupport : BeforeEachCallback {
class SwiftExportTestSupport : BeforeEachCallback, TestExecutionExceptionHandler {
/**
* Note: [BeforeEachCallback.beforeEach] allows accessing test instances while [BeforeAllCallback.beforeAll] which may look
* more preferable here does not allow it because it is called at the time when test instances are not created yet.
Expand All @@ -29,4 +33,13 @@ class SwiftExportTestSupport : BeforeEachCallback {
testRunProvider = getOrCreateTestRunProvider()
}
}

private val overwriteGoldenData = System.getProperty("kotlin.test.update.test.data") == "true"

override fun handleTestExecutionException(context: ExtensionContext, throwable: Throwable) {
if (overwriteGoldenData) {
throwable.tryUpdateTestData()
}
throw throwable
}
}

0 comments on commit 1e00112

Please sign in to comment.