Skip to content

Commit 904f50c

Browse files
committed
TODO: uncommit, there are some unfinished todo's
1 parent d5e9d50 commit 904f50c

File tree

2 files changed

+65
-23
lines changed

2 files changed

+65
-23
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgUtilClassConstructor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.utbot.framework.codegen.model.CodeGenerator
44
import org.utbot.framework.codegen.model.UtilClassKind
55
import org.utbot.framework.codegen.model.constructor.builtin.utUtilsClassId
66
import org.utbot.framework.codegen.model.tree.CgRegularClassFile
7+
import org.utbot.framework.codegen.model.tree.CgSingleLineComment
78
import org.utbot.framework.codegen.model.tree.CgUtilMethod
89
import org.utbot.framework.codegen.model.tree.buildRegularClass
910
import org.utbot.framework.codegen.model.tree.buildRegularClassBody
@@ -22,6 +23,8 @@ internal object CgUtilClassConstructor {
2223
declaredClass = buildRegularClass {
2324
id = utUtilsClassId
2425
body = buildRegularClassBody {
26+
// TODO: get actual UTBot version and use it instead of the hardcoded one
27+
content += CgSingleLineComment("UTBot version: 1.0-SNAPSHOT")
2528
content += utilMethodProvider.utilMethodIds.map { CgUtilMethod(it) }
2629
}
2730
}

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

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import com.intellij.openapi.wm.ToolWindowManager
2222
import com.intellij.psi.JavaDirectoryService
2323
import com.intellij.psi.PsiClass
2424
import com.intellij.psi.PsiClassOwner
25+
import com.intellij.psi.PsiComment
2526
import com.intellij.psi.PsiDirectory
2627
import com.intellij.psi.PsiDocumentManager
2728
import com.intellij.psi.PsiElement
@@ -49,6 +50,7 @@ import org.jetbrains.kotlin.psi.KtPsiFactory
4950
import org.jetbrains.kotlin.psi.psiUtil.endOffset
5051
import org.jetbrains.kotlin.psi.psiUtil.startOffset
5152
import org.jetbrains.kotlin.scripting.resolve.classId
53+
import org.jetbrains.plugins.groovy.lang.psi.util.childrenOfType
5254
import org.utbot.common.HTML_LINE_SEPARATOR
5355
import org.utbot.common.PathUtil.toHtmlLinkTag
5456
import org.utbot.common.appendHtmlLine
@@ -151,33 +153,18 @@ object CodeGenerationController {
151153

152154
run(EDT_LATER) {
153155
waitForCountDown(latch, timeout = 100, timeUnit = TimeUnit.MILLISECONDS) {
154-
val project = model.project
155-
val language = model.codegenLanguage
156-
val testModule = model.testModule
157-
158-
val existingUtilClass = language.getUtilClassOrNull(project, testModule)
159-
160156
val utilClassKind = utilClassListener.requiredUtilClassKind
161157
?: return@waitForCountDown // no util class needed
162158

163-
val utilClassExists = existingUtilClass != null
164-
val mockFrameworkNotUsed = !utilClassListener.mockFrameworkUsed
165-
166-
if (utilClassExists && mockFrameworkNotUsed) {
167-
// If util class already exists and mock framework is not used,
168-
// then existing util class is enough, and we don't need to generate a new one.
169-
// That's because both regular and mock versions of util class can work
170-
// with tests that do not use mocks, so we do not have to worry about
171-
// version of util class that we have at the moment.
172-
return@waitForCountDown
159+
val existingUtilClass = model.codegenLanguage.getUtilClassOrNull(model.project, model.testModule)
160+
if (shouldCreateOrUpdateUtilClass(existingUtilClass, utilClassListener)) {
161+
createOrUpdateUtilClass(
162+
testDirectory = baseTestDirectory,
163+
utilClassKind = utilClassKind,
164+
existingUtilClass = existingUtilClass,
165+
model = model
166+
)
173167
}
174-
175-
createOrUpdateUtilClass(
176-
testDirectory = baseTestDirectory,
177-
utilClassKind = utilClassKind,
178-
existingUtilClass = existingUtilClass,
179-
model = model
180-
)
181168
}
182169
}
183170

@@ -208,6 +195,39 @@ object CodeGenerationController {
208195
}
209196
}
210197

198+
private fun shouldCreateOrUpdateUtilClass(existingUtilClass: PsiFile?, utilClassListener: UtilClassListener): Boolean {
199+
val existingUtilClassVersion = existingUtilClass?.utilClassVersionOrNull
200+
// TODO: here should be the current version of UTBot
201+
val newUtilClassVersion = "1.0-SNAPSHOT"
202+
val versionIsUpdated = existingUtilClassVersion != newUtilClassVersion
203+
204+
val mockFrameworkNotUsed = !utilClassListener.mockFrameworkUsed
205+
206+
val utilClassExists = existingUtilClass != null
207+
208+
if (!utilClassExists) {
209+
// If no util class exists, then we should create a new one.
210+
return true
211+
}
212+
213+
if (versionIsUpdated) {
214+
// If an existing util class is out of date,
215+
// then we must overwrite it with a newer version.
216+
return true
217+
}
218+
219+
if (mockFrameworkNotUsed) {
220+
// If util class already exists and mock framework is not used,
221+
// then existing util class is enough, and we don't need to generate a new one.
222+
// That's because both regular and mock versions of util class can work
223+
// with tests that do not use mocks, so we do not have to worry about
224+
// version of util class that we have at the moment.
225+
return false
226+
}
227+
228+
return true
229+
}
230+
211231
/**
212232
* If [existingUtilClass] is null (no util class exists), then we create package directories for util class,
213233
* create util class itself, and put it into the corresponding directory.
@@ -312,6 +332,25 @@ object CodeGenerationController {
312332
return utUtilsFile
313333
}
314334

335+
/**
336+
* Util class must have a comment that specifies the version of UTBot it was generated with.
337+
* This property represents the version specified by this comment if it exists. Otherwise, the property is `null`.
338+
*/
339+
private val PsiFile.utilClassVersionOrNull: String?
340+
get() = runReadAction {
341+
childrenOfType<PsiComment>()
342+
.map { comment -> comment.text }
343+
.firstOrNull { text -> UTBOT_VERSION_PREFIX in text }
344+
?.substringAfterLast(UTBOT_VERSION_PREFIX)
345+
?.trim()
346+
}
347+
348+
/**
349+
* Util class must have a comment that specifies the version of UTBot it was generated with.
350+
* This prefix is the start of this comment. The version of UTBot goes after it in the comment.
351+
*/
352+
private const val UTBOT_VERSION_PREFIX = "UTBot version:"
353+
315354
/**
316355
* @param srcClass class under test
317356
* @return name of the package of a given [srcClass].

0 commit comments

Comments
 (0)