Skip to content

Commit 8f7cff8

Browse files
UTBot doesn't show test source from other modules for Gradle project #1060
Better source roots sorting
1 parent dc24fe9 commit 8f7cff8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/CodeGenerationController.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import java.util.concurrent.CancellationException
7575
import java.util.concurrent.CountDownLatch
7676
import java.util.concurrent.TimeUnit
7777
import org.jetbrains.kotlin.idea.util.projectStructure.allModules
78+
import org.utbot.intellij.plugin.ui.utils.TestSourceRoot
7879
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
7980

8081
object CodeGenerationController {
@@ -436,7 +437,7 @@ object CodeGenerationController {
436437
}
437438
}
438439

439-
fun GenerateTestsModel.getAllTestSourceRoots() : MutableList<VirtualFile> {
440+
fun GenerateTestsModel.getAllTestSourceRoots() : MutableList<TestSourceRoot> {
440441
with(if (project.isBuildWithGradle) project.allModules() else potentialTestModules) {
441442
return this.flatMap { it.suitableTestSourceRoots().toList() }.toMutableList()
442443
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt

+37-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.intellij.openapi.project.guessProjectDir
77
import com.intellij.openapi.ui.ComboBox
88
import com.intellij.openapi.ui.ComponentWithBrowseButton
99
import com.intellij.openapi.ui.FixedSizeButton
10+
import com.intellij.openapi.util.text.StringUtil
1011
import com.intellij.openapi.vfs.VirtualFile
1112
import com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile
1213
import com.intellij.ui.ColoredListCellRenderer
@@ -16,6 +17,7 @@ import com.intellij.util.ui.UIUtil
1617
import java.io.File
1718
import javax.swing.DefaultComboBoxModel
1819
import javax.swing.JList
20+
import org.jetbrains.kotlin.idea.util.rootManager
1921
import org.utbot.common.PathUtil
2022
import org.utbot.intellij.plugin.generator.CodeGenerationController.getAllTestSourceRoots
2123
import org.utbot.intellij.plugin.models.GenerateTestsModel
@@ -55,7 +57,41 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
5557
}
5658
}
5759

58-
val testRoots = model.getAllTestSourceRoots()
60+
var commonModuleSourceDirectory = ""
61+
for ((i, sourceRoot) in model.srcModule.rootManager.sourceRoots.withIndex()) {
62+
commonModuleSourceDirectory = if (i == 0) {
63+
sourceRoot.toNioPath().toString()
64+
} else {
65+
StringUtil.commonPrefix(commonModuleSourceDirectory, sourceRoot.toNioPath().toString())
66+
}
67+
}
68+
// The first sorting to obtain the best candidate
69+
val testRoots = model.getAllTestSourceRoots().distinct().sortedWith(
70+
compareByDescending<TestSourceRoot> {
71+
// Heuristics: Dirs with language == codegenLanguage should go first
72+
it.expectedLanguage == model.codegenLanguage
73+
}.thenBy {
74+
// Heuristics: move root that is 'closer' to module 'common' directory to the first position
75+
StringUtil.commonPrefixLength(commonModuleSourceDirectory, it.dir.toNioPath().toString())
76+
}).toMutableList()
77+
78+
val theBest = if (testRoots.isNotEmpty()) testRoots[0] else null
79+
80+
// The second sorting to make full list ordered
81+
testRoots.sortWith(compareByDescending<TestSourceRoot> {
82+
// Heuristics: Dirs with language == codegenLanguage should go first
83+
it.expectedLanguage == model.codegenLanguage
84+
}.thenBy {
85+
// ABC-sorting
86+
it.dir.toNioPath()
87+
}
88+
)
89+
// The best candidate should go first to be pre-selected
90+
theBest?.let {
91+
testRoots.remove(it)
92+
testRoots.add(0, it)
93+
}
94+
5995
// this method is blocked for Gradle, where multiple test modules can exist
6096
model.testModule.addDedicatedTestRoot(testRoots, model.codegenLanguage)
6197

0 commit comments

Comments
 (0)