From d2eae5ff09b70bde72a1ae750fc73c592768e685 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tamarin Date: Thu, 22 Dec 2022 11:56:53 +0300 Subject: [PATCH] Add LockFile to PythonDialogProcessor --- .../language/python/PythonDialogProcessor.kt | 106 ++++++++++-------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt index 0f171a78be..f8384e9d3d 100644 --- a/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt +++ b/utbot-intellij-python/src/main/kotlin/org/utbot/intellij/plugin/language/python/PythonDialogProcessor.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.idea.util.projectStructure.sdk import org.utbot.common.PathUtil.toPath import org.utbot.common.appendHtmlLine import org.utbot.framework.UtSettings +import org.utbot.framework.plugin.api.util.LockFile import org.utbot.intellij.plugin.ui.WarningTestsReportNotifier import org.utbot.intellij.plugin.ui.utils.showErrorDialogLater import org.utbot.intellij.plugin.ui.utils.testModules @@ -114,61 +115,68 @@ object PythonDialogProcessor { private fun createTests(project: Project, model: PythonTestsModel) { ProgressManager.getInstance().run(object : Backgroundable(project, "Generate python tests") { override fun run(indicator: ProgressIndicator) { - val pythonPath = model.srcModule.sdk?.homePath - if (pythonPath == null) { - showErrorDialogLater( - project, - message = "Couldn't find Python interpreter", - title = "Python test generation error" - ) - return - } - val methods = findSelectedPythonMethods(model) - if (methods == null) { - showErrorDialogLater( - project, - message = "Couldn't parse file. Maybe it contains syntax error?", - title = "Python test generation error" - ) + if (!LockFile.lock()) { return } - processTestGeneration( - pythonPath = pythonPath, - pythonFilePath = model.file.virtualFile.path, - pythonFileContent = getContentFromPyFile(model.file), - directoriesForSysPath = model.directoriesForSysPath, - currentPythonModule = model.currentPythonModule, - pythonMethods = methods, - containingClassName = model.containingClass?.name, - timeout = model.timeout, - testFramework = model.testFramework, - timeoutForRun = model.timeoutForRun, - visitOnlySpecifiedSource = model.visitOnlySpecifiedSource, - isCanceled = { indicator.isCanceled }, - checkingRequirementsAction = { indicator.text = "Checking requirements" }, - requirementsAreNotInstalledAction = { - askAndInstallRequirementsLater(model.project, pythonPath) - PythonTestGenerationProcessor.MissingRequirementsActionResult.NOT_INSTALLED - }, - startedLoadingPythonTypesAction = { indicator.text = "Loading information about Python types" }, - startedTestGenerationAction = { indicator.text = "Generating tests" }, - notGeneratedTestsAction = { + try { + val pythonPath = model.srcModule.sdk?.homePath + if (pythonPath == null) { + showErrorDialogLater( + project, + message = "Couldn't find Python interpreter", + title = "Python test generation error" + ) + return + } + val methods = findSelectedPythonMethods(model) + if (methods == null) { showErrorDialogLater( project, - message = "Cannot create tests for the following functions: " + it.joinToString(), + message = "Couldn't parse file. Maybe it contains syntax error?", title = "Python test generation error" ) - }, - writeTestTextToFile = { generatedCode -> - writeGeneratedCodeToPsiDocument(generatedCode, model) - }, - processMypyWarnings = { - val message = it.fold(StringBuilder()) { acc, line -> acc.appendHtmlLine(line) } - WarningTestsReportNotifier.notify(message.toString()) - }, - startedCleaningAction = { indicator.text = "Cleaning up..." }, - pythonRunRoot = Path(model.testSourceRootPath) - ) + return + } + processTestGeneration( + pythonPath = pythonPath, + pythonFilePath = model.file.virtualFile.path, + pythonFileContent = getContentFromPyFile(model.file), + directoriesForSysPath = model.directoriesForSysPath, + currentPythonModule = model.currentPythonModule, + pythonMethods = methods, + containingClassName = model.containingClass?.name, + timeout = model.timeout, + testFramework = model.testFramework, + timeoutForRun = model.timeoutForRun, + visitOnlySpecifiedSource = model.visitOnlySpecifiedSource, + isCanceled = { indicator.isCanceled }, + checkingRequirementsAction = { indicator.text = "Checking requirements" }, + requirementsAreNotInstalledAction = { + askAndInstallRequirementsLater(model.project, pythonPath) + PythonTestGenerationProcessor.MissingRequirementsActionResult.NOT_INSTALLED + }, + startedLoadingPythonTypesAction = { indicator.text = "Loading information about Python types" }, + startedTestGenerationAction = { indicator.text = "Generating tests" }, + notGeneratedTestsAction = { + showErrorDialogLater( + project, + message = "Cannot create tests for the following functions: " + it.joinToString(), + title = "Python test generation error" + ) + }, + writeTestTextToFile = { generatedCode -> + writeGeneratedCodeToPsiDocument(generatedCode, model) + }, + processMypyWarnings = { + val message = it.fold(StringBuilder()) { acc, line -> acc.appendHtmlLine(line) } + WarningTestsReportNotifier.notify(message.toString()) + }, + startedCleaningAction = { indicator.text = "Cleaning up..." }, + pythonRunRoot = Path(model.testSourceRootPath) + ) + } finally { + LockFile.unlock() + } } }) }