Skip to content

Commit 6b9c040

Browse files
committed
open file after generation; fixed bug in FileManager
1 parent d490651 commit 6b9c040

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/python/PythonDialogProcessor.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package org.utbot.intellij.plugin.python
22

3+
import com.intellij.openapi.application.invokeLater
4+
import com.intellij.openapi.fileEditor.OpenFileDescriptor
35
import com.intellij.openapi.module.Module
46
import com.intellij.openapi.progress.ProgressIndicator
57
import com.intellij.openapi.progress.ProgressManager
68
import com.intellij.openapi.progress.Task.Backgroundable
79
import com.intellij.openapi.project.Project
810
import com.intellij.openapi.roots.ProjectFileIndex
11+
import com.intellij.openapi.vfs.LocalFileSystem
912
import com.intellij.openapi.vfs.VfsUtil
1013
import com.intellij.openapi.vfs.VfsUtilCore
1114
import com.jetbrains.python.psi.PyFile
@@ -19,13 +22,13 @@ import org.utbot.intellij.plugin.ui.utils.testModule
1922
import org.utbot.python.code.PythonCode
2023
import org.utbot.python.code.PythonCode.Companion.getFromString
2124
import org.utbot.python.code.PythonCodeGenerator.generateTestCode
22-
import org.utbot.python.code.PythonCodeGenerator.saveToFile
2325
import org.utbot.python.PythonMethod
2426
import org.utbot.python.PythonTestCaseGenerator
2527
import org.utbot.python.typing.MypyAnnotations
2628
import org.utbot.python.typing.PythonTypesStorage
2729
import org.utbot.python.typing.StubFileFinder
2830
import org.utbot.python.utils.FileManager
31+
import java.io.File
2932

3033

3134
object PythonDialogProcessor {
@@ -148,9 +151,21 @@ object PythonDialogProcessor {
148151
)
149152
}
150153

154+
val files = mutableListOf<File>()
151155
notEmptyTests.forEach {
152156
val testCode = generateTestCode(it, model.directoriesForSysPath, model.moduleToImport)
153-
saveToFile("$testSourceRoot/test_${it.method.name}.py", testCode)
157+
val fileName = "test_${it.method.name}.py"
158+
val testFile = FileManager.createPermanentFile(fileName, testCode)
159+
files.add(testFile)
160+
}
161+
162+
if (files.size == 1) {
163+
val virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(files[0])
164+
if (virtualFile != null) {
165+
invokeLater {
166+
OpenFileDescriptor(model.project, virtualFile).navigate(true)
167+
}
168+
}
154169
}
155170
}
156171
})

utbot-python/src/main/kotlin/org/utbot/python/code/CodeGen.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,6 @@ object PythonCodeGenerator {
295295
)
296296
}
297297

298-
fun saveToFile(filePath: String, code: String): File {
299-
val file = File(filePath)
300-
file.writeText(code)
301-
file.createNewFile()
302-
return file
303-
}
304-
305298
fun generateMypyCheckCode(
306299
method: PythonMethod,
307300
methodAnnotations: Map<String, String>,

utbot-python/src/main/kotlin/org/utbot/python/utils/FileManager.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ object FileManager {
1010

1111
fun assignTestSourceRoot(testSourceRoot: String) {
1212
this.testSourceRoot = testSourceRoot
13+
val testsFolder = File(testSourceRoot)
14+
if (!testsFolder.exists())
15+
testsFolder.mkdir()
1316
val tmpFolder = Paths.get(testSourceRoot, tmpFolderName).toFile()
1417
if (!tmpFolder.exists())
1518
tmpFolder.mkdir()
@@ -31,4 +34,15 @@ object FileManager {
3134
writeToAssignedFile(file, content)
3235
return file
3336
}
37+
38+
fun assignPermanentFile(fileName: String): File {
39+
val fullpath = Paths.get(testSourceRoot, fileName)
40+
return fullpath.toFile()
41+
}
42+
43+
fun createPermanentFile(fileName: String, content: String): File {
44+
val file = assignPermanentFile(fileName)
45+
writeToAssignedFile(file, content)
46+
return file
47+
}
3448
}

utbot-python/src/main/kotlin/org/utbot/python/utils/PythonScriptUtils.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package org.utbot.python.utils
22

33
import org.utbot.framework.plugin.api.ClassId
44
import org.utbot.framework.plugin.api.pythonAnyClassId
5-
import org.utbot.python.code.PythonCodeGenerator.saveToFile
65
import org.utbot.python.typing.PythonTypesStorage
76

87

0 commit comments

Comments
 (0)