Polish "Refactor Gradle plugin tests to use runner's plugin classpath"

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 gh-14680
pull/14585/head
Andy Wilkinson 6 years ago
parent c51449585e
commit 8c6910cf58

@ -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>

@ -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.
@ -25,7 +25,6 @@ import java.util.ArrayList;
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;
@ -35,6 +34,11 @@ import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
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;
@ -121,14 +125,14 @@ public class GradleBuild implements TestRule {
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();
}
@ -170,20 +174,6 @@ public class GradleBuild implements TestRule {
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);
}

@ -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

@ -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

@ -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 {

@ -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'

Loading…
Cancel
Save