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
@@ -51,6 +52,7 @@ import org.utbot.framework.codegen.model.UtilClassKind
5152import org.utbot.framework.codegen.model.UtilClassKind.Companion.UT_UTILS_CLASS_NAME
5253import org.utbot.framework.plugin.api.ClassId
5354import org.utbot.framework.plugin.api.CodegenLanguage
55+ import org.utbot.intellij.plugin.inspection.UTBotInspectionManager
5456import org.utbot.intellij.plugin.models.GenerateTestsModel
5557import org.utbot.intellij.plugin.models.packageName
5658import org.utbot.intellij.plugin.process.EngineProcess
@@ -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,
@@ -151,6 +156,7 @@ object CodeGenerationController {
151156 UtTestsDialogProcessor .updateIndicator(indicator, UtTestsDialogProcessor .ProgressRange .SARIF , " Start tests with coverage" , 0.95 )
152157 RunConfigurationHelper .runTestsWithCoverage(model, testFilesPointers)
153158 }
159+ runInspectionsIfNeeded(model.project, srcClassPathToSarifReport) // TODO
154160 proc.forceTermination()
155161 UtTestsDialogProcessor .updateIndicator(indicator, UtTestsDialogProcessor .ProgressRange .SARIF , " Start tests with coverage" , 1.0 )
156162 }
@@ -161,6 +167,25 @@ object CodeGenerationController {
161167 }
162168 }
163169
170+ /* *
171+ * Runs the UTBot inspection if there are detected errors.
172+ */
173+ private fun runInspectionsIfNeeded (
174+ project : Project ,
175+ srcClassPathToSarifReport : MutableMap <Path , Sarif >
176+ ) {
177+ val sarifHasResults = srcClassPathToSarifReport.any { (_, sarif) ->
178+ sarif.getAllResults().isNotEmpty()
179+ }
180+ if (! sarifHasResults) {
181+ return
182+ }
183+ UTBotInspectionManager
184+ .getInstance(project, srcClassPathToSarifReport)
185+ .createNewGlobalContext()
186+ .doInspections(AnalysisScope (project))
187+ }
188+
164189 private fun proceedTestReport (proc : EngineProcess , model : GenerateTestsModel ) {
165190 try {
166191 // Parametrized tests are not supported in tests report yet
@@ -583,6 +608,7 @@ object CodeGenerationController {
583608 classUnderTest : ClassId ,
584609 testClass : PsiClass ,
585610 filePointer : SmartPsiElementPointer <PsiFile >,
611+ srcClassPathToSarifReport : MutableMap <Path , Sarif >,
586612 model : GenerateTestsModel ,
587613 reportsCountDown : CountDownLatch ,
588614 utilClassListener : UtilClassListener ,
@@ -661,7 +687,7 @@ object CodeGenerationController {
661687 // uploading formatted code
662688 val file = filePointer.containingFile
663689
664- saveSarifReport(
690+ val sarifReport = saveSarifReport(
665691 proc,
666692 testSetsId,
667693 testClassUpdated,
@@ -671,6 +697,9 @@ object CodeGenerationController {
671697 file?.text ? : generatedTestsCode,
672698 indicator
673699 )
700+ val srcClassPath = srcClass.containingFile.virtualFile.toNioPath()
701+ srcClassPathToSarifReport[srcClassPath] = sarifReport
702+
674703 unblockDocument(testClassUpdated.project, editor.document)
675704 }
676705 }
@@ -707,19 +736,20 @@ object CodeGenerationController {
707736 reportsCountDown : CountDownLatch ,
708737 generatedTestsCode : String ,
709738 indicator : ProgressIndicator
710- ) {
739+ ): Sarif {
711740 val project = model.project
712741
713- try {
742+ return try {
714743 // saving sarif report
715- SarifReportIdea .createAndSave(proc, testSetsId, testClassId, model, generatedTestsCode, testClass, reportsCountDown, indicator)
744+ SarifReportIdea .createAndSave(proc, testSetsId, testClassId, model, generatedTestsCode, testClass, reportsCountDown, indicator)
716745 } catch (e: Exception ) {
717746 logger.error(e) { " error in saving sarif report" }
718747 showErrorDialogLater(
719748 project,
720749 message = " Cannot save Sarif report via generated tests: error occurred '${e.message} '" ,
721750 title = " Failed to save Sarif report"
722751 )
752+ Sarif .empty()
723753 }
724754 }
725755
0 commit comments