From 1cb59639bd26a9cdcf890602605736dfe244841c Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Tue, 28 Jun 2022 12:17:03 +0300 Subject: [PATCH] Add warning on conflicting test frameworks --- .../tree/CgTestClassConstructor.kt | 13 ++++-- .../plugin/generator/TestGenerator.kt | 44 ++++++++++++++----- .../plugin/ui/GenerateTestsDialogWindow.kt | 2 + .../intellij/plugin/ui/GenerateTestsModel.kt | 3 +- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt index f9a9258fdd..83c77d7d6e 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgTestClassConstructor.kt @@ -186,10 +186,12 @@ data class TestsGenerationReport( var errors: MutableMap, ErrorsCount> = mutableMapOf() ) { val classUnderTest: KClass<*> - get() = executables.firstOrNull()?.clazz ?: error("No executables found in test report") + get() = executables.firstOrNull()?.clazz + ?: error("No executables found in test report") - // Initial message is generated lazily to avoid evaluation of classUnderTest - var initialMessage: () -> String = { "Unit tests for $classUnderTest were generated successfully." } + // Summary message is generated lazily to avoid evaluation of classUnderTest + var summaryMessage: () -> String = { "Unit tests for $classUnderTest were generated successfully." } + val initialWarnings: MutableList<() -> String> = mutableListOf() fun addMethodErrors(testCase: UtTestCase, errors: Map) { this.errors[testCase.method] = errors @@ -215,8 +217,11 @@ data class TestsGenerationReport( } override fun toString(): String = buildString { - appendHtmlLine(initialMessage()) + appendHtmlLine(summaryMessage()) appendHtmlLine() + initialWarnings.forEach { appendHtmlLine(it()) } + appendHtmlLine() + val testMethodsStatistic = executables.map { it.countTestMethods() } val errors = executables.map { it.countErrors() } val overallTestMethods = testMethodsStatistic.sumBy { it.count } diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/TestGenerator.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/TestGenerator.kt index 8e8cb5fe34..118a0d0a01 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/TestGenerator.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/TestGenerator.kt @@ -318,17 +318,8 @@ object TestGenerator { VfsUtil.createDirectories(parent.toString()) resultedReportedPath.toFile().writeText(testsCodeWithTestReport.testsGenerationReport.getFileContent()) - if (model.forceMockHappened) { - testsCodeWithTestReport.testsGenerationReport.apply { - initialMessage = { - """ - Unit tests for $classUnderTest were generated partially.
- Warning: Some test cases were ignored, because no mocking framework is installed in the project.
- Better results could be achieved by installing mocking framework. - """.trimIndent() - } - } - } + processInitialWarnings(testsCodeWithTestReport, model) + val notifyMessage = buildString { appendHtmlLine(testsCodeWithTestReport.testsGenerationReport.toString()) appendHtmlLine() @@ -349,6 +340,37 @@ object TestGenerator { TestsReportNotifier.notify(notifyMessage, model.project, model.testModule) } + private fun processInitialWarnings(testsCodeWithTestReport: TestsCodeWithTestReport, model: GenerateTestsModel) { + val hasInitialWarnings = model.forceMockHappened || model.hasTestFrameworkConflict + if (!hasInitialWarnings) { + return + } + + testsCodeWithTestReport.testsGenerationReport.apply { + summaryMessage = { "Unit tests for $classUnderTest were generated with warnings.
" } + + if (model.forceMockHappened) { + initialWarnings.add { + """ + Warning: Some test cases were ignored, because no mocking framework is installed in the project.
+ Better results could be achieved by installing mocking framework. + """.trimIndent() + } + } + if (model.hasTestFrameworkConflict) { + initialWarnings.add { + """ + Warning: There are several test frameworks in the project. + To select run configuration, please refer to the documentation depending on the project build system: + Gradle, + Maven + or Idea. + """.trimIndent() + } + } + } + } + @Suppress("unused") // this method was used in the past, not used in the present but may be used in the future private fun insertImports(testClass: PsiClass, imports: List, editor: Editor) { diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt index 9e03d22764..05363afc7f 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt @@ -506,6 +506,8 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m if (frameworkNotInstalled && createTestFrameworkNotificationDialog() == Messages.YES) { configureTestFramework() } + + model.hasTestFrameworkConflict = TestFramework.allItems.count { it.isInstalled } > 1 } private fun configureMockFrameworkIfRequired() { diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsModel.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsModel.kt index f9657c04cd..d2fcf6436c 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsModel.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsModel.kt @@ -27,7 +27,8 @@ data class GenerateTestsModel( var selectedMethods: Set?, var timeout:Long, var generateWarningsForStaticMocking: Boolean = false, - var forceMockHappened: Boolean = false + var forceMockHappened: Boolean = false, + var hasTestFrameworkConflict: Boolean = false, ) { var testSourceRoot: VirtualFile? = null var testPackageName: String? = null