Skip to content

Commit dc602ce

Browse files
authored
Merge branch 'main' into denis-fokin/move-test-specific-implementation
2 parents f8dd8fc + b30cab4 commit dc602ce

File tree

5 files changed

+58
-54
lines changed

5 files changed

+58
-54
lines changed

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/CodeGenerationController.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ import org.utbot.intellij.plugin.models.packageName
7777
import org.utbot.intellij.plugin.process.EngineProcess
7878
import org.utbot.intellij.plugin.process.RdTestGenerationResult
7979
import org.utbot.intellij.plugin.sarif.SarifReportIdea
80-
import org.utbot.intellij.plugin.ui.CommonErrorNotifier
80+
import org.utbot.intellij.plugin.ui.CommonLoggingNotifier
8181
import org.utbot.intellij.plugin.ui.DetailsTestsReportNotifier
8282
import org.utbot.intellij.plugin.ui.SarifReportNotifier
8383
import org.utbot.intellij.plugin.ui.TestReportUrlOpeningListener
@@ -760,9 +760,12 @@ object CodeGenerationController {
760760
val project = model.project
761761
val codeStyleManager = CodeStyleManager.getInstance(project)
762762
val file = smartPointer.containingFile?: return
763-
764-
if (file.virtualFile.length > UtSettings.maxTestFileSize) {
765-
CommonErrorNotifier.notify(
763+
val fileLength = runReadAction {
764+
FileDocumentManager.getInstance().getDocument(file.virtualFile)?.textLength
765+
?: file.virtualFile.length.toInt()
766+
}
767+
if (fileLength > UtSettings.maxTestFileSize && file.name != model.codegenLanguage.utilClassFileName) {
768+
CommonLoggingNotifier().notify(
766769
"Size of ${file.virtualFile.presentableName} exceeds configured limit " +
767770
"(${FileUtil.byteCountToDisplaySize(UtSettings.maxTestFileSize.toLong())}), reformatting was skipped. " +
768771
"The limit can be configured in '{HOME_DIR}/.utbot/settings.properties' with 'maxTestFileSize' property",

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit
4949
import kotlin.io.path.pathString
5050
import org.utbot.intellij.plugin.generator.CodeGenerationController.getAllTestSourceRoots
5151
import org.utbot.framework.plugin.api.util.LockFile
52+
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
5253

5354
object UtTestsDialogProcessor {
5455
private val logger = KotlinLogging.logger {}
@@ -105,7 +106,7 @@ object UtTestsDialogProcessor {
105106
focusedMethods,
106107
UtSettings.utBotGenerationTimeoutInMillis,
107108
)
108-
if (model.getAllTestSourceRoots().isEmpty()) {
109+
if (model.getAllTestSourceRoots().isEmpty() && project.isBuildWithGradle) {
109110
val errorMessage = """
110111
<html>No test source roots found in the project.<br>
111112
Please, <a href="https://www.jetbrains.com/help/idea/testing.html#add-test-root">create or configure</a> at least one test source root.

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ import com.intellij.ui.awt.RelativePoint
1919
import com.intellij.util.ui.JBFont
2020
import java.awt.Point
2121
import javax.swing.event.HyperlinkEvent
22+
import mu.KotlinLogging
2223

2324
abstract class Notifier {
25+
protected val logger = KotlinLogging.logger {}
26+
2427
protected abstract val notificationType: NotificationType
2528
protected abstract val displayId: String
26-
protected abstract fun content(project: Project?, module: Module?, info: String): String
29+
protected open fun content(project: Project?, module: Module?, info: String): String = info
2730

2831
open fun notify(info: String, project: Project? = null, module: Module? = null) {
2932
notificationGroup
@@ -47,15 +50,28 @@ abstract class WarningNotifier : Notifier() {
4750
abstract class ErrorNotifier : Notifier() {
4851
final override val notificationType: NotificationType = NotificationType.ERROR
4952

50-
final override fun notify(info: String, project: Project?, module: Module?) {
53+
override fun notify(info: String, project: Project?, module: Module?) {
5154
super.notify(info, project, module)
5255
error(content(project, module, info))
5356
}
5457
}
5558

5659
object CommonErrorNotifier : ErrorNotifier() {
5760
override val displayId: String = "UTBot plugin errors"
58-
override fun content(project: Project?, module: Module?, info: String): String = info
61+
}
62+
63+
class CommonLoggingNotifier(val type :NotificationType = NotificationType.WARNING) : Notifier() {
64+
override val displayId: String = "UTBot plugin errors"
65+
override val notificationType = type
66+
67+
override fun notify(info: String, project: Project?, module: Module?) {
68+
super.notify(info, project, module)
69+
when (notificationType) {
70+
NotificationType.WARNING -> logger.warn(content(project, module, info))
71+
NotificationType.INFORMATION -> logger.info(content(project, module, info))
72+
else -> logger.error(content(project, module, info))
73+
}
74+
}
5975
}
6076

6177
object UnsupportedJdkNotifier : ErrorNotifier() {
@@ -113,8 +129,6 @@ object SarifReportNotifier : EventLogNotifier() {
113129
override val titleText: String = "" // no title
114130

115131
override val urlOpeningListener: NotificationListener = NotificationListener.UrlOpeningListener(false)
116-
117-
override fun content(project: Project?, module: Module?, info: String): String = info
118132
}
119133

120134
object TestsReportNotifier : InformationUrlNotifier() {
@@ -123,8 +137,6 @@ object TestsReportNotifier : InformationUrlNotifier() {
123137
override val titleText: String = "UTBot: unit tests generated successfully"
124138

125139
public override val urlOpeningListener: TestReportUrlOpeningListener = TestReportUrlOpeningListener
126-
127-
override fun content(project: Project?, module: Module?, info: String): String = info
128140
}
129141

130142
// TODO replace inheritance with decorators
@@ -134,8 +146,6 @@ object WarningTestsReportNotifier : WarningUrlNotifier() {
134146
override val titleText: String = "UTBot: unit tests generated with warnings"
135147

136148
public override val urlOpeningListener: TestReportUrlOpeningListener = TestReportUrlOpeningListener
137-
138-
override fun content(project: Project?, module: Module?, info: String): String = info
139149
}
140150

141151
object DetailsTestsReportNotifier : EventLogNotifier() {
@@ -144,8 +154,6 @@ object DetailsTestsReportNotifier : EventLogNotifier() {
144154
override val titleText: String = "Test report details of the unit tests generation via UtBot"
145155

146156
public override val urlOpeningListener: TestReportUrlOpeningListener = TestReportUrlOpeningListener
147-
148-
override fun content(project: Project?, module: Module?, info: String): String = info
149157
}
150158

151159
/**

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

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.intellij.ui.SimpleTextAttributes
1616
import com.intellij.util.ArrayUtil
1717
import com.intellij.util.ui.UIUtil
1818
import java.io.File
19-
import java.util.Comparator
2019
import javax.swing.DefaultComboBoxModel
2120
import javax.swing.JList
2221
import org.jetbrains.kotlin.idea.util.rootManager
@@ -25,6 +24,7 @@ import org.utbot.intellij.plugin.generator.CodeGenerationController.getAllTestSo
2524
import org.utbot.intellij.plugin.models.GenerateTestsModel
2625
import org.utbot.intellij.plugin.ui.utils.TestSourceRoot
2726
import org.utbot.intellij.plugin.ui.utils.addDedicatedTestRoot
27+
import org.utbot.intellij.plugin.ui.utils.dedicatedTestSourceRootName
2828
import org.utbot.intellij.plugin.ui.utils.isBuildWithGradle
2929

3030
class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
@@ -59,42 +59,7 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
5959
}
6060
}
6161

62-
var commonModuleSourceDirectory = ""
63-
for ((i, sourceRoot) in model.srcModule.rootManager.sourceRoots.withIndex()) {
64-
commonModuleSourceDirectory = if (i == 0) {
65-
sourceRoot.toNioPath().toString()
66-
} else {
67-
StringUtil.commonPrefix(commonModuleSourceDirectory, sourceRoot.toNioPath().toString())
68-
}
69-
}
70-
// The first sorting to obtain the best candidate
71-
val testRoots = model.getAllTestSourceRoots().distinct().sortedWith(object : Comparator<TestSourceRoot> {
72-
override fun compare(o1: TestSourceRoot, o2: TestSourceRoot): Int {
73-
// Heuristics: Dirs with language == codegenLanguage should go first
74-
val languageOrder = (o1.expectedLanguage == model.codegenLanguage).compareTo(o2.expectedLanguage == model.codegenLanguage)
75-
if (languageOrder != 0) return -languageOrder
76-
// Heuristics: move root that is 'closer' to module 'common' directory to the first position
77-
return -StringUtil.commonPrefixLength(commonModuleSourceDirectory, o1.dir.toNioPath().toString())
78-
.compareTo(StringUtil.commonPrefixLength(commonModuleSourceDirectory, o2.dir.toNioPath().toString()))
79-
}
80-
}).toMutableList()
81-
82-
val theBest = if (testRoots.isNotEmpty()) testRoots[0] else null
83-
84-
// The second sorting to make full list ordered
85-
testRoots.sortWith(compareByDescending<TestSourceRoot> {
86-
// Heuristics: Dirs with language == codegenLanguage should go first
87-
it.expectedLanguage == model.codegenLanguage
88-
}.thenBy {
89-
// ABC-sorting
90-
it.dir.toNioPath()
91-
}
92-
)
93-
// The best candidate should go first to be pre-selected
94-
theBest?.let {
95-
testRoots.remove(it)
96-
testRoots.add(0, it)
97-
}
62+
val testRoots = model.getSortedTestRoots()
9863

9964
// this method is blocked for Gradle, where multiple test modules can exist
10065
model.testModule.addDedicatedTestRoot(testRoots, model.codegenLanguage)
@@ -122,6 +87,33 @@ class TestFolderComboWithBrowseButton(private val model: GenerateTestsModel) :
12287
}
12388
}
12489

90+
private fun GenerateTestsModel.getSortedTestRoots(): MutableList<TestSourceRoot> {
91+
var commonModuleSourceDirectory = ""
92+
for ((i, sourceRoot) in srcModule.rootManager.sourceRoots.withIndex()) {
93+
commonModuleSourceDirectory = if (i == 0) {
94+
sourceRoot.toNioPath().toString()
95+
} else {
96+
StringUtil.commonPrefix(commonModuleSourceDirectory, sourceRoot.toNioPath().toString())
97+
}
98+
}
99+
100+
return getAllTestSourceRoots().distinct().toMutableList().sortedWith(
101+
compareByDescending<TestSourceRoot> {
102+
// Heuristics: Dirs with proper code language should go first
103+
it.expectedLanguage == codegenLanguage
104+
}.thenByDescending {
105+
// Heuristics: Dirs from within module 'common' directory should go first
106+
it.dir.toNioPath().toString().startsWith(commonModuleSourceDirectory)
107+
}.thenByDescending {
108+
// Heuristics: dedicated test source root named 'utbot_tests' should go first
109+
it.dir.name == dedicatedTestSourceRootName
110+
}.thenBy {
111+
// ABC-sorting
112+
it.dir.toNioPath()
113+
}
114+
).toMutableList()
115+
}
116+
125117
private fun chooseTestRoot(model: GenerateTestsModel): VirtualFile? =
126118
ReadAction.compute<VirtualFile, RuntimeException> {
127119
val desc = object:FileChooserDescriptor(false, true, false, false, false, false) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ val Project.isBuildWithGradle get() =
163163
ExternalSystemApiUtil.isExternalSystemAwareModule(GRADLE_SYSTEM_ID, it)
164164
}
165165

166-
private const val dedicatedTestSourceRootName = "utbot_tests"
166+
const val dedicatedTestSourceRootName = "utbot_tests"
167167

168168
fun Module.addDedicatedTestRoot(testSourceRoots: MutableList<TestSourceRoot>, language: CodegenLanguage): VirtualFile? {
169169
// Don't suggest new test source roots for Gradle project where 'unexpected' test roots won't work

0 commit comments

Comments
 (0)