From abe0ca3bd18760c1074c358fa40cd081cb7d4812 Mon Sep 17 00:00:00 2001 From: Nicolay Mitropolsky Date: Mon, 9 Apr 2018 14:12:44 +0300 Subject: [PATCH] 182: multiPlatformSetup.kt: `createTempDir` is non-static in 182 --- .../multiplatform/multiPlatformSetup.kt.182 | 153 ++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 idea/tests/org/jetbrains/kotlin/idea/multiplatform/multiPlatformSetup.kt.182 diff --git a/idea/tests/org/jetbrains/kotlin/idea/multiplatform/multiPlatformSetup.kt.182 b/idea/tests/org/jetbrains/kotlin/idea/multiplatform/multiPlatformSetup.kt.182 new file mode 100644 index 00000000000..7ec2166b047 --- /dev/null +++ b/idea/tests/org/jetbrains/kotlin/idea/multiplatform/multiPlatformSetup.kt.182 @@ -0,0 +1,153 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.multiplatform + +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.module.Module +import com.intellij.openapi.module.StdModuleTypes +import com.intellij.openapi.roots.CompilerModuleExtension +import com.intellij.openapi.roots.ModuleRootModificationUtil +import com.intellij.openapi.vfs.LocalFileSystem +import junit.framework.TestCase +import org.jetbrains.kotlin.config.JvmTarget +import org.jetbrains.kotlin.config.TargetPlatformKind +import org.jetbrains.kotlin.idea.stubs.AbstractMultiModuleTest +import org.jetbrains.kotlin.idea.stubs.createFacet +import java.io.File + +// allows to configure a test mpp project +// testRoot is supposed to contain several directories which contain module sources roots +// configuration is based on those directories names +fun AbstractMultiModuleTest.setupMppProjectFromDirStructure(testRoot: File) { + assert(testRoot.isDirectory) { testRoot.absolutePath + " must be a directory" } + val dirs = testRoot.listFiles().filter { it.isDirectory } + val rootInfos = dirs.map { parseDirName(it) } + val infosByModuleId = rootInfos.groupBy { it.moduleId } + val modulesById = infosByModuleId.mapValues { (moduleId, infos) -> + createModuleWithRoots(moduleId, infos) + } + + infosByModuleId.entries.forEach { (id, rootInfos) -> + val module = modulesById[id]!! + rootInfos.flatMap { it.dependencies }.forEach { + module.addDependency(modulesById[it]!!) + } + } + + modulesById.forEach { (nameAndPlatform, module) -> + val (name, platform) = nameAndPlatform + when (platform) { + TargetPlatformKind.Common -> module.createFacet(TargetPlatformKind.Common, useProjectSettings = false) + else -> { + val commonModuleId = ModuleId(name, TargetPlatformKind.Common) + + module.createFacet(platform, implementedModuleName = commonModuleId.ideaModuleName()) + module.enableMultiPlatform() + + modulesById[commonModuleId]?.let { commonModule -> + module.addDependency(commonModule) + } + } + } + } +} + +private fun AbstractMultiModuleTest.createModuleWithRoots( + moduleId: ModuleId, + infos: List +): Module { + val module = createModule(moduleId.ideaModuleName()) + for ((_, isTestRoot, moduleRoot) in infos) { + addRoot(module, moduleRoot, isTestRoot) + + if (moduleId.platform is TargetPlatformKind.JavaScript && isTestRoot) { + setupJsTestOutput(module) + } + } + return module +} + +// test line markers for JS do not work without additional setup +private fun setupJsTestOutput(module: Module) { + ModuleRootModificationUtil.updateModel(module) { + with(it.getModuleExtension(CompilerModuleExtension::class.java)!!) { + inheritCompilerOutputPath(false) + setCompilerOutputPathForTests("js_out") + } + } +} + +private fun AbstractMultiModuleTest.createModule(name: String): Module { + val moduleDir = createTempDir("") + val module = createModule(moduleDir.toString() + "/" + name, StdModuleTypes.JAVA) + val root = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(moduleDir) + TestCase.assertNotNull(root) + object : WriteCommandAction.Simple(module.project) { + @Throws(Throwable::class) + protected override fun run() { + root!!.refresh(false, true) + } + }.execute().throwException() + return module +} + +private val testSuffixes = setOf("test", "tests") +private val platformNames = mapOf( + TargetPlatformKind.Common to listOf("header", "common", "expect"), + TargetPlatformKind.Jvm[JvmTarget.DEFAULT] to listOf("java", "jvm"), + TargetPlatformKind.JavaScript to listOf("js", "javascript") +) + +private fun parseDirName(dir: File): RootInfo { + val parts = dir.name.split("_") + return RootInfo(parseModuleId(parts), parseIsTestRoot(parts), dir, parseDependencies(parts)) +} + +private fun parseDependencies(parts: List) = + parts.filter { it.startsWith("dep(") && it.endsWith(")") }.map { + parseModuleId(it.removePrefix("dep(").removeSuffix(")").split("-")) + } + +private fun parseModuleId(parts: List): ModuleId { + val platform = parsePlatform(parts).key + val name = parseModuleName(parts) + val moduleId = ModuleId(name, platform) + return moduleId +} + +private fun parsePlatform(parts: List) = + platformNames.entries.single { (_, names) -> + names.any { name -> parts.any { part -> part.equals(name, ignoreCase = true) } } + } + +private fun parseModuleName(parts: List) = when { + parts.size > 1 -> parts.first() + else -> "testModule" +} + +private fun parseIsTestRoot(parts: List) = + testSuffixes.any { suffix -> parts.any { it.equals(suffix, ignoreCase = true) } } + +private data class ModuleId( + val groupName: String, + val platform: TargetPlatformKind<*> +) { + fun ideaModuleName() = "${groupName}_${platform.presentableName}" +} + +private val TargetPlatformKind<*>.presentableName: String + get() = when (this) { + is TargetPlatformKind.Common -> "Common" + is TargetPlatformKind.Jvm -> "JVM" + is TargetPlatformKind.JavaScript -> "JS" + } + +private data class RootInfo( + val moduleId: ModuleId, + val isTestRoot: Boolean, + val moduleRoot: File, + val dependencies: List +) \ No newline at end of file