1
1
package org.utbot.intellij.plugin.generator
2
2
3
+ import com.intellij.analysis.AnalysisScope
3
4
import com.intellij.codeInsight.CodeInsightUtil
4
5
import com.intellij.codeInsight.FileModificationService
5
6
import com.intellij.ide.fileTemplates.FileTemplateManager
6
7
import com.intellij.ide.fileTemplates.FileTemplateUtil
7
8
import com.intellij.ide.fileTemplates.JavaTemplateUtil
8
9
import com.intellij.ide.highlighter.JavaFileType
9
10
import com.intellij.openapi.application.ApplicationManager
11
+ import com.intellij.openapi.application.invokeLater
10
12
import com.intellij.openapi.application.runReadAction
11
13
import com.intellij.openapi.application.runWriteAction
12
14
import com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction
@@ -51,12 +53,12 @@ import org.utbot.framework.codegen.model.UtilClassKind
51
53
import org.utbot.framework.codegen.model.UtilClassKind.Companion.UT_UTILS_CLASS_NAME
52
54
import org.utbot.framework.plugin.api.ClassId
53
55
import org.utbot.framework.plugin.api.CodegenLanguage
56
+ import org.utbot.intellij.plugin.inspection.UnitTestBotInspectionManager
54
57
import org.utbot.intellij.plugin.models.GenerateTestsModel
55
58
import org.utbot.intellij.plugin.models.packageName
56
59
import org.utbot.intellij.plugin.process.EngineProcess
57
60
import org.utbot.intellij.plugin.process.RdTestGenerationResult
58
61
import org.utbot.intellij.plugin.sarif.SarifReportIdea
59
- import org.utbot.intellij.plugin.sarif.SourceFindingStrategyIdea
60
62
import org.utbot.intellij.plugin.ui.*
61
63
import org.utbot.intellij.plugin.ui.utils.getOrCreateSarifReportsPath
62
64
import org.utbot.intellij.plugin.ui.utils.showErrorDialogLater
@@ -65,6 +67,7 @@ import org.utbot.intellij.plugin.util.IntelliJApiHelper.Target.*
65
67
import org.utbot.intellij.plugin.util.IntelliJApiHelper.run
66
68
import org.utbot.intellij.plugin.util.RunConfigurationHelper
67
69
import org.utbot.intellij.plugin.util.extractClassMethodsIncludingNested
70
+ import org.utbot.sarif.Sarif
68
71
import org.utbot.sarif.SarifReport
69
72
import java.nio.file.Path
70
73
import java.util.concurrent.CancellationException
@@ -94,6 +97,7 @@ object CodeGenerationController {
94
97
val allTestPackages = getPackageDirectories(baseTestDirectory)
95
98
val latch = CountDownLatch (classesWithTests.size)
96
99
val testFilesPointers = mutableListOf<SmartPsiElementPointer <PsiFile >>()
100
+ val srcClassPathToSarifReport = mutableMapOf<Path , Sarif >()
97
101
val utilClassListener = UtilClassListener ()
98
102
var index = 0
99
103
for ((srcClass, generateResult) in classesWithTests) {
@@ -119,6 +123,7 @@ object CodeGenerationController {
119
123
cut,
120
124
testClass,
121
125
testFilePointer,
126
+ srcClassPathToSarifReport,
122
127
model,
123
128
latch,
124
129
utilClassListener,
@@ -153,6 +158,10 @@ object CodeGenerationController {
153
158
}
154
159
proc.forceTermination()
155
160
UtTestsDialogProcessor .updateIndicator(indicator, UtTestsDialogProcessor .ProgressRange .SARIF , " Start tests with coverage" , 1.0 )
161
+
162
+ invokeLater {
163
+ runInspectionsIfNeeded(model.project, srcClassPathToSarifReport)
164
+ }
156
165
}
157
166
}
158
167
}
@@ -161,6 +170,25 @@ object CodeGenerationController {
161
170
}
162
171
}
163
172
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
+
164
192
private fun proceedTestReport (proc : EngineProcess , model : GenerateTestsModel ) {
165
193
try {
166
194
// Parametrized tests are not supported in tests report yet
@@ -583,6 +611,7 @@ object CodeGenerationController {
583
611
classUnderTest : ClassId ,
584
612
testClass : PsiClass ,
585
613
filePointer : SmartPsiElementPointer <PsiFile >,
614
+ srcClassPathToSarifReport : MutableMap <Path , Sarif >,
586
615
model : GenerateTestsModel ,
587
616
reportsCountDown : CountDownLatch ,
588
617
utilClassListener : UtilClassListener ,
@@ -661,16 +690,20 @@ object CodeGenerationController {
661
690
// uploading formatted code
662
691
val file = filePointer.containingFile
663
692
664
- saveSarifReport(
693
+ val srcClassPath = srcClass.containingFile.virtualFile.toNioPath()
694
+ val sarifReport = saveSarifReport(
665
695
proc,
666
696
testSetsId,
667
697
testClassUpdated,
668
698
classUnderTest,
669
699
model,
670
700
reportsCountDown,
671
701
file?.text ? : generatedTestsCode,
702
+ srcClassPathToSarifReport,
703
+ srcClassPath,
672
704
indicator
673
705
)
706
+
674
707
unblockDocument(testClassUpdated.project, editor.document)
675
708
}
676
709
}
@@ -706,13 +739,26 @@ object CodeGenerationController {
706
739
model : GenerateTestsModel ,
707
740
reportsCountDown : CountDownLatch ,
708
741
generatedTestsCode : String ,
742
+ srcClassPathToSarifReport : MutableMap <Path , Sarif >,
743
+ srcClassPath : Path ,
709
744
indicator : ProgressIndicator
710
745
) {
711
746
val project = model.project
712
747
713
748
try {
714
749
// 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
+ )
716
762
} catch (e: Exception ) {
717
763
logger.error(e) { " error in saving sarif report" }
718
764
showErrorDialogLater(
0 commit comments