Skip to content

Commit

Permalink
fix: Set a Java path to compile kotlin tests for Windows machines (Is…
Browse files Browse the repository at this point in the history
…sue 410).
  • Loading branch information
SergeyDatskiv committed Nov 4, 2024
1 parent 5ecaafb commit d8dbda8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class KotlinTestCompiler(
libPaths: List<String>,
junitLibPaths: List<String>,
kotlinSDKHomeDirectory: String,
javaHomeDirectoryPath: String,
) : TestCompiler(libPaths, junitLibPaths) {
private val logger = KotlinLogging.logger { this::class.java }
private val kotlinc: String
// Direct declaration in constructor requires changes to TestCompiler, so we declare it here.
private val javaHomeDirectoryPath = javaHomeDirectoryPath

// init block to find the kotlinc compiler
init {
Expand Down Expand Up @@ -55,9 +58,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,41 @@ 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 -> {
val javaSDKHomePath = findJavaSDKHomePath(javaHomeDirectory, project) // Kotlinc relies on java to compile kotlin files.
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 d8dbda8

Please sign in to comment.