Skip to content

Commit

Permalink
Add Kotlin support for Junit 5 recipes 1: CleanupJUnitImports (#528)
Browse files Browse the repository at this point in the history
* Adding Kotlin support to the recipes, fixing the recipes to work use preVisit instead of visitCompilationUnit.

* Do a single recipe run per unit test

* Stop after pre visit, since we're only updating imports

---------

Co-authored-by: Amitoj Duggal <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent 65711d5 commit cde1c39
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-groovy")
testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion")
testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;

public class CleanupJUnitImports extends Recipe {
@Override
Expand All @@ -44,14 +45,18 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

public static class CleanupJUnitImportsVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
for (J.Import im : cu.getImports()) {
String packageName = im.getPackageName();
if (packageName.startsWith("junit") || (packageName.startsWith("org.junit") && !packageName.contains("jupiter"))) {
maybeRemoveImport(im.getTypeName());
public J preVisit(J tree, ExecutionContext ctx) {
stopAfterPreVisit();
if (tree instanceof JavaSourceFile) {
JavaSourceFile c = (JavaSourceFile) tree;
for (J.Import imp : c.getImports()) {
String packageName = imp.getPackageName();
if (packageName.startsWith("junit") || (packageName.startsWith("org.junit") && !packageName.contains("jupiter"))) {
maybeRemoveImport(imp.getTypeName());
}
}
}
return cu;
return tree;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.kotlin.KotlinParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.kotlin.Assertions.kotlin;

class CleanupJUnitImportsTest implements RewriteTest {

Expand All @@ -31,14 +33,16 @@ public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13"))
.parser(KotlinParser.builder()
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13"))
.recipe(new CleanupJUnitImports());
}

@DocumentExample
@Test
void removesUnusedImport() {
//language=java
rewriteRun(
//language=java
java(
"""
import org.junit.Test;
Expand All @@ -48,14 +52,25 @@ public class MyTest {}
"""
public class MyTest {}
"""
),
//language=kotlin
kotlin(
"""
import org.junit.Test
class MyTest {}
""",
"""
class MyTest {}
"""
)
);
}

@Test
void leavesOtherImportsAlone() {
//language=java
rewriteRun(
//language=java
java(
"""
import java.util.Arrays;
Expand All @@ -65,14 +80,25 @@ void leavesOtherImportsAlone() {
public class MyTest {
}
"""
),
//language=kotlin
kotlin(
"""
import java.util.Arrays
import java.util.Collections
import java.util.HashSet
class MyTest {
}
"""
)
);
}

@Test
void leavesUsedJUnitImportAlone() {
//language=java
rewriteRun(
//language=java
java(
"""
import org.junit.Test;
Expand All @@ -82,7 +108,19 @@ public class MyTest {
public void foo() {}
}
"""
),
//language=kotlin
kotlin(
"""
import org.junit.Test
class MyTest {
@Test
fun foo() {}
}
"""
)
);

}
}

0 comments on commit cde1c39

Please sign in to comment.