Skip to content

Remember Test Sources root choice and suggest as default #1010 #1519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GenerateTestsModel(
potentialTestModules,
srcClasses
) {

override var sourceRootHistory = project.service<Settings>().sourceRootHistory
override var codegenLanguage = project.service<Settings>().codegenLanguage

lateinit var testFramework: TestFramework
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import org.utbot.framework.plugin.api.isSummarizationCompatible
)
class Settings(val project: Project) : PersistentStateComponent<Settings.State> {
data class State(
var sourceRootHistory: MutableList<String> = mutableListOf(),
var codegenLanguage: CodegenLanguage = CodegenLanguage.defaultItem,
@OptionTag(converter = TestFrameworkConverter::class)
var testFramework: TestFramework = TestFramework.defaultItem,
Expand All @@ -65,6 +66,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
var enableSummariesGeneration: Boolean = UtSettings.enableSummariesGeneration
) {
constructor(model: GenerateTestsModel) : this(
sourceRootHistory = model.sourceRootHistory,
codegenLanguage = model.codegenLanguage,
testFramework = model.testFramework,
mockStrategy = model.mockStrategy,
Expand All @@ -88,6 +90,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>

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
Expand All @@ -108,7 +111,8 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
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()
Expand All @@ -129,6 +133,7 @@ class Settings(val project: Project) : PersistentStateComponent<Settings.State>
}

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

val codegenLanguage: CodegenLanguage get() = state.codegenLanguage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Settings>()
with(settings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,6 +28,7 @@ open class BaseTestsModel(

var testSourceRoot: VirtualFile? = null
var testPackageName: String? = null
open var sourceRootHistory : MutableList<String> = mutableListOf()
open lateinit var codegenLanguage: CodegenLanguage

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

fun getAllTestSourceRoots() : MutableList<out ITestSourceRoot> {
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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const val SRC_MAIN = "src/main/"
*/
fun getSortedTestRoots(
allTestRoots: MutableList<out ITestSourceRoot>,
sourceRootHistory: List<String>,
moduleSourcePaths: List<String>,
codegenLanguage: CodegenLanguage
): MutableList<ITestSourceRoot> {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}