From d503f7d6fffac07dbbab746c7ed47e6f169bfec9 Mon Sep 17 00:00:00 2001 From: Igor Yakovlev Date: Thu, 19 Sep 2024 20:01:09 +0200 Subject: [PATCH] [Wasm] Refactoring invalidation tests infrastructure Make target flag not string but an enum --- .../kotlin/klib/PartialLinkageTestUtils.kt | 4 +-- .../org/jetbrains/kotlin/codegen/TestModel.kt | 34 ++++++++++++------- .../incremental/AbstractInvalidationTest.kt | 7 ++-- .../incremental/JsAbstractInvalidationTest.kt | 2 +- .../WasmAbstractInvalidationTest.kt | 2 +- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/klib/PartialLinkageTestUtils.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/klib/PartialLinkageTestUtils.kt index ce3988efb9ad0..9b0ad55be7e3a 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/klib/PartialLinkageTestUtils.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/klib/PartialLinkageTestUtils.kt @@ -84,7 +84,7 @@ object PartialLinkageTestUtils { val projectName = testDir.name val projectInfoFile = File(testDir, PROJECT_INFO_FILE) - val projectInfo: ProjectInfo = ProjectInfoParser(projectInfoFile, "").parse(projectName) + val projectInfo: ProjectInfo = ProjectInfoParser(projectInfoFile).parse(projectName) if (isIgnoredTest(projectInfo)) { return onIgnoredTest() // Ignore muted tests. @@ -96,7 +96,7 @@ object PartialLinkageTestUtils { KtUsefulTestCase.assertExists(moduleTestDir) val moduleInfoFile = File(moduleTestDir, MODULE_INFO_FILE) - val moduleInfo = ModuleInfoParser(moduleInfoFile, "").parse(moduleName) + val moduleInfo = ModuleInfoParser(moduleInfoFile).parse(moduleName) val moduleBuildDirs = createModuleDirs(buildDir, moduleName) diff --git a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/TestModel.kt b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/TestModel.kt index 6a564c52eaa07..6fdf8c042bacb 100644 --- a/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/TestModel.kt +++ b/compiler/tests-compiler-utils/tests/org/jetbrains/kotlin/codegen/TestModel.kt @@ -135,7 +135,13 @@ abstract class InfoParser(protected val infoFile: File) { private fun String.splitAndTrim() = split(",").map { it.trim() }.filter { it.isNotBlank() } -class ProjectInfoParser(infoFile: File, private val target: String?) : InfoParser(infoFile) { +enum class ModelTarget { + ANY, + JS, + WASM, +} + +class ProjectInfoParser(infoFile: File, private val target: ModelTarget = ModelTarget.ANY) : InfoParser(infoFile) { private val moduleKindMap = mapOf( "plain" to ModuleKind.PLAIN, "commonjs" to ModuleKind.COMMON_JS, @@ -157,7 +163,7 @@ class ProjectInfoParser(infoFile: File, private val target: String?) : InfoParse val split = line.split(":") val opWithTarget = split[0] val (op, opTarget) = parseOpAndTarget(opWithTarget) ?: throwSyntaxError(line) - if (opTarget != null && opTarget != target) { + if (opTarget != ModelTarget.ANY && opTarget != target) { ++lineCounter return@loop false } @@ -205,7 +211,7 @@ class ProjectInfoParser(infoFile: File, private val target: String?) : InfoParse val split = line.split(":") val opWithTarget = split[0] val (op, opTarget) = parseOpAndTarget(opWithTarget) ?: throwSyntaxError(line) - if (opTarget != null && opTarget != target) return@loop false + if (opTarget != ModelTarget.ANY && opTarget != target) return@loop false when { op == MODULES_LIST -> libraries += split[1].splitAndTrim() @@ -245,7 +251,7 @@ class ProjectInfoParser(infoFile: File, private val target: String?) : InfoParse } } -class ModuleInfoParser(infoFile: File, private val target: String) : InfoParser(infoFile) { +class ModuleInfoParser(infoFile: File, private val target: ModelTarget = ModelTarget.ANY) : InfoParser(infoFile) { private fun parseModifications(): List { val modifications = mutableListOf() @@ -292,7 +298,7 @@ class ModuleInfoParser(infoFile: File, private val target: String) : InfoParser< val opWithTarget = line.substring(0, opIndex) val (op, opTarget) = parseOpAndTarget(opWithTarget) ?: throwSyntaxError(line) - if (opTarget != null && opTarget != target) { + if (opTarget != ModelTarget.ANY && opTarget != target) { if (op == MODIFICATIONS) parseModifications() return@loop false } @@ -302,10 +308,10 @@ class ModuleInfoParser(infoFile: File, private val target: String) : InfoParser< val expectedState = DirtyFileState.entries.find { it.str == op } if (expectedState != null) { val stats = expectedFileStats[expectedState.str] - if (stats == null) { - expectedFileStats[expectedState.str] = getOpArgs().toSet() + expectedFileStats[expectedState.str] = if (stats == null) { + getOpArgs().toSet() } else { - expectedFileStats[expectedState.str] = stats + getOpArgs() + stats + getOpArgs() } } else { when (op) { @@ -368,13 +374,15 @@ class ModuleInfoParser(infoFile: File, private val target: String) : InfoParser< } } -private fun parseOpAndTarget(opWithTarget: String): Pair? { - val targetStartIndex = opWithTarget.indexOf("<") - val targetEndIndex = opWithTarget.indexOf(">") - if (targetEndIndex == -1) return opWithTarget to null +private fun parseOpAndTarget(opWithTarget: String): Pair? { + val targetStartIndex = opWithTarget.indexOf('<') + val targetEndIndex = opWithTarget.indexOf('>') + + if (targetEndIndex == -1) return opWithTarget to ModelTarget.ANY + if (targetStartIndex == -1) return null if (targetStartIndex + 1 >= targetEndIndex) return null val op = opWithTarget.substring(0, targetStartIndex).trim() val target = opWithTarget.substring(targetStartIndex + 1, targetEndIndex) - return op to target + return op to ModelTarget.valueOf(target.uppercase()) } \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt b/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt index 00f1469d1be79..6f867dde73c5c 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/incremental/AbstractInvalidationTest.kt @@ -25,7 +25,6 @@ import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.* import org.jetbrains.kotlin.js.config.JSConfigurationKeys import org.jetbrains.kotlin.js.test.utils.MODULE_EMULATION_FILE import org.jetbrains.kotlin.js.test.utils.wrapWithModuleEmulationMarkers -import org.jetbrains.kotlin.js.testOld.V8IrJsTestChecker import org.jetbrains.kotlin.konan.file.ZipFileSystemCacheableAccessor import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource @@ -47,7 +46,7 @@ abstract class AbstractInvalidationTest( protected val targetBackend: TargetBackend, private val workingDirPath: String, ) { - protected abstract val targetName: String + protected abstract val modelTarget: ModelTarget protected abstract val outputDirPath: String @@ -88,11 +87,11 @@ abstract class AbstractInvalidationTest( } private fun parseProjectInfo(testName: String, infoFile: File): ProjectInfo { - return ProjectInfoParser(infoFile, targetName).parse(testName) + return ProjectInfoParser(infoFile, modelTarget).parse(testName) } private fun parseModuleInfo(moduleName: String, infoFile: File): ModuleInfo { - return ModuleInfoParser(infoFile, targetName).parse(moduleName) + return ModuleInfoParser(infoFile, modelTarget).parse(moduleName) } private val File.filesInDir diff --git a/js/js.tests/test/org/jetbrains/kotlin/incremental/JsAbstractInvalidationTest.kt b/js/js.tests/test/org/jetbrains/kotlin/incremental/JsAbstractInvalidationTest.kt index d858dd5758277..a323f4c3fefe8 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/incremental/JsAbstractInvalidationTest.kt +++ b/js/js.tests/test/org/jetbrains/kotlin/incremental/JsAbstractInvalidationTest.kt @@ -42,7 +42,7 @@ abstract class JsAbstractInvalidationTest( protected const val SOURCE_MAPPING_URL_PREFIX = "//# sourceMappingURL=" } - override val targetName: String = "js" + override val modelTarget: ModelTarget = ModelTarget.JS override val outputDirPath = System.getProperty("kotlin.js.test.root.out.dir") ?: error("'kotlin.js.test.root.out.dir' is not set") diff --git a/wasm/wasm.tests/test/org/jetbrains/kotlin/incremental/WasmAbstractInvalidationTest.kt b/wasm/wasm.tests/test/org/jetbrains/kotlin/incremental/WasmAbstractInvalidationTest.kt index 6916e388a7fc9..85da7a4597f5c 100644 --- a/wasm/wasm.tests/test/org/jetbrains/kotlin/incremental/WasmAbstractInvalidationTest.kt +++ b/wasm/wasm.tests/test/org/jetbrains/kotlin/incremental/WasmAbstractInvalidationTest.kt @@ -26,7 +26,7 @@ abstract class WasmAbstractInvalidationTest( workingDirPath: String, ) : AbstractInvalidationTest(targetBackend, workingDirPath) { - override val targetName: String = "wasm" + override val modelTarget: ModelTarget = ModelTarget.WASM override val outputDirPath = System.getProperty("kotlin.wasm.test.root.out.dir") ?: error("'kotlin.wasm.test.root.out.dir' is not set")