Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java path for kotlin tests compilation on Windows machines #412

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
SergeyDatskiv marked this conversation as resolved.
Show resolved Hide resolved
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
SergeyDatskiv marked this conversation as resolved.
Show resolved Hide resolved

// 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.
SergeyDatskiv marked this conversation as resolved.
Show resolved Hide resolved
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`
SergeyDatskiv marked this conversation as resolved.
Show resolved Hide resolved
val kotlinSDKHomeDirectory = KotlinPluginLayout.kotlinc.absolutePath
return KotlinTestCompiler(libraryPaths, junitLibraryPaths, kotlinSDKHomeDirectory)
return javaSDKHomePath
}
}
Loading