Skip to content

Commit

Permalink
Java path for kotlin tests compilation on Windows machines (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyDatskiv authored Nov 28, 2024
1 parent 3224f28 commit d2df269
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class KotlinTestCompiler(
libPaths: List<String>,
junitLibPaths: List<String>,
kotlinSDKHomeDirectory: String,
private val javaHomeDirectoryPath: String,
) : TestCompiler(libPaths, junitLibPaths) {
private val logger = KotlinLogging.logger { this::class.java }
private val kotlinc: String
Expand All @@ -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")
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
junitLibraryPaths: List<String>,
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<String>,
junitLibraryPaths: List<String>,
): KotlinTestCompiler {
// kotlinc should be under `[kotlinSDKHomeDirectory]/bin/kotlinc`
val kotlinSDKHomeDirectory = KotlinPluginLayout.kotlinc.absolutePath
return KotlinTestCompiler(libraryPaths, junitLibraryPaths, kotlinSDKHomeDirectory)
return javaSDKHomePath
}
}

0 comments on commit d2df269

Please sign in to comment.