11package org.utbot.intellij.plugin.generator
22
3+ import com.intellij.analysis.AnalysisScope
34import com.intellij.codeInsight.CodeInsightUtil
45import com.intellij.codeInsight.FileModificationService
56import com.intellij.ide.fileTemplates.FileTemplateManager
67import com.intellij.ide.fileTemplates.FileTemplateUtil
78import com.intellij.ide.fileTemplates.JavaTemplateUtil
89import com.intellij.ide.highlighter.JavaFileType
910import com.intellij.openapi.application.ApplicationManager
11+ import com.intellij.openapi.application.invokeLater
1012import com.intellij.openapi.application.runReadAction
1113import com.intellij.openapi.application.runWriteAction
1214import com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction
@@ -51,12 +53,12 @@ import org.utbot.framework.codegen.model.UtilClassKind
5153import org.utbot.framework.codegen.model.UtilClassKind.Companion.UT_UTILS_CLASS_NAME
5254import org.utbot.framework.plugin.api.ClassId
5355import org.utbot.framework.plugin.api.CodegenLanguage
56+ import org.utbot.intellij.plugin.inspection.UnitTestBotInspectionManager
5457import org.utbot.intellij.plugin.models.GenerateTestsModel
5558import org.utbot.intellij.plugin.models.packageName
5659import org.utbot.intellij.plugin.process.EngineProcess
5760import org.utbot.intellij.plugin.process.RdTestGenerationResult
5861import org.utbot.intellij.plugin.sarif.SarifReportIdea
59- import org.utbot.intellij.plugin.sarif.SourceFindingStrategyIdea
6062import org.utbot.intellij.plugin.ui.*
6163import org.utbot.intellij.plugin.ui.utils.getOrCreateSarifReportsPath
6264import org.utbot.intellij.plugin.ui.utils.showErrorDialogLater
@@ -65,6 +67,7 @@ import org.utbot.intellij.plugin.util.IntelliJApiHelper.Target.*
6567import org.utbot.intellij.plugin.util.IntelliJApiHelper.run
6668import org.utbot.intellij.plugin.util.RunConfigurationHelper
6769import org.utbot.intellij.plugin.util.extractClassMethodsIncludingNested
70+ import org.utbot.sarif.Sarif
6871import org.utbot.sarif.SarifReport
6972import java.nio.file.Path
7073import java.util.concurrent.CancellationException
@@ -94,6 +97,7 @@ object CodeGenerationController {
9497 val allTestPackages = getPackageDirectories(baseTestDirectory)
9598 val latch = CountDownLatch (classesWithTests.size)
9699 val testFilesPointers = mutableListOf<SmartPsiElementPointer <PsiFile >>()
100+ val srcClassPathToSarifReport = mutableMapOf<Path , Sarif >()
97101 val utilClassListener = UtilClassListener ()
98102 var index = 0
99103 for ((srcClass, generateResult) in classesWithTests) {
@@ -119,6 +123,7 @@ object CodeGenerationController {
119123 cut,
120124 testClass,
121125 testFilePointer,
126+ srcClassPathToSarifReport,
122127 model,
123128 latch,
124129 utilClassListener,
@@ -153,6 +158,10 @@ object CodeGenerationController {
153158 }
154159 proc.forceTermination()
155160 UtTestsDialogProcessor .updateIndicator(indicator, UtTestsDialogProcessor .ProgressRange .SARIF , " Start tests with coverage" , 1.0 )
161+
162+ invokeLater {
163+ runInspectionsIfNeeded(model.project, srcClassPathToSarifReport)
164+ }
156165 }
157166 }
158167 }
@@ -161,6 +170,25 @@ object CodeGenerationController {
161170 }
162171 }
163172
173+ /* *
174+ * Runs the UTBot inspection if there are detected errors.
175+ */
176+ private fun runInspectionsIfNeeded (
177+ project : Project ,
178+ srcClassPathToSarifReport : MutableMap <Path , Sarif >
179+ ) {
180+ val sarifHasResults = srcClassPathToSarifReport.any { (_, sarif) ->
181+ sarif.getAllResults().isNotEmpty()
182+ }
183+ if (! sarifHasResults) {
184+ return
185+ }
186+ UnitTestBotInspectionManager
187+ .getInstance(project, srcClassPathToSarifReport)
188+ .createNewGlobalContext()
189+ .doInspections(AnalysisScope (project))
190+ }
191+
164192 private fun proceedTestReport (proc : EngineProcess , model : GenerateTestsModel ) {
165193 try {
166194 // Parametrized tests are not supported in tests report yet
@@ -583,6 +611,7 @@ object CodeGenerationController {
583611 classUnderTest : ClassId ,
584612 testClass : PsiClass ,
585613 filePointer : SmartPsiElementPointer <PsiFile >,
614+ srcClassPathToSarifReport : MutableMap <Path , Sarif >,
586615 model : GenerateTestsModel ,
587616 reportsCountDown : CountDownLatch ,
588617 utilClassListener : UtilClassListener ,
@@ -661,16 +690,20 @@ object CodeGenerationController {
661690 // uploading formatted code
662691 val file = filePointer.containingFile
663692
664- saveSarifReport(
693+ val srcClassPath = srcClass.containingFile.virtualFile.toNioPath()
694+ val sarifReport = saveSarifReport(
665695 proc,
666696 testSetsId,
667697 testClassUpdated,
668698 classUnderTest,
669699 model,
670700 reportsCountDown,
671701 file?.text ? : generatedTestsCode,
702+ srcClassPathToSarifReport,
703+ srcClassPath,
672704 indicator
673705 )
706+
674707 unblockDocument(testClassUpdated.project, editor.document)
675708 }
676709 }
@@ -706,13 +739,26 @@ object CodeGenerationController {
706739 model : GenerateTestsModel ,
707740 reportsCountDown : CountDownLatch ,
708741 generatedTestsCode : String ,
742+ srcClassPathToSarifReport : MutableMap <Path , Sarif >,
743+ srcClassPath : Path ,
709744 indicator : ProgressIndicator
710745 ) {
711746 val project = model.project
712747
713748 try {
714749 // saving sarif report
715- SarifReportIdea .createAndSave(proc, testSetsId, testClassId, model, generatedTestsCode, testClass, reportsCountDown, indicator)
750+ SarifReportIdea .createAndSave(
751+ proc,
752+ testSetsId,
753+ testClassId,
754+ model,
755+ generatedTestsCode,
756+ testClass,
757+ reportsCountDown,
758+ srcClassPathToSarifReport,
759+ srcClassPath,
760+ indicator
761+ )
716762 } catch (e: Exception ) {
717763 logger.error(e) { " error in saving sarif report" }
718764 showErrorDialogLater(
0 commit comments