Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving Kotlin classfiles properly #392

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ abstract class TestCompiler(libPaths: List<String>, junitLibPaths: List<String>)
* @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<String>,
buildPath: String,
testCases: MutableList<TestCaseGeneratedByLLM>,
workingDir: String
): TestCasesCompilationResult {
var allTestCasesCompilable = true
val compilableTestCases: MutableSet<TestCaseGeneratedByLLM> = 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])
Expand All @@ -46,10 +48,11 @@ abstract class TestCompiler(libPaths: List<String>, junitLibPaths: List<String>)
*
* @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<Boolean, String>
abstract fun compileCode(path: String, projectBuildPath: String, workingDir: String): Pair<Boolean, String>

/**
* Generates the path for the command by concatenating the necessary paths.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class JavaTestCompiler(
javac = javaCompiler.absolutePath
}

override fun compileCode(path: String, projectBuildPath: String): Pair<Boolean, String> {
override fun compileCode(path: String, projectBuildPath: String, workingDir: String): Pair<Boolean, String> {
val classPaths = "\"${getClassPaths(projectBuildPath)}\""
// compile file
val errorMsg = CommandLineRunner.run(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.io.File
class KotlinTestCompiler(
libPaths: List<String>,
junitLibPaths: List<String>,
kotlinSDKHomeDirectory: String,
kotlinSDKHomeDirectory: String
) : TestCompiler(libPaths, junitLibPaths) {
private val logger = KotlinLogging.logger { this::class.java }
private val kotlinc: String
Expand Down Expand Up @@ -49,7 +49,7 @@ class KotlinTestCompiler(
kotlinc = kotlinCompiler.absolutePath
}

override fun compileCode(path: String, projectBuildPath: String): Pair<Boolean, String> {
override fun compileCode(path: String, projectBuildPath: String, workingDir: String): Pair<Boolean, String> {
logger.info { "[KotlinTestCompiler] Compiling ${path.substringAfterLast('/')}" }

val classPaths = "\"${getClassPaths(projectBuildPath)}\""
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading