Skip to content

Commit

Permalink
Add integration test covering multi-project builds
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Dec 21, 2020
1 parent c733e47 commit 2bbfd00
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ private RewriteExtension getExtension() {
protected Environment environment() {
Environment.Builder env = Environment.builder()
.scanClasspath(
getJavaSources().getFiles().stream().map(File::toPath).collect(Collectors.toList())
Stream.concat(
getDependencies().getFiles().stream(),
getJavaSources().getFiles().stream()
)
.map(File::toPath)
.collect(Collectors.toList())
)
.scanUserHome();

Expand Down Expand Up @@ -166,7 +171,7 @@ protected ChangesContainer listChanges() {
}
Collection<RefactorVisitor<?>> visitors = env.visitors(recipes);
if(visitors.size() == 0) {
getLog().warn("Could not find any Rewrite visitors matching active recipe(s): " + String.join(", ", activeRecipes) + ". " +
getLog().info("Could not find any Rewrite visitors matching active recipe(s): " + String.join(", ", activeRecipes) + ". " +
"Double check that you have taken a dependency on the jar containing these recipes.");
return new ChangesContainer(baseDir, emptyList());
}
Expand Down
14 changes: 7 additions & 7 deletions plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,19 @@ public void apply(Project project) {
SourceSetContainer sourceSets = javaPlugin.getSourceSets();

// Fix is meant to be invoked manually and so is not made a dependency of any existing task
TaskProvider<Task> rewriteFixLifecycleProvider = tasks.register("rewriteFix");
rewriteFixLifecycleProvider.configure(it -> {
TaskProvider<Task> rewriteFixAllProvider = tasks.register("rewriteFix");
rewriteFixAllProvider.configure(it -> {
it.setGroup("rewrite");
it.setDescription("Apply the active refactoring recipes to all sources");
});
// Warn hooks into the regular Java build by becoming a dependency of "check", the same way that checkstyle or unit tests do
TaskProvider<Task> rewriteWarnLifecycleProvider = tasks.register("rewriteWarn");
rewriteWarnLifecycleProvider.configure(it -> {
TaskProvider<Task> rewriteWarnAllProvider = tasks.register("rewriteWarn");
rewriteWarnAllProvider.configure(it -> {
it.setGroup("rewrite");
it.setDescription("Dry run the active refactoring recipes to all sources. No changes will be made.");
});
TaskProvider<?> checkTaskProvider = tasks.named("check");
checkTaskProvider.configure(checkTask -> checkTask.dependsOn(rewriteWarnLifecycleProvider));
checkTaskProvider.configure(checkTask -> checkTask.dependsOn(rewriteWarnAllProvider));

sourceSets.configureEach(sourceSet -> {

Expand All @@ -74,7 +74,7 @@ public void apply(Project project) {
rewriteFixTask.getActiveStyles().addAll(extension.getActiveStyles());
rewriteFixTask.getRecipes().addAll(extension.getRecipes());
});
rewriteFixLifecycleProvider.configure(it -> it.dependsOn(rewriteFixProvider));
rewriteFixAllProvider.configure(it -> it.dependsOn(rewriteFixProvider));

String rewriteDiscoverTaskName = "rewriteDiscover" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1);
TaskProvider<RewriteDiscoverTask> rewriteDiscoverProvider = tasks.register(rewriteDiscoverTaskName, RewriteDiscoverTask.class);
Expand Down Expand Up @@ -105,7 +105,7 @@ public void apply(Project project) {
rewriteWarnTask.getActiveStyles().addAll(extension.getActiveStyles());
rewriteWarnTask.getRecipes().addAll(extension.getRecipes());
});
rewriteWarnLifecycleProvider.configure(it -> it.dependsOn(rewriteWarnProvider));
rewriteWarnAllProvider.configure(it -> it.dependsOn(rewriteWarnProvider));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,17 @@
package org.openrewrite.gradle

import org.gradle.testkit.runner.TaskOutcome

import spock.lang.Ignore

/**
* Because of how the Gradle Test Kit manages the classpath of the project under test, these may fail when run from an IDE.
* To run & debug these tests from IntelliJ ensure you're delegating test execution to Gradle:
* Settings > Build, Execution, Deployment > Build Tools > Gradle > Run tests using Gradle
*
* That should be all you have to do.
* If breakpoints within your plugin aren't being hit try adding -Dorg.gradle.testkit.debug=true to the arguments and
* connecting a remote debugger on port 5005.
*/
class RewritePluginTest extends RewriteTestBase {

String rewriteYamlText = """\
Expand Down Expand Up @@ -118,21 +128,14 @@ class RewritePluginTest extends RewriteTestBase {
def "rewriteFix works on multi-project builds"() {
given:
File settings = projectDir.newFile("settings.gradle")
settings.text = """
settings.text = """\
include("a")
include("b")
""".stripIndent()
File rootBuildGradle = projectDir.newFile("build.gradle")
rootBuildGradle.text = """
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.org.openrewrite:plugin:+"
}
rootBuildGradle.text = """\
plugins {
id("org.openrewrite.rewrite").apply(false)
}
subprojects {
Expand All @@ -145,13 +148,17 @@ class RewritePluginTest extends RewriteTestBase {
dependencies {
testImplementation("junit:junit:4.12")
compileOnly("org.openrewrite.recipe:rewrite-testing-frameworks:+")
testImplementation("org.openrewrite.recipe:rewrite-testing-frameworks:+")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.JUnit5Migration")
}
}
""".stripIndent()
File aSrcDir = projectDir.newFolder("a", "src", "test", "java", "com", "foo");
File aSrcDir = projectDir.newFolder("a", "src", "test", "java", "com", "foo")
File aTestClass = new File(aSrcDir, "ATestClass.java")
aTestClass.text = """
aTestClass.text = """\
package com.foo;
import org.junit.Test;
Expand All @@ -161,9 +168,9 @@ class RewritePluginTest extends RewriteTestBase {
public void passes() { }
}
""".stripIndent()
File bSrcDir = projectDir.newFolder("b", "src", "test", "java", "com", "foo");
File bSrcDir = projectDir.newFolder("b", "src", "test", "java", "com", "foo")
File bTestClass = new File(bSrcDir, "BTestClass.java")
bTestClass.text = """
bTestClass.text = """\
package com.foo;
import org.junit.Test;
Expand All @@ -175,12 +182,33 @@ class RewritePluginTest extends RewriteTestBase {
""".stripIndent()
when:
def result = gradleRunner("6.5.1", "rewriteFix").build()
def aRewriteFixMainResult = result.task(":a:rewriteFixTest")
def bRewriteFixMainResult = result.task(":b:rewriteFixTest")

def aRewriteFixnResult = result.task(":a:rewriteFixTest")
def bRewriteFixResult = result.task(":b:rewriteFixTest")
String bTestClassExpected = """\
package com.foo;
import org.junit.jupiter.api.Test;
public class BTestClass {
@Test
public void passes() { }
}
""".stripIndent()
String aTestClassExpected = """\
package com.foo;
import org.junit.jupiter.api.Test;
public class ATestClass {
@Test
public void passes() { }
}
""".stripIndent()
then:
aRewriteFixMainResult.outcome == TaskOutcome.SUCCESS
bRewriteFixMainResult.outcome == TaskOutcome.SUCCESS
aRewriteFixnResult.outcome == TaskOutcome.SUCCESS
bRewriteFixResult.outcome == TaskOutcome.SUCCESS
aTestClass.text == aTestClassExpected
bTestClass.text == bTestClassExpected
}

def "rewriteDiscover will print some stuff"() {
Expand Down

0 comments on commit 2bbfd00

Please sign in to comment.