Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating "OR" preconditions instead of "AND" (#4003) does not actually work #4505

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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