Skip to content

Commit 2e0427a

Browse files
committed
Fix build directory for classes from Kotlin files
1 parent 8fa3bb5 commit 2e0427a

File tree

8 files changed

+184
-185
lines changed

8 files changed

+184
-185
lines changed

utbot-cli/src/main/kotlin/org/utbot/cli/GenerateTestsAbstractCommand.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ abstract class GenerateTestsAbstractCommand(name: String, help: String) :
197197
UtSettings.treatOverflowAsError = treatOverflowAsError == TreatOverflowAsError.AS_ERROR
198198

199199
return TestCaseGenerator(
200-
workingDirectory,
200+
listOf(workingDirectory),
201201
classPathNormalized,
202202
System.getProperty("java.class.path"),
203203
JdkInfoDefaultProvider().info

utbot-framework/src/main/kotlin/org/utbot/external/api/UtBotJavaApi.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object UtBotJavaApi {
113113

114114
testSets.addAll(withUtContext(utContext) {
115115
val buildPath = FileUtil.isolateClassFiles(classUnderTest).toPath()
116-
TestCaseGenerator(buildPath, classpath, dependencyClassPath, jdkInfo = JdkInfoDefaultProvider().info)
116+
TestCaseGenerator(listOf(buildPath), classpath, dependencyClassPath, jdkInfo = JdkInfoDefaultProvider().info)
117117
.generate(
118118
methodsForAutomaticGeneration.map {
119119
it.methodToBeTestedFromUserInput.executableId
@@ -173,7 +173,7 @@ object UtBotJavaApi {
173173

174174
return withUtContext(UtContext(classUnderTest.classLoader)) {
175175
val buildPath = FileUtil.isolateClassFiles(classUnderTest).toPath()
176-
TestCaseGenerator(buildPath, classpath, dependencyClassPath, jdkInfo = JdkInfoDefaultProvider().info)
176+
TestCaseGenerator(listOf(buildPath), classpath, dependencyClassPath, jdkInfo = JdkInfoDefaultProvider().info)
177177
.generate(
178178
methodsForAutomaticGeneration.map {
179179
it.methodToBeTestedFromUserInput.executableId

utbot-framework/src/main/kotlin/org/utbot/framework/plugin/api/TestCaseGenerator.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ import kotlin.reflect.KCallable
5252
* Generates test cases: one by one or a whole set for the method under test.
5353
*
5454
* Note: the instantiating of [TestCaseGenerator] may take some time,
55-
* because it requires initializing Soot for the current [buildDir] and [classpath].
55+
* because it requires initializing Soot for the current [buildDirs] and [classpath].
5656
*
5757
* @param jdkInfo specifies the JRE and the runtime library version used for analysing system classes and user's code.
58-
* @param forceSootReload forces to reinitialize Soot even if the previous buildDir equals to [buildDir] and previous
58+
* @param forceSootReload forces to reinitialize Soot even if the previous buildDirs equals to [buildDirs] and previous
5959
* classpath equals to [classpath]. This is the case for plugin scenario, as the source code may be modified.
6060
*/
6161
open class TestCaseGenerator(
62-
private val buildDir: Path,
62+
private val buildDirs: List<Path>,
6363
private val classpath: String?,
6464
private val dependencyPaths: String,
6565
private val jdkInfo: JdkInfo,
@@ -71,21 +71,21 @@ open class TestCaseGenerator(
7171
private val timeoutLogger: KLogger = KotlinLogging.logger(logger.name + ".timeout")
7272

7373
private val classpathForEngine: String
74-
get() = buildDir.toString() + (classpath?.let { File.pathSeparator + it } ?: "")
74+
get() = (buildDirs + listOfNotNull(classpath)).joinToString(File.pathSeparator)
7575

7676
init {
7777
if (!isCanceled()) {
7878
checkFrameworkDependencies(dependencyPaths)
7979

80-
logger.trace("Initializing ${this.javaClass.name} with buildDir = $buildDir, classpath = $classpath")
80+
logger.trace("Initializing ${this.javaClass.name} with buildDir = $buildDirs, classpath = $classpath")
8181

8282

8383
if (disableCoroutinesDebug) {
8484
System.setProperty(kotlinx.coroutines.DEBUG_PROPERTY_NAME, kotlinx.coroutines.DEBUG_PROPERTY_VALUE_OFF)
8585
}
8686

8787
timeoutLogger.trace().bracket("Soot initialization") {
88-
SootUtils.runSoot(buildDir, classpath, forceSootReload, jdkInfo)
88+
SootUtils.runSoot(buildDirs, classpath, forceSootReload, jdkInfo)
8989
}
9090

9191
//warmup

utbot-framework/src/main/kotlin/org/utbot/framework/util/SootUtils.kt

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import org.utbot.engine.overrides.UtOverrideMock
1919
import org.utbot.engine.overrides.collections.AbstractCollection
2020
import org.utbot.engine.overrides.collections.AssociativeArray
2121
import org.utbot.engine.overrides.collections.Collection
22-
import org.utbot.engine.overrides.collections.List
22+
import org.utbot.engine.overrides.collections.List as UtList
2323
import org.utbot.engine.overrides.collections.RangeModifiableUnlimitedArray
2424
import org.utbot.engine.overrides.collections.UtArrayList
2525
import org.utbot.engine.overrides.collections.UtGenericAssociative
@@ -58,40 +58,40 @@ object SootUtils {
5858
*
5959
* @param jdkInfo specifies the JRE and the runtime library version used for analysing system classes and user's
6060
* code.
61-
* @param forceReload forces to reinitialize Soot even if the [previousBuildDir] equals to the class buildDir.
61+
* @param forceReload forces to reinitialize Soot even if the [previousBuildDirs] equals to the class buildDir.
6262
*/
6363
fun runSoot(clazz: java.lang.Class<*>, forceReload: kotlin.Boolean, jdkInfo: JdkInfo) {
6464
val buildDir = FileUtil.locateClassPath(clazz) ?: FileUtil.isolateClassFiles(clazz)
6565
val buildDirPath = buildDir.toPath()
6666

67-
runSoot(buildDirPath, null, forceReload, jdkInfo)
67+
runSoot(listOf(buildDirPath), null, forceReload, jdkInfo)
6868
}
6969

7070

7171
/**
7272
* @param jdkInfo specifies the JRE and the runtime library version used for analysing system classes and user's
7373
* code.
74-
* @param forceReload forces to reinitialize Soot even if the [previousBuildDir] equals to [buildDirPath] and
74+
* @param forceReload forces to reinitialize Soot even if the [previousBuildDirs] equals to [buildDirPaths] and
7575
* [previousClassPath] equals to [classPath].
7676
*/
77-
fun runSoot(buildDirPath: Path, classPath: String?, forceReload: kotlin.Boolean, jdkInfo: JdkInfo) {
77+
fun runSoot(buildDirPaths: List<Path>, classPath: String?, forceReload: kotlin.Boolean, jdkInfo: JdkInfo) {
7878
synchronized(this) {
79-
if (buildDirPath != previousBuildDir || classPath != previousClassPath || forceReload) {
80-
initSoot(buildDirPath, classPath, jdkInfo)
81-
previousBuildDir = buildDirPath
79+
if (buildDirPaths != previousBuildDirs || classPath != previousClassPath || forceReload) {
80+
initSoot(buildDirPaths, classPath, jdkInfo)
81+
previousBuildDirs = buildDirPaths
8282
previousClassPath = classPath
8383
}
8484
}
8585
}
8686

87-
private var previousBuildDir: Path? = null
87+
private var previousBuildDirs: List<Path>? = null
8888
private var previousClassPath: String? = null
8989
}
9090

9191
/**
9292
* Convert code to Jimple
9393
*/
94-
private fun initSoot(buildDir: Path, classpath: String?, jdkInfo: JdkInfo) {
94+
private fun initSoot(buildDirs: List<Path>, classpath: String?, jdkInfo: JdkInfo) {
9595
G.reset()
9696
val options = Options.v()
9797

@@ -107,7 +107,7 @@ private fun initSoot(buildDir: Path, classpath: String?, jdkInfo: JdkInfo) {
107107
+ if (!classpath.isNullOrEmpty()) File.pathSeparator + "$classpath" else ""
108108
)
109109
set_src_prec(Options.src_prec_only_class)
110-
set_process_dir(listOf("$buildDir"))
110+
set_process_dir(buildDirs.map { "$it" })
111111
set_keep_line_number(true)
112112
set_ignore_classpath_errors(true) // gradle/build/resources/main does not exists, but it's not a problem
113113
set_output_format(Options.output_format_jimple)
@@ -196,7 +196,7 @@ private val classesToLoad = arrayOf(
196196
Stream::class,
197197
Arrays::class,
198198
Collection::class,
199-
List::class,
199+
UtList::class,
200200
UtStream::class,
201201
UtStream.UtStreamIterator::class
202202
).map { it.java }.toTypedArray()

utbot-framework/src/main/kotlin/org/utbot/tests/infrastructure/TestSpecificTestCaseGenerator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TestSpecificTestCaseGenerator(
3333
engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(),
3434
isCanceled: () -> Boolean = { false },
3535
): TestCaseGenerator(
36-
buildDir,
36+
listOf(buildDir),
3737
classpath,
3838
dependencyPaths,
3939
JdkInfoDefaultProvider().info,

utbot-gradle/src/main/kotlin/org/utbot/gradle/plugin/GenerateTestsAndSarifReportTask.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ open class GenerateTestsAndSarifReportTask @Inject constructor(
9090
withUtContext(UtContext(sourceSet.classLoader)) {
9191
val testCaseGenerator =
9292
TestCaseGenerator(
93-
sourceSet.workingDirectory,
93+
listOf(sourceSet.workingDirectory),
9494
sourceSet.runtimeClasspath,
9595
dependencyPaths,
9696
JdkInfoDefaultProvider().info

0 commit comments

Comments
 (0)