From 5a2ee8bc354e9e24fa76a7078dc9dd9c680e3e7c Mon Sep 17 00:00:00 2001 From: "Vassiliy.Kudryashov" Date: Wed, 14 Dec 2022 22:07:45 +0300 Subject: [PATCH] Remember Test Sources root choice and suggest as default #1010 --- .../intellij/plugin/models/GenerateTestsModel.kt | 2 +- .../org/utbot/intellij/plugin/settings/Settings.kt | 7 ++++++- .../intellij/plugin/ui/GenerateTestsDialogWindow.kt | 1 + .../org/utbot/intellij/plugin/models/BaseTestModel.kt | 11 ++++++++++- .../ui/components/TestFolderComboWithBrowseButton.kt | 1 + .../org/utbot/intellij/plugin/ui/utils/RootUtils.kt | 4 ++++ .../utbot/intellij/plugin/ui/utils/RootUtilsTest.kt | 8 +++++++- 7 files changed, 30 insertions(+), 4 deletions(-) diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt index 66097c7a90..9519674eaa 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt @@ -37,7 +37,7 @@ class GenerateTestsModel( potentialTestModules, srcClasses ) { - + override var sourceRootHistory = project.service().sourceRootHistory override var codegenLanguage = project.service().codegenLanguage lateinit var testFramework: TestFramework diff --git a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt index 8c905738ee..72c8affffd 100644 --- a/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt +++ b/utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt @@ -44,6 +44,7 @@ import org.utbot.framework.plugin.api.isSummarizationCompatible ) class Settings(val project: Project) : PersistentStateComponent { data class State( + var sourceRootHistory: MutableList = mutableListOf(), var codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem, @OptionTag(converter = TestFrameworkConverter::class) var testFramework: TestFramework = TestFramework.defaultItem, @@ -65,6 +66,7 @@ class Settings(val project: Project) : PersistentStateComponent var enableSummariesGeneration: Boolean = UtSettings.enableSummariesGeneration ) { constructor(model: GenerateTestsModel) : this( + sourceRootHistory = model.sourceRootHistory, codegenLanguage = model.codegenLanguage, testFramework = model.testFramework, mockStrategy = model.mockStrategy, @@ -88,6 +90,7 @@ class Settings(val project: Project) : PersistentStateComponent other as State + if (sourceRootHistory != other.sourceRootHistory) return false if (codegenLanguage != other.codegenLanguage) return false if (testFramework != other.testFramework) return false if (mockStrategy != other.mockStrategy) return false @@ -108,7 +111,8 @@ class Settings(val project: Project) : PersistentStateComponent return true } override fun hashCode(): Int { - var result = codegenLanguage.hashCode() + var result = sourceRootHistory.hashCode() + result = 31 * result + codegenLanguage.hashCode() result = 31 * result + testFramework.hashCode() result = 31 * result + mockStrategy.hashCode() result = 31 * result + mockFramework.hashCode() @@ -129,6 +133,7 @@ class Settings(val project: Project) : PersistentStateComponent } private var state = State() + val sourceRootHistory: MutableList get() = state.sourceRootHistory val codegenLanguage: CodegenLanguage get() = state.codegenLanguage 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 cf681a3b42..0cc2fde656 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 @@ -519,6 +519,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m } catch (ignored: ParseException) { } model.timeout = TimeUnit.SECONDS.toMillis(timeoutSpinner.number.toLong()) + model.testSourceRoot?.apply { model.updateSourceRootHistory(this.toNioPath().toString()) } val settings = model.project.service() with(settings) { diff --git a/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/models/BaseTestModel.kt b/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/models/BaseTestModel.kt index abdbda292a..97e0a4c0c6 100644 --- a/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/models/BaseTestModel.kt +++ b/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/models/BaseTestModel.kt @@ -14,6 +14,7 @@ import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots val PsiClass.packageName: String get() = this.containingFile.containingDirectory.getPackage()?.qualifiedName ?: "" +const val HISTORY_LIMIT = 10 open class BaseTestsModel( val project: Project, @@ -27,6 +28,7 @@ open class BaseTestsModel( var testSourceRoot: VirtualFile? = null var testPackageName: String? = null + open var sourceRootHistory : MutableList = mutableListOf() open lateinit var codegenLanguage: CodegenLanguage fun setSourceRootAndFindTestModule(newTestSourceRoot: VirtualFile?) { @@ -50,8 +52,15 @@ open class BaseTestsModel( fun getAllTestSourceRoots() : MutableList { with(if (project.isBuildWithGradle) project.allModules() else potentialTestModules) { - return this.flatMap { it.suitableTestSourceRoots().toList() }.toMutableList() + return this.flatMap { it.suitableTestSourceRoots().toList() }.toMutableList().distinct().toMutableList() } } + fun updateSourceRootHistory(path: String) { + sourceRootHistory.apply { + remove(path)//Remove existing entry if any + add(path)//Add the most recent entry to the end to be brought first at sorting, see org.utbot.intellij.plugin.ui.utils.RootUtilsKt.getSortedTestRoots + while (size > HISTORY_LIMIT) removeFirst() + } + } } diff --git a/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt b/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt index 2f933e9e13..a5a7bd2a8c 100644 --- a/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt +++ b/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt @@ -60,6 +60,7 @@ class TestFolderComboWithBrowseButton(private val model: BaseTestsModel) : val testRoots = getSortedTestRoots( model.getAllTestSourceRoots(), + model.sourceRootHistory, model.srcModule.rootManager.sourceRoots.map { file: VirtualFile -> file.toNioPath().toString() }, model.codegenLanguage ) diff --git a/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt b/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt index 76505578e4..162ead2748 100644 --- a/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt +++ b/utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt @@ -71,6 +71,7 @@ const val SRC_MAIN = "src/main/" */ fun getSortedTestRoots( allTestRoots: MutableList, + sourceRootHistory: List, moduleSourcePaths: List, codegenLanguage: CodegenLanguage ): MutableList { @@ -88,6 +89,9 @@ fun getSortedTestRoots( }.thenByDescending { // Heuristics: dedicated test source root named 'utbot_tests' should go first it.dirName == dedicatedTestSourceRootName + }.thenByDescending { + // Recent used root should be handy too + sourceRootHistory.indexOf(it.dirPath) }.thenBy { // ABC-sorting it.dirPath diff --git a/utbot-ui-commons/src/test/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtilsTest.kt b/utbot-ui-commons/src/test/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtilsTest.kt index ed5593d0e1..7b3371c2eb 100644 --- a/utbot-ui-commons/src/test/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtilsTest.kt +++ b/utbot-ui-commons/src/test/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtilsTest.kt @@ -56,7 +56,13 @@ internal class RootUtilsTest { "/UTBotJavaTest/utbot-framework/src/main/kotlin", "/UTBotJavaTest/utbot-framework/src/main/resources", ) - val sortedTestRoots = getSortedTestRoots(allTestRoots, moduleSourcePaths, CodegenLanguage.JAVA) + val sortedTestRoots = getSortedTestRoots( + allTestRoots, + listOf("/UTBotJavaTest/utbot-core/src/test/java"), + moduleSourcePaths, + CodegenLanguage.JAVA + ) Assertions.assertEquals("/UTBotJavaTest/utbot-framework/src/test/java", sortedTestRoots.first().toString()) + Assertions.assertEquals("/UTBotJavaTest/utbot-core/src/test/java", sortedTestRoots[1].toString()) } } \ No newline at end of file