Skip to content

Commit d078ba8

Browse files
Enlarge compile scope for Spring projects (#2122)
1 parent 3e3cf0a commit d078ba8

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,24 @@ object UtTestsDialogProcessor {
139139
return GenerateTestsDialogWindow(model)
140140
}
141141

142-
private fun compile(project: Project, files: Array<VirtualFile>): Promise<ProjectTaskManager.Result> {
143-
val wholeModules = MavenProjectsManager.getInstance(project)?.hasProjects()?:false
142+
private fun compile(
143+
project: Project,
144+
files: Array<VirtualFile>,
145+
springConfigClass: PsiClass?,
146+
): Promise<ProjectTaskManager.Result> {
147+
// For Maven project narrow compile scope may not work, see https://github.com/UnitTestBot/UTBotJava/issues/2021.
148+
// For Spring project classes may contain `@ComponentScan` annotations, so we need to compile the whole module.
149+
val isMavenProject = MavenProjectsManager.getInstance(project)?.hasProjects() ?: false
150+
val isSpringProject = springConfigClass != null
151+
val wholeModules = isMavenProject || isSpringProject
152+
144153
val buildTasks = ContainerUtil.map<Map.Entry<Module?, List<VirtualFile>>, ProjectTask>(
145154
Arrays.stream(files).collect(Collectors.groupingBy { file: VirtualFile ->
146155
ProjectFileIndex.getInstance(project).getModuleForFile(file, false)
147156
}).entries
148157
) { (key, value): Map.Entry<Module?, List<VirtualFile>?> ->
149158
if (wholeModules) {
150-
// Maven-specific case, we have to compile the whole module
159+
// This is a specific case, we have to compile the whole module
151160
ModuleBuildTaskImpl(key!!, false)
152161
} else {
153162
// Compile only chosen classes and their dependencies before generation.
@@ -166,7 +175,10 @@ object UtTestsDialogProcessor {
166175
error("Can't find configuration class $it")
167176
}
168177
}
169-
val promise = compile(project, (model.srcClasses + listOfNotNull(springConfigClass)).map { it.containingFile.virtualFile }.toTypedArray())
178+
179+
val filesToCompile = model.srcClasses.map { it.containingFile.virtualFile }.toTypedArray()
180+
181+
val promise = compile(project, filesToCompile, springConfigClass)
170182
promise.onSuccess {
171183
if (it.hasErrors() || it.isAborted)
172184
return@onSuccess

0 commit comments

Comments
 (0)