Skip to content

Commit e2d6378

Browse files
rachelcarmenatschuchortdev
authored andcommitted
Add pluginClasspaths to the configuration (#23)
* Add pluginClasspaths to the configuration
1 parent 0f6519f commit e2d6378

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ idea {
4141
}
4242

4343
dependencies {
44+
testRuntime "org.jetbrains.kotlin:kotlin-scripting-compiler:1.3.50"
45+
4446
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
4547
testImplementation group: 'junit', name: 'junit', version: '4.12'
4648

src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class KotlinCompilation {
6565
*/
6666
var classpaths: List<File> = emptyList()
6767

68+
/**
69+
* Paths to plugins to be made available in the compilation
70+
*/
71+
var pluginClasspaths: List<File> = emptyList()
72+
6873
/** Source files to be compiled */
6974
var sources: List<SourceFile> = emptyList()
7075

@@ -326,6 +331,8 @@ class KotlinCompilation {
326331
it.destination = classesDir.absolutePath
327332
it.classpath = commonClasspaths().joinToString(separator = File.pathSeparator)
328333

334+
it.pluginClasspaths = pluginClasspaths.map(File::getAbsolutePath).toTypedArray()
335+
329336
if(jdkHome != null) {
330337
it.jdkHome = jdkHome!!.absolutePath
331338
}
@@ -403,6 +410,13 @@ class KotlinCompilation {
403410

404411
/** Performs the 1st and 2nd compilation step to generate stubs and run annotation processors */
405412
private fun stubsAndApt(sourceFiles: List<File>): ExitCode {
413+
pluginClasspaths.forEach { filepath ->
414+
if (!filepath.exists()) {
415+
error("Plugin $filepath not found")
416+
return ExitCode.INTERNAL_ERROR
417+
}
418+
}
419+
406420
if(annotationProcessors.isEmpty()) {
407421
log("No services were given. Not running kapt steps.")
408422
return ExitCode.OK
@@ -473,10 +487,12 @@ class KotlinCompilation {
473487
}
474488
}
475489

490+
if (pluginClasspaths.isNotEmpty())
491+
warn("Included plugins in pluginsClasspaths will be executed twice.")
492+
476493
val k2JvmArgs = commonK2JVMArgs().also {
477494
it.freeArgs = sourcePaths
478-
it.pluginClasspaths = (it.pluginClasspaths?.toList() ?: emptyList<String>() + getResourcesPath())
479-
.distinct().toTypedArray()
495+
it.pluginClasspaths = (it.pluginClasspaths ?: emptyArray()) + arrayOf(getResourcesPath())
480496
}
481497

482498
val compilerMessageCollector = PrintingMessageCollector(

src/test/kotlin/com/tschuchort/compiletesting/KotlinCompilationTests.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.tschuchort.compiletesting
22

33
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
4+
import io.github.classgraph.ClassGraph
45
import org.assertj.core.api.Assertions.assertThat
56
import org.assertj.core.api.Assertions.fail
67
import org.junit.Rule
78
import org.junit.Test
89
import org.junit.rules.TemporaryFolder
10+
import java.io.File
911
import javax.annotation.processing.AbstractProcessor
1012
import javax.annotation.processing.RoundEnvironment
1113
import javax.lang.model.element.TypeElement
@@ -623,6 +625,28 @@ class KotlinCompilationTests {
623625
assertClassLoadable(result, "${KotlinTestProcessor.GENERATED_PACKAGE}.${KotlinTestProcessor.GENERATED_JAVA_CLASS_NAME}")
624626
}
625627

628+
@Test
629+
fun `detects the plugin provided for compilation via pluginClasspaths property`() {
630+
val result = defaultCompilerConfig().apply {
631+
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
632+
pluginClasspaths = listOf(classpathOf("kotlin-scripting-compiler-1.3.50"))
633+
}.compile()
634+
635+
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
636+
assertThat(result.messages).contains("provided plugin org.jetbrains.kotlin.scripting.compiler.plugin.ScriptingCompilerConfigurationComponentRegistrar")
637+
}
638+
639+
@Test
640+
fun `returns an internal error when adding a non existing plugin for compilation`() {
641+
val result = defaultCompilerConfig().apply {
642+
sources = listOf(SourceFile.kotlin("kSource.kt", "class KSource"))
643+
pluginClasspaths = listOf(File("./non-existing-plugin.jar"))
644+
}.compile()
645+
646+
assertThat(result.exitCode).isEqualTo(ExitCode.INTERNAL_ERROR)
647+
assertThat(result.messages).contains("non-existing-plugin.jar not found")
648+
}
649+
626650
private fun defaultCompilerConfig(): KotlinCompilation {
627651
return KotlinCompilation().apply {
628652
workingDir = temporaryFolder.root
@@ -645,6 +669,16 @@ class KotlinCompilationTests {
645669
return fail<Nothing>("Class $className could not be loaded")
646670
}
647671
}
672+
673+
/**
674+
* Returns the classpath for a dependency (format $name-$version).
675+
* This is necessary to know the actual location of a dependency
676+
* which has been included in test runtime (build.gradle).
677+
*/
678+
private fun classpathOf(dependency: String): File {
679+
val regex = Regex(".*$dependency\\.jar")
680+
return ClassGraph().classpathFiles.first { classpath -> classpath.name.matches(regex) }
681+
}
648682

649683
class InheritedClass {}
650684
}

0 commit comments

Comments
 (0)