Skip to content

Commit

Permalink
feat: If DeclarativeRecipe only has already-initialized recipes, don'…
Browse files Browse the repository at this point in the history
…t require initialization (#4267)
  • Loading branch information
kmccarp authored Jun 19, 2024
1 parent cfd15b6 commit 1e409ed
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TreeVisitor<Tree, ExecutionContext>() {
TreeVisitor<?, ExecutionContext> p = precondition.get();

@Override
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
return p.isAcceptable(sourceFile, ctx);
Expand Down Expand Up @@ -314,11 +315,17 @@ public void addValidation(Validated<Object> validated) {

@Override
public Validated<Object> validate() {
return Validated.<Object>test("initialization",
"initialize(..) must be called on DeclarativeRecipe prior to use.",
this, r -> initValidation != null)
.and(validation)
.and(initValidation);
Validated<Object> validated = Validated.none();

if (!uninitializedRecipes.isEmpty() && uninitializedRecipes.size() != recipeList.size()) {
validated = validated.and(Validated.invalid("initialization", recipeList, "DeclarativeRecipe must not contain uninitialized recipes. Be sure to call .initialize() on DeclarativeRecipe."));
}
if (!uninitializedPreconditions.isEmpty() && uninitializedPreconditions.size() != preconditions.size()) {
validated = validated.and(Validated.invalid("initialization", preconditions, "DeclarativeRecipe must not contain uninitialized preconditions. Be sure to call .initialize() on DeclarativeRecipe."));
}

return validated.and(validation)
.and(initValidation == null ? Validated.none() : initValidation);
}

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,57 @@ public PlainText visitText(PlainText text, ExecutionContext ctx) {
);
}

@Test
void uninitializedFailsValidation() {
DeclarativeRecipe dr = new DeclarativeRecipe("test", "test", "test", null,
null, null, true, null);
dr.addUninitializedPrecondition(
toRecipe(() -> new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if ("1".equals(text.getText())) {
return SearchResult.found(text);
}
return text;
}
})
);
dr.addUninitialized(
new ChangeText("2")
);
dr.addUninitialized(
new ChangeText("3")
);
Validated<Object> validation = dr.validate();
assertThat(validation.isValid()).isFalse();
assertThat(validation.failures().size()).isEqualTo(2);
assertThat(validation.failures().get(0).getProperty()).isEqualTo("initialization");
}

@Test
void uninitializedWithInitializedRecipesPassesValidation() {
DeclarativeRecipe dr = new DeclarativeRecipe("test", "test", "test", null,
null, null, true, null);
dr.setPreconditions(
List.of(
toRecipe(() -> new PlainTextVisitor<>() {
@Override
public PlainText visitText(PlainText text, ExecutionContext ctx) {
if ("1".equals(text.getText())) {
return SearchResult.found(text);
}
return text;
}
}))
);
dr.setRecipeList(List.of(
new ChangeText("2"),
new ChangeText("3")
));
Validated<Object> validation = dr.validate();
assertThat(validation.isValid()).isTrue();
}

@Test
void yamlPrecondition() {
rewriteRun(
Expand All @@ -96,17 +147,17 @@ void yamlPrecondition() {
void yamlPreconditionWithScanningRecipe() {
rewriteRun(
spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.PreconditionTest
preconditions:
- org.openrewrite.text.Find:
find: 1
recipeList:
- org.openrewrite.text.CreateTextFile:
relativeFileName: test.txt
fileContents: "test"
""", "org.openrewrite.PreconditionTest")
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.PreconditionTest
preconditions:
- org.openrewrite.text.Find:
find: 1
recipeList:
- org.openrewrite.text.CreateTextFile:
relativeFileName: test.txt
fileContents: "test"
""", "org.openrewrite.PreconditionTest")
.afterRecipe(run -> {
assertThat(run.getChangeset().getAllResults()).anySatisfy(
s -> {
Expand Down

0 comments on commit 1e409ed

Please sign in to comment.