Skip to content

Commit

Permalink
Merge pull request #63 from JetBrains-Research/create-run-command-lin…
Browse files Browse the repository at this point in the history
…e-service

Run command line service
  • Loading branch information
arksap2002 authored Sep 11, 2023
2 parents fb5f462 + 98d2fe3 commit 0c5b27f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jetbrains.research.testspark.services

import com.intellij.openapi.project.Project
import java.io.BufferedReader
import java.io.InputStreamReader

class RunCommandLineService(private val project: Project) {

/**
* Executes a command line process and returns the output as a string.
*
* @param cmd The command line arguments as an ArrayList of strings.
* @return The output of the command line process as a string.
*/
fun runCommandLine(cmd: ArrayList<String>): String {
var errorMessage = ""

val process = ProcessBuilder()
.command("bash", "-c", cmd.joinToString(" "))
.redirectErrorStream(true)
.start()

val reader = BufferedReader(InputStreamReader(process.inputStream))
var line: String?

while (reader.readLine().also { line = it } != null) {
errorMessage += line
}

process.waitFor()

return errorMessage
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import com.intellij.openapi.util.io.FileUtilRt
import org.jetbrains.research.testspark.data.TestCase
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.tools.getBuildPath
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.util.UUID
import kotlin.collections.ArrayList
import kotlin.io.path.Path
Expand All @@ -33,32 +31,6 @@ class TestCoverageCollectorService(private val project: Project) {

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

/**
* Executes a command line process and returns the output as a string.
*
* @param cmd The command line arguments as an ArrayList of strings.
* @return The output of the command line process as a string.
*/
fun runCommandLine(cmd: ArrayList<String>): String {
var errorMessage = ""

val process = ProcessBuilder()
.command("bash", "-c", cmd.joinToString(" "))
.redirectErrorStream(true)
.start()

val reader = BufferedReader(InputStreamReader(process.inputStream))
var line: String?

while (reader.readLine().also { line = it } != null) {
errorMessage += line
}

process.waitFor()

return errorMessage
}

/**
* Generates the path for the command by concatenating the necessary paths.
*
Expand Down Expand Up @@ -99,7 +71,7 @@ class TestCoverageCollectorService(private val project: Project) {
// find the proper javac
val javaCompile = File(javaHomeDirectory.path).walk().filter { it.name.equals("javac") && it.isFile }.first()
// compile file
val errorMsg = runCommandLine(
val errorMsg = project.service<RunCommandLineService>().runCommandLine(
arrayListOf(
javaCompile.absolutePath,
"-cp",
Expand Down Expand Up @@ -172,7 +144,7 @@ class TestCoverageCollectorService(private val project: Project) {
name += "$className#$testCaseName"

// run the test method with jacoco agent
val testExecutionError = runCommandLine(
val testExecutionError = project.service<RunCommandLineService>().runCommandLine(
arrayListOf(
javaRunner.absolutePath,
"-javaagent:$jacocoAgentDir=destfile=$dataFileName.exec,append=false,includes=${project.service<Workspace>().classFQN}",
Expand Down Expand Up @@ -218,7 +190,7 @@ class TestCoverageCollectorService(private val project: Project) {

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

runCommandLine(command as ArrayList<String>)
project.service<RunCommandLineService>().runCommandLine(command as ArrayList<String>)

return testExecutionError
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ 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.RunCommandLineService
import org.jetbrains.research.testspark.services.SettingsApplicationService
import org.jetbrains.research.testspark.services.SettingsProjectService
import org.jetbrains.research.testspark.services.TestCoverageCollectorService
import org.jetbrains.research.testspark.tools.evosuite.SettingsArguments
import org.jetbrains.research.testspark.tools.evosuite.error.EvoSuiteErrorManager
import org.jetbrains.research.testspark.tools.getBuildPath
Expand Down Expand Up @@ -74,7 +74,7 @@ class EvoSuiteProcessManager(
if (processStopped(project, indicator)) return

val regex = Regex("version \"(.*?)\"")
val version = regex.find(project.service<TestCoverageCollectorService>().runCommandLine(arrayListOf(settingsApplicationState!!.javaPath, "-version")))
val version = regex.find(project.service<RunCommandLineService>().runCommandLine(arrayListOf(settingsApplicationState!!.javaPath, "-version")))
?.groupValues
?.get(1)
?.split(".")
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
serviceImplementation="org.jetbrains.research.testspark.services.TestCoverageCollectorService"/>
<projectService
serviceImplementation="org.jetbrains.research.testspark.services.JavaClassBuilderService"/>
<projectService
serviceImplementation="org.jetbrains.research.testspark.services.RunCommandLineService"/>

<toolWindow id="TestSpark" secondary="true" anchor="right"
factoryClass="org.jetbrains.research.testspark.toolwindow.TestSparkToolWindowFactory"/>
Expand Down

0 comments on commit 0c5b27f

Please sign in to comment.