Skip to content

Commit

Permalink
Polish "Refactor Gradle plugin tests to use runner's plugin classpath"
Browse files Browse the repository at this point in the history
This commit removes the need for the Kotlin plugin tests to continue
to set the plugin classpath in each test build script. This is
achieved by adding the jars files containing the Kotlin plugin and
its dependencies to the runner's plugin classpath in the same way as
was already done for the dependency management plugin.

Closes spring-projectsgh-14680
  • Loading branch information
wilkinsona committed Oct 4, 2018
1 parent c514495 commit 8c6910c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<gradle.executable>./gradlew</gradle.executable>
<gradle.task>build</gradle.task>
<skip.gradle.build>false</skip.gradle.build>
<kotlin.version>1.2.10</kotlin.version>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
Expand All @@ -35,6 +34,11 @@
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.GradleRunner;
import org.jetbrains.kotlin.cli.common.PropertiesKt;
import org.jetbrains.kotlin.compilerRunner.KotlinCompilerRunner;
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
Expand Down Expand Up @@ -121,14 +125,14 @@ private List<File> pluginClasspath() {
new File(pathOfJarContaining(LaunchScript.class)),
new File(pathOfJarContaining(ClassVisitor.class)),
new File(pathOfJarContaining(DependencyManagementPlugin.class)),
new File(pathOfJarContaining(KotlinPluginWrapper.class)),
new File(pathOfJarContaining(PropertiesKt.class)),
new File(pathOfJarContaining(KotlinCompilerRunner.class)),
new File(pathOfJarContaining(KotlinPlugin.class)),
new File(pathOfJarContaining(KotlinGradleSubplugin.class)),
new File(pathOfJarContaining(ArchiveEntry.class)));
}

private String pluginClasspathAsString() {
return pluginClasspath().stream().map(File::getAbsolutePath)
.collect(Collectors.joining(","));
}

private String pathOfJarContaining(Class<?> type) {
return type.getProtectionDomain().getCodeSource().getLocation().getPath();
}
Expand Down Expand Up @@ -170,20 +174,6 @@ public GradleRunner prepareRunner(String... arguments) throws IOException {
List<String> allArguments = new ArrayList<>();
allArguments.add("-PbootVersion=" + getBootVersion());
allArguments.add("--stacktrace");

// this is necessary for the tests checking that we react correctly to the Kotlin
// plugin.
// Indeed, when using the plugins block to load the Boot plugin under test,
// relying on the plugin
// classpath set by withPluginClasspath(pluginClasspath()), the Boot plugin and
// the Kotlin plugin
// are loaded using two separate classloaders, and the Boot plugin thus doesn't
// see the Kotlin plugin
// class and can't react to it. To circumvent this test kit limitation, we set the
// classpath
// from the buildscript block of the build script, thanks to this pluginClasspath
// property
allArguments.add("-PpluginClasspath=" + pluginClasspathAsString());
allArguments.addAll(Arrays.asList(arguments));
return gradleRunner.withArguments(allArguments);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
id 'org.springframework.boot' version '{version}'
}

apply plugin: 'org.springframework.boot'
apply plugin: 'org.jetbrains.kotlin.jvm'

import org.jetbrains.kotlin.gradle.dsl.KotlinCompile

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
id 'org.springframework.boot' version '{version}'
}

apply plugin: 'org.springframework.boot'
apply plugin: 'org.jetbrains.kotlin.jvm'

import org.jetbrains.kotlin.gradle.dsl.KotlinCompile

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
}

plugins {
id 'org.jetbrains.kotlin.jvm' version '1.2.10'
id 'org.springframework.boot' version '{version}'
}


apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencyManagement {
resolutionStrategy {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
// this is necessary for the tests checking that we react correctly to the Kotlin plugin.
// Indeed, when using the plugins block to load the Boot plugin under test, relying on the plugin
// classpath set by the test kit gradle runner, the Boot plugin and the Kotlin plugin
// are loaded using two separate classloaders, and the Boot plugin thus doesn't see the Kotlin plugin
// class and can't react to it. To circumvent this test kit limitation, we set the classpath here.
buildscript {
dependencies {
classpath files(pluginClasspath.split(','))
}
plugins {
id 'org.springframework.boot' version '{version}'
}

apply plugin: 'org.springframework.boot'

task kotlinVersion {
doLast {
def kotlinVersion = project.hasProperty('kotlin.version') ? project.getProperty('kotlin.version') : 'none'
Expand Down

0 comments on commit 8c6910c

Please sign in to comment.