@@ -16,7 +16,6 @@ import com.intellij.ui.SimpleTextAttributes
1616import com.intellij.util.ArrayUtil
1717import com.intellij.util.ui.UIUtil
1818import java.io.File
19- import java.util.Comparator
2019import javax.swing.DefaultComboBoxModel
2120import javax.swing.JList
2221import org.jetbrains.kotlin.idea.util.rootManager
@@ -25,6 +24,7 @@ import org.utbot.intellij.plugin.generator.CodeGenerationController.getAllTestSo
2524import org.utbot.intellij.plugin.models.GenerateTestsModel
2625import org.utbot.intellij.plugin.ui.utils.TestSourceRoot
2726import org.utbot.intellij.plugin.ui.utils.addDedicatedTestRoot
27+ import org.utbot.intellij.plugin.ui.utils.dedicatedTestSourceRootName
2828import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
2929
3030class TestFolderComboWithBrowseButton (private val model : GenerateTestsModel ) :
@@ -59,42 +59,7 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
5959 }
6060 }
6161
62- var commonModuleSourceDirectory = " "
63- for ((i, sourceRoot) in model.srcModule.rootManager.sourceRoots.withIndex()) {
64- commonModuleSourceDirectory = if (i == 0 ) {
65- sourceRoot.toNioPath().toString()
66- } else {
67- StringUtil .commonPrefix(commonModuleSourceDirectory, sourceRoot.toNioPath().toString())
68- }
69- }
70- // The first sorting to obtain the best candidate
71- val testRoots = model.getAllTestSourceRoots().distinct().sortedWith(object : Comparator <TestSourceRoot > {
72- override fun compare (o1 : TestSourceRoot , o2 : TestSourceRoot ): Int {
73- // Heuristics: Dirs with language == codegenLanguage should go first
74- val languageOrder = (o1.expectedLanguage == model.codegenLanguage).compareTo(o2.expectedLanguage == model.codegenLanguage)
75- if (languageOrder != 0 ) return - languageOrder
76- // Heuristics: move root that is 'closer' to module 'common' directory to the first position
77- return - StringUtil .commonPrefixLength(commonModuleSourceDirectory, o1.dir.toNioPath().toString())
78- .compareTo(StringUtil .commonPrefixLength(commonModuleSourceDirectory, o2.dir.toNioPath().toString()))
79- }
80- }).toMutableList()
81-
82- val theBest = if (testRoots.isNotEmpty()) testRoots[0 ] else null
83-
84- // The second sorting to make full list ordered
85- testRoots.sortWith(compareByDescending<TestSourceRoot > {
86- // Heuristics: Dirs with language == codegenLanguage should go first
87- it.expectedLanguage == model.codegenLanguage
88- }.thenBy {
89- // ABC-sorting
90- it.dir.toNioPath()
91- }
92- )
93- // The best candidate should go first to be pre-selected
94- theBest?.let {
95- testRoots.remove(it)
96- testRoots.add(0 , it)
97- }
62+ val testRoots = model.getSortedTestRoots()
9863
9964 // this method is blocked for Gradle, where multiple test modules can exist
10065 model.testModule.addDedicatedTestRoot(testRoots, model.codegenLanguage)
@@ -122,6 +87,33 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
12287 }
12388 }
12489
90+ private fun GenerateTestsModel.getSortedTestRoots (): MutableList <TestSourceRoot > {
91+ var commonModuleSourceDirectory = " "
92+ for ((i, sourceRoot) in srcModule.rootManager.sourceRoots.withIndex()) {
93+ commonModuleSourceDirectory = if (i == 0 ) {
94+ sourceRoot.toNioPath().toString()
95+ } else {
96+ StringUtil .commonPrefix(commonModuleSourceDirectory, sourceRoot.toNioPath().toString())
97+ }
98+ }
99+
100+ return getAllTestSourceRoots().distinct().toMutableList().sortedWith(
101+ compareByDescending<TestSourceRoot > {
102+ // Heuristics: Dirs with proper code language should go first
103+ it.expectedLanguage == codegenLanguage
104+ }.thenByDescending {
105+ // Heuristics: Dirs from within module 'common' directory should go first
106+ it.dir.toNioPath().toString().startsWith(commonModuleSourceDirectory)
107+ }.thenByDescending {
108+ // Heuristics: dedicated test source root named 'utbot_tests' should go first
109+ it.dir.name == dedicatedTestSourceRootName
110+ }.thenBy {
111+ // ABC-sorting
112+ it.dir.toNioPath()
113+ }
114+ ).toMutableList()
115+ }
116+
125117 private fun chooseTestRoot (model : GenerateTestsModel ): VirtualFile ? =
126118 ReadAction .compute<VirtualFile , RuntimeException > {
127119 val desc = object : FileChooserDescriptor (false , true , false , false , false , false ) {
0 commit comments