Skip to content

Commit a209473

Browse files
authored
Fix test framework loading from settings file (#2252)
1 parent fd85321 commit a209473

File tree

7 files changed

+102
-6
lines changed

7 files changed

+102
-6
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,29 @@ abstract class TestFramework(
256256
}
257257
}
258258

259+
class UnknownTestFramework(id: String) : TestFramework(id = id, displayName = id) {
260+
override val mainPackage: String = id
261+
override val assertionsClass: ClassId = ClassId(id)
262+
override val arraysAssertionsClass: ClassId = ClassId(id)
263+
override val kotlinFailureAssertionsClass: ClassId = ClassId(id)
264+
override val testAnnotationId: ClassId = ClassId(id)
265+
override val beforeMethodId: ClassId = ClassId(id)
266+
override val afterMethodId: ClassId = ClassId(id)
267+
override val parameterizedTestAnnotationId: ClassId = ClassId(id)
268+
override val methodSourceAnnotationId: ClassId = ClassId(id)
269+
override val nestedClassesShouldBeStatic: Boolean = false
270+
override val argListClassId: ClassId = ClassId(id)
271+
272+
override fun getRunTestsCommand(
273+
executionInvoke: String,
274+
classPath: String,
275+
classesNames: List<String>,
276+
buildDirectory: String,
277+
additionalArguments: List<String>
278+
): List<String> = emptyList()
279+
280+
}
281+
259282
object TestNg : TestFramework(id = "TestNG",displayName = "TestNG") {
260283
override val mainPackage: String = TEST_NG_PACKAGE
261284

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.jetbrains.python.psi.PyClass
1616
import com.jetbrains.python.psi.PyFunction
1717
import org.utbot.framework.codegen.domain.ProjectType
1818
import org.utbot.framework.codegen.domain.TestFramework
19+
import org.utbot.intellij.plugin.language.python.settings.PythonTestFrameworkMapper
1920
import org.utbot.intellij.plugin.language.python.settings.loadStateFromModel
2021
import org.utbot.intellij.plugin.language.python.table.UtPyClassItem
2122
import org.utbot.intellij.plugin.language.python.table.UtPyFunctionItem
@@ -93,8 +94,9 @@ class PythonDialogWindow(val model: PythonTestsModel) : DialogWrapper(model.proj
9394
private fun initDefaultValues() {
9495
val settings = model.project.service<Settings>()
9596

96-
val installedTestFramework = TestFramework.allItems.singleOrNull { it.isInstalled }
97-
currentFrameworkItem = installedTestFramework ?: settings.testFramework
97+
val installedTestFramework = PythonTestFrameworkMapper.allItems.singleOrNull { it.isInstalled }
98+
val testFramework = PythonTestFrameworkMapper.handleUnknown(settings.testFramework)
99+
currentFrameworkItem = installedTestFramework ?: testFramework
98100

99101
updateTestFrameworksList()
100102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.utbot.intellij.plugin.language.python.settings
2+
3+
import org.utbot.framework.codegen.domain.TestFramework
4+
import org.utbot.intellij.plugin.settings.TestFrameworkMapper
5+
import org.utbot.python.framework.codegen.PythonTestFrameworkManager
6+
import org.utbot.python.framework.codegen.model.Pytest
7+
import org.utbot.python.framework.codegen.model.Unittest
8+
9+
object PythonTestFrameworkMapper: TestFrameworkMapper {
10+
override fun toString(value: TestFramework): String = value.id
11+
12+
override fun fromString(value: String): TestFramework = when (value) {
13+
Unittest.id -> Unittest
14+
Pytest.id -> Pytest
15+
else -> error("Unknown TestFramework $value")
16+
}
17+
18+
override fun handleUnknown(testFramework: TestFramework): TestFramework {
19+
if (allItems.contains(testFramework)) {
20+
return testFramework
21+
}
22+
return try {
23+
fromString(testFramework.id)
24+
} catch (ex: IllegalStateException) {
25+
defaultItem
26+
}
27+
}
28+
29+
val defaultItem: TestFramework get() = PythonTestFrameworkManager().defaultTestFramework
30+
val allItems: List<TestFramework> get() = PythonTestFrameworkManager().testFrameworks
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.utbot.intellij.plugin.settings
2+
3+
import org.utbot.framework.codegen.domain.Junit4
4+
import org.utbot.framework.codegen.domain.Junit5
5+
import org.utbot.framework.codegen.domain.TestFramework
6+
import org.utbot.framework.codegen.domain.TestNg
7+
8+
object JavaTestFrameworkMapper : TestFrameworkMapper {
9+
override fun toString(value: TestFramework): String = value.id
10+
11+
override fun fromString(value: String): TestFramework = when (value) {
12+
Junit4.id -> Junit4
13+
Junit5.id -> Junit5
14+
TestNg.id -> TestNg
15+
else -> error("Unknown TestFramework $value")
16+
}
17+
18+
override fun handleUnknown(testFramework: TestFramework): TestFramework {
19+
if (TestFramework.allItems.contains(testFramework)) {
20+
return testFramework
21+
}
22+
return try {
23+
fromString(testFramework.id)
24+
} catch (ex: IllegalStateException) {
25+
TestFramework.defaultItem
26+
}
27+
}
28+
}

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ import org.utbot.intellij.plugin.models.springBootTestLibraryDescriptor
114114
import org.utbot.intellij.plugin.models.springTestLibraryDescriptor
115115
import org.utbot.intellij.plugin.models.testNgNewLibraryDescriptor
116116
import org.utbot.intellij.plugin.models.testNgOldLibraryDescriptor
117+
import org.utbot.intellij.plugin.settings.JavaTestFrameworkMapper
117118
import org.utbot.intellij.plugin.settings.Settings
118119
import org.utbot.intellij.plugin.settings.loadStateFromModel
119120
import org.utbot.intellij.plugin.ui.components.CodeGenerationSettingItemRenderer
@@ -851,10 +852,11 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
851852
codegenLanguages.item = model.codegenLanguage
852853

853854
val installedTestFramework = TestFramework.allItems.singleOrNull { it.isInstalled }
855+
val testFramework = JavaTestFrameworkMapper.handleUnknown(settings.testFramework)
854856
currentFrameworkItem = when (parametrizedTestSources.isSelected) {
855-
false -> installedTestFramework ?: settings.testFramework
857+
false -> installedTestFramework ?: testFramework
856858
true -> installedTestFramework
857-
?: if (settings.testFramework != Junit4) settings.testFramework else TestFramework.parametrizedDefaultItem
859+
?: if (testFramework != Junit4) testFramework else TestFramework.parametrizedDefaultItem
858860
}
859861

860862
springTestsType.item = if (isSpringConfigSelected()) settings.springTestsType else SpringTestsType.defaultItem

utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/settings/CommonSettings.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import java.util.concurrent.CompletableFuture
3636
import kotlin.reflect.KClass
3737
import org.utbot.common.isWindows
3838
import org.utbot.framework.SummariesGenerationType
39+
import org.utbot.framework.codegen.domain.UnknownTestFramework
3940
import org.utbot.framework.plugin.api.SpringTestsType
4041
import org.utbot.framework.plugin.api.isSummarizationCompatible
4142

@@ -252,13 +253,13 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
252253

253254
// use it to serialize testFramework in State
254255
private class TestFrameworkConverter : Converter<TestFramework>() {
255-
override fun toString(value: TestFramework): String = "$value"
256+
override fun toString(value: TestFramework): String = value.id
256257

257258
override fun fromString(value: String): TestFramework = when (value) {
258259
Junit4.id -> Junit4
259260
Junit5.id -> Junit5
260261
TestNg.id -> TestNg
261-
else -> error("Unknown TestFramework $value")
262+
else -> UnknownTestFramework(value)
262263
}
263264
}
264265

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.utbot.intellij.plugin.settings
2+
3+
import org.utbot.framework.codegen.domain.TestFramework
4+
5+
interface TestFrameworkMapper {
6+
fun toString(value: TestFramework): String
7+
fun fromString(value: String): TestFramework
8+
fun handleUnknown(testFramework: TestFramework): TestFramework
9+
}

0 commit comments

Comments
 (0)