Skip to content

Commit 9cfb69e

Browse files
Vassiliy-Kudryashovtamarinvs19
authored andcommitted
Remember Test Sources root choice and suggest as default #1010 (#1519)
1 parent 980372f commit 9cfb69e

File tree

7 files changed

+30
-4
lines changed

7 files changed

+30
-4
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/models/GenerateTestsModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class GenerateTestsModel(
3737
potentialTestModules,
3838
srcClasses
3939
) {
40-
40+
override var sourceRootHistory = project.service<Settings>().sourceRootHistory
4141
override var codegenLanguage = project.service<Settings>().codegenLanguage
4242

4343
lateinit var testFramework: TestFramework

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/settings/Settings.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import org.utbot.framework.plugin.api.isSummarizationCompatible
4444
)
4545
class Settings(val project: Project) : PersistentStateComponent<Settings.State> {
4646
data class State(
47+
var sourceRootHistory: MutableList<String> = mutableListOf(),
4748
var codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
4849
@OptionTag(converter = TestFrameworkConverter::class)
4950
var testFramework: TestFramework = TestFramework.defaultItem,
@@ -65,6 +66,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
6566
var enableSummariesGeneration: Boolean = UtSettings.enableSummariesGeneration
6667
) {
6768
constructor(model: GenerateTestsModel) : this(
69+
sourceRootHistory = model.sourceRootHistory,
6870
codegenLanguage = model.codegenLanguage,
6971
testFramework = model.testFramework,
7072
mockStrategy = model.mockStrategy,
@@ -88,6 +90,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
8890

8991
other as State
9092

93+
if (sourceRootHistory != other.sourceRootHistory) return false
9194
if (codegenLanguage != other.codegenLanguage) return false
9295
if (testFramework != other.testFramework) return false
9396
if (mockStrategy != other.mockStrategy) return false
@@ -108,7 +111,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
108111
return true
109112
}
110113
override fun hashCode(): Int {
111-
var result = codegenLanguage.hashCode()
114+
var result = sourceRootHistory.hashCode()
115+
result = 31 * result + codegenLanguage.hashCode()
112116
result = 31 * result + testFramework.hashCode()
113117
result = 31 * result + mockStrategy.hashCode()
114118
result = 31 * result + mockFramework.hashCode()
@@ -129,6 +133,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
129133
}
130134

131135
private var state = State()
136+
val sourceRootHistory: MutableList<String> get() = state.sourceRootHistory
132137

133138
val codegenLanguage: CodegenLanguage get() = state.codegenLanguage
134139

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
519519
} catch (ignored: ParseException) {
520520
}
521521
model.timeout = TimeUnit.SECONDS.toMillis(timeoutSpinner.number.toLong())
522+
model.testSourceRoot?.apply { model.updateSourceRootHistory(this.toNioPath().toString()) }
522523

523524
val settings = model.project.service<Settings>()
524525
with(settings) {

utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/models/BaseTestModel.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
1414
import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
1515

1616
val PsiClass.packageName: String get() = this.containingFile.containingDirectory.getPackage()?.qualifiedName ?: ""
17+
const val HISTORY_LIMIT = 10
1718

1819
open class BaseTestsModel(
1920
val project: Project,
@@ -27,6 +28,7 @@ open class BaseTestsModel(
2728

2829
var testSourceRoot: VirtualFile? = null
2930
var testPackageName: String? = null
31+
open var sourceRootHistory : MutableList<String> = mutableListOf()
3032
open lateinit var codegenLanguage: CodegenLanguage
3133

3234
fun setSourceRootAndFindTestModule(newTestSourceRoot: VirtualFile?) {
@@ -50,8 +52,15 @@ open class BaseTestsModel(
5052

5153
fun getAllTestSourceRoots() : MutableList<out ITestSourceRoot> {
5254
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()
5456
}
5557
}
5658

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+
}
5766
}

utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/components/TestFolderComboWithBrowseButton.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TestFolderComboWithBrowseButton(private val model: BaseTestsModel) :
6060

6161
val testRoots = getSortedTestRoots(
6262
model.getAllTestSourceRoots(),
63+
model.sourceRootHistory,
6364
model.srcModule.rootManager.sourceRoots.map { file: VirtualFile -> file.toNioPath().toString() },
6465
model.codegenLanguage
6566
)

utbot-ui-commons/src/main/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtils.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const val SRC_MAIN = "src/main/"
7171
*/
7272
fun getSortedTestRoots(
7373
allTestRoots: MutableList<out ITestSourceRoot>,
74+
sourceRootHistory: List<String>,
7475
moduleSourcePaths: List<String>,
7576
codegenLanguage: CodegenLanguage
7677
): MutableList<ITestSourceRoot> {
@@ -88,6 +89,9 @@ fun getSortedTestRoots(
8889
}.thenByDescending {
8990
// Heuristics: dedicated test source root named 'utbot_tests' should go first
9091
it.dirName == dedicatedTestSourceRootName
92+
}.thenByDescending {
93+
// Recent used root should be handy too
94+
sourceRootHistory.indexOf(it.dirPath)
9195
}.thenBy {
9296
// ABC-sorting
9397
it.dirPath

utbot-ui-commons/src/test/kotlin/org/utbot/intellij/plugin/ui/utils/RootUtilsTest.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ internal class RootUtilsTest {
5656
"/UTBotJavaTest/utbot-framework/src/main/kotlin",
5757
"/UTBotJavaTest/utbot-framework/src/main/resources",
5858
)
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+
)
6065
Assertions.assertEquals("/UTBotJavaTest/utbot-framework/src/test/java", sortedTestRoots.first().toString())
66+
Assertions.assertEquals("/UTBotJavaTest/utbot-core/src/test/java", sortedTestRoots[1].toString())
6167
}
6268
}

0 commit comments

Comments
 (0)