Skip to content

Commit

Permalink
Change test to use a Java source file too; add TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 13, 2024
1 parent eaf467a commit b6da5ca
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 33 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
insert_final_newline = true
trim_trailing_whitespace = true

[src/test*/java/**.java]
indent_size = 4
ij_continuation_indent_size = 2
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

@Value
@EqualsAndHashCode(callSuper = false)
public class RemoveDependency extends Recipe {
public class RemoveDependency extends Recipe { // TODO Extend ScanningRecipe instead
@Option(displayName = "Group ID",
description = "The first part of a dependency coordinate `com.google.guava:guava:VERSION`. This can be a glob expression.",
example = "com.fasterxml.jackson*")
Expand All @@ -39,9 +39,9 @@ public class RemoveDependency extends Recipe {
example = "jackson-module*")
String artifactId;

@Option(displayName = "Unless using", description = "If a dependency is used in the code, do not remove it.")
@Option(displayName = "Unless using", description = "If a dependency is used in the code, do not remove it.", example = "org.aspectj.lang.*")
@Nullable
Boolean unlessUsing;
String unlessUsing;

// Gradle only parameter
@Option(displayName = "The dependency configuration", description = "The dependency configuration to remove from.", example = "api", required = false)
Expand Down Expand Up @@ -76,7 +76,7 @@ public String getDescription() {
public RemoveDependency(
String groupId,
String artifactId,
@Nullable Boolean unlessUsing,
@Nullable String unlessUsing,
@Nullable String configuration,
@Nullable String scope) {
this.groupId = groupId;
Expand All @@ -88,12 +88,15 @@ public RemoveDependency(
removeMavenDependency = new org.openrewrite.maven.RemoveDependency(groupId, artifactId, scope);
}

// TODO Provide a scanner that looks across java sources to see if `unlessUsing` is used
// Optionally look for use in the correct scope and submodule, but for do-no-harm v1 can skip that for now


// TODO remove this method, and instead override getVisitor, as the scanning condition is not evaluated for getRecipeList
@Override
public List<Recipe> getRecipeList() {
if (Boolean.TRUE.equals(unlessUsing)) {
// logic to check if code uses the lib, if so ->
return emptyList();
}
return Arrays.asList(removeGradleDependency, removeMavenDependency);
}

// TODO override getVisitor that only calls out to removeGradleDependency or removeMavenDependency if the scanning condition is met
}
5 changes: 0 additions & 5 deletions src/test/java/org/openrewrite/.editorconfig

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.java.Assertions.*;
import static org.openrewrite.maven.Assertions.pomXml;

class RemoveDependencyTest implements RewriteTest {
Expand All @@ -30,7 +32,7 @@ class RemoveDependencyTest implements RewriteTest {
void removeGradleDependencyUsingStringNotationWithExclusion() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi())
.recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", false, null, null)),
.recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", null, null, null)),
//language=groovy
buildGradle(
"""
Expand Down Expand Up @@ -70,7 +72,7 @@ void removeGradleDependencyUsingStringNotationWithExclusion() {
@Test
void removeMavenDependency() {
rewriteRun(
spec -> spec.recipe(new RemoveDependency("junit", "junit", false, null, null)),
spec -> spec.recipe(new RemoveDependency("junit", "junit", null, null, null)),
//language=xml
pomXml(
"""
Expand Down Expand Up @@ -118,28 +120,58 @@ void removeMavenDependency() {
}

@Test
void doNotRemoveDependencyIfItIsUsedAndUnlessUsingIsTrue() {
void doNotRemoveIfInUse() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi())
.recipe(new RemoveDependency("org.springframework.boot", "spring-boot*", true, null, null)),
//language=groovy
buildGradle(
"""
plugins {
id 'java-library'
}
repositories {
mavenCentral()
}
spec -> spec
.parser(JavaParser.fromJavaVersion().dependsOn(
//language=java
"""
package org.aspectj.lang.annotation;
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:2.7.0") {
exclude group: "junit"
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Aspect {
}
"""
))
.recipe(new RemoveDependency("org.aspectj", "aspectjrt", "org.aspectj.lang.annotation.*", null, null)),
mavenProject("example",
//language=java
srcMainJava(
java(
"""
import org.aspectj.lang.annotation.Aspect;
@Aspect
class MyLoggingInterceptor {
}
testImplementation "org.junit.vintage:junit-vintage-engine:5.6.2"
}
"""
)
),
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.22.1</version>
</dependency>
</dependencies>
</project>
"""
)
)
);
}
Expand Down

0 comments on commit b6da5ca

Please sign in to comment.