Skip to content

Commit

Permalink
Merge pull request #92 from JetBrains-Research/vartiukhov/enhancement…
Browse files Browse the repository at this point in the history
…/integrate-windows-support

Integrate seamless support for Windows
  • Loading branch information
arksap2002 authored Dec 12, 2023
2 parents 85f2397 + c73151d commit 223bc5e
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/main/kotlin/org/jetbrains/research/testspark/Util.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 Util {
companion object {
Expand All @@ -22,5 +23,19 @@ class Util {
dir.mkdirs()
}
}

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 @@ -462,14 +462,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 @@ -10,6 +10,7 @@ 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.Util
import org.jetbrains.research.testspark.data.TestCase
import org.jetbrains.research.testspark.editor.Workspace
import org.jetbrains.research.testspark.tools.getBuildPath
Expand Down Expand Up @@ -47,7 +48,9 @@ class TestCoverageCollectorService(private val project: Project) {
val hamcrestPath = "\"$pluginsPath${sep}TestSpark${sep}lib${sep}hamcrest-core-1.3.jar\""
val byteBuddy = "\"$pluginsPath${sep}TestSpark${sep}lib${sep}byte-buddy-1.14.6.jar\""
val byteBuddyAgent = "\"$pluginsPath${sep}TestSpark${sep}lib${sep}byte-buddy-agent-1.14.6.jar\""
return "$junitPath:$hamcrestPath:$mockitoPath:$byteBuddy:$byteBuddyAgent:$buildPath"

val sep = Util.classpathSeparator
return "$junitPath${sep}$hamcrestPath${sep}$mockitoPath${sep}$byteBuddy${sep}$byteBuddyAgent${sep}$buildPath"
}

/**
Expand All @@ -71,7 +74,13 @@ class TestCoverageCollectorService(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 (Util.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 @@ -135,7 +144,12 @@ class TestCoverageCollectorService(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 (Util.isWindows()) it.name.equals("java.exe") else it.name.equals("java")
isJavaName && it.isFile
}
.first()
// JaCoCo libs
val jacocoAgentDir = project.service<TestCoverageCollectorService>().getLibrary("jacocoagent.jar")
val jacocoCLIDir = project.service<TestCoverageCollectorService>().getLibrary("jacococli.jar")
Expand All @@ -151,7 +165,7 @@ class TestCoverageCollectorService(private val project: Project) {
javaRunner.absolutePath,
"-javaagent:$jacocoAgentDir=destfile=$dataFileName.exec,append=false,includes=${project.service<Workspace>().classFQN}",
"-cp",
"${project.service<TestCoverageCollectorService>().getPath(projectBuildPath)}${project.service<TestCoverageCollectorService>().getLibrary("JUnitRunner.jar")}:$resultPath",
"${project.service<TestCoverageCollectorService>().getPath(projectBuildPath)}${project.service<TestCoverageCollectorService>().getLibrary("JUnitRunner.jar")}${Util.classpathSeparator}$resultPath",
"org.jetbrains.research.SingleJUnitTestRunner",
name,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.CompilerModuleExtension
import com.intellij.openapi.roots.ModuleRootManager
import org.jetbrains.research.testspark.TestSparkBundle
import org.jetbrains.research.testspark.Util
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 @@ -135,8 +136,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(Util.classpathSeparator.toString()) }
// Include extra libraries in classpath
val librariesPaths = ModuleRootManager.getInstance(module).orderEntries().librariesOnly().pathsList.pathList
for (lib in librariesPaths) {
Expand All @@ -157,7 +158,7 @@ fun getBuildPath(project: Project): String {
continue
}

buildPath += lib.plus(":")
buildPath += lib.plus(Util.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 223bc5e

Please sign in to comment.