Skip to content

Commit 9a110cd

Browse files
Put all test sources from the module at the top Test sources root lis… (#1322)
* Put all test sources from the module at the top Test sources root list #1294 * Put all test sources from the module at the top Test sources root list #1294 Cleanup & refactoring
1 parent 304b3ba commit 9a110cd

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

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

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.intellij.ui.SimpleTextAttributes
1616
import com.intellij.util.ArrayUtil
1717
import com.intellij.util.ui.UIUtil
1818
import java.io.File
19-
import java.util.Comparator
2019
import javax.swing.DefaultComboBoxModel
2120
import javax.swing.JList
2221
import org.jetbrains.kotlin.idea.util.rootManager
@@ -25,6 +24,7 @@ import org.utbot.intellij.plugin.generator.CodeGenerationController.getAllTestSo
2524
import org.utbot.intellij.plugin.models.GenerateTestsModel
2625
import org.utbot.intellij.plugin.ui.utils.TestSourceRoot
2726
import org.utbot.intellij.plugin.ui.utils.addDedicatedTestRoot
27+
import org.utbot.intellij.plugin.ui.utils.dedicatedTestSourceRootName
2828
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
2929

3030
class 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) {

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/ModuleUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ val Project.isBuildWithGradle get() =
163163
ExternalSystemApiUtil.isExternalSystemAwareModule(GRADLE_SYSTEM_ID, it)
164164
}
165165

166-
private const val dedicatedTestSourceRootName = "utbot_tests"
166+
const val dedicatedTestSourceRootName = "utbot_tests"
167167

168168
fun Module.addDedicatedTestRoot(testSourceRoots: MutableList<TestSourceRoot>, language: CodegenLanguage): VirtualFile? {
169169
// Don't suggest new test source roots for Gradle project where 'unexpected' test roots won't work

0 commit comments

Comments
 (0)