Skip to content

Commit 66b51ea

Browse files
authored
Fix ChangeDependency isAcceptable returning true for all files (#6889)
1 parent 3e403bf commit 66b51ea

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/ChangeDependency.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ private GradleProject updateGradleModel(GradleProject gp, ExecutionContext ctx)
464464

465465
DependencyMatcher propsMatcher = requireNonNull(DependencyMatcher.build(oldGroupId + ":" + oldArtifactId).getValue());
466466
return new TreeVisitor<Tree, ExecutionContext>() {
467+
@Override
468+
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
469+
if (sourceFile instanceof Properties.File) {
470+
return sourceFile.getSourcePath().endsWith(GRADLE_PROPERTIES_FILE_NAME);
471+
}
472+
return sourceFile.getMarkers().findFirst(GradleProject.class).isPresent();
473+
}
474+
467475
@Override
468476
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
469477
if (tree instanceof Properties.File) {

rewrite-gradle/src/test/java/org/openrewrite/gradle/ChangeDependencyTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,15 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.ExecutionContext;
21+
import org.openrewrite.InMemoryExecutionContext;
22+
import org.openrewrite.SourceFile;
23+
import org.openrewrite.TreeVisitor;
2024
import org.openrewrite.test.RecipeSpec;
2125
import org.openrewrite.test.RewriteTest;
26+
import org.openrewrite.text.PlainTextParser;
27+
28+
import java.nio.file.Path;
2229

2330
import static org.assertj.core.api.Assertions.assertThat;
2431
import static org.openrewrite.gradle.Assertions.buildGradle;
@@ -1061,4 +1068,18 @@ void sharedKotlinDslStringTemplateVersionVariableCollapsesToLiteral() {
10611068
)
10621069
);
10631070
}
1071+
1072+
@Test
1073+
void isAcceptable() {
1074+
ChangeDependency recipe = new ChangeDependency(
1075+
"org.old", "artifact", "org.new", "artifact", null, null, null
1076+
);
1077+
@SuppressWarnings("unchecked")
1078+
TreeVisitor<?, ExecutionContext> visitor = (TreeVisitor<?, ExecutionContext>) recipe.getVisitor();
1079+
1080+
SourceFile sourceFile = PlainTextParser.builder().build().parse("not a gradle file")
1081+
.findFirst().orElseThrow()
1082+
.withSourcePath(Path.of("not-a-gradle-file.txt"));
1083+
assertThat(visitor.isAcceptable(sourceFile, new InMemoryExecutionContext())).isFalse();
1084+
}
10641085
}

0 commit comments

Comments
 (0)