From 89919e86b1043ee39a87dc82732e6923a15c05c1 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Fri, 11 Oct 2024 20:58:08 +0200 Subject: [PATCH 1/3] Proper kotlin classfiles save --- .../core/generation/llm/LLMWithFeedbackCycle.kt | 4 ++-- .../research/testspark/core/test/TestCompiler.kt | 5 +++-- .../testspark/core/test/java/JavaTestCompiler.kt | 9 ++++++--- .../core/test/kotlin/KotlinTestCompiler.kt | 15 +++++++++++---- .../research/testspark/tools/TestProcessor.kt | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) 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..b103d070a 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 @@ -26,12 +26,13 @@ abstract class TestCompiler(libPaths: List, junitLibPaths: List) 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]) @@ -49,7 +50,7 @@ abstract class TestCompiler(libPaths: List, junitLibPaths: List) * @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..b752a579c 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,6 +49,9 @@ class JavaTestCompiler( "-cp", classPaths, path, + /** + * We don't have to provide -d option, since javac saves class files in the same place by default + */ ), ) @@ -56,8 +59,8 @@ class JavaTestCompiler( // create .class file path val classFilePath = path.replace(".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..17fdfeb48 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.replace(".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 { From 51b9357100d4d77f7b8daa7fe26893a10c456824 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Fri, 11 Oct 2024 21:02:42 +0200 Subject: [PATCH 2/3] Comments update --- .../org/jetbrains/research/testspark/core/test/TestCompiler.kt | 2 ++ 1 file changed, 2 insertions(+) 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 b103d070a..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,6 +20,7 @@ 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( @@ -47,6 +48,7 @@ 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. */ From 292bb28949b5cd7cc5d18982132f6a8d98cca665 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Mon, 14 Oct 2024 15:03:47 +0200 Subject: [PATCH 3/3] Comments update --- .../research/testspark/core/test/java/JavaTestCompiler.kt | 2 +- .../research/testspark/core/test/kotlin/KotlinTestCompiler.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 b752a579c..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 @@ -57,7 +57,7 @@ class JavaTestCompiler( logger.info { "Error message: '$errorMsg'" } // create .class file path - val classFilePath = path.replace(".java", ".class") + val classFilePath = path.removeSuffix(".java") + ".class" // check if .class file exists return Pair(File(classFilePath).exists() && errorMsg.isBlank(), errorMsg) 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 17fdfeb48..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 @@ -74,7 +74,7 @@ class KotlinTestCompiler( logger.info { "Error message: '$errorMsg'" } - val classFilePath = path.replace(".kt", ".class") + val classFilePath = path.removeSuffix(".kt") + ".class" // check if .class file exists return Pair(File(classFilePath).exists() && errorMsg.isBlank(), errorMsg)