Skip to content

Commit d4f95c1

Browse files
committed
Existing test file is not overwritten closes
1 parent fc99784 commit d4f95c1

File tree

1 file changed

+48
-56
lines changed
  • utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator

1 file changed

+48
-56
lines changed

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

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,28 @@
11
package org.utbot.intellij.plugin.generator
22

3-
import org.utbot.common.HTML_LINE_SEPARATOR
4-
import org.utbot.common.PathUtil.classFqnToPath
5-
import org.utbot.common.PathUtil.toHtmlLinkTag
6-
import org.utbot.common.appendHtmlLine
7-
import org.utbot.framework.codegen.Import
8-
import org.utbot.framework.codegen.ParametrizedTestSource
9-
import org.utbot.framework.codegen.StaticImport
10-
import org.utbot.framework.codegen.TestsCodeWithTestReport
11-
import org.utbot.framework.codegen.model.ModelBasedTestCodeGenerator
12-
import org.utbot.framework.codegen.model.constructor.tree.TestsGenerationReport
13-
import org.utbot.framework.plugin.api.CodegenLanguage
14-
import org.utbot.framework.plugin.api.UtTestCase
15-
import org.utbot.intellij.plugin.sarif.SarifReportIdea
16-
import org.utbot.intellij.plugin.sarif.SourceFindingStrategyIdea
17-
import org.utbot.intellij.plugin.settings.Settings
18-
import org.utbot.intellij.plugin.ui.utils.getOrCreateSarifReportsPath
19-
import org.utbot.intellij.plugin.ui.utils.getOrCreateTestResourcesPath
20-
import org.utbot.sarif.SarifReport
213
import com.intellij.codeInsight.CodeInsightUtil
224
import com.intellij.codeInsight.FileModificationService
235
import com.intellij.ide.fileTemplates.FileTemplateManager
246
import com.intellij.ide.fileTemplates.FileTemplateUtil
257
import com.intellij.ide.fileTemplates.JavaTemplateUtil
8+
import com.intellij.openapi.application.ApplicationManager
269
import com.intellij.openapi.command.WriteCommandAction.runWriteCommandAction
2710
import com.intellij.openapi.command.executeCommand
2811
import com.intellij.openapi.components.service
2912
import com.intellij.openapi.editor.Document
3013
import com.intellij.openapi.editor.Editor
31-
import com.intellij.openapi.project.DumbServiceImpl
14+
import com.intellij.openapi.project.DumbService
3215
import com.intellij.openapi.project.Project
16+
import com.intellij.openapi.util.Computable
3317
import com.intellij.openapi.vfs.VfsUtil
34-
import com.intellij.psi.JavaDirectoryService
35-
import com.intellij.psi.PsiClass
36-
import com.intellij.psi.PsiClassOwner
37-
import com.intellij.psi.PsiDirectory
38-
import com.intellij.psi.PsiDocumentManager
39-
import com.intellij.psi.PsiElement
40-
import com.intellij.psi.PsiFile
41-
import com.intellij.psi.PsiMethod
18+
import com.intellij.psi.*
4219
import com.intellij.psi.codeStyle.CodeStyleManager
4320
import com.intellij.psi.codeStyle.JavaCodeStyleManager
4421
import com.intellij.psi.search.GlobalSearchScopesCore
4522
import com.intellij.testIntegration.TestIntegrationUtils
4623
import com.intellij.util.IncorrectOperationException
4724
import com.intellij.util.io.exists
4825
import com.siyeh.ig.psiutils.ImportUtils
49-
import java.nio.file.Paths
5026
import org.jetbrains.kotlin.asJava.classes.KtUltraLightClass
5127
import org.jetbrains.kotlin.idea.core.ShortenReferences
5228
import org.jetbrains.kotlin.idea.core.getPackage
@@ -59,18 +35,31 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
5935
import org.jetbrains.kotlin.psi.psiUtil.endOffset
6036
import org.jetbrains.kotlin.psi.psiUtil.startOffset
6137
import org.jetbrains.kotlin.scripting.resolve.classId
38+
import org.utbot.common.HTML_LINE_SEPARATOR
39+
import org.utbot.common.PathUtil.classFqnToPath
40+
import org.utbot.common.PathUtil.toHtmlLinkTag
41+
import org.utbot.common.appendHtmlLine
42+
import org.utbot.framework.codegen.Import
43+
import org.utbot.framework.codegen.ParametrizedTestSource
44+
import org.utbot.framework.codegen.StaticImport
45+
import org.utbot.framework.codegen.TestsCodeWithTestReport
46+
import org.utbot.framework.codegen.model.ModelBasedTestCodeGenerator
47+
import org.utbot.framework.codegen.model.constructor.tree.TestsGenerationReport
48+
import org.utbot.framework.plugin.api.CodegenLanguage
49+
import org.utbot.framework.plugin.api.UtTestCase
6250
import org.utbot.intellij.plugin.error.showErrorDialogLater
63-
import org.utbot.intellij.plugin.ui.GenerateTestsModel
64-
import org.utbot.intellij.plugin.ui.SarifReportNotifier
65-
import org.utbot.intellij.plugin.ui.TestReportUrlOpeningListener
66-
import org.utbot.intellij.plugin.ui.TestsReportNotifier
67-
import org.utbot.intellij.plugin.ui.packageName
51+
import org.utbot.intellij.plugin.sarif.SarifReportIdea
52+
import org.utbot.intellij.plugin.sarif.SourceFindingStrategyIdea
53+
import org.utbot.intellij.plugin.settings.Settings
54+
import org.utbot.intellij.plugin.ui.*
55+
import org.utbot.intellij.plugin.ui.utils.getOrCreateSarifReportsPath
56+
import org.utbot.intellij.plugin.ui.utils.getOrCreateTestResourcesPath
57+
import org.utbot.sarif.SarifReport
58+
import java.nio.file.Paths
6859

6960
object TestGenerator {
7061
fun generateTests(model: GenerateTestsModel, testCases: Map<PsiClass, List<UtTestCase>>) {
71-
runWriteCommandAction(model.project, "Generate tests with UtBot", null, {
72-
generateTestsInternal(model, testCases)
73-
})
62+
generateTestsInternal(model, testCases)
7463
}
7564

7665
private fun generateTestsInternal(model: GenerateTestsModel, testCasesByClass: Map<PsiClass, List<UtTestCase>>) {
@@ -87,7 +76,9 @@ object TestGenerator {
8776
val testClass = createTestClass(srcClass, testDirectory, model) ?: continue
8877
val file = testClass.containingFile
8978

90-
addTestMethodsAndSaveReports(testClass, file, testCases, model)
79+
runWriteCommandAction(model.project, "Generate tests with UtBot", null, {
80+
addTestMethodsAndSaveReports(testClass, file, testCases, model)
81+
})
9182
} catch (e: IncorrectOperationException) {
9283
showCreatingClassError(model.project, createTestClassName(srcClass))
9384
}
@@ -141,25 +132,26 @@ object TestGenerator {
141132
if (aPackage != null) {
142133
val scope = GlobalSearchScopesCore.directoryScope(testDirectory, false)
143134

144-
// Here we use firstOrNull(), because by some unknown reason
145-
// findClassByShortName() may return two identical objects.
146-
// Be careful, do not use singleOrNull() here, because it expects
147-
// the array to contain strictly one element and otherwise returns null.
148-
var testClass: PsiClass? = null
149-
150-
// runWhenSmart to avoid IndexNotReadyException
151-
DumbServiceImpl(model.project).runWhenSmart {
152-
testClass = aPackage.findClassByShortName(testClassName, scope)
153-
.firstOrNull {
154-
when (model.codegenLanguage) {
155-
CodegenLanguage.JAVA -> it !is KtUltraLightClass
156-
CodegenLanguage.KOTLIN -> it is KtUltraLightClass
157-
}
158-
}
159-
}
160-
testClass?.let {
161-
return if (FileModificationService.getInstance().preparePsiElementForWrite(it)) it else null
162-
}
135+
val application = ApplicationManager.getApplication()
136+
val testClass = application.executeOnPooledThread<PsiClass?> {
137+
return@executeOnPooledThread application.runReadAction<PsiClass?> {
138+
DumbService.getInstance(model.project).runReadActionInSmartMode(Computable<PsiClass?> {
139+
// Here we use firstOrNull(), because by some unknown reason
140+
// findClassByShortName() may return two identical objects.
141+
// Be careful, do not use singleOrNull() here, because it expects
142+
// the array to contain strictly one element and otherwise returns null.
143+
return@Computable aPackage.findClassByShortName(testClassName, scope)
144+
.firstOrNull {
145+
when (model.codegenLanguage) {
146+
CodegenLanguage.JAVA -> it !is KtUltraLightClass
147+
CodegenLanguage.KOTLIN -> it is KtUltraLightClass
148+
}
149+
}
150+
})
151+
}
152+
}.get()
153+
154+
return if (FileModificationService.getInstance().preparePsiElementForWrite(testClass)) testClass else null
163155
}
164156

165157
val fileTemplate = FileTemplateManager.getInstance(testDirectory.project).getInternalTemplate(

0 commit comments

Comments
 (0)