Skip to content

Commit

Permalink
merge development into running-process-improve
Browse files Browse the repository at this point in the history
  • Loading branch information
arksap2002 committed Dec 12, 2023
2 parents e07ce88 + 223bc5e commit 0a4591f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/org/jetbrains/research/testspark/DataFilesUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.research.testspark

import com.intellij.openapi.util.io.FileUtilRt
import java.io.File
import java.util.Locale

class DataFilesUtil {
companion object {
Expand Down Expand Up @@ -42,5 +43,19 @@ class DataFilesUtil {
}
folder.delete()
}

val classpathSeparator: Char
get() {
var sep = ':'
if (isWindows()) {
sep = ';'
}
return sep
}

fun isWindows(): Boolean {
val os = System.getProperty("os.name").lowercase(Locale.getDefault())
return (os.indexOf("win") >= 0)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.research.testspark.services

import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import org.jetbrains.research.testspark.Util
import java.io.BufferedReader
import java.io.InputStreamReader

Expand All @@ -17,10 +18,20 @@ class RunCommandLineService(private val project: Project) {
fun runCommandLine(cmd: ArrayList<String>): String {
var errorMessage = ""

val process = ProcessBuilder()
.command("bash", "-c", cmd.joinToString(" "))
.redirectErrorStream(true)
.start()
/**
* Since Windows does not provide bash, use cmd or similar default command line interpreter
*/
val process = if (Util.isWindows()) {
ProcessBuilder()
.command("cmd", "/c", cmd.joinToString(" "))
.redirectErrorStream(true)
.start()
} else {
ProcessBuilder()
.command("bash", "-c", cmd.joinToString(" "))
.redirectErrorStream(true)
.start()
}

val reader = BufferedReader(InputStreamReader(process.inputStream))
var line: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,20 @@ class TestCaseDisplayService(private val project: Project) {

// insert tests to a code
testCaseComponents.reversed().forEach {
PsiDocumentManager.getInstance(project).getDocument(outputFile)!!.insertString(
selectedClass.rBrace!!.textRange.startOffset,
// Fix Windows line separators
project.service<JavaClassBuilderService>().getTestMethodCodeFromClassWithTestCase(
val testMethodCode = project
.service<JavaClassBuilderService>()
.getTestMethodCodeFromClassWithTestCase(
project.service<JavaClassBuilderService>().formatJavaCode(
it.replace("\r\n", "\n").replace("verifyException(", "// verifyException("),
it.replace("\r\n", "\n")
.replace("verifyException(", "// verifyException("),
),
),
)
// Fix Windows line separators
.replace("\r\n", "\n")

PsiDocumentManager.getInstance(project).getDocument(outputFile)!!.insertString(
selectedClass.rBrace!!.textRange.startOffset,
testMethodCode,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class TestStorageProcessingService(private val project: Project) {
val hamcrestPath = getLibrary("hamcrest-core-1.3.jar")
val byteBuddy = getLibrary("byte-buddy-1.14.6.jar")
val byteBuddyAgent = getLibrary("byte-buddy-agent-1.14.6.jar")
return "$junitPath:$hamcrestPath:$mockitoPath:$byteBuddy:$byteBuddyAgent:$buildPath"
val sep = DataFilesUtil.classpathSeparator
return "$junitPath${sep}$hamcrestPath${sep}$mockitoPath${sep}$byteBuddy${sep}$byteBuddyAgent${sep}$buildPath"
}

/**
Expand All @@ -72,7 +73,13 @@ class TestStorageProcessingService(private val project: Project) {
*/
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()
val javaCompile = File(javaHomeDirectory.path).walk()
.filter {
val isCompilerName = if (DataFilesUtil.isWindows()) it.name.equals("javac.exe") else it.name.equals("javac")
isCompilerName && it.isFile
}
.first()

// compile file
val errorMsg = project.service<RunCommandLineService>().runCommandLine(
arrayListOf(
Expand Down Expand Up @@ -155,7 +162,12 @@ class TestStorageProcessingService(private val project: Project) {
generatedTestPackage: String,
): String {
// find the proper javac
val javaRunner = File(javaHomeDirectory.path).walk().filter { it.name.equals("java") && it.isFile }.first()
val javaRunner = File(javaHomeDirectory.path).walk()
.filter {
val isJavaName = if (DataFilesUtil.isWindows()) it.name.equals("java.exe") else it.name.equals("java")
isJavaName && it.isFile
}
.first()
// JaCoCo libs
val jacocoAgentDir = getLibrary("jacocoagent.jar")
val jacocoCLIDir = getLibrary("jacococli.jar")
Expand All @@ -171,7 +183,7 @@ class TestStorageProcessingService(private val project: Project) {
javaRunner.absolutePath,
"-javaagent:$jacocoAgentDir=destfile=$dataFileName.exec,append=false,includes=${project.service<Workspace>().classFQN}",
"-cp",
"${getPath(projectBuildPath)}${getLibrary("JUnitRunner.jar")}:\"$resultPath\"",
"${getPath(projectBuildPath)}${getLibrary("JUnitRunner.jar")}${DataFilesUtil.classpathSeparator}$resultPath",
"org.jetbrains.research.SingleJUnitTestRunner",
name,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import org.jetbrains.research.testspark.DataFilesUtil
import org.jetbrains.research.testspark.data.Report
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.services.ErrorService
Expand Down Expand Up @@ -104,8 +105,8 @@ fun getBuildPath(project: Project): String {

for (module in ModuleManager.getInstance(project).modules) {
val compilerOutputPath = CompilerModuleExtension.getInstance(module)?.compilerOutputPath
compilerOutputPath?.let { buildPath += compilerOutputPath.path.plus(":") }

compilerOutputPath?.let { buildPath += compilerOutputPath.path.plus(DataFilesUtil.classpathSeparator.toString()) }
// Include extra libraries in classpath
val librariesPaths = ModuleRootManager.getInstance(module).orderEntries().librariesOnly().pathsList.pathList
for (lib in librariesPaths) {
Expand All @@ -126,7 +127,7 @@ fun getBuildPath(project: Project): String {
continue
}

buildPath += lib.plus(":")
buildPath += lib.plus(DataFilesUtil.classpathSeparator.toString())
}
}
return buildPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.jetbrains.research.testspark.helpers.generateMethodDescriptor
import org.jetbrains.research.testspark.services.SettingsProjectService
import org.jetbrains.research.testspark.tools.evosuite.generation.EvoSuiteProcessManager
import org.jetbrains.research.testspark.tools.template.Tool
import java.io.File

/**
* Represents the EvoSuite class, which is a tool used to generate tests for Java code.
Expand All @@ -32,7 +33,7 @@ class EvoSuite(override val name: String = "EvoSuite") : Tool {
val project: Project = e.project!!
val projectClassPath: String = ProjectRootManager.getInstance(project).contentRoots.first().path
val settingsProjectState = project.service<SettingsProjectService>().state
val buildPath = "$projectClassPath/${settingsProjectState.buildPath}"
val buildPath = "$projectClassPath${File.separatorChar}${settingsProjectState.buildPath}"
return EvoSuiteProcessManager(project, buildPath)
}

Expand Down

0 comments on commit 0a4591f

Please sign in to comment.