GeezerVison is showing github.com — Remember Test Sources root choice and suggest as default #1010 · UnitTestBot/UTBotJava@5a2ee8b
File treeExpand file treeCollapse file tree Open diff view settings utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin main/kotlin/org/utbot/intellij/plugin test/kotlin/org/utbot/intellij/plugin/ui/utils
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,7 +37,7 @@ class GenerateTestsModel( | 37 | 37 | potentialTestModules, | 38 | 38 | srcClasses | 39 | 39 | ) { | 40 | | - | | 40 | + override var sourceRootHistory = project.service<Settings>().sourceRootHistory | 41 | 41 | override var codegenLanguage = project.service<Settings>().codegenLanguage | 42 | 42 |
| 43 | 43 | lateinit var testFramework: TestFramework | |
Collapse file Copy file name to clipboardExpand all lines: utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,6 +44,7 @@ import org.utbot.framework.plugin.api.isSummarizationCompatible | 44 | 44 | ) | 45 | 45 | class Settings(val project: Project) : PersistentStateComponent<Settings.State> { | 46 | 46 | data class State( | | 47 | + var sourceRootHistory: MutableList<String> = mutableListOf(), | 47 | 48 | var codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem, | 48 | 49 | @OptionTag(converter = TestFrameworkConverter::class) | 49 | 50 | var testFramework: TestFramework = TestFramework.defaultItem, | @@ -65,6 +66,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State> | 65 | 66 | var enableSummariesGeneration: Boolean = UtSettings.enableSummariesGeneration | 66 | 67 | ) { | 67 | 68 | constructor(model: GenerateTestsModel) : this( | | 69 | + sourceRootHistory = model.sourceRootHistory, | 68 | 70 | codegenLanguage = model.codegenLanguage, | 69 | 71 | testFramework = model.testFramework, | 70 | 72 | mockStrategy = model.mockStrategy, | @@ -88,6 +90,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State> | 88 | 90 |
| 89 | 91 | other as State | 90 | 92 |
| | 93 | + if (sourceRootHistory != other.sourceRootHistory) return false | 91 | 94 | if (codegenLanguage != other.codegenLanguage) return false | 92 | 95 | if (testFramework != other.testFramework) return false | 93 | 96 | if (mockStrategy != other.mockStrategy) return false | @@ -108,7 +111,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State> | 108 | 111 | return true | 109 | 112 | } | 110 | 113 | override fun hashCode(): Int { | 111 | | - var result = codegenLanguage.hashCode() | | 114 | + var result = sourceRootHistory.hashCode() | | 115 | + result = 31 * result + codegenLanguage.hashCode() | 112 | 116 | result = 31 * result + testFramework.hashCode() | 113 | 117 | result = 31 * result + mockStrategy.hashCode() | 114 | 118 | result = 31 * result + mockFramework.hashCode() | @@ -129,6 +133,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State> | 129 | 133 | } | 130 | 134 |
| 131 | 135 | private var state = State() | | 136 | + val sourceRootHistory: MutableList<String> get() = state.sourceRootHistory | 132 | 137 |
| 133 | 138 | val codegenLanguage: CodegenLanguage get() = state.codegenLanguage | 134 | 139 |
| |
Collapse file Copy file name to clipboardExpand all lines: utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -519,6 +519,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m | 519 | 519 | } catch (ignored: ParseException) { | 520 | 520 | } | 521 | 521 | model.timeout = TimeUnit.SECONDS.toMillis(timeoutSpinner.number.toLong()) | | 522 | + model.testSourceRoot?.apply { model.updateSourceRootHistory(this.toNioPath().toString()) } | 522 | 523 |
| 523 | 524 | val settings = model.project.service<Settings>() | 524 | 525 | with(settings) { | |
Collapse file Copy file name to clipboardExpand all lines: utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/models/BaseTestModel.kt +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle | 14 | 14 | import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots | 15 | 15 |
| 16 | 16 | val PsiClass.packageName: String get() = this.containingFile.containingDirectory.getPackage()?.qualifiedName ?: "" | | 17 | +const val HISTORY_LIMIT = 10 | 17 | 18 |
| 18 | 19 | open class BaseTestsModel( | 19 | 20 | val project: Project, | @@ -27,6 +28,7 @@ open class BaseTestsModel( | 27 | 28 |
| 28 | 29 | var testSourceRoot: VirtualFile? = null | 29 | 30 | var testPackageName: String? = null | | 31 | + open var sourceRootHistory : MutableList<String> = mutableListOf() | 30 | 32 | open lateinit var codegenLanguage: CodegenLanguage | 31 | 33 |
| 32 | 34 | fun setSourceRootAndFindTestModule(newTestSourceRoot: VirtualFile?) { | @@ -50,8 +52,15 @@ open class BaseTestsModel( | 50 | 52 |
| 51 | 53 | fun getAllTestSourceRoots() : MutableList<out ITestSourceRoot> { | 52 | 54 | with(if (project.isBuildWithGradle) project.allModules() else potentialTestModules) { | 53 | | - return this.flatMap { it.suitableTestSourceRoots().toList() }.toMutableList() | | 55 | + return this.flatMap { it.suitableTestSourceRoots().toList() }.toMutableList().distinct().toMutableList() | 54 | 56 | } | 55 | 57 | } | 56 | 58 |
| | 59 | + fun updateSourceRootHistory(path: String) { | | 60 | + sourceRootHistory.apply { | | 61 | + remove(path)//Remove existing entry if any | | 62 | + 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 | | 63 | + while (size > HISTORY_LIMIT) removeFirst() | | 64 | + } | | 65 | + } | 57 | 66 | } |
Collapse file Copy file name to clipboardExpand all lines: utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,6 +60,7 @@ class TestFolderComboWithBrowseButton(private val model: BaseTestsModel) : | 60 | 60 |
| 61 | 61 | val testRoots = getSortedTestRoots( | 62 | 62 | model.getAllTestSourceRoots(), | | 63 | + model.sourceRootHistory, | 63 | 64 | model.srcModule.rootManager.sourceRoots.map { file: VirtualFile -> file.toNioPath().toString() }, | 64 | 65 | model.codegenLanguage | 65 | 66 | ) | |
Collapse file Copy file name to clipboardExpand all lines: utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,7 @@ const val SRC_MAIN = "src/main/" | 71 | 71 | */ | 72 | 72 | fun getSortedTestRoots( | 73 | 73 | allTestRoots: MutableList<out ITestSourceRoot>, | | 74 | + sourceRootHistory: List<String>, | 74 | 75 | moduleSourcePaths: List<String>, | 75 | 76 | codegenLanguage: CodegenLanguage | 76 | 77 | ): MutableList<ITestSourceRoot> { | @@ -88,6 +89,9 @@ fun getSortedTestRoots( | 88 | 89 | }.thenByDescending { | 89 | 90 | // Heuristics: dedicated test source root named 'utbot_tests' should go first | 90 | 91 | it.dirName == dedicatedTestSourceRootName | | 92 | + }.thenByDescending { | | 93 | + // Recent used root should be handy too | | 94 | + sourceRootHistory.indexOf(it.dirPath) | 91 | 95 | }.thenBy { | 92 | 96 | // ABC-sorting | 93 | 97 | it.dirPath | |
Collapse file Copy file name to clipboardExpand all lines: utbot-ui-commons/src/test/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtilsTest.kt +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,7 +56,13 @@ internal class RootUtilsTest { | 56 | 56 | "/UTBotJavaTest/utbot-framework/src/main/kotlin", | 57 | 57 | "/UTBotJavaTest/utbot-framework/src/main/resources", | 58 | 58 | ) | 59 | | - val sortedTestRoots = getSortedTestRoots(allTestRoots, moduleSourcePaths, CodegenLanguage.JAVA) | | 59 | + val sortedTestRoots = getSortedTestRoots( | | 60 | + allTestRoots, | | 61 | + listOf("/UTBotJavaTest/utbot-core/src/test/java"), | | 62 | + moduleSourcePaths, | | 63 | + CodegenLanguage.JAVA | | 64 | + ) | 60 | 65 | Assertions.assertEquals("/UTBotJavaTest/utbot-framework/src/test/java", sortedTestRoots.first().toString()) | | 66 | + Assertions.assertEquals("/UTBotJavaTest/utbot-core/src/test/java", sortedTestRoots[1].toString()) | 61 | 67 | } | 62 | 68 | } |
|