Skip to content

Commit ed6c179

Browse files
Explicitly setting the child process working dir if we run the plugin
Refactor services a bit
1 parent 8b12b88 commit ed6c179

File tree

13 files changed

+125
-81
lines changed

13 files changed

+125
-81
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/JdkPathDefaultProvider.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

utbot-framework-api/src/main/kotlin/org/utbot/framework/JdkPathProvider.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

utbot-framework-api/src/main/kotlin/org/utbot/framework/JdkPathService.kt

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.utbot.framework.plugin.services
2+
3+
import java.nio.file.Path
4+
import java.nio.file.Paths
5+
6+
/**
7+
* Singleton to enable abstract access to path to JDK.
8+
9+
* and in the test runs.
10+
* This is necessary because the engine can be run from the various starting points, like IDEA plugin, CLI, etc.
11+
*/
12+
data class JdkInfo(
13+
val path: Path,
14+
@Suppress("unused")
15+
val version: String
16+
)
17+
18+
object JdkInfoService : PluginService<JdkInfo> {
19+
var jdkInfoProvider: JdkInfoProvider = JdkInfoDefaultProvider()
20+
21+
override fun provide(): JdkInfo = jdkInfoProvider.info
22+
}
23+
24+
interface JdkInfoProvider {
25+
val info: JdkInfo
26+
}
27+
28+
open class JdkInfoDefaultProvider : JdkInfoProvider {
29+
override val info: JdkInfo =
30+
JdkInfo(Paths.get(System.getProperty("java.home")), System.getProperty("java.version"))
31+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.utbot.framework.plugin.services
2+
3+
interface PluginService<T> {
4+
fun provide(): T
5+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.utbot.framework.plugin.services
2+
3+
import java.nio.file.Path
4+
import java.nio.file.Paths
5+
6+
/**
7+
* Singleton to enable abstract access to the working directory.
8+
*
9+
* Used in [org.utbot.instrumentation.process.ChildProcessRunner].
10+
* The purpose is to use the same working directory in [org.utbot.instrumentation.ConcreteExecutor]
11+
* and in the test runs.
12+
*/
13+
object WorkingDirService : PluginService<Path> {
14+
var workingDirProvider: WorkingDirProvider = WorkingDirDefaultProvider()
15+
16+
override fun provide(): Path = workingDirProvider.workingDir
17+
}
18+
19+
abstract class WorkingDirProvider {
20+
abstract val workingDir: Path
21+
}
22+
23+
open class WorkingDirDefaultProvider : WorkingDirProvider() {
24+
override val workingDir: Path
25+
get() = Paths.get(System.getProperty("user.dir"))
26+
}

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/process/ChildProcessRunner.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import org.utbot.common.packageName
99
import org.utbot.common.pid
1010
import org.utbot.common.scanForResourcesContaining
1111
import org.utbot.common.utBotTempDirectory
12-
import org.utbot.framework.JdkPathService
12+
import org.utbot.framework.plugin.services.JdkInfoService
1313
import org.utbot.framework.UtSettings
14+
import org.utbot.framework.plugin.services.WorkingDirService
1415
import org.utbot.instrumentation.Settings
1516
import org.utbot.instrumentation.agent.DynamicClassTransformer
1617
import java.io.File
17-
import java.nio.file.Paths
1818

1919
private val logger = KotlinLogging.logger {}
2020
private var processSeqN = 0
@@ -27,8 +27,10 @@ class ChildProcessRunner {
2727
emptyList()
2828
}
2929

30-
listOf(Paths.get(JdkPathService.jdkPath.toString(),"bin", "java").toString()) +
31-
debugCmd + listOf("-javaagent:$jarFile", "-ea", "-jar", "$jarFile")
30+
listOf(
31+
JdkInfoService.provide().path.resolve("bin${File.separatorChar}java").toString(),
32+
"-javaagent:$jarFile", "-ea", "-jar", "$jarFile"
33+
) + debugCmd
3234
}
3335

3436
var errorLogFile: File = NULL_FILE
@@ -42,7 +44,12 @@ class ChildProcessRunner {
4244
errorLogFile = File(UT_BOT_TEMP_DIR, "${hashCode()}-${processSeqN}.log")
4345
}
4446

45-
val processBuilder = ProcessBuilder(cmds).redirectError(errorLogFile)
47+
val directory = WorkingDirService.provide().toFile()
48+
49+
val processBuilder = ProcessBuilder(cmds)
50+
.redirectError(errorLogFile)
51+
.directory(directory)
52+
4653
return processBuilder.start().also {
4754
logger.debug { "Process started with PID=${it.pid}" }
4855

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/UtTestsDialogProcessor.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.intellij.util.concurrency.AppExecutorUtil
2626
import mu.KotlinLogging
2727
import org.jetbrains.kotlin.idea.util.module
2828
import org.utbot.engine.util.mockListeners.ForceMockListener
29-
import org.utbot.framework.JdkPathService
29+
import org.utbot.framework.plugin.services.JdkInfoService
3030
import org.utbot.framework.UtSettings
3131
import org.utbot.framework.plugin.api.TestCaseGenerator
3232
import org.utbot.framework.plugin.api.UtMethod
@@ -39,7 +39,7 @@ import org.utbot.intellij.plugin.models.GenerateTestsModel
3939
import org.utbot.intellij.plugin.ui.GenerateTestsDialogWindow
4040
import org.utbot.intellij.plugin.ui.utils.showErrorDialogLater
4141
import org.utbot.intellij.plugin.util.IntelliJApiHelper
42-
import org.utbot.intellij.plugin.util.PluginJdkPathProvider
42+
import org.utbot.intellij.plugin.util.PluginJdkInfoProvider
4343
import org.utbot.intellij.plugin.util.signature
4444
import org.utbot.summary.summarize
4545
import java.io.File
@@ -50,9 +50,11 @@ import java.util.concurrent.TimeUnit
5050
import org.utbot.common.filterWhen
5151
import org.utbot.engine.util.mockListeners.ForceStaticMockListener
5252
import org.utbot.framework.plugin.api.testFlow
53+
import org.utbot.framework.plugin.services.WorkingDirService
5354
import org.utbot.intellij.plugin.settings.Settings
5455
import org.utbot.intellij.plugin.ui.utils.isGradle
5556
import org.utbot.intellij.plugin.ui.utils.suitableTestSourceRoots
57+
import org.utbot.intellij.plugin.util.PluginWorkingDirProvider
5658
import org.utbot.intellij.plugin.util.isAbstract
5759
import org.utbot.intellij.plugin.ui.utils.testModules
5860
import kotlin.reflect.KClass
@@ -80,7 +82,9 @@ object UtTestsDialogProcessor {
8082
val srcModule = findSrcModule(srcClasses)
8183
val testModules = srcModule.testModules(project)
8284

83-
JdkPathService.jdkPathProvider = PluginJdkPathProvider(project)
85+
JdkInfoService.jdkInfoProvider = PluginJdkInfoProvider(project)
86+
// we want to start the child process in the same directory as the test runner
87+
WorkingDirService.workingDirProvider = PluginWorkingDirProvider(project)
8488

8589
if (project.isGradle() && testModules.flatMap { it.suitableTestSourceRoots() }.isEmpty()) {
8690
val errorMessage = """

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/util/PluginJdkPathProvider.kt renamed to utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/util/PluginJdkInfoProvider.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.utbot.intellij.plugin.util
22

33
import org.utbot.common.PathUtil.toPath
4-
import org.utbot.framework.JdkPathDefaultProvider
54
import com.intellij.openapi.project.Project
65
import com.intellij.openapi.projectRoots.ProjectJdkTable
76
import com.intellij.openapi.projectRoots.Sdk
87
import com.intellij.openapi.roots.ProjectRootManager
9-
import java.nio.file.Path
8+
import org.utbot.framework.plugin.services.JdkInfo
9+
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider
1010

11-
class PluginJdkPathProvider(
11+
class PluginJdkInfoProvider(
1212
private val project: Project
13-
) : JdkPathDefaultProvider() {
13+
) : JdkInfoDefaultProvider() {
1414

1515
private val sdk: Sdk?
1616
get() {
@@ -28,9 +28,9 @@ class PluginJdkPathProvider(
2828
return ProjectRootManager.getInstance(project).projectSdk
2929
}
3030

31-
override val jdkPath: Path
32-
get() = sdk?.let { it.homePath?.toPath() } ?: super.jdkPath // Return default JDK in case of failure
33-
34-
override val jdkVersion: String
35-
get() = sdk?.versionString ?: super.jdkVersion // Return default JDK in case of failure
31+
override val info: JdkInfo
32+
get() = JdkInfo(
33+
sdk?.homePath?.toPath() ?: super.info.path, // Return default JDK in case of failure
34+
sdk?.versionString ?: super.info.version // Return default JDK in case of failure
35+
)
3636
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.utbot.intellij.plugin.util
2+
3+
import com.intellij.openapi.project.Project
4+
import org.utbot.common.PathUtil.toPath
5+
import org.utbot.framework.plugin.services.WorkingDirDefaultProvider
6+
import java.nio.file.Path
7+
8+
class PluginWorkingDirProvider(
9+
project: Project,
10+
) : WorkingDirDefaultProvider() {
11+
12+
/**
13+
* We believe that in most cases the test runner working dir is the project root, otherwise we need to parse test
14+
* configuration, but it's not easy.
15+
*/
16+
override val workingDir: Path =
17+
project.basePath?.toPath()
18+
?: super.workingDir
19+
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/ContestEstimator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import java.util.zip.ZipInputStream
3434
import kotlin.concurrent.thread
3535
import kotlin.math.min
3636
import kotlin.system.exitProcess
37-
import org.utbot.framework.JdkPathService
37+
import org.utbot.framework.plugin.services.JdkInfoService
3838
import org.utbot.predictors.StateRewardPredictorFactoryImpl
3939
import org.utbot.framework.PathSelectorType
4040
import org.utbot.framework.UtSettings
@@ -300,7 +300,7 @@ fun main(args: Array<String>) {
300300
tools = listOf(Tool.UtBot)
301301
}
302302

303-
JdkPathService.jdkPathProvider = ContestEstimatorJdkPathProvider(javaHome)
303+
JdkInfoService.jdkInfoProvider = ContestEstimatorJdkInfoProvider(javaHome)
304304
runEstimator(estimatorArgs, methodFilter, projectFilter, processedClassesThreshold, tools)
305305
}
306306

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.utbot.contest
2+
3+
import org.utbot.common.PathUtil.toPath
4+
import org.utbot.framework.plugin.services.JdkInfo
5+
import org.utbot.framework.plugin.services.JdkInfoDefaultProvider
6+
7+
/**
8+
* This class is used to provide a path to jdk and its version in [ContestEstimator]
9+
* into the child process for concrete execution.
10+
*/
11+
class ContestEstimatorJdkInfoProvider(private val path: String) : JdkInfoDefaultProvider() {
12+
override val info: JdkInfo
13+
get() = JdkInfo(path.toPath(), super.info.version) // TODO: retrieve the correct version
14+
}

utbot-junit-contest/src/main/kotlin/org/utbot/contest/ContestEstimatorJdkPathProvider.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)