Skip to content

Commit abda14d

Browse files
committed
TODO: uncommit, there are some unfinished todo's
1 parent 61c2b42 commit abda14d

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.allNestedClasses
@@ -159,33 +161,18 @@ object CodeGenerationController {
159161

160162
run(EDT_LATER) {
161163
waitForCountDown(latch, timeout = 100, timeUnit = TimeUnit.MILLISECONDS) {
162-
val project = model.project
163-
val language = model.codegenLanguage
164-
val testModule = model.testModule
165-
166-
val existingUtilClass = language.getUtilClassOrNull(project, testModule)
167-
168164
val utilClassKind = utilClassListener.requiredUtilClassKind
169165
?: return@waitForCountDown // no util class needed
170166

171-
val utilClassExists = existingUtilClass != null
172-
val mockFrameworkNotUsed = !utilClassListener.mockFrameworkUsed
173-
174-
if (utilClassExists && mockFrameworkNotUsed) {
175-
// If util class already exists and mock framework is not used,
176-
// then existing util class is enough, and we don't need to generate a new one.
177-
// That's because both regular and mock versions of util class can work
178-
// with tests that do not use mocks, so we do not have to worry about
179-
// version of util class that we have at the moment.
180-
return@waitForCountDown
167+
val existingUtilClass = model.codegenLanguage.getUtilClassOrNull(model.project, model.testModule)
168+
if (shouldCreateOrUpdateUtilClass(existingUtilClass, utilClassListener)) {
169+
createOrUpdateUtilClass(
170+
testDirectory = baseTestDirectory,
171+
utilClassKind = utilClassKind,
172+
existingUtilClass = existingUtilClass,
173+
model = model
174+
)
181175
}
182-
183-
createOrUpdateUtilClass(
184-
testDirectory = baseTestDirectory,
185-
utilClassKind = utilClassKind,
186-
existingUtilClass = existingUtilClass,
187-
model = model
188-
)
189176
}
190177
}
191178

@@ -216,6 +203,39 @@ object CodeGenerationController {
216203
}
217204
}
218205

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

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

0 commit comments

Comments
 (0)