Skip to content

Commit

Permalink
add jacoco run after modifying
Browse files Browse the repository at this point in the history
  • Loading branch information
arksap2002 committed Aug 17, 2023
1 parent f78b841 commit ab1f63e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ 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.module.Module
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.util.io.FileUtilRt
import org.jetbrains.research.testspark.editor.Workspace
import java.io.File
import java.util.*
import kotlin.collections.ArrayList
Expand Down Expand Up @@ -135,8 +135,6 @@ class CommandLineService(private val project: Project) {
fun createXmlFromJacoco(
className: String,
dataFileName: String,
cutModule: Module,
classFQN: String,
testCaseName: String,
projectBuildPath: String,
generatedTestPackage: String,
Expand All @@ -146,13 +144,13 @@ class CommandLineService(private val project: Project) {
// JaCoCo libs
val jacocoAgentDir = project.service<CommandLineService>().getLibrary("jacocoagent.jar")
val jacocoCLIDir = project.service<CommandLineService>().getLibrary("jacococli.jar")
val sourceRoots = ModuleRootManager.getInstance(cutModule).getSourceRoots(false)
val sourceRoots = ModuleRootManager.getInstance(project.service<Workspace>().cutModule!!).getSourceRoots(false)

// run the test method with jacoco agent
val testExecutionError = runCommandLine(
arrayListOf(
javaRunner.absolutePath,
"-javaagent:$jacocoAgentDir=destfile=$dataFileName.exec,append=false,includes=$classFQN",
"-javaagent:$jacocoAgentDir=destfile=$dataFileName.exec,append=false,includes=${project.service<Workspace>().classFQN}",
"-cp",
"${project.service<CommandLineService>().getPath(projectBuildPath)}${project.service<CommandLineService>().getLibrary("JUnitRunner.jar")}:$resultPath",
"org.jetbrains.research.SingleJUnitTestRunner",
Expand All @@ -178,7 +176,7 @@ class CommandLineService(private val project: Project) {

// for classpath containing cut
command.add("--classfiles")
command.add(CompilerModuleExtension.getInstance(cutModule)?.compilerOutputPath!!.path)
command.add(CompilerModuleExtension.getInstance(project.service<Workspace>().cutModule!!)?.compilerOutputPath!!.path)

// for each source folder
sourceRoots.forEach { root ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class TestCaseDisplayService(private val project: Project) {
val resetButton = createResetButton(document, textFieldEditor, testCodeFormatted, testCase.testName)

// Enable reset button when editor is changed
addListenerToTestDocument(document, resetButton, textFieldEditor, checkbox, testCase.testCode)
addListenerToTestDocument(document, resetButton, textFieldEditor, checkbox, testCase.testCode, testCase.testName)

// Set border
textFieldEditor.border = getBorder(testCase.testName)
Expand Down Expand Up @@ -266,8 +266,8 @@ class TestCaseDisplayService(private val project: Project) {
settingsProjectState.colorRed,
settingsProjectState.colorGreen,
settingsProjectState.colorBlue,
30
)
30,
),
)
if (editor.background.equals(highlightColor)) return
defaultEditorColor = editor.background
Expand Down Expand Up @@ -374,14 +374,14 @@ class TestCaseDisplayService(private val project: Project) {
// Apply filter with folders and java files with main class
descriptor.withFileFilter { file ->
file.isDirectory || (
file.extension?.lowercase(Locale.getDefault()) == "java" && (
PsiManager.getInstance(project).findFile(file!!) as PsiJavaFile
).classes.stream().map { it.name }
.toArray()
.contains(
(PsiManager.getInstance(project).findFile(file) as PsiJavaFile).name.removeSuffix(".java"),
)
file.extension?.lowercase(Locale.getDefault()) == "java" && (
PsiManager.getInstance(project).findFile(file!!) as PsiJavaFile
).classes.stream().map { it.name }
.toArray()
.contains(
(PsiManager.getInstance(project).findFile(file) as PsiJavaFile).name.removeSuffix(".java"),
)
)
}

val fileChooser = FileChooser.chooseFiles(
Expand Down Expand Up @@ -770,7 +770,7 @@ class TestCaseDisplayService(private val project: Project) {
document: Document,
textFieldEditor: EditorTextField,
testCode: String,
testCaseName: String
testCaseName: String,
): JButton {
val resetButton = JButton(TestSparkLabelsBundle.defaultValue("resetButton"))
resetButton.isEnabled = false
Expand Down Expand Up @@ -800,75 +800,69 @@ class TestCaseDisplayService(private val project: Project) {
textFieldEditor: EditorTextField,
checkbox: JCheckBox,
testCaseCode: String,
testCaseName: String,
) {
document.addDocumentListener(object : DocumentListener {
override fun documentChanged(event: DocumentEvent) {
SwingUtilities.invokeLater {
val fileName: String = ('A'..'Z').toList().random().toString() +
(List(20) { ('a'..'z').toList().random() }.joinToString("")) +
".java"

val code = project.service<JavaClassBuilderService>().generateCode(
fileName.split(".")[0],
document.text,
project.service<Workspace>().testGenerationData.importsCode,
project.service<Workspace>().testGenerationData.packageLine,
project.service<Workspace>().testGenerationData.runWith,
project.service<Workspace>().testGenerationData.otherInfo,
)

var buildPath: String = ProjectRootManager.getInstance(project).contentRoots.first().path
if (project.service<SettingsProjectService>().state.buildPath.isEmpty()) {
// User did not set own path
buildPath = getBuildPath(project)
}

val generatedTestPath: String = project.service<CommandLineService>().saveGeneratedTests(
project.service<Workspace>().testGenerationData.packageLine,
code,
project.service<CommandLineService>().resultPath,
fileName,
)
val fileName: String = ('A'..'Z').toList().random().toString() +
(List(20) { ('a'..'z').toList().random() }.joinToString("")) +
".java"

val code = project.service<JavaClassBuilderService>().generateCode(
fileName.split(".")[0],
document.text,
project.service<Workspace>().testGenerationData.importsCode,
project.service<Workspace>().testGenerationData.packageLine,
project.service<Workspace>().testGenerationData.runWith,
project.service<Workspace>().testGenerationData.otherInfo,
)

println(project.service<CommandLineService>().compileCode(generatedTestPath, buildPath).first)
var buildPath: String = ProjectRootManager.getInstance(project).contentRoots.first().path
if (project.service<SettingsProjectService>().state.buildPath.isEmpty()) {
// User did not set own path
buildPath = getBuildPath(project)
}

val javaHomeDirectory = ProjectRootManager.getInstance(project).projectSdk!!.homeDirectory!!
val generatedTestPath: String = project.service<CommandLineService>().saveGeneratedTests(
project.service<Workspace>().testGenerationData.packageLine,
code,
project.service<Workspace>().resultPath!!,
fileName,
)

val javaRunner =
File(javaHomeDirectory.path).walk().filter { it.name.equals("java") && it.isFile }.first()
project.service<CommandLineService>().compileCode(generatedTestPath, buildPath).first

// val testExecutionError = project.service<CommandLineService>().createXmlFromJacoco(
// generatedTestFile.name.split('.')[0],
// dataFileName,
// cutModule,
// classFQN,
// testCase.name,
// buildPath,
// generatedTestPackage,
// )
val dataFileName = "${project.service<Workspace>().resultPath!!}/jacoco-${(List(20) { ('a'..'z').toList().random() }.joinToString(""))}"

textFieldEditor.editor!!.markupModel.removeAllHighlighters()
project.service<CommandLineService>().createXmlFromJacoco(
fileName.split(".")[0],
dataFileName,
testCaseName,
buildPath,
project.service<Workspace>().testGenerationData.packageLine,
)

// textFieldEditor.border = getBorder(testCase.name)
textFieldEditor.editor!!.markupModel.removeAllHighlighters()

resetButton.isEnabled = document.text != testCaseCode
textFieldEditor.border = getBorder(testCaseName)

val modifiedLineIndexes = getModifiedLines(
testCaseCode.split("\n"),
document.text.split("\n"),
)
resetButton.isEnabled = document.text != testCaseCode

for (index in modifiedLineIndexes) {
textFieldEditor.editor!!.markupModel.addLineHighlighter(
DiffColors.DIFF_MODIFIED,
index,
HighlighterLayer.FIRST,
)
}
val modifiedLineIndexes = getModifiedLines(
testCaseCode.split("\n"),
document.text.split("\n"),
)

// select checkbox
checkbox.isSelected = true
for (index in modifiedLineIndexes) {
textFieldEditor.editor!!.markupModel.addLineHighlighter(
DiffColors.DIFF_MODIFIED,
index,
HighlighterLayer.FIRST,
)
}

// select checkbox
checkbox.isSelected = true
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class LLMProcessManager(
project.service<Workspace>().key = getKey(
project,
"${project.service<Workspace>().classFQN}#${codeType.objectDescription}",
)
)
}

// update build path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,11 @@ class TestCoverageCollector(
// Execute each test method separately
for (testCase in testCases) {
// name of .exec and .xml files
val dataFileName = "${generatedTestFile.parentFile.absolutePath}/jacoco-${(List(20) { ('a'..'z').toList().random() }.joinToString(""))}"
val dataFileName = "${project.service<Workspace>().resultPath!!}/jacoco-${(List(20) { ('a'..'z').toList().random() }.joinToString(""))}"

val testExecutionError = project.service<CommandLineService>().createXmlFromJacoco(
generatedTestFile.name.split('.')[0],
dataFileName,
project.service<Workspace>().cutModule!!,
project.service<Workspace>().classFQN!!,
testCase.name,
projectBuildPath,
generatedTestPackage,
Expand Down

0 comments on commit ab1f63e

Please sign in to comment.