Skip to content

Commit

Permalink
Merge pull request #91 from JetBrains-Research/running-process-improve
Browse files Browse the repository at this point in the history
Running process improving
  • Loading branch information
pderakhshanfar authored Dec 12, 2023
2 parents 223bc5e + 3e00bac commit 6a1da4b
Show file tree
Hide file tree
Showing 37 changed files with 385 additions and 2,514 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.intellij.openapi.util.io.FileUtilRt
import java.io.File
import java.util.Locale

class Util {
class DataFilesUtil {
companion object {
fun makeTmp() {
val sep = File.separatorChar
Expand All @@ -24,6 +24,26 @@ class Util {
}
}

fun cleanFolder(path: String) {
val folder = File(path)

if (!folder.exists()) return

if (folder.isDirectory) {
val files = folder.listFiles()
if (files != null) {
for (file in files) {
if (file.isDirectory) {
cleanFolder(file.absolutePath)
} else {
file.delete()
}
}
}
}
folder.delete()
}

val classpathSeparator: Char
get() {
var sep = ':'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jetbrains.research.testspark.data

import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.tools.llm.test.TestCaseGeneratedByLLM

/**
Expand All @@ -18,13 +17,6 @@ class TestGenerationData {
var runWith: String = ""
var otherInfo: String = ""

// Maps a workspace file to the test generation jobs that were triggered on it.
// Currently, the file key is represented by its presentableUrl
var testGenerationResults: HashMap<String, ArrayList<Workspace.TestJob>> = HashMap()

// Maps a test generation job id to its corresponding test job information
var pendingTestResults: HashMap<String, Workspace.TestJobInfo> = HashMap()

// changing parameters with a large prompt
var polyDepthReducing: Int = 0
var inputParamsDepthReducing: Int = 0
Expand All @@ -43,8 +35,6 @@ class TestGenerationData {
packageLine = ""
runWith = ""
otherInfo = ""
testGenerationResults.clear()
pendingTestResults.clear()
polyDepthReducing = 0
inputParamsDepthReducing = 0
compilableTestCases.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import com.intellij.util.ui.JBUI
import org.jetbrains.research.testspark.TestSparkBundle
import org.jetbrains.research.testspark.TestSparkLabelsBundle
import org.jetbrains.research.testspark.data.TestCase
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.services.COVERAGE_SELECTION_TOGGLE_TOPIC
import org.jetbrains.research.testspark.services.ErrorService
import org.jetbrains.research.testspark.services.JavaClassBuilderService
import org.jetbrains.research.testspark.services.LLMChatService
import org.jetbrains.research.testspark.services.ReportLockingService
import org.jetbrains.research.testspark.services.TestCaseDisplayService
import org.jetbrains.research.testspark.services.TestCoverageCollectorService
import org.jetbrains.research.testspark.services.TestStorageProcessingService
import org.jetbrains.research.testspark.services.TestsExecutionResultService
import org.jetbrains.research.testspark.tools.llm.test.TestSuiteGeneratedByLLM
import org.jetbrains.research.testspark.tools.processStopped
Expand All @@ -42,6 +41,7 @@ import javax.swing.FocusManager
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JLabel
import javax.swing.JOptionPane
import javax.swing.JPanel
import javax.swing.JTextField
import javax.swing.ScrollPaneConstants
Expand All @@ -52,7 +52,7 @@ import javax.swing.border.MatteBorder
class TestCasePanelFactory(
private val project: Project,
private val testCase: TestCase,
private val editor: Editor,
editor: Editor,
private val checkbox: JCheckBox,
) {
private val panel = JPanel()
Expand All @@ -69,8 +69,12 @@ class TestCasePanelFactory(
private var allRequestsNumber = 1
private var currentRequestNumber = 1

private val testCaseCodeToListOfCoveredLines: HashMap<String, Set<Int>> = hashMapOf()

private val dimensionSize = 7

private var isRemoved = false

// Add an editor to modify the test source code
private val languageTextField = LanguageTextField(
Language.findLanguageByID("JAVA"),
Expand Down Expand Up @@ -227,7 +231,7 @@ class TestCasePanelFactory(
val buttonsPanel = JPanel()
buttonsPanel.layout = BoxLayout(buttonsPanel, BoxLayout.X_AXIS)
buttonsPanel.add(Box.createRigidArea(Dimension(checkbox.preferredSize.width, checkbox.preferredSize.height)))
runTestButton.isEnabled = false
runTestButton.isEnabled = true
buttonsPanel.add(runTestButton)
loadingLabel.isVisible = false
buttonsPanel.add(loadingLabel)
Expand All @@ -242,7 +246,17 @@ class TestCasePanelFactory(
panel.add(requestPanel)
panel.add(buttonsPanel)

runTestButton.addActionListener { runTest() }
runTestButton.addActionListener {
val choice = JOptionPane.showConfirmDialog(
null,
TestSparkBundle.message("runCautionMessage"),
TestSparkBundle.message("confirmationTitle"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE,
)

if (choice == JOptionPane.OK_OPTION) runTest()
}
resetButton.addActionListener { reset() }
resetToLastRunButton.addActionListener { resetToLastRun() }
removeButton.addActionListener { remove() }
Expand Down Expand Up @@ -315,15 +329,15 @@ class TestCasePanelFactory(
* Updates the user interface based on the provided code.
*/
private fun updateUI() {
updateTestCase()
updateTestCaseInformation()

val lastRunCode = lastRunCodes[currentRequestNumber - 1]
languageTextField.editor!!.markupModel.removeAllHighlighters()

resetButton.isEnabled = testCase.testCode != initialCodes[currentRequestNumber - 1]
resetToLastRunButton.isEnabled = testCase.testCode != lastRunCode

val error = getError(testCase.id, testCase.testCode)
val error = getError()
if (error.isNullOrBlank()) {
project.service<TestsExecutionResultService>().addCurrentPassedTest(testCase.id)
} else {
Expand Down Expand Up @@ -351,6 +365,15 @@ class TestCasePanelFactory(

// select checkbox
checkbox.isSelected = true

if (testCaseCodeToListOfCoveredLines.containsKey(testCase.testCode)) {
testCase.coveredLines = testCaseCodeToListOfCoveredLines[testCase.testCode]!!
} else {
testCase.coveredLines = setOf()
}

project.service<ReportLockingService>().updateTestCase(testCase)
project.service<TestCaseDisplayService>().updateUI()
}

/**
Expand Down Expand Up @@ -401,17 +424,6 @@ class TestCasePanelFactory(
.getTestMethodNameFromClassWithTestCase(testCase.testName, code)
testCase.testCode = code

// run new code
project.service<Workspace>().updateTestCase(
project.service<TestCoverageCollectorService>()
.updateDataWithTestCase(
"${project.service<JavaClassBuilderService>().getClassFromTestCaseCode(testCase.testCode)}.java",
testCase.id,
testCase.testName,
code,
),
)

// update numbers
allRequestsNumber++
currentRequestNumber = allRequestsNumber
Expand All @@ -437,30 +449,29 @@ class TestCasePanelFactory(
* label in the test case upper panel, removes all highlighters from the language text field,
* and updates the UI.
*/
private fun runTest() {
fun runTest() {
if (isRemoved) return
if (!runTestButton.isEnabled) return

loadingLabel.isVisible = true
runTestButton.isEnabled = false
resetToLastRunButton.isEnabled = false
languageTextField.editor!!.markupModel.removeAllHighlighters()

SwingUtilities.invokeLater {
project.service<Workspace>().updateTestCase(
project.service<TestCoverageCollectorService>()
.updateDataWithTestCase(
"${project.service<JavaClassBuilderService>().getClassFromTestCaseCode(testCase.testCode)}.java",
testCase.id,
testCase.testName,
testCase.testCode,
),
)
updateBorder()
updateErrorLabel()
val newTestCase = project.service<TestStorageProcessingService>()
.processNewTestCase(
"${project.service<JavaClassBuilderService>().getClassFromTestCaseCode(testCase.testCode)}.java",
testCase.id,
testCase.testName,
testCase.testCode,
)
testCase.coveredLines = newTestCase.coveredLines

lastRunCodes[currentRequestNumber - 1] = testCase.testCode
testCaseCodeToListOfCoveredLines[testCase.testCode] = testCase.coveredLines

lastRunCodes[currentRequestNumber - 1] = testCase.testCode
loadingLabel.isVisible = false

project.service<TestCaseDisplayService>().updateUI()
updateUI()
}
}

Expand All @@ -476,25 +487,10 @@ class TestCasePanelFactory(
private fun reset() {
WriteCommandAction.runWriteCommandAction(project) {
languageTextField.document.setText(initialCodes[currentRequestNumber - 1])
updateBorder()
project.service<Workspace>().updateTestCase(testCase)
resetButton.isEnabled = false
if (getError(testCase.id, testCase.testCode)!!.isBlank()) {
project.service<TestsExecutionResultService>().addPassedTest(testCase.id, testCase.testCode)
} else {
project.service<TestsExecutionResultService>()
.addFailedTest(testCase.id, testCase.testCode, errorLabel.toolTipText)
}
resetToLastRunButton.isEnabled = false
runTestButton.isEnabled = false
updateErrorLabel()
languageTextField.editor!!.markupModel.removeAllHighlighters()

currentCodes[currentRequestNumber - 1] = testCase.testCode
lastRunCodes[currentRequestNumber - 1] = testCase.testCode

updateTestCase()
project.service<TestCaseDisplayService>().updateUI()
updateUI()
}
}

Expand All @@ -503,18 +499,10 @@ class TestCasePanelFactory(
*/
private fun resetToLastRun() {
WriteCommandAction.runWriteCommandAction(project) {
val code = lastRunCodes[currentRequestNumber - 1]
languageTextField.document.setText(code)
resetToLastRunButton.isEnabled = false
runTestButton.isEnabled = false
updateBorder()
updateErrorLabel()
languageTextField.editor!!.markupModel.removeAllHighlighters()

languageTextField.document.setText(lastRunCodes[currentRequestNumber - 1])
currentCodes[currentRequestNumber - 1] = testCase.testCode

updateTestCase()
project.service<TestCaseDisplayService>().updateUI()
updateUI()
}
}

Expand All @@ -525,21 +513,28 @@ class TestCasePanelFactory(
* and updating the UI.
*/
private fun remove() {
// Remove the highlighting of the test
project.messageBus.syncPublisher(COVERAGE_SELECTION_TOGGLE_TOPIC)
.testGenerationResult(testCase.id, false, editor)

// Remove the test case from the cache
project.service<TestCaseDisplayService>().removeTestCase(testCase.testName)

runTestButton.isEnabled = false
isRemoved = true

project.service<ReportLockingService>().removeTestCase(testCase)
project.service<TestCaseDisplayService>().updateUI()
}

/**
* Determines if the "Run" button is enabled.
*
* @return true if the "Run" button is enabled, false otherwise.
*/
fun isRunEnabled() = runTestButton.isEnabled

/**
* Updates the border of the languageTextField based on the provided test name and text.
*/
private fun updateBorder() {
languageTextField.border = getBorder(testCase.id, testCase.testCode)
languageTextField.border = getBorder()
}

/**
Expand All @@ -549,18 +544,17 @@ class TestCasePanelFactory(
* @param testCaseCode the code of the test case
* @return the error message for the test case
*/
private fun getError(testCaseId: Int, testCaseCode: String) =
project.service<TestsExecutionResultService>().getError(testCaseId, testCaseCode)
fun getError() = project.service<TestsExecutionResultService>().getError(testCase.id, testCase.testCode)

/**
* Returns the border for a given test case.
*
* @param testCaseId the id of the test case
* @return the border for the test case
*/
private fun getBorder(testCaseId: Int, testCaseCode: String): Border {
private fun getBorder(): Border {
val size = 3
return when (getError(testCaseId, testCaseCode)) {
return when (getError()) {
null -> JBUI.Borders.empty()
"" -> MatteBorder(size, size, size, size, JBColor.GREEN)
else -> MatteBorder(size, size, size, size, JBColor.RED)
Expand All @@ -574,7 +568,6 @@ class TestCasePanelFactory(
*/
private fun createRunTestButton(): JButton {
val runTestButton = JButton(TestSparkLabelsBundle.defaultValue("run"), TestSparkIcons.runTest)
runTestButton.isEnabled = false
runTestButton.isOpaque = false
runTestButton.isContentAreaFilled = false
runTestButton.isBorderPainted = true
Expand All @@ -588,7 +581,6 @@ class TestCasePanelFactory(
*/
private fun switchToAnotherCode() {
languageTextField.document.setText(currentCodes[currentRequestNumber - 1])
updateTestCase()
updateUI()
}

Expand Down Expand Up @@ -642,7 +634,7 @@ class TestCasePanelFactory(
/**
* Updates the current test case with the specified test name and test code.
*/
private fun updateTestCase() {
private fun updateTestCaseInformation() {
testCase.testName =
project.service<JavaClassBuilderService>()
.getTestMethodNameFromClassWithTestCase(testCase.testName, languageTextField.document.text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ object TestSparkIcons {
@JvmField
val documentation = IconLoader.getIcon("/icons/documentation.svg", javaClass)

@JvmField
val settings = IconLoader.getIcon("/icons/settings.svg", javaClass)

@JvmField
val pluginIcon = IconLoader.getIcon("/META-INF/pluginIcon.svg", javaClass)
}
Loading

0 comments on commit 6a1da4b

Please sign in to comment.