From d586a84f310c953d38fd6cf626cb98517cff8dae Mon Sep 17 00:00:00 2001 From: Alexey Sedunov Date: Tue, 10 Jul 2018 18:20:37 +0300 Subject: [PATCH] Configuration: Include test roots of dependent modules to the module scope #KT-23914 Fixed --- .../KotlinGradleProjectResolverExtension.kt | 1 + .../MultiplatformProjectImportingTest.kt | 28 +++++++++++++ idea/src/META-INF/plugin.xml | 2 + idea/src/META-INF/plugin.xml.172 | 2 + idea/src/META-INF/plugin.xml.173 | 2 + idea/src/META-INF/plugin.xml.182 | 2 + idea/src/META-INF/plugin.xml.as31 | 2 + idea/src/META-INF/plugin.xml.as32 | 2 + .../KotlinNonJvmOrderEnumerationHandler.kt | 42 +++++++++++++++++++ 9 files changed, 83 insertions(+) create mode 100644 idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmOrderEnumerationHandler.kt diff --git a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinGradleProjectResolverExtension.kt b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinGradleProjectResolverExtension.kt index 603251b20ac..e242d125e1e 100644 --- a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinGradleProjectResolverExtension.kt +++ b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinGradleProjectResolverExtension.kt @@ -216,6 +216,7 @@ class KotlinGradleProjectResolverExtension : AbstractProjectResolverExtension() val moduleDependencyData = ModuleDependencyData(ideModule.data, targetModule.data) moduleDependencyData.scope = DependencyScope.COMPILE moduleDependencyData.isExported = false + moduleDependencyData.isProductionOnTestDependency = targetModule.sourceSetName == "test" ideModule.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData) } diff --git a/idea/idea-gradle/tests/org/jetbrains/kotlin/gradle/MultiplatformProjectImportingTest.kt b/idea/idea-gradle/tests/org/jetbrains/kotlin/gradle/MultiplatformProjectImportingTest.kt index df515fa85de..06dc9d6fb86 100644 --- a/idea/idea-gradle/tests/org/jetbrains/kotlin/gradle/MultiplatformProjectImportingTest.kt +++ b/idea/idea-gradle/tests/org/jetbrains/kotlin/gradle/MultiplatformProjectImportingTest.kt @@ -19,11 +19,14 @@ package org.jetbrains.kotlin.gradle import com.intellij.openapi.roots.DependencyScope import com.intellij.openapi.roots.LibraryOrderEntry import com.intellij.openapi.roots.OrderRootType +import com.intellij.openapi.roots.impl.ModuleOrderEntryImpl +import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.PathUtil import junit.framework.TestCase import org.jetbrains.kotlin.idea.codeInsight.gradle.GradleImportingTestCase import org.jetbrains.kotlin.idea.codeInsight.gradle.facetSettings import org.jetbrains.kotlin.idea.facet.KotlinFacet +import org.jetbrains.kotlin.idea.util.rootManager import org.jetbrains.kotlin.test.KotlinTestUtils import org.junit.Test @@ -34,9 +37,25 @@ class MultiplatformProjectImportingTest : GradleImportingTestCase() { .filterIsInstance() .flatMap { it.getUrls(OrderRootType.CLASSES).map { it.replace(projectPath, "") } } + private fun assertProductionOnTestDependency(moduleName: String, depModuleName: String, expected: Boolean) { + val depOrderEntry = getModule(moduleName) + .rootManager + .orderEntries + .filterIsInstance() + .first { it.moduleName == depModuleName } + assert(depOrderEntry.isProductionOnTestDependency == expected) + } + + private fun assertFileInModuleScope(file: VirtualFile, moduleName: String) { + assert(getModule(moduleName).getModuleWithDependenciesAndLibrariesScope(true).contains(file)) + } + @Test fun testPlatformToCommonDependency() { createProjectSubFile("settings.gradle", "include ':common', ':jvm', ':js'") + val commonTestFile = createProjectSubFile("common/src/test/kotlin/test.kt", "") + createProjectSubFile("jvm/src/test/kotlin/test.kt", "") + createProjectSubFile("js/src/test/kotlin/test.kt", "") val kotlinVersion = "1.1.0" @@ -75,10 +94,19 @@ class MultiplatformProjectImportingTest : GradleImportingTestCase() { ) importProject() + assertModuleModuleDepScope("jvm_main", "common_main", DependencyScope.COMPILE) assertModuleModuleDepScope("jvm_test", "common_test", DependencyScope.COMPILE) assertModuleModuleDepScope("js_main", "common_main", DependencyScope.COMPILE) assertModuleModuleDepScope("js_test", "common_test", DependencyScope.COMPILE) + + assertProductionOnTestDependency("jvm_main", "common_main", false) + assertProductionOnTestDependency("jvm_test", "common_test", true) + assertProductionOnTestDependency("js_main", "common_main", false) + assertProductionOnTestDependency("js_test", "common_test", true) + + assertFileInModuleScope(commonTestFile, "jvm_test") + assertFileInModuleScope(commonTestFile, "js_test") } @Test diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index c4bd602bc5f..6b448f7e7c2 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -838,6 +838,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. + + org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention Kotlin diff --git a/idea/src/META-INF/plugin.xml.172 b/idea/src/META-INF/plugin.xml.172 index 72c1c298c54..a0e9e3e824c 100644 --- a/idea/src/META-INF/plugin.xml.172 +++ b/idea/src/META-INF/plugin.xml.172 @@ -835,6 +835,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. + + org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention Kotlin diff --git a/idea/src/META-INF/plugin.xml.173 b/idea/src/META-INF/plugin.xml.173 index 5ef8e195bc6..186ba6cbb5c 100644 --- a/idea/src/META-INF/plugin.xml.173 +++ b/idea/src/META-INF/plugin.xml.173 @@ -838,6 +838,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. + + org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention Kotlin diff --git a/idea/src/META-INF/plugin.xml.182 b/idea/src/META-INF/plugin.xml.182 index 6d98ad8c3fb..cd37d19e7e6 100644 --- a/idea/src/META-INF/plugin.xml.182 +++ b/idea/src/META-INF/plugin.xml.182 @@ -839,6 +839,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. + + org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention Kotlin diff --git a/idea/src/META-INF/plugin.xml.as31 b/idea/src/META-INF/plugin.xml.as31 index f37db502f32..800529b5a53 100644 --- a/idea/src/META-INF/plugin.xml.as31 +++ b/idea/src/META-INF/plugin.xml.as31 @@ -838,6 +838,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. + + org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention Kotlin diff --git a/idea/src/META-INF/plugin.xml.as32 b/idea/src/META-INF/plugin.xml.as32 index d0b53ed6bb6..fc3c0d9bff4 100644 --- a/idea/src/META-INF/plugin.xml.as32 +++ b/idea/src/META-INF/plugin.xml.as32 @@ -838,6 +838,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. + + org.jetbrains.kotlin.idea.intentions.FoldInitializerAndIfToElvisIntention Kotlin diff --git a/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmOrderEnumerationHandler.kt b/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmOrderEnumerationHandler.kt new file mode 100644 index 00000000000..8abd267e433 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/roots/KotlinNonJvmOrderEnumerationHandler.kt @@ -0,0 +1,42 @@ +/* + * 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.roots + +import com.intellij.openapi.module.Module +import com.intellij.openapi.roots.ModuleRootModel +import com.intellij.openapi.roots.OrderEnumerationHandler +import com.intellij.openapi.roots.OrderRootType +import org.jetbrains.jps.model.module.JpsModuleSourceRootType +import org.jetbrains.kotlin.config.KotlinResourceRootType +import org.jetbrains.kotlin.config.KotlinSourceRootType + +object KotlinNonJvmOrderEnumerationHandler : OrderEnumerationHandler() { + class Factory : OrderEnumerationHandler.Factory() { + override fun isApplicable(module: Module) = true + override fun createHandler(module: Module) = KotlinNonJvmOrderEnumerationHandler + } + + private val kotlinTestSourceRootTypes: Set> = + setOf(KotlinSourceRootType.TestSource, KotlinResourceRootType.TestResource) + + override fun addCustomModuleRoots( + type: OrderRootType, + rootModel: ModuleRootModel, + result: MutableCollection, + includeProduction: Boolean, + includeTests: Boolean + ): Boolean { + if (type == OrderRootType.SOURCES) { + if (includeProduction) { + rootModel.getSourceRoots(includeTests).mapTo(result) { it.url } + } else { + rootModel.getSourceRoots(kotlinTestSourceRootTypes).mapTo(result) { it.url } + } + return true + } + return false + } +} \ No newline at end of file