diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt index f7e38ea81..536555f5d 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/test/kotlin/KotlinTestCompiler.kt @@ -13,6 +13,7 @@ class KotlinTestCompiler( libPaths: List, junitLibPaths: List, kotlinSDKHomeDirectory: String, + private val javaHomeDirectoryPath: String, ) : TestCompiler(libPaths, junitLibPaths) { private val logger = KotlinLogging.logger { this::class.java } private val kotlinc: String @@ -32,7 +33,6 @@ class KotlinTestCompiler( * as failed compilation because `kotlinc` will complain about * `java` command missing in PATH. * - * TODO(vartiukhov): find a way to locate `java` on Windows */ val isCompilerName = if (DataFilesUtil.isWindows()) { it.name.equals("kotlinc") @@ -55,9 +55,14 @@ class KotlinTestCompiler( logger.info { "[KotlinTestCompiler] Compiling ${path.substringAfterLast('/')}" } val classPaths = "\"${getClassPaths(projectBuildPath)}\"" + + // We need to ensure JAVA is in the path variable + // See: https://github.com/JetBrains-Research/TestSpark/issues/410 + val setJavaPathOnWindows = "set PATH=%PATH%;$javaHomeDirectoryPath\\bin\\&&" + // Compile file // See: https://github.com/JetBrains-Research/TestSpark/issues/402 - val kotlinc = if (DataFilesUtil.isWindows()) "\"$kotlinc\"" else "'$kotlinc'" + val kotlinc = if (DataFilesUtil.isWindows()) "$setJavaPathOnWindows\"$kotlinc\"" else "'$kotlinc'" val executionResult = CommandLineRunner.run( arrayListOf( diff --git a/src/main/kotlin/org/jetbrains/research/testspark/tools/factories/TestCompilerFactory.kt b/src/main/kotlin/org/jetbrains/research/testspark/tools/factories/TestCompilerFactory.kt index aa9a12aef..025761b24 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/factories/TestCompilerFactory.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/factories/TestCompilerFactory.kt @@ -24,33 +24,43 @@ object TestCompilerFactory { // TODO add the warning window that for Java we always need the javaHomeDirectoryPath return when (language) { - SupportedLanguage.Java -> createJavaCompiler(project, libraryPaths, junitLibraryPaths, javaHomeDirectory) - SupportedLanguage.Kotlin -> createKotlinCompiler(libraryPaths, junitLibraryPaths) + SupportedLanguage.Java -> { + val javaSDKHomePath = findJavaSDKHomePath(javaHomeDirectory, project) + JavaTestCompiler(libraryPaths, junitLibraryPaths, javaSDKHomePath) + } + SupportedLanguage.Kotlin -> { + // Kotlinc relies on java to compile kotlin files. + val javaSDKHomePath = findJavaSDKHomePath(javaHomeDirectory, project) + // kotlinc should be under `[kotlinSDKHomeDirectory]/bin/kotlinc` + val kotlinSDKHomeDirectory = KotlinPluginLayout.kotlinc.absolutePath + KotlinTestCompiler(libraryPaths, junitLibraryPaths, kotlinSDKHomeDirectory, javaSDKHomePath) + } } } - private fun createJavaCompiler( + /** + * Finds the home path of the Java SDK. + * + * @param javaHomeDirectory The directory where Java SDK is installed. If null, the project's configured SDK path is used. + * @param project The project for which the Java SDK home path is being determined. + * @return The home path of the Java SDK. + * @throws JavaSDKMissingException If no Java SDK is configured for the project. + */ + private fun findJavaSDKHomePath( + javaHomeDirectory: String?, project: Project, - libraryPaths: List, - junitLibraryPaths: List, - javaHomeDirectory: String? = null, - ): JavaTestCompiler { - val javaSDKHomePath = javaHomeDirectory - ?: ProjectRootManager.getInstance(project).projectSdk?.homeDirectory?.path + ): String { + val javaSDKHomePath = + javaHomeDirectory + ?: ProjectRootManager + .getInstance(project) + .projectSdk + ?.homeDirectory + ?.path if (javaSDKHomePath == null) { throw JavaSDKMissingException(LLMMessagesBundle.get("javaSdkNotConfigured")) } - - return JavaTestCompiler(libraryPaths, junitLibraryPaths, javaSDKHomePath) - } - - private fun createKotlinCompiler( - libraryPaths: List, - junitLibraryPaths: List, - ): KotlinTestCompiler { - // kotlinc should be under `[kotlinSDKHomeDirectory]/bin/kotlinc` - val kotlinSDKHomeDirectory = KotlinPluginLayout.kotlinc.absolutePath - return KotlinTestCompiler(libraryPaths, junitLibraryPaths, kotlinSDKHomeDirectory) + return javaSDKHomePath } }