Skip to content

Commit

Permalink
Improve verification that static mocking is configured (#1091)
Browse files Browse the repository at this point in the history
* Improve verification that static mocking is configured

* Wrap file reading with try/catch
  • Loading branch information
EgorkaKulikov authored Oct 5, 2022
1 parent d447c86 commit c34324a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
val MOCKITO_EXTENSIONS_FILE_CONTENT = "mock-maker-inline"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -762,15 +762,15 @@ 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)
if (!mockitoExtensionsPath.exists()) Files.createDirectory(mockitoExtensionsPath)

if (!mockitoMockMakerPath.exists()) {
Files.createFile(mockitoMockMakerPath)
Files.write(mockitoMockMakerPath, MOCKITO_EXTENSIONS_FILE_CONTENT)
Files.write(mockitoMockMakerPath, listOf(MOCKITO_EXTENSIONS_FILE_CONTENT))
}
}

Expand Down Expand Up @@ -1027,14 +1027,20 @@ 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

try {
val fileLines = Files.readAllLines(mockMakerPath)
fileLines.singleOrNull() == MOCKITO_EXTENSIONS_FILE_CONTENT
} catch (e: java.io.IOException) {
return false
}

}
}
}
}

Expand Down

0 comments on commit c34324a

Please sign in to comment.