diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/generation/llm/LLMWithFeedbackCycle.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/generation/llm/LLMWithFeedbackCycle.kt index e12dbfbe2..7daaf8ff9 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/generation/llm/LLMWithFeedbackCycle.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/generation/llm/LLMWithFeedbackCycle.kt @@ -263,9 +263,9 @@ class LLMWithFeedbackCycle( indicator.setText("Compilation tests checking") val testCasesCompilationResult = - testCompiler.compileTestCases(generatedTestCasesPaths, buildPath, testCases) + testCompiler.compileTestCases(generatedTestCasesPaths, buildPath, testCases, resultPath) val testSuiteCompilationResult = - testCompiler.compileCode(File(generatedTestSuitePath).absolutePath, buildPath) + testCompiler.compileCode(File(generatedTestSuitePath).absolutePath, buildPath, resultPath) // saving the compilable test cases compilableTestCases.addAll(testCasesCompilationResult.compilableTestCases) diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/TestCompiler.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/TestCompiler.kt index 1c0297cee..9e78092f4 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/TestCompiler.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/TestCompiler.kt @@ -20,18 +20,20 @@ abstract class TestCompiler(libPaths: List, junitLibPaths: List) * @param generatedTestCasesPaths A list of file paths where the generated test cases are located. * @param buildPath All the directories where the compiled code of the project under test is saved. This path is used as a classpath to run each test case. * @param testCases A mutable list of `TestCaseGeneratedByLLM` objects representing the test cases to be compiled. + * @param workingDir The path of the directory that contains package directories of the code to compile * @return A `TestCasesCompilationResult` object containing the overall compilation success status and a set of compilable test cases. */ fun compileTestCases( generatedTestCasesPaths: List, buildPath: String, testCases: MutableList, + workingDir: String ): TestCasesCompilationResult { var allTestCasesCompilable = true val compilableTestCases: MutableSet = mutableSetOf() for (index in generatedTestCasesPaths.indices) { - val compilable = compileCode(generatedTestCasesPaths[index], buildPath).first + val compilable = compileCode(generatedTestCasesPaths[index], buildPath, workingDir).first allTestCasesCompilable = allTestCasesCompilable && compilable if (compilable) { compilableTestCases.add(testCases[index]) @@ -46,10 +48,11 @@ abstract class TestCompiler(libPaths: List, junitLibPaths: List) * * @param path The path of the code file to compile. * @param projectBuildPath All the directories where the compiled code of the project under test is saved. This path is used as a classpath to run each test case. + * @param workingDir The path of the directory that contains package directories of the code to compile * @return A pair containing a boolean value indicating whether the compilation was successful (true) or not (false), * and a string message describing any error encountered during compilation. */ - abstract fun compileCode(path: String, projectBuildPath: String): Pair + abstract fun compileCode(path: String, projectBuildPath: String, workingDir: String): Pair /** * Generates the path for the command by concatenating the necessary paths. diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/java/JavaTestCompiler.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/java/JavaTestCompiler.kt index 24d875506..a13e844d9 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/java/JavaTestCompiler.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/java/JavaTestCompiler.kt @@ -37,7 +37,7 @@ class JavaTestCompiler( javac = javaCompiler.absolutePath } - override fun compileCode(path: String, projectBuildPath: String): Pair { + override fun compileCode(path: String, projectBuildPath: String, workingDir: String): Pair { val classPaths = "\"${getClassPaths(projectBuildPath)}\"" // compile file val errorMsg = CommandLineRunner.run( @@ -49,15 +49,18 @@ class JavaTestCompiler( "-cp", classPaths, path, + /** + * We don't have to provide -d option, since javac saves class files in the same place by default + */ ), ) logger.info { "Error message: '$errorMsg'" } // create .class file path - val classFilePath = path.replace(".java", ".class") + val classFilePath = path.removeSuffix(".java") + ".class" - // check is .class file exists - return Pair(File(classFilePath).exists(), errorMsg) + // check if .class file exists + return Pair(File(classFilePath).exists() && errorMsg.isBlank(), errorMsg) } override fun getClassPaths(buildPath: String): String { diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt index 3e1573c48..da10a5c28 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt @@ -10,7 +10,7 @@ import java.io.File class KotlinTestCompiler( libPaths: List, junitLibPaths: List, - kotlinSDKHomeDirectory: String, + kotlinSDKHomeDirectory: String ) : TestCompiler(libPaths, junitLibPaths) { private val logger = KotlinLogging.logger { this::class.java } private val kotlinc: String @@ -49,7 +49,7 @@ class KotlinTestCompiler( kotlinc = kotlinCompiler.absolutePath } - override fun compileCode(path: String, projectBuildPath: String): Pair { + override fun compileCode(path: String, projectBuildPath: String, workingDir: String): Pair { logger.info { "[KotlinTestCompiler] Compiling ${path.substringAfterLast('/')}" } val classPaths = "\"${getClassPaths(projectBuildPath)}\"" @@ -64,13 +64,20 @@ class KotlinTestCompiler( "-cp", classPaths, path, + /** + * Forcing kotlinc to save a classfile in the same place, as '.kt' file + */ + "-d", + workingDir ), ) logger.info { "Error message: '$errorMsg'" } - // No need to save the .class file for kotlin, so checking the error message is enough - return Pair(errorMsg.isBlank(), errorMsg) + val classFilePath = path.removeSuffix(".kt") + ".class" + + // check if .class file exists + return Pair(File(classFilePath).exists() && errorMsg.isBlank(), errorMsg) } override fun getClassPaths(buildPath: String): String = commonPath.plus(buildPath) diff --git a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt index 1bf6acb2a..f304c965e 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt @@ -178,7 +178,7 @@ class TestProcessor( ) // compilation checking - val compilationResult = testCompiler.compileCode(generatedTestPath, buildPath) + val compilationResult = testCompiler.compileCode(generatedTestPath, buildPath, resultPath) if (!compilationResult.first) { testsExecutionResultManager.addFailedTest(testId, testCode, compilationResult.second) } else {