Skip to content

Commit 7c6ab37

Browse files
committed
Move testsourceroot-choosing heuristics from ModuleUtils to TestFolderCombo
1 parent e68a3c9 commit 7c6ab37

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ object CodeGenerationController {
416416
// all test roots for the given test module
417417
val testRoots = runReadAction {
418418
testModule
419-
.suitableTestSourceRoots(this)
420-
.mapNotNull { psiManager.findDirectory(it) }
419+
.suitableTestSourceRoots()
420+
.mapNotNull { psiManager.findDirectory(it.dir) }
421421
}
422422

423423
// return an util class from one of the test source roots or null if no util class was found

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import javax.swing.DefaultComboBoxModel
1818
import javax.swing.JList
1919
import org.jetbrains.kotlin.idea.util.projectStructure.allModules
2020
import org.utbot.common.PathUtil
21-
import org.utbot.framework.plugin.api.CodegenLanguage
2221
import org.utbot.intellij.plugin.models.GenerateTestsModel
22+
import org.utbot.intellij.plugin.ui.utils.TestSourceRoot
2323
import org.utbot.intellij.plugin.ui.utils.addDedicatedTestRoot
2424
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
2525
import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
@@ -59,19 +59,20 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
5959
val suggestedModules =
6060
if (model.project.isBuildWithGradle) model.project.allModules() else model.potentialTestModules
6161

62-
val testRootsByLanguage = CodegenLanguage.allItems.associateWith { language ->
63-
suggestedModules.flatMap { module ->
64-
module.suitableTestSourceRoots(language)
62+
val testRoots = suggestedModules.flatMap {
63+
it.suitableTestSourceRoots()
64+
}.sortedWith(
65+
compareByDescending<TestSourceRoot> {
66+
// Heuristics: Dirs with language == codegenLanguage should go first
67+
it.expectedLanguage == model.codegenLanguage
68+
}.thenBy {
69+
// Heuristics: User is more likely to choose the shorter path
70+
it.dir.path.length
6571
}
66-
}
67-
68-
// testRoots for default codegen language should go before other testRoots (this impacts default-selected test root item)
69-
val testRoots = with (testRootsByLanguage.entries) {
70-
filter { it.key == model.codegenLanguage } + filter { it.key != model.codegenLanguage }
71-
}.flatMap { it.value }.toMutableList()
72+
).toMutableList()
7273

7374
// this method is blocked for Gradle, where multiple test modules can exist
74-
model.testModule.addDedicatedTestRoot(testRoots)
75+
model.testModule.addDedicatedTestRoot(testRoots, model.codegenLanguage)
7576

7677
if (testRoots.isNotEmpty()) {
7778
configureRootsCombo(testRoots)
@@ -105,12 +106,12 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
105106
files.singleOrNull()
106107
}
107108

108-
private fun configureRootsCombo(testRoots: List<VirtualFile>) {
109+
private fun configureRootsCombo(testRoots: List<TestSourceRoot>) {
109110
val selectedRoot = testRoots.first()
110111

111112
// do not update model.testModule here, because fake test source root could have been chosen
112-
model.testSourceRoot = selectedRoot
113-
newItemList(testRoots.toSet())
113+
model.testSourceRoot = selectedRoot.dir
114+
newItemList(testRoots.map { it.dir }.toSet())
114115
}
115116

116117
private fun newItemList(comboItems: Set<Any>) {

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

+24-17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ import org.jetbrains.kotlin.platform.TargetPlatformVersion
3737

3838
private val logger = KotlinLogging.logger {}
3939

40+
data class TestSourceRoot(
41+
val dir: VirtualFile,
42+
val expectedLanguage: CodegenLanguage
43+
)
44+
4045
/**
4146
* @return jdk version of the module
4247
*/
@@ -60,12 +65,6 @@ fun Module.kotlinTargetPlatform(): TargetPlatformVersion {
6065
?.singleOrNull() ?: error("Can't determine target platform for module $this")
6166
}
6267

63-
fun Module.suitableTestSourceRoots(): List<VirtualFile> =
64-
suitableTestSourceRoots(CodegenLanguage.JAVA) + suitableTestSourceRoots(CodegenLanguage.KOTLIN)
65-
66-
fun Module.suitableTestSourceFolders(): List<SourceFolder> =
67-
suitableTestSourceFolders(CodegenLanguage.JAVA) + suitableTestSourceFolders(CodegenLanguage.KOTLIN)
68-
6968
/**
7069
* Gets a path to test resources source root.
7170
*
@@ -121,8 +120,8 @@ private fun findPotentialModulesForTests(project: Project, srcModule: Module): L
121120
/**
122121
* Finds all suitable test root virtual files.
123122
*/
124-
fun Module.suitableTestSourceRoots(codegenLanguage: CodegenLanguage): List<VirtualFile> {
125-
val sourceRootsInModule = suitableTestSourceFolders(codegenLanguage).mapNotNull { it.file }
123+
fun Module.suitableTestSourceRoots(): List<TestSourceRoot> {
124+
val sourceRootsInModule = suitableTestSourceFolders().mapNotNull { it.testSourceRoot }
126125

127126
if (sourceRootsInModule.isNotEmpty()) {
128127
return sourceRootsInModule
@@ -133,11 +132,20 @@ fun Module.suitableTestSourceRoots(codegenLanguage: CodegenLanguage): List<Virtu
133132
ModuleUtilCore.collectModulesDependsOn(this, dependentModules)
134133

135134
return dependentModules
136-
.flatMap { it.suitableTestSourceFolders(codegenLanguage) }
137-
.mapNotNull { it.file }
135+
.flatMap { it.suitableTestSourceFolders() }
136+
.mapNotNull { it.testSourceRoot }
138137
}
139138

140-
private fun Module.suitableTestSourceFolders(codegenLanguage: CodegenLanguage): List<SourceFolder> {
139+
private val SourceFolder.testSourceRoot:TestSourceRoot?
140+
get() {
141+
val file = file
142+
val expectedLanguage = expectedLanguageForTests
143+
if (file != null && expectedLanguage != null)
144+
return TestSourceRoot(file, expectedLanguage)
145+
return null
146+
}
147+
148+
private fun Module.suitableTestSourceFolders(): List<SourceFolder> {
141149
val sourceFolders = ModuleRootManager.getInstance(this)
142150
.contentEntries
143151
.flatMap { it.sourceFolders.toList() }
@@ -146,10 +154,8 @@ private fun Module.suitableTestSourceFolders(codegenLanguage: CodegenLanguage):
146154
return sourceFolders
147155
.filterNot { it.isForGeneratedSources() }
148156
.filter { it.isTestSource }
149-
.filter { it.expectedLanguageForTests == codegenLanguage }
150-
// Heuristics: User is more likely to choose the shorter path
151-
.sortedBy { it.url.length }
152157
}
158+
153159
private val GRADLE_SYSTEM_ID = ProjectSystemId("GRADLE")
154160

155161
val Project.isBuildWithGradle get() =
@@ -158,19 +164,20 @@ val Project.isBuildWithGradle get() =
158164
}
159165

160166
private const val dedicatedTestSourceRootName = "utbot_tests"
161-
fun Module.addDedicatedTestRoot(testSourceRoots: MutableList<VirtualFile>): VirtualFile? {
167+
168+
fun Module.addDedicatedTestRoot(testSourceRoots: MutableList<TestSourceRoot>, language: CodegenLanguage): VirtualFile? {
162169
// Don't suggest new test source roots for Gradle project where 'unexpected' test roots won't work
163170
if (project.isBuildWithGradle) return null
164171
// Dedicated test root already exists
165-
if (testSourceRoots.any { file -> file.name == dedicatedTestSourceRootName }) return null
172+
if (testSourceRoots.any { root -> root.dir.name == dedicatedTestSourceRootName }) return null
166173

167174
val moduleInstance = ModuleRootManager.getInstance(this)
168175
val testFolder = moduleInstance.contentEntries.flatMap { it.sourceFolders.toList() }
169176
.firstOrNull { it.rootType in testSourceRootTypes }
170177
(testFolder?.let { testFolder.file?.parent }
171178
?: testFolder?.contentEntry?.file ?: this.guessModuleDir())?.let {
172179
val file = FakeVirtualFile(it, dedicatedTestSourceRootName)
173-
testSourceRoots.add(file)
180+
testSourceRoots.add(TestSourceRoot(file, language))
174181
// We return "true" IFF it's case of not yet created fake directory
175182
return if (VfsUtil.findRelativeFile(it, dedicatedTestSourceRootName) == null) file else null
176183
}

0 commit comments

Comments
 (0)