From 3afa35075a624c5caa6eaa4c08c887cd0ce45992 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Wed, 9 Oct 2024 21:56:17 +0200 Subject: [PATCH 1/6] CommandLineRunner API changes --- .../testspark/core/test/java/JavaTestCompiler.kt | 7 ++++--- .../core/test/kotlin/KotlinTestCompiler.kt | 16 +++++++++------- .../testspark/core/utils/CommandLineRunner.kt | 13 ++++++------- .../research/testspark/tools/TestProcessor.kt | 6 +++--- .../generation/EvoSuiteProcessManager.kt | 2 +- 5 files changed, 23 insertions(+), 21 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 4553f5702..c0e69fff0 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 @@ -34,7 +34,7 @@ class JavaTestCompiler( println("javac found at '${javaCompile.absolutePath}'") // compile file - val errorMsg = CommandLineRunner.run( + val executionResult = CommandLineRunner.run( arrayListOf( javaCompile.absolutePath, "-cp", @@ -42,13 +42,14 @@ class JavaTestCompiler( path, ), ) + val executionMsg = executionResult.second - log.info { "Error message: '$errorMsg'" } + log.info { "Execution result: '${executionMsg}'" } // create .class file path val classFilePath = path.replace(".java", ".class") // check is .class file exists - return Pair(File(classFilePath).exists(), errorMsg) + return Pair(File(classFilePath).exists() && (executionResult.first == 0), executionMsg) } 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 fe74d036a..14fac0d86 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 @@ -15,7 +15,7 @@ class KotlinTestCompiler(libPaths: List, junitLibPaths: List) : // TODO find the kotlinc if it is not in PATH val classPaths = "\"${getClassPaths(projectBuildPath)}\"" // Compile file - val errorMsg = CommandLineRunner.run( + val executionResult = CommandLineRunner.run( arrayListOf( "kotlinc", "-cp", @@ -23,16 +23,18 @@ class KotlinTestCompiler(libPaths: List, junitLibPaths: List) : path, ), ) + val executionMsg = executionResult.second + val execSuccessful = executionResult.first == 0 - if (errorMsg.isNotEmpty()) { - log.info { "Error message: '$errorMsg'" } - if (errorMsg.contains("kotlinc: command not found'")) { - throw RuntimeException(errorMsg) + if (!execSuccessful) { + log.info { "Error message: '$executionMsg'" } + if (executionMsg.contains("kotlinc: command not found'")) { + throw RuntimeException(executionMsg) } } - // No need to save the .class file for kotlin, so checking the error message is enough - return Pair(errorMsg.isBlank(), errorMsg) + // TODO `.class` files are not saving for Kotlin + return Pair(execSuccessful, executionMsg) } override fun getClassPaths(buildPath: String): String = commonPath.plus(buildPath) diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt index 97e870bae..5d2d79eb0 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt @@ -9,13 +9,13 @@ class CommandLineRunner { protected val log = KotlinLogging.logger {} /** - * Executes a command line process and returns the output as a string. + * Executes a command line process * * @param cmd The command line arguments as an ArrayList of strings. - * @return The output of the command line process as a string. + * @return A pair containing exit value and a string message containing execution results */ - fun run(cmd: ArrayList): String { - var errorMessage = "" + fun run(cmd: ArrayList): Pair { + var executionMsg = "" /** * Since Windows does not provide bash, use cmd or similar default command line interpreter @@ -37,12 +37,11 @@ class CommandLineRunner { var line: String? while (reader.readLine().also { line = it } != null) { - errorMessage += line + executionMsg += line } process.waitFor() - - return errorMessage + return Pair(process.exitValue(), executionMsg) } } } 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..ae3a63ce5 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt @@ -99,7 +99,7 @@ class TestProcessor( } else { "-javaagent:$jacocoAgentLibraryPath=destfile=$dataFileName.exec,append=false" } - val testExecutionError = CommandLineRunner.run( + val testExecutionResult = CommandLineRunner.run( arrayListOf( javaRunner.absolutePath, javaAgentFlag, @@ -110,7 +110,7 @@ class TestProcessor( ), ) - log.info("Test execution error message: $testExecutionError") + log.info("Test execution message: ${testExecutionResult.second}") // Prepare the command for generating the Jacoco report val command = mutableListOf( @@ -140,7 +140,7 @@ class TestProcessor( CommandLineRunner.run(command as ArrayList) - return testExecutionError + return if (testExecutionResult.first == 0) "" else testExecutionResult.second } /** diff --git a/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt b/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt index 3ae554a8e..04071cf6c 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt @@ -84,7 +84,7 @@ class EvoSuiteProcessManager( if (ToolUtils.isProcessStopped(errorMonitor, indicator)) return null val regex = Regex("version \"(.*?)\"") - val version = regex.find(CommandLineRunner.run(arrayListOf(evoSuiteSettingsState.javaPath, "-version"))) + val version = regex.find(CommandLineRunner.run(arrayListOf(evoSuiteSettingsState.javaPath, "-version")).second) ?.groupValues ?.get(1) ?.split(".") From 336d100f5b5deca5b868ae0f611fbc0302e02dfe Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Wed, 9 Oct 2024 22:05:47 +0200 Subject: [PATCH 2/6] klint fixes --- .../research/testspark/core/test/java/JavaTestCompiler.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c0e69fff0..2038b1896 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 @@ -44,7 +44,7 @@ class JavaTestCompiler( ) val executionMsg = executionResult.second - log.info { "Execution result: '${executionMsg}'" } + log.info { "Execution result: '$executionMsg'" } // create .class file path val classFilePath = path.replace(".java", ".class") From aa221840bf05b9d0dd3db1544f710069bcf4e5f1 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Mon, 14 Oct 2024 19:38:46 +0200 Subject: [PATCH 3/6] Data class for execution results --- .../core/generation/llm/LLMWithFeedbackCycle.kt | 4 +++- .../testspark/core/test/TestCompiler.kt | 11 +++++++++-- .../core/test/java/JavaTestCompiler.kt | 13 +++++-------- .../core/test/kotlin/KotlinTestCompiler.kt | 17 +++++------------ .../testspark/core/utils/CommandLineRunner.kt | 6 +++--- .../research/testspark/tools/TestProcessor.kt | 8 ++++---- .../generation/EvoSuiteProcessManager.kt | 3 ++- 7 files changed, 31 insertions(+), 31 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 2aa764100..999fe0592 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 @@ -270,7 +270,9 @@ class LLMWithFeedbackCycle( onWarningCallback?.invoke(WarningType.COMPILATION_ERROR_OCCURRED) nextPromptMessage = - "I cannot compile the tests that you provided. The error is:\n${testSuiteCompilationResult.second}\n Fix this issue in the provided tests.\nGenerate public classes and public methods. Response only a code with tests between ```, do not provide any other text." + "I cannot compile the tests that you provided. The error is:\n${testSuiteCompilationResult.executionMessage}" + + "\n Fix this issue in the provided tests.\nGenerate public classes and public methods. Response only a code with tests between ```," + + " do not provide any other text." log.info { nextPromptMessage } continue } 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..d5e940e1e 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 @@ -8,6 +8,13 @@ data class TestCasesCompilationResult( val compilableTestCases: MutableSet, ) +data class ExecutionResult( + val exitCode: Int, + val executionMessage: String, +) { + fun isSuccessful(): Boolean = exitCode == 0 +} + abstract class TestCompiler(libPaths: List, junitLibPaths: List) { val separator = DataFilesUtil.classpathSeparator val dependencyLibPath = libPaths.joinToString(separator.toString()) @@ -31,7 +38,7 @@ abstract class TestCompiler(libPaths: List, junitLibPaths: List) val compilableTestCases: MutableSet = mutableSetOf() for (index in generatedTestCasesPaths.indices) { - val compilable = compileCode(generatedTestCasesPaths[index], buildPath).first + val compilable = compileCode(generatedTestCasesPaths[index], buildPath).isSuccessful() allTestCasesCompilable = allTestCasesCompilable && compilable if (compilable) { compilableTestCases.add(testCases[index]) @@ -49,7 +56,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): ExecutionResult /** * 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 2038b1896..19d69bb1b 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 @@ -1,6 +1,7 @@ package org.jetbrains.research.testspark.core.test.java import io.github.oshai.kotlinlogging.KotlinLogging +import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.test.TestCompiler import org.jetbrains.research.testspark.core.utils.CommandLineRunner import org.jetbrains.research.testspark.core.utils.DataFilesUtil @@ -14,7 +15,7 @@ class JavaTestCompiler( private val log = KotlinLogging.logger { this::class.java } - override fun compileCode(path: String, projectBuildPath: String): Pair { + override fun compileCode(path: String, projectBuildPath: String): ExecutionResult { val classPaths = "\"${getClassPaths(projectBuildPath)}\"" // find the proper javac val javaCompile = File(javaHomeDirectoryPath).walk() @@ -42,14 +43,10 @@ class JavaTestCompiler( path, ), ) - val executionMsg = executionResult.second + log.info { "Exit code: '${executionResult.exitCode}'; Execution message: '${executionResult.executionMessage}'" } - log.info { "Execution result: '$executionMsg'" } - // create .class file path - val classFilePath = path.replace(".java", ".class") - - // check is .class file exists - return Pair(File(classFilePath).exists() && (executionResult.first == 0), executionMsg) + // TODO check for classfiles + return executionResult } 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 14fac0d86..2824b23b3 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 @@ -1,6 +1,7 @@ package org.jetbrains.research.testspark.core.test.kotlin import io.github.oshai.kotlinlogging.KotlinLogging +import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.test.TestCompiler import org.jetbrains.research.testspark.core.utils.CommandLineRunner @@ -9,7 +10,7 @@ class KotlinTestCompiler(libPaths: List, junitLibPaths: List) : private val log = KotlinLogging.logger { this::class.java } - override fun compileCode(path: String, projectBuildPath: String): Pair { + override fun compileCode(path: String, projectBuildPath: String): ExecutionResult { log.info { "[KotlinTestCompiler] Compiling ${path.substringAfterLast('/')}" } // TODO find the kotlinc if it is not in PATH @@ -23,18 +24,10 @@ class KotlinTestCompiler(libPaths: List, junitLibPaths: List) : path, ), ) - val executionMsg = executionResult.second - val execSuccessful = executionResult.first == 0 + log.info { "Exit code: '${executionResult.exitCode}'; Execution message: '${executionResult.executionMessage}'" } - if (!execSuccessful) { - log.info { "Error message: '$executionMsg'" } - if (executionMsg.contains("kotlinc: command not found'")) { - throw RuntimeException(executionMsg) - } - } - - // TODO `.class` files are not saving for Kotlin - return Pair(execSuccessful, executionMsg) + // TODO check for classfiles + return executionResult } override fun getClassPaths(buildPath: String): String = commonPath.plus(buildPath) diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt index 5d2d79eb0..4e3e449a0 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt @@ -1,6 +1,7 @@ package org.jetbrains.research.testspark.core.utils import io.github.oshai.kotlinlogging.KotlinLogging +import org.jetbrains.research.testspark.core.test.ExecutionResult import java.io.BufferedReader import java.io.InputStreamReader @@ -14,7 +15,7 @@ class CommandLineRunner { * @param cmd The command line arguments as an ArrayList of strings. * @return A pair containing exit value and a string message containing execution results */ - fun run(cmd: ArrayList): Pair { + fun run(cmd: ArrayList): ExecutionResult { var executionMsg = "" /** @@ -32,7 +33,6 @@ class CommandLineRunner { .redirectErrorStream(true) .start() } - val reader = BufferedReader(InputStreamReader(process.inputStream)) var line: String? @@ -41,7 +41,7 @@ class CommandLineRunner { } process.waitFor() - return Pair(process.exitValue(), executionMsg) + return ExecutionResult(process.exitValue(), executionMsg) } } } 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 ae3a63ce5..8d18c8135 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt @@ -110,7 +110,7 @@ class TestProcessor( ), ) - log.info("Test execution message: ${testExecutionResult.second}") + log.info("Test execution message: ${testExecutionResult.executionMessage}") // Prepare the command for generating the Jacoco report val command = mutableListOf( @@ -140,7 +140,7 @@ class TestProcessor( CommandLineRunner.run(command as ArrayList) - return if (testExecutionResult.first == 0) "" else testExecutionResult.second + return if (testExecutionResult.isSuccessful()) "" else testExecutionResult.executionMessage } /** @@ -179,8 +179,8 @@ class TestProcessor( // compilation checking val compilationResult = testCompiler.compileCode(generatedTestPath, buildPath) - if (!compilationResult.first) { - testsExecutionResultManager.addFailedTest(testId, testCode, compilationResult.second) + if (!compilationResult.isSuccessful()) { + testsExecutionResultManager.addFailedTest(testId, testCode, compilationResult.executionMessage) } else { val dataFileName = "$resultPath/jacoco-${fileName.split(".")[0]}" diff --git a/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt b/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt index 04071cf6c..9db5b3fc3 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt @@ -84,7 +84,8 @@ class EvoSuiteProcessManager( if (ToolUtils.isProcessStopped(errorMonitor, indicator)) return null val regex = Regex("version \"(.*?)\"") - val version = regex.find(CommandLineRunner.run(arrayListOf(evoSuiteSettingsState.javaPath, "-version")).second) + val versionCommandResult = CommandLineRunner.run(arrayListOf(evoSuiteSettingsState.javaPath, "-version")) + val version = regex.find(versionCommandResult.executionMessage) ?.groupValues ?.get(1) ?.split(".") From 6d2c4d4300a10417403f160cd9539116d79bd448 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Wed, 16 Oct 2024 01:28:58 +0200 Subject: [PATCH 4/6] Minor refactoring --- .../research/testspark/core/exception/Exceptions.kt | 8 ++++++++ .../core/generation/llm/LLMWithFeedbackCycle.kt | 11 +++++++---- .../testspark/core/test/java/JavaTestCompiler.kt | 3 ++- .../testspark/core/test/kotlin/KotlinTestCompiler.kt | 3 ++- .../testspark/core/utils/CommandLineRunner.kt | 7 ++++--- .../research/testspark/tools/TestProcessor.kt | 7 +++++-- .../evosuite/generation/EvoSuiteProcessManager.kt | 1 + 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt index 3d2bccd6a..22859d000 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt @@ -29,3 +29,11 @@ class JavaCompilerNotFoundException(message: String) : TestSparkException(messag * @param message A descriptive message explaining the specific error that led to this exception. */ class JavaSDKMissingException(message: String) : TestSparkException(message) + + +/** + * Represents an exception thrown when a class file could not be found in the same path after the code compilation. + * + * @param message A descriptive message explaining the error + */ +class ClassFileNotFoundException(message: String) : TestSparkException(message) \ No newline at end of file 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 ef143942f..31fcde547 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 @@ -281,10 +281,13 @@ class LLMWithFeedbackCycle( onWarningCallback?.invoke(WarningType.COMPILATION_ERROR_OCCURRED) - nextPromptMessage = - "I cannot compile the tests that you provided. The error is:\n${testSuiteCompilationResult.executionMessage}" + - "\n Fix this issue in the provided tests.\nGenerate public classes and public methods. Response only a code with tests between ```," + - " do not provide any other text." + nextPromptMessage = """ + I cannot compile the tests that you provided. The error is: + ``` + ${testSuiteCompilationResult.executionMessage} + ``` + Fix this issue in the provided tests.\nGenerate public classes and public methods. Response only a code with tests between ```, do not provide any other text. + """.trimIndent() log.info { nextPromptMessage } continue } 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 642e74906..ce5b1f932 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 @@ -1,6 +1,7 @@ package org.jetbrains.research.testspark.core.test.java import io.github.oshai.kotlinlogging.KotlinLogging +import org.jetbrains.research.testspark.core.exception.ClassFileNotFoundException import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.exception.JavaCompilerNotFoundException import org.jetbrains.research.testspark.core.test.TestCompiler @@ -59,7 +60,7 @@ class JavaTestCompiler( val classFilePath = path.replace(".java", ".class") if (!File(classFilePath).exists()) { - throw IllegalStateException("Class files wasnt saved proeprly") + throw ClassFileNotFoundException("Expected class file at $classFilePath after the compilation of file $path, but it does not exist.") } return executionResult } 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 840dfd984..abcd86a9e 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 @@ -1,6 +1,7 @@ package org.jetbrains.research.testspark.core.test.kotlin import io.github.oshai.kotlinlogging.KotlinLogging +import org.jetbrains.research.testspark.core.exception.ClassFileNotFoundException import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.exception.KotlinCompilerNotFoundException import org.jetbrains.research.testspark.core.test.TestCompiler @@ -75,7 +76,7 @@ class KotlinTestCompiler( val classFilePath = path.removeSuffix(".kt") + ".class" if (!File(classFilePath).exists()) { - throw IllegalStateException("NO classfiles saved") + throw ClassFileNotFoundException("Expected class file at $classFilePath after the compilation of file $path, but it does not exist.") } return executionResult } diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt index 4e3e449a0..a9d0343c9 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/utils/CommandLineRunner.kt @@ -13,13 +13,13 @@ class CommandLineRunner { * Executes a command line process * * @param cmd The command line arguments as an ArrayList of strings. - * @return A pair containing exit value and a string message containing execution results + * @return A pair containing exit code and a string message containing stdout and stderr of the executed process. */ fun run(cmd: ArrayList): ExecutionResult { var executionMsg = "" /** - * Since Windows does not provide bash, use cmd or similar default command line interpreter + * Since Windows does not provide bash, use cmd or simila r default command line interpreter */ val process = if (DataFilesUtil.isWindows()) { ProcessBuilder() @@ -34,10 +34,11 @@ class CommandLineRunner { .start() } val reader = BufferedReader(InputStreamReader(process.inputStream)) + val separator = System.lineSeparator() var line: String? while (reader.readLine().also { line = it } != null) { - executionMsg += line + executionMsg += "$line$separator" } process.waitFor() 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 9a9d340f0..5eadc9158 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt @@ -64,6 +64,7 @@ class TestProcessor( * @param projectBuildPath The build path of the project. * @param generatedTestPackage The package where the generated test class is located. * @return An empty string if the test execution is successful, otherwise an error message. + * TODO refactor method signature to return exit code as well, see `TestCompiler` for example */ fun createXmlFromJacoco( className: String, @@ -110,7 +111,8 @@ class TestProcessor( ), ) - log.info("Test execution message: ${testExecutionResult.executionMessage}") + log.info("Exit code: '${testExecutionResult.exitCode}'; Execution message: '${testExecutionResult.executionMessage}'") + // Prepare the command for generating the Jacoco report val command = mutableListOf( @@ -287,7 +289,8 @@ class TestProcessor( } children("sourcefile") { isCorrectSourceFile = - this.attributes.getValue("name") == projectContext.fileUrlAsString!!.split(File.separatorChar).last() + this.attributes.getValue("name") == projectContext.fileUrlAsString!!.split(File.separatorChar) + .last() children("line") { if (isCorrectSourceFile && this.attributes.getValue("mi") == "0") { setOfLines.add(this.attributes.getValue("nr").toInt()) diff --git a/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt b/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt index 9db5b3fc3..d5a26810f 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/evosuite/generation/EvoSuiteProcessManager.kt @@ -85,6 +85,7 @@ class EvoSuiteProcessManager( val regex = Regex("version \"(.*?)\"") val versionCommandResult = CommandLineRunner.run(arrayListOf(evoSuiteSettingsState.javaPath, "-version")) + log.info("Version command result: exit code '${versionCommandResult.exitCode}', message '${versionCommandResult.executionMessage}'") val version = regex.find(versionCommandResult.executionMessage) ?.groupValues ?.get(1) From 826574938c7b02407d93458b6cfbd2347a71fa2c Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Wed, 16 Oct 2024 01:32:36 +0200 Subject: [PATCH 5/6] apply ktlint --- .../research/testspark/core/exception/Exceptions.kt | 3 +-- .../jetbrains/research/testspark/core/test/TestCompiler.kt | 2 +- .../research/testspark/core/test/java/JavaTestCompiler.kt | 2 +- .../testspark/core/test/kotlin/KotlinTestCompiler.kt | 6 +++--- .../org/jetbrains/research/testspark/tools/TestProcessor.kt | 1 - 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt index 22859d000..c1b1f337f 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/exception/Exceptions.kt @@ -30,10 +30,9 @@ class JavaCompilerNotFoundException(message: String) : TestSparkException(messag */ class JavaSDKMissingException(message: String) : TestSparkException(message) - /** * Represents an exception thrown when a class file could not be found in the same path after the code compilation. * * @param message A descriptive message explaining the error */ -class ClassFileNotFoundException(message: String) : TestSparkException(message) \ No newline at end of file +class ClassFileNotFoundException(message: String) : TestSparkException(message) 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 d1a0a1832..3d85f15c1 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 @@ -34,7 +34,7 @@ abstract class TestCompiler(libPaths: List, junitLibPaths: List) generatedTestCasesPaths: List, buildPath: String, testCases: MutableList, - workingDir: String + workingDir: String, ): TestCasesCompilationResult { var allTestCasesCompilable = true val compilableTestCases: MutableSet = mutableSetOf() 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 ce5b1f932..4486eac52 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 @@ -2,8 +2,8 @@ package org.jetbrains.research.testspark.core.test.java import io.github.oshai.kotlinlogging.KotlinLogging import org.jetbrains.research.testspark.core.exception.ClassFileNotFoundException -import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.exception.JavaCompilerNotFoundException +import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.test.TestCompiler import org.jetbrains.research.testspark.core.utils.CommandLineRunner import org.jetbrains.research.testspark.core.utils.DataFilesUtil 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 abcd86a9e..e1487ebba 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 @@ -2,8 +2,8 @@ package org.jetbrains.research.testspark.core.test.kotlin import io.github.oshai.kotlinlogging.KotlinLogging import org.jetbrains.research.testspark.core.exception.ClassFileNotFoundException -import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.exception.KotlinCompilerNotFoundException +import org.jetbrains.research.testspark.core.test.ExecutionResult import org.jetbrains.research.testspark.core.test.TestCompiler import org.jetbrains.research.testspark.core.utils.CommandLineRunner import org.jetbrains.research.testspark.core.utils.DataFilesUtil @@ -12,7 +12,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 @@ -69,7 +69,7 @@ class KotlinTestCompiler( * Forcing kotlinc to save a classfile in the same place, as '.kt' file */ "-d", - workingDir + workingDir, ), ) logger.info { "Exit code: '${executionResult.exitCode}'; Execution message: '${executionResult.executionMessage}'" } 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 5eadc9158..0ed9346ca 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt @@ -113,7 +113,6 @@ class TestProcessor( log.info("Exit code: '${testExecutionResult.exitCode}'; Execution message: '${testExecutionResult.executionMessage}'") - // Prepare the command for generating the Jacoco report val command = mutableListOf( javaRunner.absolutePath, From c46416eb9e96d6ff19bda8ca96ba25713b6f4183 Mon Sep 17 00:00:00 2001 From: Iurii Zaitsev Date: Wed, 16 Oct 2024 01:37:41 +0200 Subject: [PATCH 6/6] style fix --- .../org/jetbrains/research/testspark/tools/TestProcessor.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 0ed9346ca..8b5de49db 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/TestProcessor.kt @@ -288,8 +288,7 @@ class TestProcessor( } children("sourcefile") { isCorrectSourceFile = - this.attributes.getValue("name") == projectContext.fileUrlAsString!!.split(File.separatorChar) - .last() + this.attributes.getValue("name") == projectContext.fileUrlAsString!!.split(File.separatorChar).last() children("line") { if (isCorrectSourceFile && this.attributes.getValue("mi") == "0") { setOfLines.add(this.attributes.getValue("nr").toInt())