From 7379cd5b1d4401cb9a3a15b830daf46d0a7c4793 Mon Sep 17 00:00:00 2001 From: "Rachel M. Carmena" Date: Thu, 3 Oct 2019 17:38:02 +0200 Subject: [PATCH 1/5] Add pluginClasspaths to the configuration --- .../compiletesting/KotlinCompilation.kt | 18 ++++++++++++++++-- .../compiletesting/KotlinCompilationTests.kt | 12 ++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt index f59325f1..6664f465 100644 --- a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt +++ b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt @@ -65,6 +65,11 @@ class KotlinCompilation { */ var classpaths: List = emptyList() + /** + * Paths to plugins to be made available in the compilation + */ + var pluginClasspaths: List = emptyList() + /** Source files to be compiled */ var sources: List = emptyList() @@ -326,6 +331,8 @@ class KotlinCompilation { it.destination = classesDir.absolutePath it.classpath = commonClasspaths().joinToString(separator = File.pathSeparator) + it.pluginClasspaths = pluginClasspaths.map(File::getAbsolutePath).toTypedArray() + if(jdkHome != null) { it.jdkHome = jdkHome!!.absolutePath } @@ -403,6 +410,13 @@ class KotlinCompilation { /** Performs the 1st and 2nd compilation step to generate stubs and run annotation processors */ private fun stubsAndApt(sourceFiles: List): ExitCode { + pluginClasspaths.forEach { filepath -> + if (!filepath.exists()) { + error("Plugin $filepath not found") + return ExitCode.INTERNAL_ERROR + } + } + if(annotationProcessors.isEmpty()) { log("No services were given. Not running kapt steps.") return ExitCode.OK @@ -475,8 +489,8 @@ class KotlinCompilation { val k2JvmArgs = commonK2JVMArgs().also { it.freeArgs = sourcePaths - it.pluginClasspaths = (it.pluginClasspaths?.toList() ?: emptyList() + getResourcesPath()) - .distinct().toTypedArray() + if (it.pluginClasspaths?.isEmpty()!!) + it.pluginClasspaths = arrayOf(getResourcesPath()) } val compilerMessageCollector = PrintingMessageCollector( diff --git a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt index e3a18458..9f364202 100644 --- a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt +++ b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt @@ -6,6 +6,7 @@ import org.assertj.core.api.Assertions.fail import org.junit.Rule import org.junit.Test import org.junit.rules.TemporaryFolder +import java.io.File import javax.annotation.processing.AbstractProcessor import javax.annotation.processing.RoundEnvironment import javax.lang.model.element.TypeElement @@ -623,6 +624,17 @@ class KotlinCompilationTests { assertClassLoadable(result, "${KotlinTestProcessor.GENERATED_PACKAGE}.${KotlinTestProcessor.GENERATED_JAVA_CLASS_NAME}") } + @Test + fun `checks configuration with pluginClasspaths`() { + val result = defaultCompilerConfig().apply { + sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource")) + pluginClasspaths = listOf(File("./plugin.jar")) + }.compile() + + assertThat(result.exitCode).isEqualTo(ExitCode.INTERNAL_ERROR) + assertThat(result.messages.isEmpty()) + } + private fun defaultCompilerConfig(): KotlinCompilation { return KotlinCompilation().apply { workingDir = temporaryFolder.root From 0def979ee6f6085b4af3366989ee4b8d7e324a2f Mon Sep 17 00:00:00 2001 From: "Rachel M. Carmena" Date: Thu, 3 Oct 2019 20:30:53 +0200 Subject: [PATCH 2/5] Requested changes after @tschuchortdev review --- .../kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt | 3 +-- .../com/tschuchort/compiletesting/KotlinCompilationTests.kt | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt index 6664f465..79222797 100644 --- a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt +++ b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt @@ -489,8 +489,7 @@ class KotlinCompilation { val k2JvmArgs = commonK2JVMArgs().also { it.freeArgs = sourcePaths - if (it.pluginClasspaths?.isEmpty()!!) - it.pluginClasspaths = arrayOf(getResourcesPath()) + it.pluginClasspaths = it.pluginClasspaths?.plus(arrayOf(getResourcesPath())) } val compilerMessageCollector = PrintingMessageCollector( diff --git a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt index 9f364202..abfad7ac 100644 --- a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt +++ b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt @@ -625,10 +625,10 @@ class KotlinCompilationTests { } @Test - fun `checks configuration with pluginClasspaths`() { + fun `returns an internal error when adding a non existing plugin for compilation`() { val result = defaultCompilerConfig().apply { sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource")) - pluginClasspaths = listOf(File("./plugin.jar")) + pluginClasspaths = listOf(File("./non-existing-plugin.jar")) }.compile() assertThat(result.exitCode).isEqualTo(ExitCode.INTERNAL_ERROR) From c85aa6532a258ebdbdb8ccae1aace2c15898843a Mon Sep 17 00:00:00 2001 From: "Rachel M. Carmena" Date: Fri, 4 Oct 2019 11:18:50 +0200 Subject: [PATCH 3/5] Requested changes after @tschuchortdev review --- .../kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt index 79222797..40ff1bff 100644 --- a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt +++ b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt @@ -489,7 +489,7 @@ class KotlinCompilation { val k2JvmArgs = commonK2JVMArgs().also { it.freeArgs = sourcePaths - it.pluginClasspaths = it.pluginClasspaths?.plus(arrayOf(getResourcesPath())) + it.pluginClasspaths = (it.pluginClasspaths ?: emptyArray()) + arrayOf(getResourcesPath()) } val compilerMessageCollector = PrintingMessageCollector( From 6707daa114c27477a73e7aba6df823ec442af291 Mon Sep 17 00:00:00 2001 From: "Rachel M. Carmena" Date: Sun, 6 Oct 2019 16:59:44 +0200 Subject: [PATCH 4/5] Requested changes after @tschuchortdev review --- build.gradle | 4 +++- .../compiletesting/KotlinCompilationTests.kt | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 8b77f506..f6a8f8d8 100644 --- a/build.gradle +++ b/build.gradle @@ -41,6 +41,8 @@ idea { } dependencies { + testRuntime "org.jetbrains.kotlin:kotlin-scripting-compiler:1.3.50" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" testImplementation group: 'junit', name: 'junit', version: '4.12' @@ -67,4 +69,4 @@ compileKotlin { compileTestKotlin { kotlinOptions.jvmTarget = "1.8" kotlinOptions.freeCompilerArgs += ["-Xskip-runtime-version-check"] -} \ No newline at end of file +} diff --git a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt index abfad7ac..9ee91f49 100644 --- a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt +++ b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt @@ -1,6 +1,7 @@ package com.tschuchort.compiletesting import com.tschuchort.compiletesting.KotlinCompilation.ExitCode +import io.github.classgraph.ClassGraph import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.fail import org.junit.Rule @@ -624,6 +625,17 @@ class KotlinCompilationTests { assertClassLoadable(result, "${KotlinTestProcessor.GENERATED_PACKAGE}.${KotlinTestProcessor.GENERATED_JAVA_CLASS_NAME}") } + @Test + fun `detects the plugin provided for compilation via pluginClasspaths property`() { + val result = defaultCompilerConfig().apply { + sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource")) + pluginClasspaths = listOf(classpathOf("kotlin-scripting-compiler-1.3.50")) + }.compile() + + assertThat(result.exitCode).isEqualTo(ExitCode.OK) + assertThat(result.messages).contains("provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar") + } + @Test fun `returns an internal error when adding a non existing plugin for compilation`() { val result = defaultCompilerConfig().apply { @@ -632,7 +644,7 @@ class KotlinCompilationTests { }.compile() assertThat(result.exitCode).isEqualTo(ExitCode.INTERNAL_ERROR) - assertThat(result.messages.isEmpty()) + assertThat(result.messages).contains("non-existing-plugin.jar not found") } private fun defaultCompilerConfig(): KotlinCompilation { @@ -657,6 +669,12 @@ class KotlinCompilationTests { return fail("Class $className could not be loaded") } } + + private fun classpathOf(dependency: String): File { + val regex = Regex(".*$dependency\\.jar") + val classGraph = ClassGraph().whitelistJars() + return classGraph.classpathFiles.first { classpath -> classpath.name.matches(regex) } + } class InheritedClass {} } From 6f37d94a0ed27ba29d4851e8c336952fab21d6db Mon Sep 17 00:00:00 2001 From: "Rachel M. Carmena" Date: Sun, 6 Oct 2019 20:05:10 +0200 Subject: [PATCH 5/5] Requested changes after @tschuchortdev review --- .../com/tschuchort/compiletesting/KotlinCompilation.kt | 3 +++ .../tschuchort/compiletesting/KotlinCompilationTests.kt | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt index 40ff1bff..5cd967e1 100644 --- a/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt +++ b/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt @@ -487,6 +487,9 @@ class KotlinCompilation { } } + if (pluginClasspaths.isNotEmpty()) + warn("Included plugins in pluginsClasspaths will be executed twice.") + val k2JvmArgs = commonK2JVMArgs().also { it.freeArgs = sourcePaths it.pluginClasspaths = (it.pluginClasspaths ?: emptyArray()) + arrayOf(getResourcesPath()) diff --git a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt index 9ee91f49..eb74dcbf 100644 --- a/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt +++ b/src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt @@ -670,10 +670,14 @@ class KotlinCompilationTests { } } + /** + * Returns the classpath for a dependency (format $name-$version). + * This is necessary to know the actual location of a dependency + * which has been included in test runtime (build.gradle). + */ private fun classpathOf(dependency: String): File { val regex = Regex(".*$dependency\\.jar") - val classGraph = ClassGraph().whitelistJars() - return classGraph.classpathFiles.first { classpath -> classpath.name.matches(regex) } + return ClassGraph().classpathFiles.first { classpath -> classpath.name.matches(regex) } } class InheritedClass {}