Skip to content

Commit

Permalink
Creating "OR" preconditions instead of "AND" (#4003) does not actuall…
Browse files Browse the repository at this point in the history
…y work (#4505)

* Added tests for declarative recipes as preconditions

Includes the example from the documentation.

* Allow declarative recipes to be used as preconditions

* Remove arguments to `toArray`

* Remove DeclarativeRecipe specificity

* Improve formatting

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
DidierLoiseau and timtebeek authored Sep 20, 2024
1 parent 1ed5224 commit 7bc2966
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public final List<Recipe> getRecipeList() {
getName() + " declares the ScanningRecipe " + precondition.getName() + " as a precondition." +
"ScanningRecipe cannot be used as Preconditions.");
}
andPreconditions.add(precondition::getVisitor);
andPreconditions.add(() -> orVisitors(precondition));
}
PreconditionBellwether bellwether = new PreconditionBellwether(Preconditions.and(andPreconditions.toArray(new Supplier[]{})));
List<Recipe> recipeListWithBellwether = new ArrayList<>(recipeList.size() + 1);
Expand All @@ -279,6 +279,16 @@ public final List<Recipe> getRecipeList() {
return recipeListWithBellwether;
}

private static TreeVisitor<?, ExecutionContext> orVisitors(Recipe recipe) {
List<TreeVisitor<?, ExecutionContext>> conditions = new ArrayList<>();
conditions.add(recipe.getVisitor());
for (Recipe r : recipe.getRecipeList()) {
conditions.add(orVisitors(r));
}
//noinspection unchecked
return Preconditions.or(conditions.toArray(new TreeVisitor[0]));
}

private static boolean isScanningRecipe(Recipe recipe) {
if (recipe instanceof ScanningRecipe) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,65 @@ void yamlPrecondition() {
);
}

@Test
void yamlDeclarativeRecipeAsPrecondition() {
rewriteRun(
spec -> spec.recipeFromYaml(
"""
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.PreconditionTest
description: Test.
preconditions:
- org.openrewrite.DeclarativePrecondition
recipeList:
- org.openrewrite.text.ChangeText:
toText: 3
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.DeclarativePrecondition
recipeList:
- org.openrewrite.text.Find:
find: 1
""",
"org.openrewrite.PreconditionTest"
),
text("1", "3"),
text("2")
);
}

@Test
void orPreconditions() {
// As documented https://docs.openrewrite.org/reference/yaml-format-reference#creating-or-preconditions-instead-of-and
rewriteRun(
spec -> spec.recipeFromYaml(
"""
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.DoSomething
description: Test.
preconditions:
- org.sample.FindAnyJson
recipeList:
- org.openrewrite.text.ChangeText:
toText: 2
---
type: specs.openrewrite.org/v1beta/recipe
name: org.sample.FindAnyJson
recipeList:
- org.openrewrite.FindSourceFiles:
filePattern: "**/my.json"
- org.openrewrite.FindSourceFiles:
filePattern: "**/your.json"
- org.openrewrite.FindSourceFiles:
filePattern: "**/our.json"
""",
"org.sample.DoSomething"
),
text("1", "2", spec -> spec.path("a/my.json")),
text("a", spec -> spec.path("a/not-my.json"))
);
}

@Test
void yamlPreconditionWithScanningRecipe() {
rewriteRun(
Expand Down

0 comments on commit 7bc2966

Please sign in to comment.