Skip to content

Commit

Permalink
fix: Dont generate the same file multiple times & fix CreateTextFile (#…
Browse files Browse the repository at this point in the history
…4356)

* add failing test

* fix recipe and RecipeRunCycle to not allow generate to overwrite

* use TreeSet instead of manual checking each time

* put generated back into a list to avoid any additional unchecked cast issues
  • Loading branch information
pstreef authored Jul 25, 2024
1 parent 70b904b commit 84f5858
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public InMemoryLargeSourceSet(List<SourceFile> ls) {
}

protected InMemoryLargeSourceSet(@Nullable InMemoryLargeSourceSet initialState,
@Nullable Map<SourceFile, List<Recipe>> deletions,
List<SourceFile> ls) {
@Nullable Map<SourceFile, List<Recipe>> deletions,
List<SourceFile> ls) {
this.initialState = initialState;
this.ls = ls;
this.deletions = deletions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;

import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.Recipe.PANIC;

Expand Down Expand Up @@ -105,23 +105,23 @@ public LSS scanSources(LSS sourceSet) {
}

public LSS generateSources(LSS sourceSet) {
List<SourceFile> generatedInThisCycle = allRecipeStack.reduce(sourceSet, recipe, ctx, (acc, recipeStack) -> {
Set<SourceFile> generatedInThisCycle = allRecipeStack.reduce(sourceSet, recipe, ctx, (acc, recipeStack) -> {
Recipe recipe = recipeStack.peek();
if (recipe instanceof ScanningRecipe) {
//noinspection unchecked
ScanningRecipe<Object> scanningRecipe = (ScanningRecipe<Object>) recipe;
List<SourceFile> generated = new ArrayList<>(scanningRecipe.generate(scanningRecipe.getAccumulator(rootCursor, ctx), unmodifiableList(acc), ctx));
List<SourceFile> generated = new ArrayList<>(scanningRecipe.generate(scanningRecipe.getAccumulator(rootCursor, ctx), unmodifiableSet(acc), ctx));
generated.replaceAll(source -> addRecipesThatMadeChanges(recipeStack, source));
acc.addAll(generated);
if (!generated.isEmpty()) {
madeChangesInThisCycle.add(recipe);
}
}
return acc;
}, new ArrayList<>());
}, new TreeSet<>(Comparator.comparing(SourceFile::getSourcePath)));

// noinspection unchecked
return (LSS) sourceSet.generate(generatedInThisCycle);
return (LSS) sourceSet.generate(new ArrayList<>(generatedInThisCycle));
}

public LSS editSources(LSS sourceSet) {
Expand Down Expand Up @@ -238,7 +238,7 @@ private void recordSourceFileResult(@Nullable String beforePath, @Nullable Strin
}

private @Nullable SourceFile handleError(Recipe recipe, SourceFile sourceFile, @Nullable SourceFile after,
Throwable t) {
Throwable t) {
ctx.getOnError().accept(t);

if (t instanceof RecipeRunException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean created) {
@Override
public SourceFile visit(@Nullable Tree tree, ExecutionContext ctx) {
SourceFile sourceFile = (SourceFile) requireNonNull(tree);
if ((created.get() || Boolean.TRUE.equals(overwriteExisting)) && path.equals(sourceFile.getSourcePath())) {
if (Boolean.TRUE.equals(overwriteExisting) && path.equals(sourceFile.getSourcePath())) {
if (sourceFile instanceof PlainText) {
return ((PlainText) sourceFile).withText(fileContents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RewriteTest;

import java.nio.file.Path;

import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.test.SourceSpecs.text;

Expand Down Expand Up @@ -128,4 +130,29 @@ void shouldOverrideDifferentSourceFileType() {
)
);
}

@Test
void shouldNotOverrideIfCreatedInSameRun() {
rewriteRun(
spec -> spec.recipeFromYaml("""
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.test.CreateTextFileDuplication
displayName: Create Text File test recipe
description: Create Text File test recipe.
recipeList:
- org.openrewrite.text.CreateTextFile:
relativeFileName: duplicate.txt
overwriteExisting: false
fileContents: |
hi
- org.openrewrite.text.CreateTextFile:
relativeFileName: duplicate.txt
overwriteExisting: false
fileContents: |
hello
""", "org.openrewrite.test.CreateTextFileDuplication"),
text(null, "hi", spec -> spec.path(Path.of("duplicate.txt")))
);
}
}

0 comments on commit 84f5858

Please sign in to comment.