[imltogradle] Don't generate unnecessary tests-jar with real dependencies when a module doesn't have any source root

This helps to prevent "fake" circular dependencies when
module A depends on tests of module B but module B doesn't have test source roots

In particular this commit fixes when running `:kotlin-ide.kotlin.generators:testClasses` task:
```
Circular dependency between the following tasks:
:kotlin-ide.kotlin.gradle.gradle-tooling:compileTestJava
+--- :kotlin-ide.kotlin.gradle.gradle-tooling:compileTestKotlin
|    \--- :kotlin-ide.kotlin.test-framework:testJar
|         +--- :kotlin-ide.kotlin.test-framework:compileTestKotlin
|         |    +--- :kotlin-ide.kotlin.idea:testJar
|         |    |    +--- :kotlin-ide.kotlin.idea:compileTestKotlin
|         |    |    |    \--- :kotlin-ide.kotlin.gradle.gradle-tooling:testJar
|         |    |    |         +--- :kotlin-ide.kotlin.gradle.gradle-tooling:compileTestKotlin (*)
|         |    |    |         \--- :kotlin-ide.kotlin.gradle.gradle-tooling:testClasses
|         |    |    |              \--- :kotlin-ide.kotlin.gradle.gradle-tooling:compileTestJava (*)
|         |    |    \--- :kotlin-ide.kotlin.idea:testClasses
|         |    |         \--- :kotlin-ide.kotlin.idea:compileTestJava
|         |    |              +--- :kotlin-ide.kotlin.gradle.gradle-tooling:testJar (*)
|         |    |              \--- :kotlin-ide.kotlin.idea:compileTestKotlin (*)
|         |    \--- :kotlin-ide.kotlin.jvm:testJar
|         |         +--- :kotlin-ide.kotlin.jvm:compileTestKotlin
|         |         |    +--- :kotlin-ide.kotlin.gradle.gradle-tooling:testJar (*)
|         |         |    +--- :kotlin-ide.kotlin.idea:testJar (*)
|         |         |    \--- :kotlin-ide.kotlin.repl:testJar
|         |         |         +--- :kotlin-ide.kotlin.repl:compileTestKotlin
|         |         |         |    \--- :kotlin-ide.kotlin.idea:testJar (*)
|         |         |         \--- :kotlin-ide.kotlin.repl:testClasses
|         |         |              \--- :kotlin-ide.kotlin.repl:compileTestJava
|         |         |                   +--- :kotlin-ide.kotlin.idea:testJar (*)
|         |         |                   \--- :kotlin-ide.kotlin.repl:compileTestKotlin (*)
|         |         \--- :kotlin-ide.kotlin.jvm:testClasses
|         |              \--- :kotlin-ide.kotlin.jvm:compileTestJava
|         |                   +--- :kotlin-ide.kotlin.gradle.gradle-tooling:testJar (*)
|         |                   +--- :kotlin-ide.kotlin.idea:testJar (*)
|         |                   +--- :kotlin-ide.kotlin.jvm:compileTestKotlin (*)
|         |                   \--- :kotlin-ide.kotlin.repl:testJar (*)
|         \--- :kotlin-ide.kotlin.test-framework:testClasses
|              \--- :kotlin-ide.kotlin.test-framework:compileTestJava
|                   +--- :kotlin-ide.kotlin.idea:testJar (*)
|                   +--- :kotlin-ide.kotlin.jvm:testJar (*)
|                   \--- :kotlin-ide.kotlin.test-framework:compileTestKotlin (*)
\--- :kotlin-ide.kotlin.test-framework:testJar (*)
```
This commit is contained in:
Nikita Bobko
2021-06-08 16:54:07 +02:00
parent bcd28615a6
commit bf287c0b85
@@ -211,6 +211,13 @@ fun convertJpsModule(imlFile: File, jpsModule: JpsModule): String {
?.joinToString { "\"$it\"" }
?: ""
val testsJar =
if (jpsModule.sourceRoots.any { it.rootType.isForTests }) "testsJar()"
else """
// Fake empty configuration in order to make `DependencyHandler.projectTests(name: String)` work
configurations.getOrCreate("tests-jar")
""".trimIndent()
val deps = jpsModule.dependencies.flatMap { convertJpsDependencyElement(it) }
.distinctBy { it.normalizedForComparison() }
.joinToString("\n") { it.convertToGradleCall() }
@@ -258,6 +265,6 @@ fun convertJpsModule(imlFile: File, jpsModule: JpsModule): String {
| kotlinOptions.useOldBackend = true // KT-45697
|}
|
|testsJar()
|$testsJar
""".trimMarginWithInterpolations()
}