Skip to content

Commit

Permalink
[Wasm] Refactoring invalidation tests infrastructure
Browse files Browse the repository at this point in the history
Make target flag not string but an enum
  • Loading branch information
igoriakovlev authored and qodana-bot committed Sep 21, 2024
1 parent e22dc26 commit d503f7d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ abstract class InfoParser<Info>(protected val infoFile: File) {

private fun String.splitAndTrim() = split(",").map { it.trim() }.filter { it.isNotBlank() }

class ProjectInfoParser(infoFile: File, private val target: String?) : InfoParser<ProjectInfo>(infoFile) {
enum class ModelTarget {
ANY,
JS,
WASM,
}

class ProjectInfoParser(infoFile: File, private val target: ModelTarget = ModelTarget.ANY) : InfoParser<ProjectInfo>(infoFile) {
private val moduleKindMap = mapOf(
"plain" to ModuleKind.PLAIN,
"commonjs" to ModuleKind.COMMON_JS,
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -245,7 +251,7 @@ class ProjectInfoParser(infoFile: File, private val target: String?) : InfoParse
}
}

class ModuleInfoParser(infoFile: File, private val target: String) : InfoParser<ModuleInfo>(infoFile) {
class ModuleInfoParser(infoFile: File, private val target: ModelTarget = ModelTarget.ANY) : InfoParser<ModuleInfo>(infoFile) {

private fun parseModifications(): List<ModuleInfo.Modification> {
val modifications = mutableListOf<ModuleInfo.Modification>()
Expand Down Expand Up @@ -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
}
Expand All @@ -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) {
Expand Down Expand Up @@ -368,13 +374,15 @@ class ModuleInfoParser(infoFile: File, private val target: String) : InfoParser<
}
}

private fun parseOpAndTarget(opWithTarget: String): Pair<String, String?>? {
val targetStartIndex = opWithTarget.indexOf("<")
val targetEndIndex = opWithTarget.indexOf(">")
if (targetEndIndex == -1) return opWithTarget to null
private fun parseOpAndTarget(opWithTarget: String): Pair<String, ModelTarget>? {
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())
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit d503f7d

Please sign in to comment.