Skip to content

Commit

Permalink
Add JUnit Jupiter dependency to the right scope (#586)
Browse files Browse the repository at this point in the history
* Add JUnit Jupiter dependency to the right scope

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
timtebeek and github-actions[bot] committed Aug 21, 2024
1 parent 671fe03 commit 52502ed
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.openrewrite.java.dependencies.AddDependency;
import org.openrewrite.maven.MavenIsoVisitor;
import org.openrewrite.maven.tree.ResolvedDependency;
import org.openrewrite.maven.tree.Scope;
import org.openrewrite.xml.tree.Xml;

import java.util.List;
Expand Down Expand Up @@ -64,14 +63,14 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AddDependency.Accumulator acc
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx, Cursor parent) {
if(!(tree instanceof SourceFile)) {
if (!(tree instanceof SourceFile)) {
return tree;
}
SourceFile s = (SourceFile) tree;
if(gv.isAcceptable(s, ctx)) {
if (gv.isAcceptable(s, ctx)) {
s = (SourceFile) gv.visitNonNull(s, ctx);
}
if(mv.isAcceptable(s, ctx)) {
if (mv.isAcceptable(s, ctx)) {
s = (SourceFile) mv.visitNonNull(s, ctx);
}
return s;
Expand All @@ -81,7 +80,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AddDependency.Accumulator acc

private static AddDependency addJupiterDependency() {
return new AddDependency("org.junit.jupiter", "junit-jupiter", "5.x", null,
"org.junit..*", null, null, null, null, "test",
"org.junit..*", null, null, null, null, null,
null, null, null, null);
}

Expand All @@ -93,16 +92,16 @@ private static class AddJupiterGradle extends GroovyIsoVisitor<ExecutionContext>
@Override
public G.CompilationUnit visitCompilationUnit(G.CompilationUnit t, ExecutionContext ctx) {
Optional<GradleProject> maybeGp = t.getMarkers().findFirst(GradleProject.class);
if(!maybeGp.isPresent()) {
if (!maybeGp.isPresent()) {
return t;
}
GradleProject gp = maybeGp.get();
GradleDependencyConfiguration trc = gp.getConfiguration("testRuntimeClasspath");
if(trc == null) {
if (trc == null) {
return t;
}
ResolvedDependency jupiterApi = trc.findResolvedDependency("org.junit.jupiter", "junit-jupiter-api");
if(jupiterApi == null) {
if (jupiterApi == null) {
t = (G.CompilationUnit) addJupiterDependency().getVisitor(acc)
.visitNonNull(t, ctx);
}
Expand All @@ -115,11 +114,12 @@ public G.CompilationUnit visitCompilationUnit(G.CompilationUnit t, ExecutionCont
@EqualsAndHashCode(callSuper = false)
private static class AddJupiterMaven extends MavenIsoVisitor<ExecutionContext> {
AddDependency.Accumulator acc;

@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
Xml.Document d = document;
List<ResolvedDependency> jupiterApi = getResolutionResult().findDependencies("org.junit.jupiter", "junit-jupiter-api", Scope.Test);
if(jupiterApi.isEmpty()) {
List<ResolvedDependency> jupiterApi = getResolutionResult().findDependencies("org.junit.jupiter", "junit-jupiter-api", null);
if (jupiterApi.isEmpty()) {
d = (Xml.Document) addJupiterDependency().getVisitor(acc)
.visitNonNull(d, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.junit5;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.*;
import static org.openrewrite.maven.Assertions.pomXml;

class AddJupiterDependenciesTest implements RewriteTest {

@Language("java")
private static final String SOME_TEST = """
import org.junit.jupiter.api.Test;
class FooTest {
@Test
void bar() {
}
}
""";

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new AddJupiterDependencies())
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api"));
}

@DocumentExample
@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/585")
void addToTestScope() {
rewriteRun(
mavenProject("project",
srcTestJava(java(SOME_TEST)),
pomXml(
//language=xml
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1</version>
</project>
""",
spec -> spec.after(pom -> {
assertThat(pom)
.contains("junit-jupiter")
.contains("<scope>test</scope>");
return pom;
})
)
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/585")
void addToCompileScope() {
rewriteRun(
mavenProject("project",
srcMainJava(java(SOME_TEST)),
pomXml(
//language=xml
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>project</artifactId>
<version>0.0.1</version>
</project>
""",
spec -> spec.after(pom -> {
assertThat(pom)
.contains("junit-jupiter")
.doesNotContain("<scope>test</scope>");
return pom;
})
)
)
);
}
}

0 comments on commit 52502ed

Please sign in to comment.