From ff827eb545f80704c6d57c9c6d3ff45f37185701 Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Wed, 5 Oct 2022 10:14:26 +0300 Subject: [PATCH 1/2] Improve verification that static mocking is configured --- .../codegen/model/util/DependencyPatterns.kt | 4 ++-- .../plugin/ui/GenerateTestsDialogWindow.kt | 21 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/DependencyPatterns.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/DependencyPatterns.kt index f84398ecbb..0a905ccdfa 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/DependencyPatterns.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/util/DependencyPatterns.kt @@ -87,6 +87,6 @@ val mockitoPatterns = listOf(MOCKITO_JAR_PATTERN, MOCKITO_MVN_PATTERN) val MOCKITO_BASIC_MODULE_PATTERN = Regex("mockito-core") val mockitoModulePatterns = listOf(MOCKITO_BASIC_MODULE_PATTERN) -const val MOCKITO_EXTENSIONS_STORAGE = "mockito-extensions" +const val MOCKITO_EXTENSIONS_FOLDER = "mockito-extensions" const val MOCKITO_MOCKMAKER_FILE_NAME = "org.mockito.plugins.MockMaker" -val MOCKITO_EXTENSIONS_FILE_CONTENT = listOf("mock-maker-inline") \ No newline at end of file +val MOCKITO_EXTENSIONS_FILE_CONTENT = "mock-maker-inline" \ No newline at end of file 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 3154d10689..78e653da23 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 @@ -109,7 +109,7 @@ import org.utbot.framework.codegen.StaticsMocking import org.utbot.framework.codegen.TestFramework import org.utbot.framework.codegen.TestNg import org.utbot.framework.codegen.model.util.MOCKITO_EXTENSIONS_FILE_CONTENT -import org.utbot.framework.codegen.model.util.MOCKITO_EXTENSIONS_STORAGE +import org.utbot.framework.codegen.model.util.MOCKITO_EXTENSIONS_FOLDER import org.utbot.framework.codegen.model.util.MOCKITO_MOCKMAKER_FILE_NAME import org.utbot.framework.plugin.api.CodeGenerationSettingItem import org.utbot.framework.plugin.api.CodegenLanguage @@ -762,7 +762,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m * for further details. */ private fun configureMockitoResources(testResourcesPath: Path) { - val mockitoExtensionsPath = "$testResourcesPath/$MOCKITO_EXTENSIONS_STORAGE".toPath() + val mockitoExtensionsPath = "$testResourcesPath/$MOCKITO_EXTENSIONS_FOLDER".toPath() val mockitoMockMakerPath = "$mockitoExtensionsPath/$MOCKITO_MOCKMAKER_FILE_NAME".toPath() if (!testResourcesPath.exists()) Files.createDirectory(testResourcesPath) @@ -770,7 +770,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m if (!mockitoMockMakerPath.exists()) { Files.createFile(mockitoMockMakerPath) - Files.write(mockitoMockMakerPath, MOCKITO_EXTENSIONS_FILE_CONTENT) + Files.write(mockitoMockMakerPath, listOf(MOCKITO_EXTENSIONS_FILE_CONTENT)) } } @@ -1027,14 +1027,15 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m .map { f -> Paths.get(urlToPath(f.url)) } } - return entriesPaths.all { path -> - if (Files.exists(path)) { - val fileNames = Files.walk(path).map { it.fileName }.toList() - fileNames.any { it.toString() == MOCKITO_MOCKMAKER_FILE_NAME } - } else { - false + return entriesPaths.all { entryPath -> + if (!Files.exists(entryPath)) return false + + val mockMakerPath = "$entryPath/$MOCKITO_EXTENSIONS_FOLDER/$MOCKITO_MOCKMAKER_FILE_NAME".toPath() + if (!Files.exists(mockMakerPath)) return false + + val fileLines = Files.readAllLines(mockMakerPath) + fileLines.singleOrNull() == MOCKITO_EXTENSIONS_FILE_CONTENT } - } } } From e358e0ae986300b0b9896388c67a44c9b984755f Mon Sep 17 00:00:00 2001 From: Egor Kulikov Date: Wed, 5 Oct 2022 12:07:42 +0300 Subject: [PATCH 2/2] Wrap file reading with try/catch --- .../intellij/plugin/ui/GenerateTestsDialogWindow.kt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 78e653da23..f474cf1d01 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 @@ -1033,8 +1033,13 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m val mockMakerPath = "$entryPath/$MOCKITO_EXTENSIONS_FOLDER/$MOCKITO_MOCKMAKER_FILE_NAME".toPath() if (!Files.exists(mockMakerPath)) return false - val fileLines = Files.readAllLines(mockMakerPath) - fileLines.singleOrNull() == MOCKITO_EXTENSIONS_FILE_CONTENT + try { + val fileLines = Files.readAllLines(mockMakerPath) + fileLines.singleOrNull() == MOCKITO_EXTENSIONS_FILE_CONTENT + } catch (e: java.io.IOException) { + return false + } + } } }