Skip to content

Commit

Permalink
fix services
Browse files Browse the repository at this point in the history
  • Loading branch information
arksap2002 committed Aug 16, 2023
1 parent 3020a6f commit 2076671
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@ package org.jetbrains.research.testspark.services

import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.process.ScriptRunnerUtil
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.io.FileUtilRt
import java.io.File
import java.util.*
import kotlin.collections.ArrayList
import kotlin.io.path.Path
import kotlin.io.path.createDirectories

class CommandLineService(private val project: Project) {
private val sep = File.separatorChar

val id = UUID.randomUUID().toString()
val testResultDirectory = "${FileUtilRt.getTempDirectory()}${sep}testSparkResults$sep"
val testResultName = "test_gen_result_$id"

val resultPath = "$testResultDirectory$testResultName"

private val javaHomeDirectory = ProjectRootManager.getInstance(project).projectSdk!!.homeDirectory!!

private val log = Logger.getInstance(this::class.java)

/**
* Executes a command line process and returns the output as a string.
*
Expand Down Expand Up @@ -44,4 +62,58 @@ class CommandLineService(private val project: Project) {
val pluginsPath = System.getProperty("idea.plugins.path")
return "$pluginsPath${sep}TestSpark${sep}lib${sep}$libraryName"
}

/**
* Compiles the code at the specified path using the provided project build path.
*
* @param path The path of the code file to compile.
* @param projectBuildPath The project build path to use during compilation.
* @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.
*/
fun compileCode(path: String, projectBuildPath: String): Pair<Boolean, String> {
// find the proper javac
val javaCompile = File(javaHomeDirectory.path).walk().filter { it.name.equals("javac") && it.isFile }.first()
// compile file
val errorMsg = project.service<CommandLineService>().runCommandLine(
arrayListOf(
javaCompile.absolutePath,
"-cp",
project.service<CommandLineService>().getPath(projectBuildPath),
path,
),
)

// create .class file path
val classFilePath = path.replace(".java", ".class")

// check is .class file exists
return Pair(File(classFilePath).exists(), errorMsg)
}

/**
* Save the generated tests to a specified directory.
*
* @param packageString The package string where the generated tests will be saved.
* @param code The generated test code.
* @param resultPath The result path where the generated tests will be saved.
* @param testFileName The name of the test file.
* @return The path where the generated tests are saved.
*/
fun saveGeneratedTests(packageString: String, code: String, resultPath: String, testFileName: String): String {
// Generate the final path for the generated tests
var generatedTestPath = "$resultPath${File.separatorChar}"
packageString.split(".").forEach { directory ->
if (directory.isNotBlank()) generatedTestPath += "$directory${File.separatorChar}"
}
Path(generatedTestPath).createDirectories()

// Save the generated test suite to the file
val testFile = File("$generatedTestPath${File.separatorChar}$testFileName")
testFile.createNewFile()
log.info("Save test in file " + testFile.absolutePath)
testFile.writeText(code)

return generatedTestPath
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,15 @@ class TestCaseDisplayService(private val project: Project) {
) {
document.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
// val generatedTestPath: String = project.service<CommandLineService>().saveGeneratedTests(
// packageString,
// document.text,
// resultPath,
// testFileName,
// )
//
// project.service<CommandLineService>().compileCode(generatedTestPath, projectBuildPath).first

textFieldEditor.editor!!.markupModel.removeAllHighlighters()

resetButton.isEnabled = document.text != testCaseCode
Expand Down Expand Up @@ -898,13 +907,7 @@ class TestCaseDisplayService(private val project: Project) {
private fun getBorder(testCaseName: String): Border {
val size = 3
return if (project.service<TestsExecutionResultService>().isTestCasePassing(testCaseName)) {
MatteBorder(
size,
size,
size,
size,
JBColor.GREEN,
)
MatteBorder(size, size, size, size, JBColor.GREEN)
} else {
MatteBorder(size, size, size, size, JBColor.RED)
}
Expand Down
15 changes: 5 additions & 10 deletions src/main/kotlin/org/jetbrains/research/testspark/tools/Pipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.io.FileUtilRt
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiFile
import org.jetbrains.research.testspark.TestSparkBundle
import org.jetbrains.research.testspark.Util
import org.jetbrains.research.testspark.actions.getSurroundingClass
import org.jetbrains.research.testspark.data.FragmentToTestDada
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.services.CommandLineService
import org.jetbrains.research.testspark.tools.template.generation.ProcessManager
import java.io.File
import java.util.UUID

/**
* Pipeline class represents a pipeline for running the test generation process.
Expand All @@ -34,19 +32,16 @@ class Pipeline(
) {
private val project = e.project!!

private val sep = File.separatorChar

private val projectClassPath: String = ProjectRootManager.getInstance(project).contentRoots.first().path

private val id = UUID.randomUUID().toString()
private val testResultDirectory = "${FileUtilRt.getTempDirectory()}${sep}testSparkResults$sep"
private val testResultName = "test_gen_result_$id"
private val testResultDirectory = project.service<CommandLineService>().testResultDirectory
private val testResultName = project.service<CommandLineService>().testResultName
private val resultPath = project.service<CommandLineService>().resultPath

private var baseDir = "$testResultDirectory$testResultName-validation"

private val serializeResultPath = "\"$testResultDirectory$testResultName\""

private val resultPath = "$testResultDirectory$testResultName"

private val vFile = e.dataContext.getData(CommonDataKeys.VIRTUAL_FILE)!!
private val fileUrl = vFile.presentableUrl
private val modificationStamp = vFile.modificationStamp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.jetbrains.research.testspark.data.CodeType
import org.jetbrains.research.testspark.data.FragmentToTestDada
import org.jetbrains.research.testspark.data.Report
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.services.CompilableService
import org.jetbrains.research.testspark.services.CommandLineService
import org.jetbrains.research.testspark.services.ErrorService
import org.jetbrains.research.testspark.services.SettingsProjectService
import org.jetbrains.research.testspark.tools.getBuildPath
Expand Down Expand Up @@ -169,7 +169,13 @@ class LLMProcessManager(
} else {
generatedTestCasesPaths = saveGeneratedTestCases(generatedTestSuite, resultPath)
}
val generatedTestPath: String = saveGeneratedTests(generatedTestSuite, resultPath)

val generatedTestPath: String = project.service<CommandLineService>().saveGeneratedTests(
generatedTestSuite.packageString,
generatedTestSuite.toStringWithoutExpectedException(),
resultPath,
testFileName
)

// Correct files creating checking
var isFilesExists = true
Expand Down Expand Up @@ -197,7 +203,7 @@ class LLMProcessManager(
// compile the test file
indicator.text = TestSparkBundle.message("compilationTestsChecking")
coverageCollector.compileTestCases()
val compilationResult = project.service<CompilableService>().compileCode(File("$generatedTestPath${File.separatorChar}$testFileName").absolutePath, buildPath)
val compilationResult = project.service<CommandLineService>().compileCode(File("$generatedTestPath${File.separatorChar}$testFileName").absolutePath, buildPath)

if (!compilationResult.first && !isLastIteration(requestsCount)) {
log.info("Incorrect result: \n$generatedTestSuite")
Expand Down Expand Up @@ -257,29 +263,5 @@ class LLMProcessManager(
return testCaseFilePaths
}

/**
* Saves the generated test suite to a file at the specified result path.
*
* @param generatedTestSuite the test suite generated by LLM
* @param resultPath the path where the generated tests should be saved
* @return the path where the tests are saved
*/
private fun saveGeneratedTests(generatedTestSuite: TestSuiteGeneratedByLLM, resultPath: String): String {
// Generate the final path for the generated tests
var generatedTestPath = "$resultPath${File.separatorChar}"
generatedTestSuite.packageString.split(".").forEach { directory ->
if (directory.isNotBlank()) generatedTestPath += "$directory${File.separatorChar}"
}
Path(generatedTestPath).createDirectories()

// Save the generated test suite to the file
val testFile = File("$generatedTestPath${File.separatorChar}$testFileName")
testFile.createNewFile()
log.info("Save test in file " + testFile.absolutePath)
testFile.writeText(generatedTestSuite.toStringWithoutExpectedException())

return generatedTestPath
}

private fun isLastIteration(requestsCount: Int) = requestsCount > maxRequests
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.jetbrains.research.testspark.data.Report
import org.jetbrains.research.testspark.data.TestCase
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.services.CommandLineService
import org.jetbrains.research.testspark.services.CompilableService
import org.jetbrains.research.testspark.services.TestsExecutionResultService
import org.jetbrains.research.testspark.tools.llm.error.LLMErrorManager
import org.jetbrains.research.testspark.tools.llm.test.TestCaseGeneratedByLLM
Expand Down Expand Up @@ -82,7 +81,7 @@ class TestCoverageCollector(
*/
fun compileTestCases() {
for (index in generatedTestPaths.indices) {
if (project.service<CompilableService>().compileCode(generatedTestPaths[index], projectBuildPath).first) {
if (project.service<CommandLineService>().compileCode(generatedTestPaths[index], projectBuildPath).first) {
project.service<Workspace>().testGenerationData.compilableTestCases.add(testCases[index])
}
}
Expand Down Expand Up @@ -151,7 +150,7 @@ class TestCoverageCollector(

log.info("Runs command: ${command.joinToString(" ")}")

val reportGenerationError = project.service<CommandLineService>().runCommandLine(command as ArrayList<String>)
project.service<CommandLineService>().runCommandLine(command as ArrayList<String>)

// check if XML report is produced
if (!File("$dataFileName.xml").exists()) {
Expand Down
2 changes: 0 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@
serviceImplementation="org.jetbrains.research.testspark.services.ErrorService"/>
<projectService
serviceImplementation="org.jetbrains.research.testspark.services.TestsExecutionResultService"/>
<projectService
serviceImplementation="org.jetbrains.research.testspark.services.CompilableService"/>
<projectService
serviceImplementation="org.jetbrains.research.testspark.services.CommandLineService"/>

Expand Down

0 comments on commit 2076671

Please sign in to comment.